Example #1
0
        public async Task <IActionResult> PlayGameAsync(Guid linkOfGuid, Player player)
        {
            _logger.LogInformation($"room with id {linkOfGuid} start there game");
            await _sessionPlayRooms.DeleteAsync(linkOfGuid);

            var room = await _playRooms.Get(linkOfGuid);

            if (room == null)
            {
                var newPlayRoom = new PlayRoom()
                {
                    FirstPlayer  = player,
                    SecondPlayer = null
                };
                //
                await _playRooms.AddWithGuidAsync(linkOfGuid, newPlayRoom);

                _logger.LogInformation($"{player.Login} make his turn, he put {player.Command}");
                return(Ok());
            }

            room.SecondPlayer = player;
            await _playRooms.DeleteAsync(linkOfGuid);

            await _sessionPlayRooms.AddWithGuidAsync(linkOfGuid, room);

            _logger.LogInformation($"{player.Login} make his turn, he put {player.Command}");
            return(Ok());
        }
        /// <summary>
        /// 匹配房间玩家全部准备后调用的回调,创建一个游戏房间
        /// </summary>
        private void CreatePlayRoomAndStart(int matchRoomId, List <ClientPeer> clientList)
        {
            PlayRoom playRoom = Sessions.playSession.CreatePlayRoom(clientList);

            Console.WriteLine("匹配房间{0}->游戏房间{1}", matchRoomId, playRoom.roomId);
            playRoom.GameStart();
        }
Example #3
0
        /// <summary>
        /// Invoked when a new client joins the system
        /// </summary>
        public void Joined()
        {
            // 1: Add user to list of connected users
            // 2: If waiting list is empty add user to waiting list
            // 3: Else find an opponent (first in the waiting list) and remove him from the waiting list
            // 4: Create room and assign both users
            // 5: Create a group for this room
            // 6: Setup match (playRoom Id, initial ball direction, player on the left and right etc...)
            // 7: Notify the group the match can start
            // 8: Add the game to the list of games that the Engine must simulate
            var user = new User()
            {
                Id       = Context.ConnectionId,
                Username = Caller.username
            };

            _userRepository.AddUser(user);
            if (_userRepository.WaitingList.Count() == 0)
            {
                _userRepository.AddToWaitingList(user);
                Caller.wait();
            }
            else
            {
                var opponent = _userRepository.WaitingList.First();
                _userRepository.RemoveFromWaitingList(opponent);
                var playRoom = new PlayRoom()
                {
                    Id      = Guid.NewGuid().ToString(),
                    Player1 = opponent,
                    Player2 = user
                };
                _roomRepository.Add(playRoom);
                Task t1 = Groups.Add(opponent.Id, playRoom.Id);
                Task t2 = Groups.Add(user.Id, playRoom.Id);

                t1.Wait();
                t2.Wait();

                // Rough solution. We have to be sure the clients have received the group add messages over the wire
                // TODO: ask maybe on Jabbr or on StackOverflow and think about a better solution
                Thread.Sleep(3000);

                Player player1 = Engine.CreatePlayer(playRoom.Player1, 1, true);
                Player player2 = Engine.CreatePlayer(playRoom.Player2, 2, false);

                Game game = Engine.CreateGame(playRoom.Id, player1, player2);

                dynamic matchOptions = new ExpandoObject();
                matchOptions.PlayRoomId    = playRoom.Id;
                matchOptions.Player1       = playRoom.Player1;
                matchOptions.Player2       = playRoom.Player2;
                matchOptions.BallDirection = game.Ball.Direction;

                Clients[playRoom.Id].setupMatch(matchOptions);

                Thread.Sleep(3000);
                Engine.AddGame(game);
            }
        }
Example #4
0
        /// <summary>
        /// Fired when a user disconnects.
        /// </summary>
        /// <returns></returns>
        public Task Disconnect()
        {
            // 1: Get the user that disconnected
            // 2: Remove him from the list of connected users
            // 3: If the user was playing, notify the opponent that the user disconnected
            // 4: Re-queue the opponent in the waiting list
            // 5: Remove the room from the list
            User user = _userRepository.ConnectedUsers.Where(u => u.Id.Equals(Context.ConnectionId)).FirstOrDefault();

            if (user != null)
            {
                _userRepository.RemoveUser(user);
                _userRepository.RemoveFromWaitingList(user);
                PlayRoom room = _roomRepository.Rooms.Where(r => (r.Player1.Id.Equals(user.Id) || r.Player2.Id.Equals(user.Id))).FirstOrDefault();
                // if the user was in the middle of a match
                if (room != null)
                {
                    var opponent = room.Player1.Id.Equals(user.Id) ? room.Player2 : room.Player1;
                    _userRepository.AddToWaitingList(opponent);
                    _roomRepository.Remove(room);
                    Engine.RemoveGame(room.Id);
                    return(Clients[opponent.Id].opponentLeft());
                }
            }
            return(null);
        }
Example #5
0
        public async Task<IActionResult> PutPlayRoom(int id, PlayRoom playRoom)
        {
            if (id != playRoom.Id)
            {
                return BadRequest();
            }

            _context.Entry(playRoom).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!PlayRoomExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return NoContent();
        }       
Example #6
0
        public async Task<ActionResult<PlayRoom>> PostCreateRoom(PlayRoom playRoom)
        {
            if(playRoom == null || playRoom.OwnerId == 0 
                || string.IsNullOrWhiteSpace(playRoom.OwnerName)
                || string.IsNullOrWhiteSpace(playRoom.RoomName))
            {
                return BadRequest();
            }

            playRoom.DateCreated = DateTime.UtcNow;
            _context.PlayRooms.Add(playRoom);

            await _context.SaveChangesAsync();

            return CreatedAtAction("GetPlayRoom", new { id = playRoom.Id }, playRoom);
        }