public void addScore(long score) { this.score += score; PlayerScoreMessage message = new PlayerScoreMessage(game.gamePlayers); SocketMessage socketMessage = new SocketMessage(); socketMessage.type = PlayerScoreMessage.TYPE; socketMessage.data = JsonConvert.SerializeObject(message); game.SendMessage(JsonConvert.SerializeObject(socketMessage)); game.newBestScore(this.score); }
void ParseMessage(object sender, MessageEventArgs e) { SocketMessage bsObj = JsonConvert.DeserializeObject <SocketMessage>(e.Data); ping = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond - bsObj.sendTime; switch (bsObj.type) { case EnemiesDataMessage.TYPE: EnemiesDataMessage enemiesData = JsonConvert.DeserializeObject <EnemiesDataMessage>(bsObj.data); lock (x) { enemies = new List <Enemy>(); if (enemiesData.enemiesList != null) { foreach (EnemyDummy dummy in enemiesData.enemiesList) { enemies.Add(Enemy.createFromDummy(dummy)); } } } break; case PositionChangedMessage.TYPE: PositionChangedMessage positionChange = JsonConvert.DeserializeObject <PositionChangedMessage>(bsObj.data); GamePlayer gamePlayer = currentGame.gamePlayers.getPlayer(positionChange.playerId); gamePlayer.position = positionChange.position; break; case PlayerScoreMessage.TYPE: PlayerScoreMessage data = JsonConvert.DeserializeObject <PlayerScoreMessage>(bsObj.data); currentGame.gamePlayers = data.players; break; case BulletsDataReceiveMessage.TYPE: BulletsDataReceiveMessage bulletsData = JsonConvert.DeserializeObject <BulletsDataReceiveMessage>(bsObj.data); lock (bulletLock) { bullets = new List <Bullet>(); if (bulletsData.bulletList != null) { bullets = bulletsData.bulletList; } } break; case PlayerAngleMessage.TYPE: PlayerAngleMessage angleData = JsonConvert.DeserializeObject <PlayerAngleMessage>(bsObj.data); angle = angleData.angle; break; case BestScoreMessage.TYPE: BestScoreMessage scoreData = JsonConvert.DeserializeObject <BestScoreMessage>(bsObj.data); if (currentGame != null) { currentGame.bestScore = scoreData.score; } break; case GoldenToothMessage.TYPE: GoldenToothMessage goldenToothData = JsonConvert.DeserializeObject <GoldenToothMessage>(bsObj.data); if (goldenTooth == null) { goldenTooth = new GoldenTooth(); } goldenTooth.position = goldenToothData.position; break; case PlayerDataMessage.TYPE: PlayerDataMessage playerData = JsonConvert.DeserializeObject <PlayerDataMessage>(bsObj.data); if (currentPlayer == null) { currentPlayer = new Player(playerData.id, playerData.name); } break; case GameDataMessage.TYPE: GameFacade game = JsonConvert.DeserializeObject <GameFacade>(bsObj.data); if (currentGame == null) { currentGame = new GameFacade(); } currentGame.name = game.name; lock (playerLock) { if (game.gamePlayers.P1 != null) { currentGame.gamePlayers.P1 = game.gamePlayers.P1; IMoveStrategy strategy = new P1MoveStrategy(); currentGame.gamePlayers.P1.moveLeft = new MoveLeftCommand(strategy, game.gamePlayers.P1.position); currentGame.gamePlayers.P1.moveRight = new MoveRightCommand(strategy, game.gamePlayers.P1.position); } else { currentGame.gamePlayers.P1 = null; } if (game.gamePlayers.P2 != null) { currentGame.gamePlayers.P2 = game.gamePlayers.P2; IMoveStrategy strategy = new P2MoveStrategy(); currentGame.gamePlayers.P2.moveLeft = new MoveLeftCommand(strategy, game.gamePlayers.P2.position); currentGame.gamePlayers.P2.moveRight = new MoveRightCommand(strategy, game.gamePlayers.P2.position); } else { currentGame.gamePlayers.P2 = null; } if (game.gamePlayers.P3 != null) { currentGame.gamePlayers.P3 = game.gamePlayers.P3; IMoveStrategy strategy = new P3MoveStrategy(); currentGame.gamePlayers.P3.moveLeft = new MoveLeftCommand(strategy, game.gamePlayers.P3.position); currentGame.gamePlayers.P3.moveRight = new MoveRightCommand(strategy, game.gamePlayers.P3.position); } else { currentGame.gamePlayers.P3 = null; } if (game.gamePlayers.P4 != null) { currentGame.gamePlayers.P4 = game.gamePlayers.P4; IMoveStrategy strategy = new P4MoveStrategy(); currentGame.gamePlayers.P4.moveLeft = new MoveLeftCommand(strategy, game.gamePlayers.P4.position); currentGame.gamePlayers.P4.moveRight = new MoveRightCommand(strategy, game.gamePlayers.P4.position); } else { currentGame.gamePlayers.P4 = null; } } break; } }