コード例 #1
0
        public Game StartAGame(long id)
        {
            Game game = Context.Set <Game>().AsNoTracking()
                        .Include(g => g.Missions)
                        .Include(g => g.Agents)
                        .Include(g => g.Actions)
                        .First(g => g.Id == id);

            var randomOrderedAgents   = Shuffle <Agent>(game.Agents);
            var randomOrderedMissions = Shuffle <Mission>(game.Missions);

            for (var i = 0; i < randomOrderedAgents.Count; i++)
            {
                var sourceAgent = randomOrderedAgents.ElementAt(i);
                var targetAgent = randomOrderedAgents.ElementAt((i == (game.Agents.Count - 1)) ? 0 : (i + 1));

                var mission = randomOrderedMissions.ElementAt(i);

                sourceAgent.TargetId  = targetAgent.Id;
                sourceAgent.MissionId = mission.Id;
                sourceAgent.Status    = Constantes.AGENT_STATUS_ALIVE;
                sourceAgent.Life      = Constantes.AGENT_LIFE_AT_START;

                Context.Agents.Update(sourceAgent);

                //mission.agentId
                mission.IsUsed = true;
                Context.Missions.Update(mission);
            }

            game.Status = Constantes.GAME_STATUS_STARTED;
            Context.Games.Update(game);

            var action = new Models.Action()
            {
                GameId = game.Id,
                Type   = Constantes.ACTTION_TYPE_GAME_STARTED
            };

            Context.Actions.Add(action);

            Context.SaveChanges();
            this.SendToAll(game.Id.ToString(), Constantes.REQUEST_METHOD_NAME, new Request
            {
                GameId = game.Id,
                Type   = Constantes.REQUEST_TYPE_GAME_STATUS
            });

            foreach (var agent in game.Agents)
            {
                _webPushService.SendPushNotification(agent.Id, Constantes.ACTTION_TYPE_GAME_STARTED);
            }
            return(game);
        }
コード例 #2
0
        public IActionResult Push([FromBody] Request req)
        {
            if (req == null)
            {
                throw new Exception();
            }
            try
            {
                switch (req.Type)
                {
                case Constantes.REQUEST_TYPE_CHANGE_MISSION:
                    if (!this.ChangeMission(req.EmitterId))
                    {
                        return(Ok(req));
                    }
                    req.IsTreated = true;
                    break;

                case Constantes.REQUEST_TYPE_CONFIRM_KILL:
                    var killerToUpdate = Context.Agents.Include(a => a.Mission).First(a => a.TargetId == req.EmitterId);
                    req.ReceiverId = killerToUpdate.Id;
                    this.Kill(req.EmitterId, killerToUpdate);
                    req.IsTreated = true;
                    break;

                case Constantes.REQUEST_TYPE_CONFIRM_UNMASK:
                    this.Unmask(req.EmitterId);
                    req.IsTreated = true;
                    break;

                case Constantes.REQUEST_TYPE_SUICIDE:
                    this.Suicide(req.EmitterId);
                    req.IsTreated = true;
                    break;

                case Constantes.REQUEST_TYPE_ASK_UNMASK:
                    if (CheckFinal(req.GameId))
                    {
                        throw new Exception(string.Format("Impossible de démasquer lorsqu'il ne reste que {0} joueurs", Constantes.FINAL_NB));
                    }
                    if (!this.AskUnmask(req.EmitterId, req.ReceiverId))
                    {
                        Context.SaveChanges();
                        this.SendToAll(req.GameId.ToString(), Constantes.REQUEST_METHOD_NAME, new Request()
                        {
                            Type      = Constantes.REQUEST_TYPE_WRONG_KILLER,
                            GameId    = req.GameId,
                            IsTreated = true
                        });
                        throw new Exception("Non, ce n'est pas votre killer");
                    }
                    else
                    {
                        _webPushService.SendPushNotification(req.ReceiverId, req.Type);
                    }
                    break;

                case Constantes.REQUEST_TYPE_ASK_KILL:
                    _webPushService.SendPushNotification(req.ReceiverId, req.Type);
                    break;
                }

                if (req.ParentRequestId > 0)
                {
                    var treatedRequest = Context.Requests.AsNoTracking().First(r => r.Id == req.ParentRequestId);
                    treatedRequest.IsTreated = true;
                    Context.Update(treatedRequest);
                }
                Context.Add(req);
                CheckGameEnd(req.GameId);
                Context.SaveChanges();

                req.Game          = null;
                req.Emitter       = null;
                req.Receiver      = null;
                req.ParentRequest = null;
                this.SendToAll(req.GameId.ToString(), Constantes.REQUEST_METHOD_NAME, req);
            }
            catch (Exception ex)
            {
                return(StatusCode(406, ex.Message));
            }


            return(Ok(req));
        }