コード例 #1
0
        public async Task <ServiceResponse <CreateRoomViewModel> > CreateRoom(CreateRoomRequest request)
        {
            var player = await GetPlayer();

            if (player == null)
            {
                return(ServiceResponse <CreateRoomViewModel> .Error("No user found"));
            }

            var isRoomNameValid = NamesValidator.IsNameValid(request.RoomName);

            if (!isRoomNameValid)
            {
                return(ServiceResponse <CreateRoomViewModel> .Error("Room name is too short"));
            }

            var newRoom = new Room
            {
                Id             = Guid.NewGuid(),
                RoomName       = request.RoomName,
                PlayerMinCount = request.PlayerMinCount == 0 ? 2 : request.PlayerMinCount,
                PlayerMaxCount = request.PlayerMaxCount
            };

            await _context.Rooms.AddAsync(newRoom);

            player.RoomId = newRoom.Id;
            _context.Update(player);
            await _context.SaveChangesAsync();

            return(ServiceResponse <CreateRoomViewModel> .Ok(new CreateRoomViewModel(newRoom)));
        }
コード例 #2
0
        public async Task <ServiceResponse <bool> > CreateGame(CreateGameRequest request)
        {
            var player = await GetPlayer();

            if (player == null)
            {
                return(ServiceResponse <bool> .Error("No player found"));
            }

            var room = _context.Rooms.FirstOrDefault(x => x.Id == player.RoomId);

            if (room == null)
            {
                return(ServiceResponse <bool> .Error("No room found"));
            }

            var isRoomNameValid = NamesValidator.IsNameValid(request.GameName);

            if (!isRoomNameValid)
            {
                return(ServiceResponse <bool> .Error("Game name is too short"));
            }

            var newGame = new Game
            {
                Name         = request.GameName,
                RoomId       = room.Id,
                GameMasterId = player.Id
            };

            await _context.Games.AddAsync(newGame);

            await _context.SaveChangesAsync();

            player.GameId       = newGame.Id;
            player.IsGameMaster = true;
            _context.Update(player);
            await _context.SaveChangesAsync();

            return(ServiceResponse <bool> .Ok(true, "Game created"));
        }