//public async Task<IActionResult> Edit(int id, [Bind("TeamID,TeamName,Password,CompetitionID")] Team team)
        public async Task <IActionResult> Edit(int id, [Bind("TeamID, TeamName, Score")] Team team)
        {
            if (id != team.TeamID)
            {
                return(NotFound());
            }

            var saveTeam = await _context.Teams.FindAsync(team.TeamID);

            saveTeam.Score = team.Score;

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(saveTeam);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!TeamExists(team.TeamID))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction("Index", "Teams", new { id = saveTeam.CompetitionID }));
            }
            return(RedirectToAction("Index", "Teams", new { id = saveTeam.CompetitionID }));
            //return View(returnTeam);
        }
        public async Task <IActionResult> Edit(int id, [Bind("ID,CompetitionName,Status,BucketName")] Competition competition)
        {
            if (id != competition.ID)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(competition);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!CompetitionExists(competition.ID))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }

            return(View(competition));
        }
        public async Task <IActionResult> Edit(int id, [Bind("ID,CategoryName")] CategoryDefault categoryDefault)
        {
            if (id != categoryDefault.ID)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(categoryDefault);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!CategoryDefaultExists(categoryDefault.ID))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(categoryDefault));
        }
Exemple #4
0
        public async Task <IActionResult> Details(int id, [Bind("ID,Name,Description,Value,Flag,CompetitionID,CompetitionCategoryID")] Challenge challenge, List <IFormFile> files)
        {
            if (id != challenge.ID)
            {
                return(NotFound());
            }

            if (files.Count != 0)
            {
                var competition = await _context.Competitions
                                  .Include(c => c.CompetitionCategories)
                                  .ThenInclude(cc => cc.Challenges)
                                  .AsNoTracking()
                                  .FirstOrDefaultAsync(m => m.ID == challenge.CompetitionID);

                var competitionCategory = await _context.CompetitionCategories
                                          .AsNoTracking()
                                          .FirstOrDefaultAsync(m => m.ID == challenge.CompetitionCategoryID);

                var temp_Challenge = await _context.Challenges
                                     .AsNoTracking()
                                     .FirstOrDefaultAsync(m => m.ID == challenge.ID);

                await DeleteFile(competition.BucketName, competitionCategory.CategoryName, temp_Challenge.FileName);

                foreach (var file in files)
                {
                    challenge.FileName = file.FileName;
                    await UploadFileToS3(file, competition.BucketName, competitionCategory.CategoryName);
                }
            }


            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(challenge);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!ChallengeExists(challenge.ID))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction("Index", "Challenges", new { id = challenge.CompetitionID }));
            }
            return(View(challenge));
        }
 public async Task <IActionResult> Edit([Bind("ID, TimeStamp, CompetitionID, TeamID, ChallengeID, TeamChallengeID, Score, PreviousHash, Hash")] Block block)
 {
     if (ModelState.IsValid)
     {
         try
         {
             _context.Update(block);
             await _context.SaveChangesAsync();
         }
         catch (DbUpdateConcurrencyException)
         {
             if (!BlockExists(block.ID))
             {
                 return(NotFound());
             }
             else
             {
                 throw;
             }
         }
         return(RedirectToAction("Index", "Blockchain", new { id = block.CompetitionID }));
     }
     return(RedirectToAction("Index", "Blockchain", new { id = block.CompetitionID }));
 }
        public async Task <IActionResult> Details([Bind("ID, Flag, CompetitionID")] Challenge challenge, int?id)
        {
            if (challenge.Flag == null)
            {
                var temp_challenge1 = await _context.Challenges
                                      .FirstOrDefaultAsync(m => m.ID == challenge.ID);

                var competition1 = await _context.Competitions.FindAsync(temp_challenge1.CompetitionID);

                string bucketName = competition1.BucketName;
                var    category   = await _context.CompetitionCategories.FindAsync(temp_challenge1.CompetitionCategoryID);

                string folderName = category.CategoryName;
                if (temp_challenge1.FileName != null)
                {
                    string fileName     = temp_challenge1.FileName;
                    Regex  pattern      = new Regex("[+]");
                    string tempFileName = pattern.Replace(fileName, "%2B");
                    tempFileName.Replace(' ', '+');
                    ViewData["FileLink"] = "https://s3-ap-southeast-1.amazonaws.com/" + bucketName + "/" + folderName + "/" + tempFileName;
                }
                ViewData["CompetitionID"] = temp_challenge1.CompetitionID;
                ViewData["ChallengeID"]   = temp_challenge1.ID;

                ViewData["Invalid"]   = true;
                ViewData["WrongFlag"] = false;
                return(View(temp_challenge1));
            }

            Team team = null;

            var competition = await _context.Competitions
                              .Include(c => c.CompetitionCategories)
                              .ThenInclude(cc => cc.Challenges)
                              .Include(c => c.Teams)
                              .ThenInclude(t => t.TeamUsers)
                              .AsNoTracking()
                              .FirstOrDefaultAsync(m => m.ID == challenge.CompetitionID);

            var userId = this.User.FindFirst(ClaimTypes.NameIdentifier).Value;

            foreach (var Team in competition.Teams)
            {
                foreach (var TeamUser in Team.TeamUsers)
                {
                    if (TeamUser.UserId.Equals(userId))
                    {
                        team = Team;
                        break;
                    }
                }
            }

            //Get all challenges this team has solved
            var teamChallengesList = await _context.Teams
                                     .Include(t => t.TeamChallenges)
                                     .AsNoTracking()
                                     .FirstOrDefaultAsync(m => m.TeamID == team.TeamID);

            foreach (var teamChallenges in teamChallengesList.TeamChallenges)
            {
                if (teamChallenges.ChallengeId == challenge.ID)
                {
                    if (teamChallenges.Solved == true)
                    {
                        return(RedirectToAction("Details", "Challenges", new { id }));
                    }
                }
            }

            var localvarchallenge = await _context.Challenges
                                    .AsNoTracking()
                                    .FirstOrDefaultAsync(m => m.ID == challenge.ID);

            //if (ModelState.IsValid)
            //{
            //    _context.Add(challenge);
            //    await _context.SaveChangesAsync();
            //    return RedirectToAction(nameof(Index));
            //}

            //if (challenge.CompetitionID == null)
            //{
            //    return NotFound();
            //}

            var temp_challenge = await _context.Challenges
                                 .FirstOrDefaultAsync(m => m.ID == challenge.ID);

            if (temp_challenge == null)
            {
                return(NotFound());
            }
            // Flag is correct
            if (challenge.Flag.Equals(temp_challenge.Flag))
            {
                //Add entry to TeamChallenge
                TeamChallenge teamChallenge = new TeamChallenge();
                teamChallenge.ChallengeId = localvarchallenge.ID;
                teamChallenge.TeamId      = team.TeamID;
                teamChallenge.Solved      = true;
                _context.Add(teamChallenge);
                await _context.SaveChangesAsync();

                //Add entry to chain
                //Block and block data
                Block block = new Block();
                block.TimeStamp       = DateTime.Now;
                block.CompetitionID   = challenge.CompetitionID;
                block.TeamID          = team.TeamID;
                block.ChallengeID     = localvarchallenge.ID;
                block.TeamChallengeID = teamChallenge.TeamChallengeID;
                block.Score           = localvarchallenge.Value;
                //Previous Hash
                Blockchain blockchain  = new Blockchain(_context);
                Block      latestBlock = await blockchain.GetLatestBlock();

                block.PreviousHash = latestBlock.Hash;
                //Current Hash
                string data = block.TimeStamp + ";" + block.CompetitionID + ";" + block.TeamID + ";" + block.ChallengeID + ";" + block.TeamChallengeID + ";" + block.Score + ";" + block.PreviousHash;
                block.Hash = GenerateSHA512String(data);

                _context.Add(block);
                await _context.SaveChangesAsync();

                //Add points to team score
                team.Score += localvarchallenge.Value;
                _context.Update(team);
                await _context.SaveChangesAsync();

                return(RedirectToAction("Index", "Challenges", new { id = challenge.CompetitionID }));
            }
            else
            {
                //Wrong flag
                TeamChallenge teamChallenge = new TeamChallenge();
                teamChallenge.ChallengeId = localvarchallenge.ID;
                teamChallenge.TeamId      = team.TeamID;
                teamChallenge.Solved      = false;
                _context.Add(teamChallenge);
                await _context.SaveChangesAsync();

                var Challenge = await _context.Challenges
                                .FirstOrDefaultAsync(m => m.ID == id);

                if (Challenge == null)
                {
                    return(NotFound());
                }
                //Stop field from being populated at View
                Challenge.Flag = null;

                var Competition = await _context.Competitions.FindAsync(Challenge.CompetitionID);

                string bucketName = Competition.BucketName;
                var    category   = await _context.CompetitionCategories.FindAsync(Challenge.CompetitionCategoryID);

                string folderName = category.CategoryName;
                if (Challenge.FileName != null)
                {
                    string fileName     = Challenge.FileName;
                    Regex  pattern      = new Regex("[+]");
                    string tempFileName = pattern.Replace(fileName, "%2B");
                    tempFileName.Replace(' ', '+');
                    ViewData["FileLink"] = "https://s3-ap-southeast-1.amazonaws.com/" + bucketName + "/" + folderName + "/" + tempFileName;
                }
                ViewData["CompetitionID"] = Challenge.CompetitionID;
                ViewData["ChallengeID"]   = Challenge.ID;

                //if (ValidateUserJoined(Challenge.CompetitionID).Result == true)
                //{
                ViewData["Invalid"]   = false;
                ViewData["WrongFlag"] = true;
                return(View(Challenge));
                //}
            }
        }