public async Task <ActionResult <Session> > Create()
        {
            UserModel user = await userManager.GetUserAsync(HttpContext.User);

            if (user?.UserId == null)
            {
                return(StatusCode(StatusCodes.Status403Forbidden));
            }

            Session session = SessionFactory.Create(user);

            await sessionService.Create(session);

            Game game = new Game {
                Id = session.Id
            };

            Board board = BoardFactory.New(session.Id);

            GameOperations.AddPlayer(game, user.UserId);


            await gameService.Create(game);

            await boardService.Create(board);

            user.AddSession(session);
            await userManager.UpdateAsync(user);

            return(CreatedAtRoute("GetSession", new { id = session.Id }, session));
        }
示例#2
0
        public async Task <ActionResult> JoinSession([FromRoute] string sessionId)
        {
            Task <UserModel> userTask = userManager.GetUserAsync(HttpContext.User);
            Task <Game>      gameTask = gameService.Get(sessionId);
            Session?         session  = await sessionService.Get(sessionId);

            if (session == null)
            {
                return(Status404NotFound("Session"));
            }
            if (session.LinkEnabled == false)
            {
                return(Status400Invalid("Session is private."));
            }
            if ((session.Players.Count + session.Invitations.Length) >= 4)
            {
                return(Status400Invalid("Session is full."));
            }
            Game game = await gameTask;

            if (game.Status != GameStatus.Preparing)
            {
                return(Status400Invalid("Game has started."));
            }
            UserModel user = await userTask;

            if (session.Players.Any((kvp) => kvp.Key == user.UserId))
            {
                return(NoContent());
            }

            user.AddSession(session);
            List <Task> updateTasks = new List <Task> {
                userManager.UpdateAsync(user)
            };

            GameOperations.AddPlayer(game, user.UserId);
            updateTasks.Add(gameService.Update(sessionId, game));
            session.Players.Add(user.UserId, PlayerSummary.CreateFromUser(user));
            updateTasks.Add(sessionService.Update(sessionId, session));
            Task.WaitAll(updateTasks.ToArray());
            return(NoContent());
        }