Esempio n. 1
0
    public void JoinRoom(bool isRunningGame, EnterRoomModel result)
    {
        if (result == null)
        {
            Logger.Error("[Main.JoinRoom] result is null");
            return;
        }

        if (result.player == null)
        {
            Logger.Debug("[Main.JoinRoom] player is null");
            return;
        }

        PlayerManager.inst.JoinPlayer(result.player, true);
        if (result.otherPlayers != null)
        {
            foreach (PlayerModel i in result.otherPlayers)
            {
                PlayerManager.inst.JoinPlayer(i, false);
            }
        }
        Cursor.lockState = CursorLockMode.Locked;
        Cursor.visible   = false;
        this.isOnGUI     = false;
        if (isRunningGame)
        {
            StartGame(isRunningGame, result.runningGameContext);
        }
        else
        {
            MapInfo.inst.EnableZone(false);
        }
    }
Esempio n. 2
0
        public async Task EnterRoom(EnterRoomModel enterRoomModel)
        {
            if (enterRoomModel == null)
            {
                await Clients.Caller.SendAsync("Error", new { Error = "invalid_model" });

                return;
            }

            string confirmToken = null;

            //check token
            if (enterRoomModel.ParticipantType == ParticipantType.Player)
            {
                confirmToken = await _roomRepo.GetPlayerTokenByIdAsync(enterRoomModel.RoomId);
            }
            else if (enterRoomModel.ParticipantType == ParticipantType.Listener)
            {
                confirmToken = await _roomRepo.GetListenTokenByIdAsync(enterRoomModel.RoomId);
            }

            //send error if participant`s type is invalid
            if (confirmToken == null)
            {
                await Clients.Caller.SendAsync("Error", new { Error = "invalid_type" });

                return;
            }
            //send error if token is invalid
            if (confirmToken != enterRoomModel.Token)
            {
                await Clients.Caller.SendAsync("Error", new { Error = "invalid_token" });

                return;
            }
            //get room
            var room = await _roomRepo.FindByIdAsync(enterRoomModel.RoomId);

            //send error if room doesn`t exist
            if (room == null)
            {
                await Clients.Caller.SendAsync("Error", new { Error = "doesnt_exist" });
            }
            //Get the players who are in the room now
            var participants = await _participantRepo.GetAsync(p => p.RoomId == room.Id && p.ParticipantType == ParticipantType.Player);

            //Delete the room if there are no players in it, and the waiting time for the first player has expired
            if (!participants.Any() && room.FirstConnectionExpired < DateTime.Now)
            {
                await _roomRepo.RemoveAsync(room, false);

                await Clients.Caller.SendAsync("Error", new { Error = "doesnt_exist" });

                await _roomRepo.SaveChangesAsync();

                return;
            }

            //Add participant to the room
            await Groups.AddToGroupAsync(Context.ConnectionId, room.Name);

            var participant = new Participant
            {
                ConnectionId    = Context.ConnectionId,
                RoomId          = room.Id,
                ParticipantType = enterRoomModel.ParticipantType,
                Name            = enterRoomModel.Name
            };
            await _participantRepo.CreateAsync(participant, false);

            var participantViewModel = participant.Adapt <ParticipantViewModel>();

            participantViewModel.RoomName = room.Name;
            await Clients.Group(room.Name).SendAsync("Join", participantViewModel);

            await _participantRepo.SaveChangesAsync();
        }
Esempio n. 3
0
 public void JoinPlayer(EnterRoomModel result)
 {
     Logger.DebugHighlight("[PacketNotification.JoinPlayer]");
     PlayerManager.inst.JoinPlayer(result.player, false);
 }