Example #1
0
        public State(Data.Contest ctx)
        {
            id = $"{ctx.ContestId}";

            switch (ctx.GetState())
            {
            case ContestState.Finalized:
                end_of_updates = DateTimeOffset.Now;
                thawed         = ctx.UnfreezeTime;
                ended          = ctx.EndTime;
                finalized      = frozen.HasValue ? thawed : ended;
                frozen         = ctx.FreezeTime;
                started        = ctx.StartTime;
                break;

            case ContestState.Ended:
                ended   = ctx.EndTime;
                frozen  = ctx.FreezeTime;
                started = ctx.StartTime;
                break;

            case ContestState.Frozen:
                frozen  = ctx.FreezeTime;
                started = ctx.StartTime;
                break;

            case ContestState.Started:
                started = ctx.StartTime;
                break;

            case ContestState.NotScheduled:
            case ContestState.ScheduledToStart:
            default:
                break;
            }
        }
Example #2
0
        public async Task <IActionResult> ChangeState(string target)
        {
            var now = DateTimeOffset.Now;

            var newcont = new Data.Contest
            {
                StartTime    = Contest.StartTime,
                EndTime      = Contest.EndTime,
                FreezeTime   = Contest.FreezeTime,
                UnfreezeTime = Contest.UnfreezeTime,
            };

            var state = newcont.GetState(now);

            if (target == "startnow")
            {
                if (!newcont.EndTime.HasValue)
                {
                    return(GoBackHome("Error no end time specified."));
                }
                now += TimeSpan.FromSeconds(30);
                DateTimeOffset old;

                if (newcont.StartTime.HasValue)
                {
                    // from scheduled to start
                    if (newcont.StartTime.Value < now)
                    {
                        return(GoBackHome("Error starting contest for the remaining time is less than 30 secs."));
                    }
                    old = newcont.StartTime.Value;
                }
                else
                {
                    // from delay to start
                    old = DateTimeOffset.UnixEpoch;
                }

                newcont.StartTime = now;
                newcont.EndTime   = now + (newcont.EndTime.Value - old);
                if (newcont.FreezeTime.HasValue)
                {
                    newcont.FreezeTime = now + (newcont.FreezeTime.Value - old);
                }
                if (newcont.UnfreezeTime.HasValue)
                {
                    newcont.UnfreezeTime = now + (newcont.UnfreezeTime.Value - old);
                }
            }
            else if (target == "freeze")
            {
                if (state != ContestState.Started)
                {
                    return(GoBackHome("Error contest is not started."));
                }
                newcont.FreezeTime = now;
            }
            else if (target == "endnow")
            {
                if (state != ContestState.Started && state != ContestState.Frozen)
                {
                    return(GoBackHome("Error contest has not started or has ended."));
                }
                newcont.EndTime = now;

                if (newcont.FreezeTime.HasValue && newcont.FreezeTime.Value > now)
                {
                    newcont.FreezeTime = now;
                }
            }
            else if (target == "unfreeze")
            {
                if (state != ContestState.Ended)
                {
                    return(GoBackHome("Error contest has not ended."));
                }
                newcont.UnfreezeTime = now;
            }
            else if (target == "delay")
            {
                if (state != ContestState.ScheduledToStart)
                {
                    return(GoBackHome("Error contest has been started."));
                }

                var old = newcont.StartTime.Value;
                newcont.StartTime = null;
                if (newcont.EndTime.HasValue)
                {
                    newcont.EndTime = DateTimeOffset.UnixEpoch + (newcont.EndTime.Value - old);
                }
                if (newcont.FreezeTime.HasValue)
                {
                    newcont.FreezeTime = DateTimeOffset.UnixEpoch + (newcont.FreezeTime.Value - old);
                }
                if (newcont.UnfreezeTime.HasValue)
                {
                    newcont.UnfreezeTime = DateTimeOffset.UnixEpoch + (newcont.UnfreezeTime.Value - old);
                }
            }

            await Store.UpdateAsync(Contest.ContestId,
                                    c => new Data.Contest
            {
                StartTime    = newcont.StartTime,
                EndTime      = newcont.EndTime,
                FreezeTime   = newcont.FreezeTime,
                UnfreezeTime = newcont.UnfreezeTime,
            });


            await HttpContext.AuditAsync("changed time", $"{Contest.ContestId}");

            return(GoBackHome("Contest state changed."));
        }