Exemple #1
0
// an avatar is moving from one pod to another
// so delete any object matching by sessionId from old pod
// then add them to the new pod
func (fieldContext *FieldContextDef) moveAllAvatarBySessionId(sessionId string, oldPodId int32, newPodId int32, destinationX int16, destinationY int16, destinationZ int16) *gkerr.GkErrDef {
	gklog.LogTrace("moving all object by session id")
	var gkErr *gkerr.GkErrDef

	for _, fieldObject := range fieldContext.podMap[oldPodId].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)

			for _, websocketConnectionContext := range fieldContext.podMap[oldPodId].websocketConnectionMap {
				if sessionId == websocketConnectionContext.sessionId {
					fieldObject.isoXYZ.X = destinationX
					fieldObject.isoXYZ.Y = destinationY
					fieldObject.isoXYZ.Z = destinationZ
					gklog.LogTrace(fmt.Sprintf("moveAllAvatarBySessionId new destination: %d,%d,%d", fieldObject.isoXYZ.X, fieldObject.isoXYZ.Y, fieldObject.isoXYZ.Z))
				}
				fieldContext.queueMessageToClient(websocketConnectionContext.sessionId, messageToClient)
			}

			for _, websocketConnectionContext := range fieldContext.podMap[newPodId].websocketConnectionMap {
				gkErr = fieldContext.sendSingleAvatarObject(websocketConnectionContext, fieldObject)
				if gkErr != nil {
					return gkErr
				}
			}

			delete(fieldContext.podMap[oldPodId].avatarMap, fieldObject.id)
			fieldContext.podMap[newPodId].avatarMap[fieldObject.id] = fieldObject
		}
	}
	return nil
}
Exemple #2
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
}
Exemple #3
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
}
Exemple #4
0
// I think this should be (mostly) moved to the message package
func (fieldContext *FieldContextDef) sendUserName(websocketConnectionContext *websocketConnectionContextDef, userName string) *gkerr.GkErrDef {

	var messageToClient *message.MessageToClientDef = new(message.MessageToClientDef)
	messageToClient.Command = message.UserNameReq
	messageToClient.JsonData = []byte("{ \"userName\": \"" + userName + "\" }")
	fieldContext.queueMessageToClient(websocketConnectionContext.sessionId, messageToClient)

	return nil
}
Exemple #5
0
func (fieldContext *FieldContextDef) doTerrainClear(websocketConnectionContext *websocketConnectionContextDef) *gkerr.GkErrDef {

	var messageToClient *message.MessageToClientDef = new(message.MessageToClientDef)
	messageToClient.Command = message.ClearTerrainReq
	messageToClient.JsonData = []byte("{}")
	messageToClient.Data = make([]byte, 0, 0)
	fieldContext.queueMessageToClient(websocketConnectionContext.sessionId, messageToClient)

	return nil
}
Exemple #6
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
}
Exemple #7
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)
	}
}
Exemple #8
0
func (fieldContext *FieldContextDef) sendAllPastChat(websocketConnectionContext *websocketConnectionContextDef) *gkerr.GkErrDef {

	var messageToClient *message.MessageToClientDef = new(message.MessageToClientDef)
	var gkErr *gkerr.GkErrDef

	messageToClient.Command = message.SendPastChatReq
	messageToClient.JsonData, gkErr = fieldContext.getPastChatJsonData()
	if gkErr != nil {
		return gkErr
	}
	messageToClient.Data = make([]byte, 0, 0)

	fieldContext.queueMessageToClient(websocketConnectionContext.sessionId, messageToClient)

	return nil
}
Exemple #9
0
func (fieldContext *FieldContextDef) sendUserPrefRestore(websocketConnectionContext *websocketConnectionContextDef) *gkerr.GkErrDef {

	var messageToClient *message.MessageToClientDef = new(message.MessageToClientDef)
	var gkErr *gkerr.GkErrDef

	messageToClient.Command = message.UserPrefRestoreReq
	messageToClient.JsonData, gkErr = fieldContext.getUserPrefJsonData(websocketConnectionContext)
	if gkErr != nil {
		return gkErr
	}
	messageToClient.Data = make([]byte, 0, 0)

	fieldContext.queueMessageToClient(websocketConnectionContext.sessionId, messageToClient)

	return nil
}
Exemple #10
0
func (fieldContext *FieldContextDef) sendEveryoneRainEvent() {
	var rainCommand string

	if fieldContext.rainContext.rainCurrentlyOn {
		rainCommand = message.TurnOnRainReq
	} else {
		rainCommand = message.TurnOffRainReq
	}
	var podId int32 = firstPodId // rain only in the first pod

	for _, websocketConnectionContext := range fieldContext.podMap[podId].websocketConnectionMap {
		var messageToClient *message.MessageToClientDef = new(message.MessageToClientDef)
		messageToClient.Command = rainCommand
		fieldContext.queueMessageToClient(websocketConnectionContext.sessionId, messageToClient)
	}
}
Exemple #11
0
func (fieldContext *FieldContextDef) removeDandelion() {

	var messageToClient *message.MessageToClientDef = new(message.MessageToClientDef)
	var fileName = "dandelion"

	messageToClient.Command = message.DelSvgReq

	for podId, podEntry := range fieldContext.podMap {
		for _, fieldObject := range podEntry.objectMap {
			if fieldObject.fileName == fileName {
				messageToClient.JsonData = []byte(fmt.Sprintf("{ \"id\": \"%s\"}", fieldObject.id))
				for _, websocketConnectionContext := range podEntry.websocketConnectionMap {
					fieldContext.queueMessageToClient(websocketConnectionContext.sessionId, messageToClient)
				}
				fieldContext.delTerrainObject(podId, fieldObject)
				break
			}
		}
	}
}
Exemple #12
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)
		}
	}
}
Exemple #13
0
func (fieldContext *FieldContextDef) handlePingReq(messageFromClient *message.MessageFromClientDef) *gkerr.GkErrDef {

	var messageToClient *message.MessageToClientDef = new(message.MessageToClientDef)
	var pingReq pingReqDef
	var gkErr *gkerr.GkErrDef
	var err error

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

	messageToClient.Command = message.PingRes
	messageToClient.JsonData = []byte(fmt.Sprintf("{ \"pingId\": \"%s\" }", pingReq.PingId))

	fieldContext.queueMessageToClient(messageFromClient.SessionId, messageToClient)

	return nil
}
Exemple #14
0
func (fieldContext *FieldContextDef) addDandelion() {

	var messageToClient *message.MessageToClientDef = new(message.MessageToClientDef)
	var svgJsonData *message.SvgJsonDataDef = new(message.SvgJsonDataDef)
	var fileName = "dandelion"

	svgJsonData.Id = fieldContext.getNextObjectId()
	//	svgJsonData.IsoXYZ.X = int16(rand.Int31n(50))
	//	svgJsonData.IsoXYZ.Y = int16(rand.Int31n(50))

	var podId int32 = firstPodId // dandelions are only in the first pod

	for _, websocketConnectionContext := range fieldContext.podMap[podId].websocketConnectionMap {
		//		var singleSession *ses.SingleSessionDef
		//		singleSession = fieldContext.sessionContext.GetSessionFromId(websocketConnectionContext.sessionId)
		var terrainJson *terrainJsonDef
		terrainJson = fieldContext.podMap[podId].terrainJson

		var index = rand.Int31n(int32(len(terrainJson.jsonMapData.TileList)))

		if terrainJson.jsonMapData.TileList[index].Terrain == "grass" {

			svgJsonData.IsoXYZ.X = int16(terrainJson.jsonMapData.TileList[index].X)
			svgJsonData.IsoXYZ.Y = int16(terrainJson.jsonMapData.TileList[index].Y)
			//svgJsonData.IsoXYZ.Z = int16(terrainJson.jsonMapData.TileList[index].Z)
			svgJsonData.IsoXYZ.Z = 0

			messageToClient.BuildSvgMessageToClient(fieldContext.terrainSvgDir, message.AddSvgReq, fileName, svgJsonData)

			fieldContext.queueMessageToClient(websocketConnectionContext.sessionId, messageToClient)

			var fieldObject *fieldObjectDef = new(fieldObjectDef)
			fieldObject.id = svgJsonData.Id
			fieldObject.fileName = fileName
			fieldObject.isoXYZ = svgJsonData.IsoXYZ
			fieldContext.addTerrainObject(fieldObject, podId)
		}
	}
}
Exemple #15
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)
		}
	}
}
Exemple #16
0
func (fieldContext *FieldContextDef) uploadNewPodInfo(websocketConnectionContext *websocketConnectionContextDef, podId int32) *gkerr.GkErrDef {

	var gkErr *gkerr.GkErrDef

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

	gkErr = fieldContext.sendAllAvatarObjects(podId, websocketConnectionContext)
	if gkErr != nil {
		return gkErr
	}

	var messageToClient *message.MessageToClientDef = new(message.MessageToClientDef)
	var podTitle string = fieldContext.podMap[podId].title
	messageToClient.Command = message.NewPodTitleReq
	messageToClient.JsonData = []byte(fmt.Sprintf("{ \"podTitle\": \"%s\" }", podTitle))
	messageToClient.Data = make([]byte, 0, 0)

	fieldContext.queueMessageToClient(websocketConnectionContext.sessionId, messageToClient)

	return nil
}
Exemple #17
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
}