private bool UpdateGame(PlayMoveRq request, Model.Game game, out string errorMessage) { errorMessage = string.Empty; string myFacebookId = User.GetFacebookId(); //Update moves game.Moves.AddRange(request.LastTurn.PlayerMoves.ToDataContract()); //Update turn var opponant = game.GetMyOpponant(myFacebookId); game.TurnOfPlayer = opponant.FacebookId; game.LastTurn = request.LastTurn.ToDataContract(); //Remove played tiles var player = game.GetMyPlayer(myFacebookId); var newTiles = new List<string>(); int playedTileCount = 0; for (int i = 0; i < player.Tiles.Count; i++) { if (i > 5 || !request.LastTurn.PlayerMoves.Exists(m => string.Equals(m.Tile, player.Tiles[i]))) { newTiles.Add(player.Tiles[i]); } else { playedTileCount++; } } player.Tiles = newTiles; if (playedTileCount != request.LastTurn.PlayerMoves.Count) { errorMessage = "Invalid tiles played! Please refresh and try again!"; return false; } // Update points player.Points = player.Points + request.LastTurn.Points; // Check if winner if (player.Tiles.Count == 0) { game.Winner = new Model.Winner() { RemainingTiles = opponant.Tiles.Count, }; player.Points += opponant.Tiles.Count; opponant.Points -= opponant.Tiles.Count; if (player.Points >= opponant.Points) { game.Winner.FacebookId = player.FacebookId; game.Winner.WonBy = player.Points - opponant.Points; } else { game.Winner.FacebookId = opponant.FacebookId; game.Winner.WonBy = opponant.Points - player.Points; } } //Update game in elastic store if (game.Winner != null) { _gameDataManager.SaveGame(game); } //Update game in the main store if (!_gameFileManager.SaveGame(game)) { errorMessage = "Server error! Couldn't update your move. Try again.."; return false; } return true; }
private void NotifyGameHost(Model.Game game) { try { var context = GlobalHost.ConnectionManager.GetHubContext<GameHub>(); string connectionId = _connectionManager.GetConnectionId(game.GetMyOpponant(User.GetFacebookId()).FacebookId); context.Clients.Client(connectionId).GameStarted(game.ToViewModel()); } catch (Exception ex) { Logger.LogException(ex, _source, "NotifyGameHost", Severity.Major); } }