Esempio n. 1
0
        public async Task Verify(string flag)
        {
            Challenge chall = await SharedFlagManager.GetChallByFlag(_context, flag);

            if (chall == null)
            {
                await ReplyAsync("Your flag is incorrect.");
            }
            else
            {
                await ReplyAsync($"Your flag for **{chall.Title}** is correct.");
            }
        }
Esempio n. 2
0
        public async Task Flag(string flag)
        {
            var challenge = await SharedFlagManager.GetChallByFlag(_context, flag, includeArchived : true);

            var config = await _context.Configuration.AsQueryable().FirstOrDefaultAsync();

            if (config != null && config.IsFinished)
            {
                await ReplyAsync("The competition is already over.");

                return;
            }

            if (challenge == null)
            {
                await ReplyAsync("Your flag is incorrect.");

                if (config != null && config.GuildId != 0 && config.LogsChannelId != 0)
                {
                    var logsChannel = _client.GetGuild(config.GuildId).GetTextChannel(config.LogsChannelId);
                    await logsChannel.SendMessageAsync($"<@{Context.User.Id}> submitted a wrong flag: **{Format.Sanitize(flag).Replace("@", "@\u200B")}**");
                }
                return;
            }

            if (challenge.State == 3)
            {
                await ReplyAsync("You are trying to submit a flag for an archived challenge.");

                if (config != null && config.GuildId != 0 && config.LogsChannelId != 0)
                {
                    var logsChannel = _client.GetGuild(config.GuildId).GetTextChannel(config.LogsChannelId);
                    await logsChannel.SendMessageAsync($"<@{Context.User.Id}> submitted a flag for an archived challenge: **{Format.Sanitize(flag).Replace("@", "@\u200B")}**");
                }
                return;
            }

            var user = await UserManager.GetOrCreateUser(_context, Context.User.Id, Context.User.ToString());

            bool isTeam = (user.Team != null);

            if ((!isTeam && user.Solves.Select(x => x.Challenge).Contains(challenge)) || (isTeam && user.Team.Solves.Select(x => x.Challenge).Contains(challenge)))
            {
                await ReplyAsync("You already solved that challenge!");

                return;
            }

            if (config != null && config.GuildId != 0 && config.TodaysRoleId != 0)
            {
                var lastChall = await _context.Challenges.AsQueryable().OrderByDescending(x => x.ReleaseDate).FirstOrDefaultAsync(x => x.State == 2);

                if (lastChall == challenge)
                {
                    if (isTeam)
                    {
                        foreach (var member in user.Team.Members)
                        {
                            if (member.DiscordId == 0)
                            {
                                continue;
                            }
                            var guildUser = _client.GetGuild(config.GuildId).GetUser(member.DiscordId);
                            if (guildUser != null)
                            {
                                var role = _client.GetGuild(config.GuildId).GetRole(config.TodaysRoleId);
                                await guildUser.AddRoleAsync(role);
                            }
                        }
                    }
                    else
                    {
                        var guildUser = _client.GetGuild(config.GuildId).GetUser(Context.User.Id);
                        if (guildUser != null)
                        {
                            var role = _client.GetGuild(config.GuildId).GetRole(config.TodaysRoleId);
                            await guildUser.AddRoleAsync(role);
                        }
                    }
                }
            }

            if (isTeam)
            {
                user.Team.LastUpdated = DateTime.UtcNow;
            }
            else
            {
                user.LastUpdated = DateTime.UtcNow;
            }

            var solve = new Solve
            {
                User      = user,
                Team      = user.Team,
                Challenge = challenge,
                SolvedAt  = DateTime.UtcNow,
                Announced = true
            };

            await _context.Solves.AddAsync(solve);

            await _context.SaveChangesAsync();

            await ReplyAsync($"Congratulations! You solved **{challenge.Title}** challenge!");

            bool dynamicScoring = _configuration.GetValue <bool>("DynamicScoring");
            await SolvesManager.AnnounceWebsiteSolves(_client, _context, dynamicScoring);

            await SolvesManager.AnnounceSolve(_client, _context, challenge, user);

            await LeaderboardManager.UpdateLeaderboard(_client, _context, dynamicScoring);

            await RolesManager.UpdateRoles(_client, _context);
        }
Esempio n. 3
0
        public async Task OnPostAsync(string flag)
        {
            if (!_signInManager.IsSignedIn(User))
            {
                Error = "You must log in first in order to submit a flag";
                return;
            }

            var config = await _context.Configuration.FirstOrDefaultAsync();

            if (config != null && config.IsFinished)
            {
                Error = "The competition is already over";
                return;
            }

            var appUser = await _userManager.GetUserAsync(User);

            flag = flag?.Trim();
            var challenge = await SharedFlagManager.GetChallByFlag(_context, flag, includeArchived : true);

            if (challenge == null)
            {
                _logger.LogInformation($"User \"{appUser.UserName}\" submitted a wrong flag ({flag})");
                Error = "Your flag is incorrect";
                return;
            }

            if (challenge.State == 3)
            {
                Error = "You are trying to submit a flag for an archived challenge";
                return;
            }

            await _context.Entry(appUser).Reference(x => x.User).Query().Include(x => x.Team).LoadAsync();

            if (appUser.User.Team == null)
            {
                await _context.Entry(appUser.User).Collection(x => x.Solves).Query().Include(x => x.Challenge).LoadAsync();
            }
            else
            {
                await _context.Entry(appUser.User.Team).Collection(x => x.Solves).Query().Include(x => x.Challenge).LoadAsync();
            }

            if ((appUser.User.Team == null && appUser.User.Solves.Select(x => x.Challenge).Contains(challenge)) || (appUser.User.Team != null && appUser.User.Team.Solves.Select(x => x.Challenge).Contains(challenge)))
            {
                Error = "You already solved that challenge";
                return;
            }

            var time = DateTime.UtcNow;

            if (time.Hour == 13 && time.Minute == 37)
            {
                _logger.LogInformation($"User \"{appUser.UserName}\" got the \"13:37 Hacker\" role");
                await _userManager.AddToRoleAsync(appUser, "13:37 Hacker");
            }

            if (appUser.User.Team != null)
            {
                appUser.User.Team.LastUpdated = DateTime.UtcNow;
            }
            else
            {
                appUser.User.LastUpdated = DateTime.UtcNow;
            }

            var solve = new Solve
            {
                User      = appUser.User,
                Team      = appUser.User.Team,
                Challenge = challenge,
                SolvedAt  = DateTime.UtcNow,
                Announced = false
            };

            await _context.Solves.AddAsync(solve);

            await _context.SaveChangesAsync();

            Success = $"Congratulations! You solved {challenge.Title} challenge!";
        }
Esempio n. 4
0
        public async Task <IActionResult> SubmitAsync(string apiKey, [FromBody] SubmitData data)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            string flag = data.Flag.Trim();

            var user = await _userManager.Users.FirstOrDefaultAsync(x => x.ApiKey == apiKey);

            if (user == null)
            {
                return(StatusCode(403));
            }

            var config = await _context.Configuration.FirstOrDefaultAsync();

            if (config != null && config.IsFinished)
            {
                return(Json(new { Success = false, Error = "The competition is already over" }));
            }

            var challenge = await SharedFlagManager.GetChallByFlag(_context, flag, includeArchived : true);

            if (challenge == null)
            {
                return(Json(new { Success = false, Error = "Your flag is incorrect" }));
            }
            else if (challenge.State == 3)
            {
                return(Json(new { Success = false, Error = "You are trying to submit a flag for an archived challenge" }));
            }

            await _context.Entry(user).Reference(x => x.User).Query().Include(x => x.Team).LoadAsync();

            bool isTeam = (user.User.Team != null);

            if (isTeam)
            {
                await _context.Entry(user.User.Team).Collection(x => x.Solves).Query().Include(x => x.Challenge).LoadAsync();
            }
            else
            {
                await _context.Entry(user.User).Collection(x => x.Solves).Query().Include(x => x.Challenge).LoadAsync();
            }

            if ((!isTeam && user.User.Solves.Select(x => x.Challenge).Contains(challenge)) || (isTeam && user.User.Team.Solves.Select(x => x.Challenge).Contains(challenge)))
            {
                return(Json(new { Success = false, Error = "You already solved that challenge" }));
            }

            if (isTeam)
            {
                user.User.Team.LastUpdated = DateTime.UtcNow;
            }
            else
            {
                user.User.LastUpdated = DateTime.UtcNow;
            }

            var solve = new Solve
            {
                User      = user.User,
                Team      = user.User.Team,
                Challenge = challenge,
                SolvedAt  = DateTime.UtcNow,
                Announced = false
            };

            await _context.Solves.AddAsync(solve);

            await _context.SaveChangesAsync();

            return(Json(new {
                Success = true, Challenge = new
                {
                    challenge.Id,
                    challenge.Title
                }
            }));
        }