private void ClientUpdateGameListReceived(object sender, UpdateGameListReceivedEventArgs e)
        {
            //посмотрим, есть ли в списке
            var findedGame = GamesInfo.FirstOrDefault(x => x.SessionId == e.gameInfo.SessionId);

            if (findedGame != null)
            {
                if (e.gameInfo.Players.Count != 1 || !e.gameInfo.IsActive)
                {
                    GamesInfo.Remove(findedGame);
                }
            }
            else
            {
                if (e.gameInfo.Players.Count > 0)
                {
                    GamesInfo.Add(e.gameInfo);
                }
            }

            var findedGameForDelete = GamesInfo.FirstOrDefault(x => x.SessionId == "1");

            GamesInfo.Remove(findedGameForDelete);

            if (GamesInfo.Count == 0)
            {
                GamesInfo.Add(new GameInformation()
                {
                    SessionId = "1", Parameters = "нет игр"
                });
            }
        }
 private void Start()
 {
     string[] gameDirectories = Directory.GetDirectories(Application.streamingAssetsPath);
     if (gameDirectories.Length == 0)
     {
         startGame(""); // Will display scene error on loading game
     }
     else if (gameDirectories.Length == 1)
     {
         startGame(Path.GetFileName(gameDirectories[0]));
     }
     else
     {
         if (File.Exists(Application.streamingAssetsPath + "/GamesInfo.txt"))
         {
             gamesInfo = JsonUtility.FromJson <GamesInfo>(File.ReadAllText(Application.streamingAssetsPath + "/GamesInfo.txt"));
         }
         foreach (string gameDirectory in gameDirectories)
         {
             GameObject newGame = Instantiate(gamePrefab);
             newGame.name = Path.GetFileName(gameDirectory);
             newGame.GetComponentInChildren <TMPro.TMP_Text>().text = newGame.name;
             newGame.transform.SetParent(scrollContent.transform);
             newGame.transform.localScale = new Vector3(1, 1, 1);
         }
     }
 }
        public async void Refresh()
        {
            var list = await Client.GetGamesAsync();

            GamesInfo.Clear();
            if (list == null)
            {
                GamesInfo.Add(new GameInformation()
                {
                    SessionId = "1", Parameters = "сервер не доступен"
                });
            }
            else
            {
                foreach (var gameInformation in list)
                {
                    GamesInfo.Add(gameInformation);
                }

                if (GamesInfo.Count == 0)
                {
                    GamesInfo.Add(new GameInformation()
                    {
                        SessionId = "1", Parameters = "нет игр"
                    });
                }
            }
        }
Beispiel #4
0
        /// <summary>
        /// It will insert new game if no game is available or updapte user in game
        /// </summary>
        /// <param name="userId">Uniqie id of user</param>
        /// <returns>GameId: Usinque id of game</returns>
        public int?UpsertUser(int userId)
        {
            int?      gameId    = -1;
            GamesInfo gamesInfo = _gamesRepository.GetAvailableGame();

            if (gamesInfo == null || gamesInfo.GameId <= 0)
            {
                GamesInfo newGameInfo = new GamesInfo()
                {
                    User1Id      = userId,
                    NumberOfRows = 3
                };
                gameId = _gamesRepository.InsertGameInfo(newGameInfo);
            }
            else
            {
                gamesInfo.User2Id = userId;
                bool isSuccess = _gamesRepository.UpdateGameInfo((int)gamesInfo.GameId, gamesInfo);
                if (isSuccess)
                {
                    gameId = gamesInfo.GameId;
                }
            }
            return(gameId);
        }
Beispiel #5
0
        /// <summary>
        /// It will update the winner for perticular game
        /// </summary>
        /// <param name="gameId">Unique id of game</param>
        /// <param name="userId">Unique id of user</param>
        /// <returns>Boolean indicating success/failure</returns>
        public bool UpdateWinner(int gameId, int userId)
        {
            GamesInfo gamesInfo = new GamesInfo
            {
                Winner = userId
            };
            bool isSuccess = _gamesRepository.UpdateGameInfo(gameId, gamesInfo);

            return(isSuccess);
        }
Beispiel #6
0
 private object GetDbParamsFromGamesInfo(int gameId, GamesInfo gamesInfo)
 {
     return(new
     {
         v_gameId = gameId,
         v_user1Id = gamesInfo.User1Id,
         v_user2Id = gamesInfo.User2Id,
         v_winner = gamesInfo.Winner,
         v_numberOfRows = gamesInfo.NumberOfRows
     });
 }
Beispiel #7
0
 private string GetGameStatus(GamesInfo gamesInfo)
 {
     if (gamesInfo.Winner != null)
     {
         return("Completed");
     }
     else
     {
         return(gamesInfo.User2Id != null ? "In Progress" : "Waiting for Player");
     }
 }
Beispiel #8
0
        /// <summary>
        /// It will insert record in Games table
        /// </summary>
        /// <param name="gamesInfo">Params realted to games like userId, numberOf rows</param>
        /// <returns>GameId:- unique id of game</returns>
        public int?InsertGameInfo(GamesInfo gamesInfo)
        {
            int?   lastInsertedId = -1;
            string query          = $@"insert into Games (User1Id, User2Id, Winner, NumberOfRows)
                                          values (@v_user1Id, @v_user2Id, @v_winner, @v_numberOfRows);
                              select last_insert_rowid();";
            var    dbParams       = GetDbParamsFromGamesInfo(-1, gamesInfo);

            using (var connection = new SQLiteConnection(_connString))
            {
                lastInsertedId = connection.Query <int>(query, dbParams, commandType: CommandType.Text)?.FirstOrDefault();
            }
            return(lastInsertedId);
        }
Beispiel #9
0
        /// <summary>
        /// Updates the record indb for perticualr game Id
        /// </summary>
        /// <param name="gameId">Unique id of game</param>
        /// <param name="gamesInfo">Params realted to games like userId, numberOf rows</param>
        /// <returns>Boolen indicating success/failure</returns>
        public bool UpdateGameInfo(int gameId, GamesInfo gamesInfo)
        {
            bool   isSuccessfullyUpdated = false;
            string query    = $@"update Games
                              set User1Id = ifnull(@v_user1Id, User1Id),
                                  User2Id = ifnull(@v_user2Id, User2Id),
                                  Winner = ifnull(@v_winner, Winner),
                                  NumberOfRows = ifnull(@v_numberOfRows, NumberOfRows)
                              where GameId = @v_gameId;";
            var    dbParams = GetDbParamsFromGamesInfo(gameId, gamesInfo);

            using (var connection = new SQLiteConnection(_connString))
            {
                isSuccessfullyUpdated = connection.Execute(query, dbParams, commandType: CommandType.Text) > 0;
            }
            return(isSuccessfullyUpdated);
        }
Beispiel #10
0
        private GamesDetailInfo GetGamesDetailInfo(GamesInfo gamesInfo)
        {
            GamesDetailInfo gamesDetailInfo = new GamesDetailInfo();

            if (gamesInfo != null)
            {
                gamesDetailInfo.GameId       = gamesInfo.GameId;
                gamesDetailInfo.NumberOfRows = gamesInfo.NumberOfRows;
                UsersInfo user1Info = _usersRepository.GetUsersInfo((int)gamesInfo.User1Id);
                gamesDetailInfo.User1Id      = gamesInfo.User1Id;
                gamesDetailInfo.User1EmailId = user1Info.EmailId;
                if (gamesInfo.User2Id != null && gamesInfo.User2Id > 0)
                {
                    UsersInfo user2Info = _usersRepository.GetUsersInfo((int)gamesInfo.User2Id);
                    gamesDetailInfo.User2Id      = gamesInfo.User2Id;
                    gamesDetailInfo.User2EmailId = user2Info.EmailId;
                }
                gamesDetailInfo.GameStatus = GetGameStatus(gamesInfo);
            }
            return(gamesDetailInfo);
        }
        private async void ListView_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
        {
            var selectedGame = (GameInformation)e.AddedItems.FirstOrDefault();

            if (selectedGame != null)
            {
                if (selectedGame.SessionId == "1")
                {
                    var clienInfo = await Client.RegisterAsync(uid, "Player");

                    if (clienInfo != null)
                    {
                        GamesInfo.Clear();
                        Refresh();
                    }
                }
                else
                {
                    //OnSelectGame(selectedGame);
                    Client.JoinGameAsync(uid, selectedGame.SessionId);
                }
            }
        }
Beispiel #12
0
        /// <summary>
        /// It will fetch games info based on gameId.
        /// </summary>
        /// <param name="gameId">UniqueId of game</param>
        /// <returns>Details inforamation about game</returns>
        public GamesDetailInfo GetGamesInfo(int gameId)
        {
            GamesInfo gamesInfo = _gamesRepository.GetGamesInfo(gameId);

            return(GetGamesDetailInfo(gamesInfo));
        }