Ejemplo n.º 1
0
        public static void RandomizePlayers()
        {
            if (mArgs.Count != 2)
            {
                Log("Usage: RandomizePlayers <idTournament>");
                return;
            }

            if (!long.TryParse(mArgs[1], out long idTournament))
            {
                throw new ArgumentException("idTournament");
            }

            using (var c = new PostgresqlDataLayer(mDbConfig).GetConn())
            {
                var t = c.BeginTransaction();

                try
                {
                    var players = c.Query <Player>("SELECT id, idUser FROM players p JOIN teamplayers tp ON tp.idPlayer = p.id JOIN tournamentteams tt ON tt.idteam = tp.idTeam WHERE tt.idTournament = @idTournament", new { idTournament }, t);

                    var sql = new StringBuilder();

                    foreach (var player in players)
                    {
                        var name    = NameGenerator.GetRandomName();
                        var surname = NameGenerator.GetRandomSurname();

                        sql.AppendFormat("UPDATE players SET name = '{0}', surname='{1}' WHERE id = {2};\r\n", name, surname, player.Id);
                        sql.AppendFormat("UPDATE users SET name = '{0}' WHERE id = {1};\r\n", name + " " + surname, player.IdUser);
                    }

                    var query = sql.ToString();
                    c.Execute(query, t);

                    t.Commit();
                }
                catch
                {
                    t.Rollback();
                    throw;
                }
            }
        }
Ejemplo n.º 2
0
        public static void RandomizeTeams()
        {
            if (mArgs.Count != 2)
            {
                Log("Usage: RandomizeTeams <idTournament>");
                return;
            }

            if (!long.TryParse(mArgs[1], out long idTournament))
            {
                throw new ArgumentException("idTournament");
            }

            using (var c = new PostgresqlDataLayer(mDbConfig).GetConn())
            {
                var t = c.BeginTransaction();

                try
                {
                    var teams = c.Query <Team>("SELECT id, name FROM teams t JOIN tournamentteams tt ON tt.idTeam = t.id WHERE tt.idTournament = @idTournament", new { idTournament }, t);

                    var sql = new StringBuilder();

                    foreach (var team in teams)
                    {
                        sql.AppendFormat("UPDATE teams SET name = '{0}', logoImgUrl = '' WHERE id = {1};\r\n", NameGenerator.GetTeamName(team.Id), team.Id);
                    }

                    var query = sql.ToString();
                    c.Execute(query, t);

                    t.Commit();
                }
                catch
                {
                    t.Rollback();
                    throw;
                }
            }
        }