Esempio n. 1
0
        public async Task JoinRoom(PlayerViewModel request)
        {
            request.ConnectionId = Context.ConnectionId;
            if (!string.IsNullOrEmpty(request.UID))
            {
                var expiredRooms = rooms.Where(r => r.StartDate.HasValue).ToList();
                expiredRooms.ForEach(r => rooms.Remove(r));

                var connectingUser = await PlayerViewModel.JoinRoomAsync_v2(request, rooms);

                if (connectingUser.IsSucceed)
                {
                    string ip = Context.Connection.RemoteIpAddress.ToString();
                    connectingUser.Data.IsOnline = true;
                    connectingUser.Data.Ip       = ip;
                    var data = new ConnectViewModel()
                    {
                        Player = connectingUser.Data,
                        Others = PlayerViewModel.Repository.GetModelListBy(p => p.Uid != connectingUser.Data.UID &&
                                                                           p.Room == connectingUser.Data.Room && !string.IsNullOrEmpty(p.ConnectionId)).Data
                    };
                    SendToClient(data, GameReponseKey.JoinRoom, Context.ConnectionId);
                    SendToGroup(data.Player, GameReponseKey.NewMember, data.Player.Room, false);
                    if (connectingUser.Data.CurrentRoom.Players.Count >= 4)
                    {
                        SendToAll(connectingUser.Data.CurrentRoom, GameReponseKey.RoomStarted);
                    }
                    await UpdateOnlineStatus(connectingUser.Data);
                }
                else
                {
                    await Clients.Client(Context.ConnectionId).SendAsync(receiveMethod, connectingUser);
                }
            }
        }