Esempio n. 1
0
        public override async Task <CreateChallengeResponse> CreateChallenge(CreateChallengeRequest request, ServerCallContext context)
        {
            // TODO: Validation...

            var result = await _challengeService.CreateChallengeAsync(
                new ChallengeName(request.Name),
                request.Game.ToEnumeration <Game>(),
                new BestOf(request.BestOf),
                new Entries(request.Entries),
                new ChallengeDuration(TimeSpan.FromDays(request.Duration)),
                new UtcNowDateTimeProvider(),
                new Scoring(request.Scoring.Items.OrderBy(item => item.Order).ToDictionary(item => item.StatName, item => item.StatWeighting)));

            if (result.IsValid)
            {
                var response = new CreateChallengeResponse
                {
                    Challenge = ChallengeProfile.Map(result.Response)
                };

                return(context.Ok(response));
            }

            throw context.FailedPreconditionRpcException(result);
        }
Esempio n. 2
0
        public IHttpActionResult CreateChallenge(string data)
        {
            var testCreateChallengeMessage = new CreateChallengeMessage {
                OwnerId = 2, VictimId = 1, ChallengeType = "freeze"
            };
            var encoded = UrlEncode(testCreateChallengeMessage);

            var decodedInput = HttpUtility.UrlDecode(data);
            var message      = JsonConvert.DeserializeObject <CreateChallengeMessage>(decodedInput);

            var ownerId       = message.OwnerId;
            var victimId      = message.VictimId;
            var challengeType = message.ChallengeType;

            // need to check if the victim has a sensor of the type required by the challenge
            using (var context = new FirstSampleContext())
            {
                var result = context.SensorSet.Join(context.ChallengeTypeSet,
                                                    s => s.SensorType,
                                                    ct => ct.SensorType,
                                                    (s, ct) => new { s, ct }).
                             Where(sct => sct.ct.Type == challengeType && sct.s.UserId == victimId)
                             .ToList();
                CreateChallengeResponse response = new CreateChallengeResponse {
                    Status = "impossible"
                };
                if (result.Count == 0)
                {
                    return(Ok(response));
                }
                var sensorId        = result[0].s.SensorId;
                var challengeTypeId = result[0].ct.ChallengeTypeId;
                var newChallenge    = new Challenge
                {
                    ChallengeTypeId = challengeTypeId,
                    OwnerId         = ownerId,
                    VictimId        = victimId,
                    SensorId        = sensorId,
                    StartDate       = DateTime.Now,
                    Status          = "created"
                };

                context.ChallengeSet.Add(newChallenge);
                context.SaveChanges();
                response = new CreateChallengeResponse {
                    Status = "created"
                };
                return(Ok(response));
            }
        }