public static async Task UpdateChannelTopicAsync(ulong RaceId) { DatabaseHandler database = new DatabaseHandler(Globals.MySqlConnectionString); RaceItem race = database.GetRaceInformation(RaceId); EntrantsSummary entrantsSummary = database.GetEntrantsSummary(race.RaceId); database.Dispose(); SocketTextChannel raceChannel = (SocketTextChannel)client.GetChannel(race.TextChannelId); string newTopic = "**" + race.Status + "** | " + race.Description; if (race.Status == "Entry Open") { newTopic += " | Entered: " + (entrantsSummary.NotReady + entrantsSummary.Ready) + " | Ready: " + entrantsSummary.Ready; } else { newTopic += " | Racing: " + (entrantsSummary.Ready + entrantsSummary.Done + entrantsSummary.Forfeited + entrantsSummary.Disqalified) + " | Done: " + entrantsSummary.Done + " | Forfeited: " + entrantsSummary.Forfeited; } await raceChannel.ModifyAsync(x => { x.Topic = newTopic; }); }
public static async Task <bool> AttemptRaceFinishAsync(RaceItem Race) { DatabaseHandler database = new DatabaseHandler(Globals.MySqlConnectionString); EntrantsSummary entrantsSummary = database.GetEntrantsSummary(Race.RaceId); var raceServer = client.GetGuild(Globals.GuildId); var raceRole = raceServer.GetRole(Race.RoleId); var raceChannel = raceServer.GetTextChannel(Race.TextChannelId); //we sometimes may need to know if the race is actually finishing bool raceIsFinishing = false; //Racers are stored as "Ready" until they finish, forfeit, or are disqualified if (entrantsSummary.Ready == 0) { await raceChannel.SendMessageAsync("Everyone is finished! GGs all around! This channel will be deleted in 10 minutes."); database.UpdateRace(Race.RaceId, Status: "Recently Completed"); var newTimer = new CountdownTimer(); newTimer.Interval = 600000; newTimer.race = Race; newTimer.AutoReset = false; newTimer.Elapsed += DeleteFinishedRaceAsync; newTimer.Enabled = true; newTimer.Start(); _completedRaceTimerList.Add(newTimer); raceIsFinishing = true; _ = UpdateRacesChannelAsync(); } _ = UpdateChannelTopicAsync(Race.RaceId); database.Dispose(); return(raceIsFinishing); }
public EntrantsSummary GetEntrantsSummary(ulong RaceId) { MySqlCommand cmd; MySqlDataReader dataReader; //for reading the results of the query try { cmd = _connection.CreateCommand(); cmd.CommandText = "SELECT Status,Count(*) AS Entrants FROM entries WHERE RaceId = @RaceId GROUP BY Status"; cmd.Parameters.AddWithValue("@RaceId", RaceId); dataReader = cmd.ExecuteReader(); } catch (Exception e) { Console.WriteLine("Exception thrown: " + e.Message); throw; } EntrantsSummary entrantsSummary = new EntrantsSummary(RaceId); string status; //stores the status from the dataReader so we can decide which field in entrantsSummary to update int entrantsCount; while (dataReader.Read()) { status = (string)dataReader["Status"]; entrantsCount = int.Parse(dataReader["Entrants"] + ""); switch (status) { case "Not Ready": entrantsSummary.NotReady = entrantsCount; break; case "Ready": entrantsSummary.Ready = entrantsCount; break; case "Done": entrantsSummary.Done = entrantsCount; break; case "Forfeited": entrantsSummary.Forfeited = entrantsCount; break; case "Disqualified": entrantsSummary.Disqalified = entrantsCount; break; default: //should never trigger, but if it does for some reason, we'll just ignore the data break; } } dataReader.Close(); dataReader.Dispose(); return(entrantsSummary); }
public static async Task <bool> AttemptRaceStartAsync(RaceItem Race) { DatabaseHandler database = new DatabaseHandler(Globals.MySqlConnectionString); EntrantsSummary entrantsSummary = database.GetEntrantsSummary(Race.RaceId); var raceServer = client.GetGuild(Globals.GuildId); var raceRole = raceServer.GetRole(Race.RoleId); var raceChannel = raceServer.GetTextChannel(Race.TextChannelId); //sometimes we need to know if we're actually starting the race. bool raceIsStarting = false; //See if the number of ready entrants + disqualified entrants equals the total number of entrants //It is possible (but rare) for an entrant to be marked disqualified before a race starts //Excessive DQs may result in a penalty at some point, so it's important to record them if (entrantsSummary.Ready + entrantsSummary.Disqalified == entrantsSummary.TotalEntrants) { //we don't want a situation where there is only one racer who is ready, but the race starts //because of DQed entrants. if (entrantsSummary.Ready > 1) { //All of the entrants are ready, and we have enough entrants, so we can start the race await raceChannel.SendMessageAsync(raceRole.Mention + " Everyone is ready! Race will start in 10 seconds."); database.UpdateRace(Race.RaceId, Status: "Countdown"); var newTimer = new CountdownTimer(); newTimer.Interval = 7000; newTimer.race = Race; newTimer.AutoReset = false; newTimer.Elapsed += CountdownRaceAsync; newTimer.Enabled = true; newTimer.Start(); //check for and remove any force start timers that may be waiting to fire RemoveTimer(_forceStartTimerList, Race.RaceId); _ = UpdateRacesChannelAsync(); raceIsStarting = true; } } _ = UpdateChannelTopicAsync(Race.RaceId); database.Dispose(); return(raceIsStarting); }
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); }