Пример #1
0
        public static (Game, List <Ranking>) RecordIRLGame(string[] members, float[] scores, Server server, LeagueConfig leagueConfig)
        {
            var dbCon = DBConnection.Instance();

            string logId = GetIRLLogId();

            if (dbCon.IsConnect())
            {
                using var command  = SqlClientFactory.Instance.CreateCommand();
                command.Connection = dbCon.Connection;

                var scoreColumns = scores == null
                    ? ""
                    : $"{User1ScoreCol}, {User2ScoreCol}, {User3ScoreCol}{(scores.Length > 3 ? $", {User3ScoreCol}" : "")}";

                var scoreValues = scores == null
                    ? ""
                    : $", @score1, @score2, @score3{(scores.Length > 3 ? $", @score4" : "")}";

                command.CommandText = $"INSERT INTO {GameTableName} ({User1IdCol}, {User2IdCol}, {User3IdCol}, {User4IdCol}, {IdCol}, {PlatformCol}, {ServerIdCol}{scoreColumns}) " +
                                      $"VALUES ({members[0]}, {members[1]}, {members[2]}, {members[3]}, @logId, @gameType, @serverId{scoreValues});";
                command.CommandType = CommandType.Text;

                command.Parameters.Add(new SqlParameter("@logId", SqlDbType.NVarChar)
                {
                    Value = logId
                });
                command.Parameters.Add(new SqlParameter("@gameType", SqlDbType.NVarChar)
                {
                    Value = GameType.IRL
                });
                command.Parameters.Add(new SqlParameter("@serverId", SqlDbType.NVarChar)
                {
                    Value = server.Id
                });
                if (scores != null)
                {
                    for (int i = 0; i < scores.Length && i < 4; i++)
                    {
                        command.Parameters.Add(new SqlParameter($"@score{i + 1}", SqlDbType.Float)
                        {
                            Value = scores[i]
                        });
                    }
                }
                command.ExecuteNonQuery();

                var game     = GetGameFromLogId(logId, server);
                var rankings = RankingDbService.UpdateRankings(game, leagueConfig);

                return(game, rankings);
            }
            throw (new DbConnectionException());
        }
Пример #2
0
        public static (Game, List <Ranking>) RecordOnlineGame(RiichiGame gameLog, Server serverWithUsers)
        {
            var dbCon = DBConnection.Instance();

            var logId       = gameLog.Ref;
            var fullLog     = gameLog.FullLog;
            var gameType    = gameLog.GameType;
            var playerNames = gameLog.Names;
            var playerMjIds = gameLog.UserIds; //mahjongsoul internal Ids
            var scores      = gameLog.FinalScores;
            var userList    = serverWithUsers.Users;
            var platform    = gameLog.GameType;

            // TODO feed timestamp from game data
            if (playerNames.Length < 4)
            {
                throw new Exception("No sanma allowed");
            }

            var nameIdScorePos = new List <(string, string, int, int)>(); //0=start east, 1=start south,...

            for (int i = 0; i < 4; i++)
            {
                nameIdScorePos.Add((playerNames[i], playerMjIds[i], scores[i], i));
            }
            // We sort them by comparing their scores minus their initial pos, since initPos<<<score it's ok, I guess?
            nameIdScorePos.Sort((tuple1, tuple2) => (tuple2.Item3 - tuple2.Item4).CompareTo((tuple1.Item3 - tuple1.Item4)));
            int[]    sortedScores    = nameIdScorePos.Select(x => x.Item3).ToArray();
            string[] sortedNames     = nameIdScorePos.Select(x => x.Item1).ToArray();
            string[] sortedMjIds     = nameIdScorePos.Select(x => x.Item2).ToArray();
            string[] sortedPlayerIds = new string[4];

            List <int> toRecordMjId = new List <int>();

            for (int i = 0; i < 4; i++)
            {
                User user = null;
                switch (gameType)
                {
                //Get the User objects from the Name or Ids present in the log
                case GameType.Mahjsoul:
                    user = userList.Find(x => x.MahjsoulUserId == sortedMjIds[i]);
                    if (user == null)
                    {
                        user = userList.Find(x => x.MahjsoulName == sortedNames[i]);
                        toRecordMjId.Add(i);
                    }
                    break;

                case GameType.Tenhou:
                    user = userList.Find(x => x.TenhouName == sortedNames[i]);
                    break;

                default:
                    throw (new Exception("Bad game type, can't record"));
                }
                if (user == null)
                {
                    throw (new Exception($"couldn't find player with this name on the server: {sortedNames[i]}"));
                }
                sortedPlayerIds[i] = user.Id;
            }

            if (dbCon.IsConnect())
            {
                using var command   = SqlClientFactory.Instance.CreateCommand();
                command.Connection  = dbCon.Connection;
                command.CommandText = $"INSERT INTO {GameTableName} ({User1IdCol}, {User2IdCol}, {User3IdCol}, {User4IdCol}, {User1ScoreCol}, {User2ScoreCol}, {User3ScoreCol}, {User4ScoreCol}, {IdCol}, {FullLogIdCol}, {PlatformCol}, {ServerIdCol}) " +
                                      $"VALUES (@playerId1, @playerId2, @playerId3, @playerId4, {sortedScores[0]}, {sortedScores[1]}, {sortedScores[2]}, {sortedScores[3]}, @logId, @fullLog, @platform, @serverId);";
                command.CommandType = CommandType.Text;

                command.Parameters.Add(new SqlParameter("@playerId1", SqlDbType.NVarChar)
                {
                    Value = sortedPlayerIds[0]
                });
                command.Parameters.Add(new SqlParameter("@playerId2", SqlDbType.NVarChar)
                {
                    Value = sortedPlayerIds[1]
                });
                command.Parameters.Add(new SqlParameter("@playerId3", SqlDbType.NVarChar)
                {
                    Value = sortedPlayerIds[2]
                });
                command.Parameters.Add(new SqlParameter("@playerId4", SqlDbType.NVarChar)
                {
                    Value = sortedPlayerIds[3]
                });
                command.Parameters.Add(new SqlParameter("@fullLog", SqlDbType.NVarChar)
                {
                    Value = fullLog
                });
                command.Parameters.Add(new SqlParameter("@logId", SqlDbType.NVarChar)
                {
                    Value = logId
                });
                command.Parameters.Add(new SqlParameter("@platform", SqlDbType.NVarChar)
                {
                    Value = platform
                });
                command.Parameters.Add(new SqlParameter("@serverId", SqlDbType.NVarChar)
                {
                    Value = serverWithUsers.Id
                });
                command.ExecuteNonQuery();

                //Add the majsouldID to the new users:
                foreach (var idx in toRecordMjId)
                {
                    UserDbService.SetMahjsoulUserId(sortedPlayerIds[idx], sortedMjIds[idx]);
                }
                var config   = LeagueConfigDbService.GetLeagueConfig(serverWithUsers.LeagueConfigId);
                var game     = GetGameFromLogId(logId, serverWithUsers);
                var rankings = RankingDbService.UpdateRankings(game, config);

                return(game, rankings);
            }
            throw (new DbConnectionException());
        }