Example #1
0
        public async Task CreateRoom(PlayerViewModel request)
        {
            request.ConnectionId = Context.ConnectionId;
            if (!string.IsNullOrEmpty(request.UID))
            {
                var connectingUser = await PlayerViewModel.CreateRoomAsync(request, rooms);

                if (connectingUser.IsSucceed)
                {
                    string ip = Context.Connection.RemoteIpAddress.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6
                        ? Context.Connection.RemoteIpAddress.MapToIPv6().ToString()
                        : Context.Connection.RemoteIpAddress.MapToIPv4().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.CreateRoom, Context.ConnectionId);
                    SendToAll(data.Player.CurrentRoom, GameReponseKey.NewRoom, true, false);
                    await UpdateOnlineStatus(connectingUser.Data);
                }
                else
                {
                    await Clients.Client(Context.ConnectionId).SendAsync(receiveMethod, connectingUser);
                }
            }
        }