Пример #1
0
func (fieldContext *FieldContextDef) getUserPrefJsonData(websocketConnectionContext *websocketConnectionContextDef) ([]byte, *gkerr.GkErrDef) {
	var singleSession *ses.SingleSessionDef
	var userPrefsList []database.DbUserPrefDef
	var gkErr *gkerr.GkErrDef

	singleSession = fieldContext.sessionContext.GetSessionFromId(websocketConnectionContext.sessionId)

	userPrefsList, gkErr = fieldContext.persistenceContext.GetUserPrefsList(singleSession.GetUserName())

	var jsonData []byte = make([]byte, 0, 512)

	jsonData = append(jsonData, []byte("{\"userPrefList\":[")...)

	for i, e := range userPrefsList {
		if i > 0 {
			jsonData = append(jsonData, ',')
		}
		jsonData = append(jsonData, []byte(fmt.Sprintf("{\"prefName\": \"%s\", \"prefValue\": \"%s\"}", e.PrefName, e.PrefValue))...)
	}

	jsonData = append(jsonData, ']')
	jsonData = append(jsonData, '}')

	return jsonData, gkErr
}
Пример #2
0
func (fieldContext *FieldContextDef) removeAvatarBySessionId(sessionId string) *gkerr.GkErrDef {
	var websocketConnectionContext *websocketConnectionContextDef
	var gkErr *gkerr.GkErrDef
	var singleSession *ses.SingleSessionDef
	singleSession = fieldContext.sessionContext.GetSessionFromId(sessionId)
	var podId int32 = singleSession.GetCurrentPodId()

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

	var fieldObject *fieldObjectDef
	var ok bool

	fieldObject, ok = fieldContext.podMap[podId].avatarMap[websocketConnectionContext.avatarId]
	if ok {
		fieldContext.sendAllRemoveMessageForObject(websocketConnectionContext.sessionId, fieldObject)
	}

	if websocketConnectionContext.avatarId != "" {
		delete(fieldContext.podMap[podId].avatarMap, websocketConnectionContext.avatarId)
	}

	return nil
}
Пример #3
0
func (fieldContext *FieldContextDef) sendSingleAvatarObject(websocketConnectionContext *websocketConnectionContextDef, fieldObject *fieldObjectDef) *gkerr.GkErrDef {
	var gkErr *gkerr.GkErrDef

	var svgJsonData *message.SvgJsonDataDef = new(message.SvgJsonDataDef)

	svgJsonData.Id = fieldObject.id
	svgJsonData.IsoXYZ = fieldObject.isoXYZ
	gklog.LogTrace("sourceSessionId: " + fieldObject.sourceSessionId)
	if fieldObject.sourceSessionId != websocketConnectionContext.sessionId {
		var singleSession *ses.SingleSessionDef
		singleSession = fieldContext.sessionContext.GetSessionFromId(fieldObject.sourceSessionId)
		svgJsonData.UserName = singleSession.GetUserName()
		gklog.LogTrace("going to send to ws userName: " + singleSession.GetUserName())
	}

	var messageToClient *message.MessageToClientDef = new(message.MessageToClientDef)
	gkErr = messageToClient.BuildSvgMessageToClient(fieldContext.avatarSvgDir, message.AddSvgReq, fieldObject.fileName, svgJsonData)
	if gkErr != nil {
		return gkErr
	}

	fieldContext.queueMessageToClient(websocketConnectionContext.sessionId, messageToClient)

	return nil
}
Пример #4
0
func (fieldContext *FieldContextDef) handleGetAvatarSvgReq(messageFromClient *message.MessageFromClientDef) *gkerr.GkErrDef {

	var messageToClient *message.MessageToClientDef = new(message.MessageToClientDef)
	var getSvg getSvgDef
	var gkErr *gkerr.GkErrDef
	var err error

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

	var randomByte byte
	randomByte = fieldContext.sessionContext.RandContext.GetRandomByte()
	var svgJsonData *message.SvgJsonDataDef = new(message.SvgJsonDataDef)
	svgJsonData.Id = fieldContext.getNextObjectId()
	svgJsonData.IsoXYZ.X = int16((randomByte&0x07)*3) - 10
	svgJsonData.IsoXYZ.Y = int16(((randomByte>>4)&0x07)*3) - 10

	gkErr = messageToClient.BuildSvgMessageToClient(fieldContext.avatarSvgDir, message.GetAvatarSvgRes, getSvg.SvgName, svgJsonData)
	if gkErr != nil {
		return gkErr
	}

	var singleSession *ses.SingleSessionDef
	singleSession = fieldContext.sessionContext.GetSessionFromId(messageFromClient.SessionId)
	var podId int32 = singleSession.GetCurrentPodId()

	fieldContext.queueMessageToClient(messageFromClient.SessionId, messageToClient)

	var fieldObject *fieldObjectDef = new(fieldObjectDef)
	fieldObject.id = svgJsonData.Id
	fieldObject.fileName = getSvg.SvgName
	fieldObject.isoXYZ = svgJsonData.IsoXYZ
	fieldObject.sourceSessionId = messageFromClient.SessionId
	fieldContext.addAvatarObject(podId, fieldObject)

	var websocketConnectionContext *websocketConnectionContextDef

	websocketConnectionContext, gkErr = fieldContext.getWebsocketConnectionContextById(messageFromClient.SessionId)

	if gkErr != nil {
		return gkErr
	}

	websocketConnectionContext.avatarId = fieldObject.id

	gkErr = fieldContext.sendNewAvatarToAll(podId, messageFromClient.SessionId, fieldObject.id)
	if gkErr != nil {
		return gkErr
	}

	return nil
}
Пример #5
0
func (fieldContext *FieldContextDef) sendAllRemoveMessageForObject(sessionId string, fieldObject *fieldObjectDef) {
	var messageToClient *message.MessageToClientDef = new(message.MessageToClientDef)

	var singleSession *ses.SingleSessionDef
	singleSession = fieldContext.sessionContext.GetSessionFromId(sessionId)
	var podId int32 = singleSession.GetCurrentPodId()

	messageToClient.Command = message.DelSvgReq
	messageToClient.JsonData = []byte(fmt.Sprintf("{ \"id\": \"%s\"}", fieldObject.id))
	for _, websocketConnectionContext := range fieldContext.podMap[podId].websocketConnectionMap {
		fieldContext.queueMessageToClient(websocketConnectionContext.sessionId, messageToClient)
	}
}
Пример #6
0
func (fieldContext *FieldContextDef) doTerrainSvg(websocketConnectionContext *websocketConnectionContextDef) *gkerr.GkErrDef {

	var terrainSentMap map[string]string = make(map[string]string)
	var gkErr *gkerr.GkErrDef

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

	var terrainJson *terrainJsonDef

	terrainJson = fieldContext.podMap[singleSession.GetCurrentPodId()].terrainJson

	for i := 0; i < len(terrainJson.jsonMapData.TileList); i++ {
		var terrain string = terrainJson.jsonMapData.TileList[i].Terrain
		var ok bool

		if terrain != "" {
			_, ok = terrainSentMap[terrain]
			if !ok {
				var messageToClient *message.MessageToClientDef = new(message.MessageToClientDef)
				gkErr = messageToClient.BuildSvgMessageToClient(fieldContext.terrainSvgDir, message.SetTerrainSvgReq, terrain, nil)
				if gkErr != nil {
					return gkErr
				}
				fieldContext.queueMessageToClient(websocketConnectionContext.sessionId, messageToClient)

				terrainSentMap[terrain] = terrain
			}
		}
	}

	for i := 0; i < len(terrainJson.jsonMapData.ObjectList); i++ {
		var terrain string = terrainJson.jsonMapData.ObjectList[i].Object
		var ok bool

		_, ok = terrainSentMap[terrain]
		if !ok {
			var messageToClient *message.MessageToClientDef = new(message.MessageToClientDef)
			gkErr = messageToClient.BuildSvgMessageToClient(fieldContext.terrainSvgDir, message.SetTerrainSvgReq, terrain, nil)
			if gkErr != nil {
				return gkErr
			}
			fieldContext.queueMessageToClient(websocketConnectionContext.sessionId, messageToClient)

			terrainSentMap[terrain] = terrain
		}
	}

	return nil
}
Пример #7
0
func (httpContext *httpContextDef) handleGameInitial(res http.ResponseWriter, req *http.Request) {
	var gameData gameDataDef
	var gkErr *gkerr.GkErrDef
	var singleSession *ses.SingleSessionDef
	var token string

	token = req.Form.Get(_tokenParam)
	gklog.LogTrace("got token: " + token)
	var userName string
	userName = httpContext.tokenContext.getUserFromToken(token)
	gklog.LogTrace("got username: "******"not valid token", res, req)
		return
	}

	var lastPodId int32
	lastPodId, gkErr = httpContext.persistenceContext.GetLastPodId(userName)
	if gkErr != nil {
		errorMessage := "persistenceContext.getLastPodName"
		gklog.LogGkErr(errorMessage, gkErr)
		httpContext.redirectToError(errorMessage, res, req)
		return
	}

	singleSession = httpContext.sessionContext.NewSingleSession(userName, lastPodId, req.RemoteAddr)

	gameData.Title = "game"
	gameData.WebAddressPrefix = httpContext.gameConfig.WebAddressPrefix
	gameData.WebsocketAddressPrefix = httpContext.gameConfig.WebsocketAddressPrefix
	gameData.AudioAddressPrefix = httpContext.gameConfig.AudioAddressPrefix
	gameData.WebsocketPath = httpContext.gameConfig.WebsocketPath
	gameData.SessionId = singleSession.GetSessionId()

	gkErr = _gameTemplate.Build(gameData)
	if gkErr != nil {
		errorMessage := "_gameTemplate.Build"
		gklog.LogGkErr(errorMessage, gkErr)
		httpContext.redirectToError(errorMessage, res, req)
		return
	}

	gkErr = _gameTemplate.Send(res, req)
	if gkErr != nil {
		gklog.LogGkErr("_gameTemplate.Send", gkErr)
		return
	}
}
Пример #8
0
func (fieldContext *FieldContextDef) doTerrainMap(websocketConnectionContext *websocketConnectionContextDef) *gkerr.GkErrDef {

	var gkErr *gkerr.GkErrDef

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

	var messageToClient *message.MessageToClientDef = new(message.MessageToClientDef)
	messageToClient.Command = message.SetTerrainMapReq
	var jsonFileName string = fieldContext.terrainSvgDir + string(os.PathSeparator) + "map_terrain_" + strconv.FormatInt(int64(singleSession.GetCurrentPodId()), 10) + ".json"
	messageToClient.JsonData, gkErr = gkcommon.GetFileContents(jsonFileName)
	if gkErr != nil {
		return gkErr
	}
	var lf []byte = []byte("\n")
	var tb []byte = []byte("\t")
	var sp []byte = []byte(" ")
	var nl []byte = []byte("")
	var te []byte = []byte("errain")
	var bj []byte = []byte("bject")
	//	Not typos

	messageToClient.JsonData = bytes.Replace(messageToClient.JsonData, lf, nl, -1)
	messageToClient.JsonData = bytes.Replace(messageToClient.JsonData, sp, nl, -1)
	messageToClient.JsonData = bytes.Replace(messageToClient.JsonData, tb, nl, -1)
	messageToClient.JsonData = bytes.Replace(messageToClient.JsonData, te, nl, -1)
	messageToClient.JsonData = bytes.Replace(messageToClient.JsonData, bj, nl, -1)
	fieldContext.queueMessageToClient(websocketConnectionContext.sessionId, messageToClient)

	return nil
}
Пример #9
0
func (fieldContext *FieldContextDef) getWebsocketConnectionContextById(sessionId string) (*websocketConnectionContextDef, *gkerr.GkErrDef) {
	var websocketConnectionContext *websocketConnectionContextDef = nil
	var gkErr *gkerr.GkErrDef
	var ok bool

	var singleSession *ses.SingleSessionDef
	singleSession = fieldContext.sessionContext.GetSessionFromId(sessionId)
	var podId int32 = singleSession.GetCurrentPodId()

	websocketConnectionContext, ok = fieldContext.podMap[podId].websocketConnectionMap[sessionId]
	if !ok {
		gkErr = gkerr.GenGkErr("getWebsocketConnectionContextById", nil, ERROR_ID_COULD_NOT_GET_WEBSOCKET_CONNECTION_CONTEXT)
		return nil, gkErr
	}

	return websocketConnectionContext, nil
}
Пример #10
0
func (fieldContext *FieldContextDef) handleWebsocketOpened(websocketOpenedMessage WebsocketOpenedMessageDef) *gkerr.GkErrDef {

	var singleSession *ses.SingleSessionDef

	singleSession = fieldContext.sessionContext.GetSessionFromId(websocketOpenedMessage.SessionId)
	var podId int32 = singleSession.GetCurrentPodId()

	var websocketConnectionContext *websocketConnectionContextDef
	var gkErr *gkerr.GkErrDef
	var ok bool

	websocketConnectionContext, ok = fieldContext.podMap[podId].websocketConnectionMap[websocketOpenedMessage.SessionId]
	if ok {
		gkErr = gkerr.GenGkErr("opening already opened session", nil, ERROR_ID_OPENING_ALREADY_OPEN_SESSION)
		return gkErr
	}

	websocketConnectionContext = new(websocketConnectionContextDef)

	websocketConnectionContext.sessionId = websocketOpenedMessage.SessionId
	websocketConnectionContext.messageToClientChan = websocketOpenedMessage.MessageToClientChan
	websocketConnectionContext.initQueue()

	fieldContext.podMap[podId].websocketConnectionMap[websocketOpenedMessage.SessionId] = websocketConnectionContext

	var userName string = singleSession.GetUserName()

	gkErr = fieldContext.sendUserName(websocketConnectionContext, userName)
	if gkErr != nil {
		return gkErr
	}

	gkErr = fieldContext.uploadNewPodInfo(websocketConnectionContext, 1)

	gkErr = fieldContext.sendAllPastChat(websocketConnectionContext)
	if gkErr != nil {
		return gkErr
	}

	gkErr = fieldContext.sendUserPrefRestore(websocketConnectionContext)
	if gkErr != nil {
		return gkErr
	}

	return nil
}
Пример #11
0
func (fieldContext *FieldContextDef) setAllAvatars(sessionId string, fieldObject *fieldObjectDef) {

	var messageToClient *message.MessageToClientDef = new(message.MessageToClientDef)

	messageToClient.Command = message.SetSvgReq
	messageToClient.JsonData = []byte(fmt.Sprintf("{ \"id\": \"%s\", \"x\": %d, \"y\": %d, \"z\": %d }", fieldObject.id, fieldObject.isoXYZ.X, fieldObject.isoXYZ.Y, fieldObject.isoXYZ.Z))

	var singleSession *ses.SingleSessionDef
	singleSession = fieldContext.sessionContext.GetSessionFromId(sessionId)
	var podId int32 = singleSession.GetCurrentPodId()

	for _, websocketConnectionContext := range fieldContext.podMap[podId].websocketConnectionMap {
		gklog.LogTrace("compare session " + websocketConnectionContext.sessionId + " " + sessionId)

		if websocketConnectionContext.sessionId != sessionId {
			gklog.LogTrace("Trace about to queue up move command")
			fieldContext.queueMessageToClient(websocketConnectionContext.sessionId, messageToClient)
		}
	}
}
Пример #12
0
func (fieldContext *FieldContextDef) handleSetAvatarSvgReq(messageFromClient *message.MessageFromClientDef) *gkerr.GkErrDef {

	var gkErr *gkerr.GkErrDef
	var err error
	var setSvg setSvgDef

	gklog.LogTrace("json raw: " + string(messageFromClient.JsonData))

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

	var singleSession *ses.SingleSessionDef
	singleSession = fieldContext.sessionContext.GetSessionFromId(messageFromClient.SessionId)
	var podId int32 = singleSession.GetCurrentPodId()

	var fieldObject *fieldObjectDef
	var ok bool
	fieldObject, ok = fieldContext.podMap[podId].avatarMap[setSvg.Id]
	if ok {
		var cord int
		cord, _ = strconv.Atoi(setSvg.X)
		fieldObject.isoXYZ.X = int16(cord)
		cord, _ = strconv.Atoi(setSvg.Y)
		fieldObject.isoXYZ.Y = int16(cord)
		cord, _ = strconv.Atoi(setSvg.Z)
		fieldObject.isoXYZ.Z = int16(cord)

		gklog.LogTrace("one")
		fieldContext.setAllAvatars(messageFromClient.SessionId, fieldObject)
	} else {
		gkErr = gkerr.GenGkErr("move object", nil, ERROR_ID_COULD_NOT_FIND_OBJECT_TO_MOVE)
		gklog.LogGkErr("", gkErr)
	}

	return nil
}
Пример #13
0
func (fieldContext *FieldContextDef) removeAllAvatarBySessionId(sessionId string) {
	gklog.LogTrace("removing all object by session id")

	var singleSession *ses.SingleSessionDef
	singleSession = fieldContext.sessionContext.GetSessionFromId(sessionId)
	var podId int32 = singleSession.GetCurrentPodId()

	for _, fieldObject := range fieldContext.podMap[podId].avatarMap {
		if fieldObject.sourceSessionId == sessionId {
			var messageToClient *message.MessageToClientDef = new(message.MessageToClientDef)

			messageToClient.Command = message.DelSvgReq
			messageToClient.JsonData = []byte(fmt.Sprintf("{ \"id\": \"%s\"}", fieldObject.id))
			messageToClient.Data = make([]byte, 0, 0)

			//fieldContext.removeSendRemoveAvatarBySessionId(podId, messageToClient)
			for _, websocketConnectionContext := range fieldContext.podMap[podId].websocketConnectionMap {
				fieldContext.queueMessageToClient(websocketConnectionContext.sessionId, messageToClient)
			}
			delete(fieldContext.podMap[podId].avatarMap, fieldObject.id)
		}
	}
}
Пример #14
0
// websocketConnectionContext entry must be moved from old pod to new pod
func (fieldContext *FieldContextDef) handleUserPrefReq(messageFromClient *message.MessageFromClientDef) *gkerr.GkErrDef {

	var userPrefReq userPrefReqDef
	var gkErr *gkerr.GkErrDef
	var err error

	var singleSession *ses.SingleSessionDef
	singleSession = fieldContext.sessionContext.GetSessionFromId(messageFromClient.SessionId)

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

	gkErr = fieldContext.persistenceContext.SetUserPref(singleSession.GetUserName(), userPrefReq.PrefName, userPrefReq.PrefValue)
	if gkErr != nil {
		// inserting user preferences is non critical
		// so just log the error
		gklog.LogGkErr("fieldContext.persistenceContext.SetUserPref", gkErr)
	}

	return nil
}
Пример #15
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
}