예제 #1
0
        private void DrawChallengeSelector(TeamChallenge challenge)
        {
            SerializedObject soChallenge = new SerializedObject(challenge);

            EditorGUILayout.BeginVertical();
            EditorGUILayout.PropertyField(soChallenge.FindProperty("type"), GUIContent.none);
            if (challenge.type == ChallengeType.HAVE_TAG)
            {
                EditorGUILayout.PropertyField(soChallenge.FindProperty("executor"), GUIContent.none);
                EditorGUILayout.PropertyField(soChallenge.FindProperty("_tag"), GUIContent.none);
            }
            else if (challenge.type == ChallengeType.METHOD)
            {
                EditorGUILayout.PropertyField(soChallenge.FindProperty("executor"), GUIContent.none);
                EditorGUILayout.PropertyField(soChallenge.FindProperty("method"), GUIContent.none);
                EditorGUILayout.PropertyField(soChallenge.FindProperty("_tag"), GUIContent.none);
                EditorGUILayout.BeginHorizontal();
                EditorGUILayout.LabelField("Level", new GUILayoutOption[] { GUILayout.Width(100) });
                EditorGUILayout.PropertyField(soChallenge.FindProperty("level"), GUIContent.none, new GUILayoutOption[] { GUILayout.Width(40) });
                EditorGUILayout.EndHorizontal();
                EditorGUILayout.BeginHorizontal();
                EditorGUILayout.LabelField("Difficulty", new GUILayoutOption[] { GUILayout.Width(100) });
                EditorGUILayout.PropertyField(soChallenge.FindProperty("difficulty"), GUIContent.none, new GUILayoutOption[] { GUILayout.Width(40) });
                EditorGUILayout.EndHorizontal();
            }
            EditorGUILayout.EndVertical();
            EditorUtility.SetDirty(challenge);
            soChallenge.ApplyModifiedProperties();
        }
        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));
                //}
            }
        }