Esempio n. 1
0
        public static async Task MarkEntrantDoneAsync(RaceItem Race, ulong UserId)
        {
            //Get the required information from Discord
            var raceServer  = client.GetGuild(Globals.GuildId);
            var entrant     = raceServer.GetUser(UserId);
            var raceChannel = raceServer.GetTextChannel(Race.TextChannelId);

            //Attempt to update the database. If the update function returns null, then the user isn't entered in the race
            DatabaseHandler database           = new DatabaseHandler(Globals.MySqlConnectionString);
            EntrantItem     entrantInformation = database.MarkEntrantFinished(Race.RaceId, UserId, Race.StartTime);

            database.Dispose();

            //if we get a result back from MarkEntrantFinished, let the racer know their place and finish time
            if (entrantInformation == null)
            {
                return;
            }

            var raceRole = raceServer.GetRole(Race.RoleId);
            await entrant.RemoveRoleAsync(raceRole);

            await raceChannel.SendMessageAsync(entrant.Mention + ", you finished in **" + AddOrdinal(entrantInformation.Place) + "** place with a time of **" + entrantInformation.FinishedTime + "**");

            await AttemptRaceFinishAsync(Race);
        }
Esempio n. 2
0
        public Task CommandNotDone()
        {
            //We can't process this message if it's not in a race channel, so we need to make sure it's coming from one
            SocketTextChannel messageChannel = (SocketTextChannel)Context.Client.GetChannel(Context.Channel.Id);

            if (!(messageChannel.CategoryId == Globals.RacesCategoryId))
            {
                return(Task.CompletedTask);
            }

            //we need to get the race information from the database
            ulong           RaceId   = GetRaceId(Context.Channel.Name);
            DatabaseHandler database = new DatabaseHandler(Globals.MySqlConnectionString);
            RaceItem        race     = database.GetRaceInformation(RaceId);
            EntrantItem     entrant  = database.GetEntrantInformation(RaceId, Context.User.Id);

            database.Dispose();

            //don't continue with this command if the entrant isn't marked done.
            if (entrant.Status != "Done")
            {
                return(Task.CompletedTask);
            }

            if (race.Status != "In Progress" && race.Status != "Recently Completed")
            {
                return(Task.CompletedTask);
            }

            return(Task.Factory.StartNew(() => RaceManager.MarkEntrantNotDoneAsync(race, Context.User.Id)));
        }
Esempio n. 3
0
        public static async Task ForfeitEntrantAsync(RaceItem Race, ulong UserId)
        {
            //get required info from Discord
            var guild       = client.GetGuild(Globals.GuildId);
            var raceChannel = guild.GetTextChannel(Race.TextChannelId);
            var raceRole    = guild.GetRole(Race.RoleId);
            var entrant     = guild.GetUser(UserId);

            DatabaseHandler database = new DatabaseHandler(Globals.MySqlConnectionString);

            //Get the get the entrant's status.
            EntrantItem entrantStatus = database.GetEntrantInformation(Race.RaceId, UserId);

            //if no result was returned, the user isn't entered
            if (entrantStatus == null)
            {
                database.Dispose();
                return;
            }

            //We can't forfeit a player who isn't still racing.
            if (entrantStatus.Status != "Ready")
            {
                database.Dispose();
                return;
            }

            //attempt to forfeit the racer
            if (!database.UpdateEntry(Race.RaceId, UserId, "Forfeited"))
            {
                await entrant.RemoveRoleAsync(raceRole);

                await raceChannel.SendMessageAsync(entrant.Mention + ", you have forfeited from the race.");
                await AttemptRaceFinishAsync(Race);
            }
            //UpdateEntry shouldn't return true since we've already checked to see if the racer is entered, but if it does, we need to let the racer know.
            else
            {
                await raceChannel.SendMessageAsync(entrant.Mention + ", something went wrong when I tried to remove you. Please let a moderator know.");
            }

            database.Dispose();
        }
Esempio n. 4
0
        public EntrantItem MarkEntrantFinished(ulong RaceId, ulong UserId, DateTime StartTime)
        {
            MySqlCommand cmd;
            int          result = 0;

            TimeSpan raceTime = StartTime - DateTime.Now;

            EntrantsSummary raceSummary = GetEntrantsSummary(RaceId);
            EntrantItem     entrant     = null;

            int place = raceSummary.Done + 1;

            try
            {
                //Build the command
                cmd             = _connection.CreateCommand();
                cmd.CommandText = "UPDATE entries SET Status = 'Done',FinishedTime = @FinishedTime,Place = @Place  WHERE RaceId = @RaceId AND UserId = @UserId";
                cmd.Parameters.AddWithValue("@RaceId", RaceId);
                cmd.Parameters.AddWithValue("@UserId", UserId);
                cmd.Parameters.AddWithValue("@FinishedTime", raceTime.ToString(@"hh\:mm\:ss"));
                cmd.Parameters.AddWithValue("@Place", place);

                result = cmd.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception thrown: " + e.Message);
                throw;
            }
            //If the UserId/RaceId combo exists in the entries table, get the entrant summary and return it.
            if (result > 0)
            {
                entrant = GetEntrantInformation(RaceId, UserId);
            }

            return(entrant);
        }
Esempio n. 5
0
        public EntrantItem GetEntrantInformation(ulong RaceId, ulong UserId)
        {
            MySqlCommand    cmd;
            MySqlDataReader dataReader; //for reading the results of the query

            try
            {
                cmd             = _connection.CreateCommand();
                cmd.CommandText = "SELECT * from entries WHERE RaceId = @RaceId AND UserId = @UserId";
                cmd.Parameters.AddWithValue("@RaceId", RaceId);
                cmd.Parameters.AddWithValue("@UserId", UserId);
                dataReader = cmd.ExecuteReader();
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception thrown: " + e.Message);
                throw;
            }

            //we should never read in more than one result, so we don't have to mess with a loop for dataReader.Read()

            EntrantItem entrant = null;

            if (dataReader.Read())
            {
                TimeSpan convertedDateTime;
                int      place;
                string   comment;

                if (dataReader["FinishedTime"] == DBNull.Value)
                {
                    convertedDateTime = TimeSpan.Zero;
                }
                else
                {
                    convertedDateTime = (TimeSpan)dataReader["FinishedTime"];
                }

                if (dataReader["Place"] == DBNull.Value)
                {
                    place = -1;
                }
                else
                {
                    place = int.Parse(dataReader["Place"] + "");
                }

                if (dataReader["Comment"] == DBNull.Value)
                {
                    comment = null;
                }
                else
                {
                    comment = (string)dataReader["Comment"];
                }

                entrant = new EntrantItem(
                    (ulong)dataReader["ID"],
                    (ulong)dataReader["RaceId"],
                    (ulong)dataReader["UserId"],
                    (string)dataReader["Status"],
                    convertedDateTime,
                    place,
                    comment);
            }

            dataReader.Close();
            dataReader.Dispose();

            return(entrant);
        }