예제 #1
0
        public virtual async Task JoinLobbyAsync()
        {
            using (var db = new Database())
            {
                if (!(Context.User as SocketGuildUser).IsRegistered(out var player))
                {
                    await SimpleEmbedAsync("You must register in order to join a lobby.");

                    return;
                }

                var lobby = db.Lobbies.FirstOrDefault(x => x.ChannelId == Context.Channel.Id);
                if (lobby == null)
                {
                    await SimpleEmbedAsync("This channel is not a lobby.");

                    return;
                }

                var bans = db.Bans.Where(x => x.UserId == Context.User.Id && x.GuildId == Context.Guild.Id).ToArray().Where(x => !x.IsExpired).OrderByDescending(x => x.ExpiryTime).ToArray();
                if (bans.Length != 0)
                {
                    var latest = bans.First();
                    if (lobby.HideQueue)
                    {
                        await Context.Message.DeleteAsync();
                        await SimpleEmbedAndDeleteAsync($"You are still banned from matchmaking for another: {RavenBOT.Common.Extensions.GetReadableLength(latest.RemainingTime)}", Color.Red, TimeSpan.FromSeconds(5));

                        return;
                    }
                    await SimpleEmbedAsync($"{Context.User.Mention} - You are still banned from matchmaking for another: {RavenBOT.Common.Extensions.GetReadableLength(latest.RemainingTime)}", Color.Red);

                    return;
                }

                var queue = db.GetQueuedPlayers(Context.Guild.Id, Context.Channel.Id).ToList();

                //Not sure if this is actually needed.
                if (queue.Count >= lobby.PlayersPerTeam * 2)
                {
                    if (lobby.HideQueue)
                    {
                        await Context.Message.DeleteAsync();
                        await SimpleEmbedAndDeleteAsync("Queue is full, wait for teams to be chosen before joining.", Color.Red, TimeSpan.FromSeconds(5));

                        return;
                    }

                    //Queue will be reset after teams are completely picked.
                    await SimpleEmbedAsync($"{Context.User.Mention} - Queue is full, wait for teams to be chosen before joining.", Color.DarkBlue);

                    return;
                }

                var comp = db.GetOrCreateCompetition(Context.Guild.Id);
                if (!comp.AllowMultiQueueing)
                {
                    var queued = db.QueuedPlayers.Where(x => x.GuildId == Context.Guild.Id && x.UserId == Context.User.Id && x.ChannelId != Context.Channel.Id).ToArray();
                    if (queued.Length > 0)
                    {
                        var guildChannels = queued.Select(x => MentionUtils.MentionChannel(x.ChannelId));

                        if (lobby.HideQueue)
                        {
                            await Context.Message.DeleteAsync();
                            await SimpleEmbedAndDeleteAsync($"MultiQueuing is not enabled in this server.\nPlease leave: {string.Join("\n", guildChannels)}", Color.Red, TimeSpan.FromSeconds(5));

                            return;
                        }
                        await SimpleEmbedAsync($"{Context.User.Mention} - MultiQueuing is not enabled in this server.\nPlease leave: {string.Join("\n", guildChannels)}", Color.Red);

                        return;
                    }
                }

                if (lobby.MinimumPoints != null)
                {
                    if (player.Points < lobby.MinimumPoints)
                    {
                        if (lobby.HideQueue)
                        {
                            await Context.Message.DeleteAsync();
                            await SimpleEmbedAndDeleteAsync($"You need a minimum of {lobby.MinimumPoints} points to join this lobby.", Color.Red, TimeSpan.FromSeconds(5));

                            return;
                        }
                        await SimpleEmbedAsync($"{Context.User.Mention} - You need a minimum of {lobby.MinimumPoints} points to join this lobby.", Color.Red);

                        return;
                    }
                }

                var currentGame = db.GetLatestGame(lobby);
                if (currentGame != null)
                {
                    if (currentGame.GameState == GameState.Picking)
                    {
                        if (lobby.HideQueue)
                        {
                            await Context.Message.DeleteAsync();
                            await SimpleEmbedAndDeleteAsync("Current game is picking teams, wait until this is completed.", Color.DarkBlue, TimeSpan.FromSeconds(5));

                            return;
                        }
                        await SimpleEmbedAsync("Current game is picking teams, wait until this is completed.", Color.DarkBlue);

                        return;
                    }
                }

                if (queue.Any(x => x.UserId == Context.User.Id))
                {
                    if (lobby.HideQueue)
                    {
                        await Context.Message.DeleteAsync();

                        // await SimpleEmbedAndDeleteAsync("You are already queued.", Color.DarkBlue, TimeSpan.FromSeconds(5));
                        return;
                    }

                    // await SimpleEmbedAsync($"{Context.User.Mention} - You are already queued.", Color.DarkBlue);
                    return;
                }

                if (comp.RequeueDelay.HasValue)
                {
                    if (QueueDelays.ContainsKey(Context.Guild.Id))
                    {
                        var currentGuild = QueueDelays[Context.Guild.Id];
                        if (currentGuild.ContainsKey(Context.User.Id))
                        {
                            var currentUserLastJoin = currentGuild[Context.User.Id];
                            if (currentUserLastJoin + comp.RequeueDelay.Value > DateTime.UtcNow)
                            {
                                var remaining = currentUserLastJoin + comp.RequeueDelay.Value - DateTime.UtcNow;
                                if (lobby.HideQueue)
                                {
                                    await SimpleEmbedAndDeleteAsync($"You cannot requeue for another {RavenBOT.Common.Extensions.GetReadableLength(remaining)}", Color.Red);

                                    return;
                                }
                                await SimpleEmbedAsync($"{Context.User.Mention} - You cannot requeue for another {RavenBOT.Common.Extensions.GetReadableLength(remaining)}", Color.Red);

                                return;
                            }
                            else
                            {
                                currentUserLastJoin = DateTime.UtcNow;
                            }
                        }
                        else
                        {
                            currentGuild.Add(Context.User.Id, DateTime.UtcNow);
                        }
                    }
                    else
                    {
                        var newDict = new Dictionary <ulong, DateTime>();
                        newDict.Add(Context.User.Id, DateTime.UtcNow);
                        QueueDelays.Add(Context.Guild.Id, newDict);
                    }
                }

                db.QueuedPlayers.Add(new Models.QueuedPlayer
                {
                    UserId    = Context.User.Id,
                    ChannelId = lobby.ChannelId,
                    GuildId   = lobby.GuildId
                });
                if (queue.Count + 1 >= lobby.PlayersPerTeam * 2)
                {
                    db.SaveChanges();
                    await LobbyService.LobbyFullAsync(Context, lobby);

                    return;
                }
                else
                {
                    if (lobby.HideQueue)
                    {
                        await Context.Message.DeleteAsync();
                        await SimpleEmbedAsync($"A player has joined the queue. **[{queue.Count + 1}/{lobby.PlayersPerTeam * 2}]**");
                    }
                    else
                    {
                        if (Premium.IsPremium(Context.Guild.Id))
                        {
                            await SimpleEmbedAsync($"{player.GetDisplayNameSafe()} joined the queue. **[{queue.Count + 1}/{lobby.PlayersPerTeam * 2}]**", Color.Green);
                        }
                        else
                        {
                            await ReplyAsync("", false, new EmbedBuilder
                            {
                                Description = $"{player.GetDisplayNameSafe()} joined the queue. **[{queue.Count + 1}/{lobby.PlayersPerTeam * 2}]**\n" +
                                              $"[Get Premium to remove ELO bot branding]({Premium.PremiumConfig.ServerInvite})",
                                Color = Color.Green
                            }.Build());
                        }
                    }
                }

                db.SaveChanges();
            }
        }
예제 #2
0
        public virtual async Task ForceJoinAsync(params SocketGuildUser[] users)
        {
            using (var db = new Database())
            {
                var lobby = db.Lobbies.FirstOrDefault(x => x.ChannelId == Context.Channel.Id);
                if (lobby == null)
                {
                    await SimpleEmbedAsync("Channel is not a lobby.", Color.Red);

                    return;
                }
                var userIds     = users.Select(x => x.Id).ToList();
                var userPlayers = db.Players.Where(x => x.GuildId == Context.Guild.Id && userIds.Contains(x.UserId));
                var queue       = db.QueuedPlayers.Where(x => x.GuildId == Context.Guild.Id && x.ChannelId == Context.Channel.Id).ToList();
                int queueCount  = queue.Count;
                var latestGame  = db.GameResults.Where(x => x.LobbyId == Context.Channel.Id).OrderByDescending(x => x.GameId).FirstOrDefault();
                if (latestGame != null && latestGame.GameState == GameState.Picking)
                {
                    await SimpleEmbedAndDeleteAsync("Current game is picking teams, wait until this is completed.", Color.Red);

                    return;
                }

                var added = new List <ulong>();
                foreach (var player in userPlayers)
                {
                    if (queueCount >= lobby.PlayersPerTeam * 2)
                    {
                        //Queue will be reset after teams are completely picked.
                        await SimpleEmbedAsync("Queue is full, wait for teams to be chosen before joining.", Color.DarkBlue);

                        break;
                    }

                    if (queue.Any(x => x.UserId == player.UserId))
                    {
                        await SimpleEmbedAsync($"{MentionUtils.MentionUser(player.UserId)} is already queued.", Color.DarkBlue);

                        continue;
                    }

                    added.Add(player.UserId);
                    db.QueuedPlayers.Add(new QueuedPlayer
                    {
                        UserId    = player.UserId,
                        GuildId   = Context.Guild.Id,
                        ChannelId = Context.Channel.Id
                    });
                    queueCount++;
                }
                db.SaveChanges();
                await SimpleEmbedAsync($"{string.Join("", added.Select(MentionUtils.MentionUser))} - added to queue.", Color.Green);

                if (queueCount >= lobby.PlayersPerTeam * 2)
                {
                    db.SaveChanges();

                    await LobbyService.LobbyFullAsync(Context, lobby);

                    return;
                }
            }
        }