public async void CheckForPasswordedMatchesAndSendNotificationsAsync(object state, ElapsedEventArgs args) { _db = new LadderDbContext(); var match = _db.Matches.FirstOrDefault(match => match.MatchState == MatchState.Passworded); if (match == null) { return; } //very bad here, need to refactor foreach (var player in match.Players) { var playerfromDb = _db.Users.SingleOrDefault(user => user.Nick == player); await _client.GetUser(playerfromDb.DiscordId).SendMessageAsync( "Match is starting." + Environment.NewLine + $"Team 1: {string.Join(", ",match.Players.Take(Constants.REQUIRED_AMOUNT_OF_PLAYERS / 2))}" + Environment.NewLine + $"Team 2: {string.Join(", ",match.Players.TakeLast(Constants.REQUIRED_AMOUNT_OF_PLAYERS / 2))}" + Environment.NewLine + "Open the game and paste this into the console:" + Environment.NewLine + $"`name {player}; password {match.Password}; connect {match.ServerIp};`"); } match.MatchState = MatchState.SentNotifications; _db.Update(match); await _db.SaveChangesAsync(); }
private async Task MessageReceived(SocketMessage message) { if (message.Author.Id != Constants.AUTHORIZED_USER_ID) { return; } _db = new LadderDbContext(); var matchId = Guid.NewGuid(); await _db.Matches.AddAsync( new Match { Id = matchId, Players = new string[0], MatchState = MatchState.Created }); await _db.SaveChangesAsync(); var channel = _client.GetChannel(Constants.AUTHORIZED_CHANNEL) as IMessageChannel; var announcement = await channel.SendMessageAsync( $"||{matchId}||" + Environment.NewLine + "@everyone" + Environment.NewLine + "New match!"); await announcement.AddReactionAsync(new Emoji(Constants.PLUS_REACTION_EMOTE)); }
async void CheckForMatchesWithEnoughPlayersAndPasswordServerAsync(object state, ElapsedEventArgs args) { _db = new LadderDbContext(); var match = _db.Matches.FirstOrDefault(match => match.MatchState == MatchState.GotEnoughPlayers); if (match == null) { return; } var pw = "pug"; await SendCommandAsync($"rcon myrcon g_password {pw}"); match.MatchState = MatchState.Passworded; match.ServerIp = $"{_ip}:29070"; match.Password = pw; _db.Update(match); await _db.SaveChangesAsync(); }
private async Task ReactionAdded(Cacheable <IUserMessage, ulong> message, ISocketMessageChannel channel, SocketReaction reaction) { if (reaction.User.Value.IsBot) { return; } if (reaction.Emote.Name != Constants.PLUS_REACTION_EMOTE) { await message.Value.RemoveReactionAsync(reaction.Emote, reaction.User.Value, RequestOptions.Default); return; } await message.Value.RemoveReactionAsync(new Emoji(Constants.PLUS_REACTION_EMOTE), _client.CurrentUser); _db = new LadderDbContext(); if (!_db.Users.Any(user => user.Nick == reaction.User.Value.Username)) { await _db.Users.AddAsync(new User { Id = Guid.NewGuid(), Nick = reaction.User.Value.Username, DiscordId = reaction.User.Value.Id }); } var matchId = Guid.Parse(message.Value.Content.Split(Environment.NewLine)[0].Split("||")[1]); var match = _db.Matches.SingleOrDefault(match => match.Id == matchId); match.Players = match.Players.Append(reaction.User.Value.Username).ToArray(); if (match.Players.Length == Constants.REQUIRED_AMOUNT_OF_PLAYERS) { match.MatchState = MatchState.GotEnoughPlayers; } await _db.SaveChangesAsync(); }