Ejemplo n.º 1
0
        public async Task <CardAttemptPayload> AttemptKataAsync(
            AttemptKataInput input,
            [GlobalState] int currentUserId,
            [Service] AppDbContext db,
            [Service] KataService kata,
            CancellationToken cancellationToken)
        {
            var now        = DateTime.UtcNow;
            var engineName = input.EvaluationEngine switch {
                EvaluationEngine.Python => "python",
                _ => throw new QueryException(
                          ErrorBuilder
                          .New()
                          .SetMessage("Invalid execution engine")
                          .SetCode("INVALID_EXECUTION_ENGINE").Build())
            };

            var req = new KataRequest
            {
                Source = input.Guess,
                ExecutionEnvironment = engineName,
                ExecutionContext     = "default",
            };

            try
            {
                var kataResult = await kata.RunKata(req);

                var user = await db.Users.FindAsync(currentUserId);

                var card = await db.Cards.FindAsync(input.CardId);

                var cardAttempt = new CardAttempt
                {
                    Result               = (AttemptResult)kataResult.ResponseCode,
                    ResultText           = kataResult.ResponseText,
                    TimeBeforeResponding = TimeSpan.FromSeconds(1), // TODO
                    AttemptedAt          = now,
                    User = user,
                    Card = card,
                };

                card.MarkAttempt(cardAttempt.Result);
                db.CardAttempts.Add(cardAttempt);
                await db.SaveChangesAsync();

                return(new CardAttemptPayload(cardAttempt, input.ClientMutationId));
            }
            catch (KataExecutionException)
            {
                throw new QueryException(
                          ErrorBuilder
                          .New()
                          .SetMessage("Internal error, kata did not execute.")
                          .SetCode("KATA_RUNTIME").Build());
            }
        }
Ejemplo n.º 2
0
        public async Task <CardAttemptPayload> ReportAttemptAsync(
            [GlobalState] int currentUserId,
            ReportAttemptInput input,
            [Service] AppDbContext db,
            CancellationToken cancellationToken)
        {
            var card = await db.Cards.FindAsync(input.CardId);

            var cardAttempt = new CardAttempt
            {
                TimeBeforeResponding = TimeSpan.FromMilliseconds(input.TimeBeforeResponding),
                Result      = input.Result,
                User        = await db.Users.FindAsync(currentUserId),
                Card        = card,
                ResultText  = input.ResultText,
                AttemptedAt = DateTime.UtcNow,
            };

            card.MarkAttempt(cardAttempt.Result);
            db.CardAttempts.Add(cardAttempt);
            await db.SaveChangesAsync();

            return(new CardAttemptPayload(cardAttempt, input.ClientMutationId));
        }
Ejemplo n.º 3
0
 public CardAttemptPayload(CardAttempt cardAttempt, string?clientMutationId)
 {
     CardAttempt      = cardAttempt;
     ClientMutationId = clientMutationId;
 }