Ejemplo n.º 1
0
// websocketConnectionContext entry must be moved from old pod to new pod
func (fieldContext *FieldContextDef) handleNewPodReq(messageFromClient *message.MessageFromClientDef) *gkerr.GkErrDef {

	var newPodReq newPodReqDef
	var gkErr *gkerr.GkErrDef
	var err error

	err = json.Unmarshal(messageFromClient.JsonData, &newPodReq)
	if err != nil {
		gkErr = gkerr.GenGkErr("json.Unmarshal", err, ERROR_ID_JSON_UNMARSHAL)
		return gkErr
	}

	var destinationX, destinationY, destinationZ int

	destinationX, _ = strconv.Atoi(newPodReq.X)
	destinationY, _ = strconv.Atoi(newPodReq.Y)
	destinationZ, _ = strconv.Atoi(newPodReq.Z)

	var websocketConnectionContext *websocketConnectionContextDef

	websocketConnectionContext, gkErr = fieldContext.getWebsocketConnectionContextById(messageFromClient.SessionId)
	if gkErr != nil {
		return gkErr
	}

	var singleSession *ses.SingleSessionDef
	singleSession = fieldContext.sessionContext.GetSessionFromId(websocketConnectionContext.sessionId)

	var oldPodId int32 = singleSession.GetCurrentPodId()

	var newPodId int64
	newPodId, _ = strconv.ParseInt(newPodReq.PodId, 10, 32)

	if (fieldContext.isPodIdValid(int32(newPodId))) && (oldPodId != int32(newPodId)) {

		gkErr = fieldContext.moveAllAvatarBySessionId(messageFromClient.SessionId, oldPodId, int32(newPodId), int16(destinationX), int16(destinationY), int16(destinationZ))
		if gkErr != nil {
			gklog.LogGkErr("", gkErr)
			return gkErr
		}
		delete(fieldContext.podMap[oldPodId].websocketConnectionMap, messageFromClient.SessionId)

		singleSession.SetCurrentPodId(int32(newPodId))

		fieldContext.podMap[int32(newPodId)].websocketConnectionMap[messageFromClient.SessionId] = websocketConnectionContext

		gkErr = fieldContext.uploadNewPodInfo(websocketConnectionContext, int32(newPodId))
		if gkErr != nil {
			gklog.LogGkErr("", gkErr)
			return gkErr
		}

		gkErr = fieldContext.reAddAvatarBySessionId(messageFromClient.SessionId, int32(newPodId))
		if gkErr != nil {
			gklog.LogGkErr("", gkErr)
			return gkErr
		}
	} else {
		gkErr = gkerr.GenGkErr(fmt.Sprintf("invalid podId: %d", newPodId), nil, ERROR_ID_INVALID_POD_ID)
		gklog.LogGkErr("", gkErr)
		return gkErr
	}

	return nil
}