Ejemplo n.º 1
0
        public async Task <IActionResult> Patch(
            Guid id,
            [FromServices] ManagementServiceClient mgmt,
            [FromServices] IHubContext <OnlineJudgeHub> hub,
            CancellationToken token)
        {
            var judge = await DB.JudgeStatuses
                        .Include(x => x.RelatedStateMachineIds)
                        .Include(x => x.SubStatuses)
                        .SingleOrDefaultAsync(x => x.Id == id, token);

            if (judge == null)
            {
                return(Result(404, "Not found"));
            }

            var request = JsonConvert.DeserializeObject <JudgeStatus>(RequestBody);

            if (request.Result != JudgeResult.Pending)
            {
                return(Result(400, "Operation is not supported"));
            }

            if (judge.RelatedStateMachineIds.Count < 1)
            {
                return(Result(404, "State machine is not found"));
            }

            if (!User.IsSignedIn() || judge.UserId != User.Current.Id && !IsMasterOrHigher && !await HasPermissionToProblemAsync(judge.ProblemId) && !await HasPermissionToContestAsync(judge.ContestId, token) && !await HasPermissionToSpecifiedGroupAsync(judge.GroupId, token))
            {
                return(Result(401, "No permission"));
            }

            if ((judge.Result == JudgeResult.Pending || judge.Result == JudgeResult.Running) && !IsMasterOrHigher)
            {
                return(Result(400, "The state machine is running."));
            }

            foreach (var x in judge.SubStatuses)
            {
                x.Result           = JudgeResult.Pending;
                x.Hint             = null;
                x.TimeUsedInMs     = 0;
                x.MemoryUsedInByte = 0;
            }
            judge.Hint   = null;
            judge.Result = JudgeResult.Pending;

            await DB.SaveChangesAsync(token);

            await mgmt.PatchStateMachineInstanceAsync(judge.RelatedStateMachineIds.Last().StateMachineId, "Start", token);

            hub.Clients.All.InvokeAsync("ItemUpdated", "judge", judge.Id);
            return(Result(200, "Patch succeeded"));
        }
Ejemplo n.º 2
0
 static void Main(string[] args)
 {
     while (true)
     {
         var id      = Guid.Parse(Console.ReadLine());
         var builder = new DbContextOptionsBuilder <OnlineJudgeContext>();
         var connStr = JsonConvert.DeserializeObject <dynamic>(File.ReadAllText("config.json")).Oj;
         builder.UseMySql((string)connStr);
         using (var db = new OnlineJudgeContext(builder.Options))
         {
             var statemachineId = db.JudgeStatusStateMachines.Where(x => x.StatusId == id).Last().StateMachineId;
             var mgmt           = new ManagementServiceClient("https://mgmtsvc.1234.sh", @"C:\Users\Yuko\Documents\webapi-client.pfx", "123456");
             mgmt.PatchStateMachineInstanceAsync(statemachineId, "Start");
             Console.WriteLine("OK");
         }
     }
 }