Ejemplo n.º 1
0
        private void SaveGameStatus(string gameId, Game game, Player opponant, Player player, UserSession session)
        {
            game.Status = GameStatus.Finished.ToString();
            _gameProvider.SaveGameStatus(gameId, GameStatus.Finished);
            var tilesDiff = opponant.Tiles.Count;
            player.Points = player.Points + tilesDiff;
            opponant.Points = opponant.Points - tilesDiff;

            var winner = player.Points >= opponant.Points ? player : opponant;
            _gameProvider.SetGameWinner(gameId, winner.Id);

            int totalPoints = 0, level = 0;
            _gameProvider.UpdatePlayerProfile(winner.Id, winner.Points, out level, out totalPoints);
            if (string.Equals(session.Account.Id, winner.Id))
            {
                session.Account.Points = totalPoints;
                session.Account.Level = level;
            }
        }
Ejemplo n.º 2
0
 public bool SaveGamePlayerStatus(string gameId, Player player)
 {
     return _gameDataProvider.SaveGamePlayerStatus(gameId, player);
 }
Ejemplo n.º 3
0
        private Move PlayBotMove(UserSession session, Player bot, int gameIndex)
        {
            var game = _gameProvider.GetGame(session.ActiveGames[gameIndex]);
            var moves = _gameProvider.GetGameMoves(game.Id);
            var gameTiles = ToGameTiles(moves);
            List<string> tilesToBePlayed = GetMaxTilesCanBePlayed(bot.Tiles);
            int maxPoints = 0;
            List<GameTile> nextMoves = new List<GameTile>();
            foreach (var tile in tilesToBePlayed)
            {

                //Get all tile moves which match the color of the tile
                List<GameTile> matchingGameTiles = GetMatchingTileMoves(tile, gameTiles);
                //for each tile move get all sorrounding empty tile positions.
                foreach (var matchingGameTile in matchingGameTiles)
                {
                    //Get all empty position for the matching tile.
                    List<GameTile> emptyPositions = GetEmptyTilePositions(gameTiles, matchingGameTile, tile);
                    //for each empty position place all the tiles to be played in all direction and get the maximum points.
                    var myTiles = new List<string>();
                    myTiles.AddRange(tilesToBePlayed);
                    var tileIndex = myTiles.FindIndex(t => t == tile);
                    myTiles.RemoveAt(tileIndex);
                    foreach (var emptyPosition in emptyPositions)
                    {
                        GetMaxPoints("-x", gameTiles, tile, myTiles, emptyPosition, matchingGameTile, ref maxPoints, ref nextMoves);
                        GetMaxPoints("+x", gameTiles, tile, myTiles, emptyPosition, matchingGameTile, ref maxPoints, ref nextMoves);
                        GetMaxPoints("-y", gameTiles, tile, myTiles, emptyPosition, matchingGameTile, ref maxPoints, ref nextMoves);
                        GetMaxPoints("+y", gameTiles, tile, myTiles, emptyPosition, matchingGameTile, ref maxPoints, ref nextMoves);
                    }
                }
            }
            //parse nextmoves into bot move and return
            Move move = new Move() {Player = bot.Id, Points = maxPoints};
            List<string> tileMoves = new List<string>();
            foreach (var nextMove in nextMoves)
            {
                tileMoves.Add(string.Format("{0},{1},{2}", nextMove.Row, nextMove.Col, nextMove.Tile));
            }
            move.MoveCode = string.Join("|", tileMoves);
            return move;
        }
Ejemplo n.º 4
0
 private void GiveNextTilesToPlayer(Game game, ref Player player, Move userMove)
 {
     List<string> gameTiles = _gameProvider.GetGameTiles(game.Id);
     var parts = userMove.MoveCode.Split('|');
     foreach (var part in parts)
     {
         var mvs = part.Split(',');
         player.Tiles.Remove(mvs[2]);
     }
     player.TilesRemaining = player.TilesRemaining - parts.Length;
     if (player.TilesRemaining > 6)
         player.Tiles.AddRange(_movesDataProvider.GetRandomTiles(parts.Length, ref gameTiles));
     else
         player.Tiles.AddRange(_movesDataProvider.GetRandomTiles(player.TilesRemaining - player.Tiles.Count, ref gameTiles));
     _gameProvider.SetGameTiles(game.Id, gameTiles);
 }
Ejemplo n.º 5
0
 public bool SetPlayerTurn(string gameId, Player player)
 {
     if (string.IsNullOrWhiteSpace(player.GameConnectionid))
     {
         var gameConnection = Connections.GetAsync(Relations.GamePlayer, gameId, player.Id).Result;
         player.GameConnectionid = gameConnection.Id;
     }
     Connection conn = new Connection(Relations.GamePlayer, player.GameConnectionid);
     conn.Set("isactive", player.IsActive);
     conn.SaveAsync();
     return true;
 }
Ejemplo n.º 6
0
 public bool SaveGamePlayerStatus(string gameId, Player player)
 {
     if (string.IsNullOrWhiteSpace(player.GameConnectionid))
     {
         var gameConnection = Connections.GetAsync(Relations.GamePlayer, gameId, player.Id).Result;
         player.GameConnectionid = gameConnection.Id;
     }
     Connection conn = new Connection(Relations.GamePlayer, player.GameConnectionid);
     conn.Set("points", player.Points);
     conn.Set("tiles", string.Join("|", player.Tiles));
     conn.Set("isactive", player.IsActive);
     conn.Set("tiles_remaining", player.TilesRemaining);
     conn.SaveAsync();
     return true;
 }
Ejemplo n.º 7
0
 public static Player ToModelPlayer(this User user, Connection gameConnection)
 {
     if (user == null)
         return null;
     Player player = new Player(user.ToModelAccount(null))
     {
         Points = gameConnection.Get<int>("points"), // Game points
         IsActive = gameConnection.Get<bool>("isactive"),
         IsHost = gameConnection.Get<bool>("ishost"),
         Tiles = gameConnection.Get<string>("tiles").Split('|').ToList(),
         TilesRemaining = gameConnection.Get<int>("tiles_remaining"),
         GameConnectionid = gameConnection.Id
     };
     return player;
 }