Esempio n. 1
0
        private static void _Unspectate(ClientAccount account)
        {
            if (account == null || account.Account == null)
            {
                return;
            }

            var room = account.CurrentSpectatingRoom;

            if (room != null)
            {
                lock (room.Spectators)
                {
                    room.Spectators.Remove(account.Account.UserName);
                    account.CurrentSpectatingRoom = null;
                }
            }
        }
Esempio n. 2
0
 private static void _Logout(ClientAccount account, bool forced = false)
 {
     Trace.TraceInformation("{0} logged out", account.Account.UserName);
     if (account == null || account.LobbyService == null ||
         account.LobbyService.currentAccount == null)
     {
         return;
     }
     if (account.CurrentRoom != null)
     {
         if (_ExitRoom(account, forced) != RoomOperationResult.Success)
         {
             try
             {
                 account.OpContext.Channel.Close();
             }
             catch (Exception)
             {
                 account.OpContext.Channel.Abort();
             }
             return;
         }
     }
     lock (loggedInAccounts)
     {
         Trace.Assert(loggedInAccounts.ContainsKey(account.Account.UserName));
         if (!loggedInAccounts.ContainsKey(account.Account.UserName))
         {
             return;
         }
         account.LobbyService.currentAccount = null;
         account.CurrentSpectatingRoom       = null;
         loggedInAccounts.Remove(account.Account.UserName);
         try
         {
             account.OpContext.Channel.Close();
         }
         catch (Exception)
         {
             account.OpContext.Channel.Abort();
         }
     }
 }
Esempio n. 3
0
        public LoginStatus Login(int version, string username, string hash, out Account retAccount, out string reconnectionString, out LoginToken reconnectionToken)
        {
            reconnectionToken  = new LoginToken();
            reconnectionString = null;
            if (version != Misc.ProtocolVersion)
            {
                retAccount = null;
                return(LoginStatus.OutdatedVersion);
            }

            Account authenticatedAccount = Authenticate(username, hash);

            if (authenticatedAccount == null)
            {
                retAccount = null;
                return(LoginStatus.InvalidUsernameAndPassword);
            }
            var connection = OperationContext.Current.GetCallbackChannel <IGameClient>();

            lock (loggedInAccounts)
            {
                ClientAccount disconnected = null;
                if (loggedInAccounts.ContainsKey(username))
                {
                    disconnected = loggedInAccounts[username];
                    var ping = disconnected.CallbackChannel;
                    try
                    {
                        if (ping.Ping())
                        {
                            reconnectionString = null;
                            retAccount         = null;
                            return(LoginStatus.InvalidUsernameAndPassword);
                        }
                    }
                    catch (Exception)
                    {
                    }
                    disconnected.CallbackChannel = connection;
                    currentAccount = disconnected;
                    var room = disconnected.CurrentRoom;
                    if (room != null)
                    {
                        if (room.Room.State == RoomState.Gaming &&
                            !disconnected.Account.IsDead)
                        {
                            reconnectionString = room.Room.IpAddress.ToString() + ":" + room.Room.IpPort;
                            reconnectionToken  = disconnected.Account.LoginToken;
                        }
                        else
                        {
                            disconnected.CurrentRoom = null;
                        }
                    }
                }
                else
                {
                    var acc = new ClientAccount()
                    {
                        Account         = authenticatedAccount,
                        CallbackChannel = connection,
                        LobbyService    = this
                    };
                    loggedInAccounts.Add(username, acc);
                    currentAccount = acc;
                    // hack
                    var roomresult = from r in rooms.Values where r.Room.Seats.Any(st => st.Account == authenticatedAccount) select r;
                    if (roomresult.Count() > 0)
                    {
                        acc.CurrentRoom = roomresult.First();
                    }
                }
            }
            Trace.TraceInformation("{0} logged in", username);
            EventHandler faultHandler = (o, s) =>
            {
                try
                {
                    if (currentAccount.CurrentRoom.Room.State == RoomState.Gaming)
                    {
                        return;
                    }
                    _Logout(currentAccount);
                }
                catch (Exception)
                {
                }
            };

            OperationContext.Current.Channel.Faulted += faultHandler;
            OperationContext.Current.Channel.Closed  += faultHandler;
            retAccount = currentAccount.Account;
            _Unspectate(currentAccount);
            currentAccount.OpContext = OperationContext.Current;
            return(LoginStatus.Success);
        }
Esempio n. 4
0
 public LobbyServiceImpl()
 {
     currentAccount = null;
 }
Esempio n. 5
0
 private static void _Logout(ClientAccount account, bool forced = false)
 {
     Trace.TraceInformation("{0} logged out", account.Account.UserName);
     if (account == null || account.LobbyService == null ||
         account.LobbyService.currentAccount == null) return;
     if (account.CurrentRoom != null)
     {
         if (_ExitRoom(account, forced) != RoomOperationResult.Success)
         {
             try
             {
                 account.OpContext.Channel.Close();
             }
             catch (Exception)
             {
                 account.OpContext.Channel.Abort();
             }
             return;
         }
     }
     lock (loggedInAccounts)
     {
         Trace.Assert(loggedInAccounts.ContainsKey(account.Account.UserName));
         if (!loggedInAccounts.ContainsKey(account.Account.UserName)) return;
         account.LobbyService.currentAccount = null;
         account.CurrentSpectatingRoom = null;
         loggedInAccounts.Remove(account.Account.UserName);
         try
         {
             account.OpContext.Channel.Close();
         }
         catch (Exception)
         {
             account.OpContext.Channel.Abort();
         }
     }
 }
Esempio n. 6
0
        private static void _Unspectate(ClientAccount account)
        {
            if (account == null || account.Account == null) return;

            var room = account.CurrentSpectatingRoom;
            if (room != null)
            {
                lock (room.Spectators)
                {
                    room.Spectators.Remove(account.Account.UserName);
                    account.CurrentSpectatingRoom = null;
                }
            }
        }
Esempio n. 7
0
        private static RoomOperationResult _ExitRoom(ClientAccount account, bool forced = false)
        {
            if (account == null) return RoomOperationResult.Invalid;
            var room = account.CurrentRoom;
            if (room == null) return RoomOperationResult.Invalid;

            lock (room.Room)
            {
                lock (loggedInAccounts)
                {
                    var seat = room.Room.Seats.FirstOrDefault(s => s.Account == account.Account);
                    if (seat == null) return RoomOperationResult.Invalid;

                    if (!forced && room.Room.State == RoomState.Gaming
                         && !account.Account.IsDead)
                    {
                        return RoomOperationResult.Locked;
                    }

                    bool findAnotherHost = false;
                    if (seat.State == SeatState.Host)
                    {
                        findAnotherHost = true;
                    }
                    seat.Account = null;
                    seat.State = SeatState.Empty;
                    account.CurrentRoom = null;

                    if (_DestroyRoomIfEmpty(room))
                    {
                        return RoomOperationResult.Success;
                    }

                    if (findAnotherHost)
                    {
                        foreach (var host in room.Room.Seats)
                        {
                            if (host.Account != null)
                            {
                                host.State = SeatState.Host;
                                break;
                            }
                        }
                    }
                    _NotifyRoomLayoutChanged(room.Room.Id);
                    return RoomOperationResult.Success;
                }
            }
        }
Esempio n. 8
0
 public void Logout()
 {
     if (currentAccount == null) return;
     Trace.TraceInformation("{0} logged out", currentAccount.Account.UserName);
     _Logout(currentAccount);
     currentAccount = null;
 }
Esempio n. 9
0
        public LoginStatus Login(int version, string username, string hash, out Account retAccount, out string reconnectionString, out LoginToken reconnectionToken)
        {
            reconnectionToken = new LoginToken();
            reconnectionString = null;
            if (version != Misc.ProtocolVersion)
            {
                retAccount = null;
                return LoginStatus.OutdatedVersion;
            }

            Account authenticatedAccount = Authenticate(username, hash);
            if (authenticatedAccount == null)
            {
                retAccount = null;
                return LoginStatus.InvalidUsernameAndPassword;
            }
            var connection = OperationContext.Current.GetCallbackChannel<IGameClient>();
            lock (loggedInAccounts)
            {
                ClientAccount disconnected = null;
                if (loggedInAccounts.ContainsKey(username))
                {
                    disconnected = loggedInAccounts[username];
                    var ping = disconnected.CallbackChannel;
                    try
                    {
                        if (ping.Ping())
                        {
                            reconnectionString = null;
                            retAccount = null;
                            return LoginStatus.InvalidUsernameAndPassword;
                        }
                    }
                    catch (Exception)
                    {
                    }
                    disconnected.CallbackChannel = connection;
                    currentAccount = disconnected;
                    var room = disconnected.CurrentRoom;
                    if (room != null)
                    {
                        if (room.Room.State == RoomState.Gaming
                            && !disconnected.Account.IsDead)
                        {
                            reconnectionString = room.Room.IpAddress.ToString() + ":" + room.Room.IpPort;
                            reconnectionToken = disconnected.Account.LoginToken;
                        }
                        else
                        {
                            disconnected.CurrentRoom = null;
                        }
                    }
                }
                else
                {
                    var acc = new ClientAccount()
                    {
                        Account = authenticatedAccount,
                        CallbackChannel = connection,
                        LobbyService = this
                    };
                    loggedInAccounts.Add(username, acc);
                    currentAccount = acc;
                    // hack
                    var roomresult = from r in rooms.Values where r.Room.Seats.Any(st => st.Account == authenticatedAccount) select r;
                    if (roomresult.Count() > 0)
                    {
                        acc.CurrentRoom = roomresult.First();
                    }
                }
            }
            Trace.TraceInformation("{0} logged in", username);
            EventHandler faultHandler = (o, s) =>
                        {
                            try
                            {
                                if (currentAccount.CurrentRoom.Room.State == RoomState.Gaming) return;
                                _Logout(currentAccount);
                            }
                            catch (Exception)
                            {
                            }
                        };

            OperationContext.Current.Channel.Faulted += faultHandler;
            OperationContext.Current.Channel.Closed += faultHandler;
            retAccount = currentAccount.Account;
            _Unspectate(currentAccount);
            currentAccount.OpContext = OperationContext.Current;
            return LoginStatus.Success;
        }
Esempio n. 10
0
 public LobbyServiceImpl()
 {
     currentAccount = null;
 }
        private static RoomOperationResult _ExitRoom(ClientAccount account, bool forced = false)
        {
            if (account == null)
            {
                return(RoomOperationResult.Invalid);
            }
            var room = account.CurrentRoom;

            if (room == null)
            {
                return(RoomOperationResult.Invalid);
            }

            lock (room.Room)
            {
                lock (loggedInAccounts)
                {
                    var seat = room.Room.Seats.FirstOrDefault(s => s.Account == account.Account);
                    if (seat == null)
                    {
                        return(RoomOperationResult.Invalid);
                    }

                    if (!forced && room.Room.State == RoomState.Gaming &&
                        !account.Account.IsDead)
                    {
                        return(RoomOperationResult.Locked);
                    }

                    bool findAnotherHost = false;
                    if (seat.State == SeatState.Host)
                    {
                        findAnotherHost = true;
                    }
                    seat.Account        = null;
                    seat.State          = SeatState.Empty;
                    account.CurrentRoom = null;
                    account.LastAction  = DateTime.Now;

                    if (_DestroyRoomIfEmpty(room))
                    {
                        return(RoomOperationResult.Success);
                    }

                    if (findAnotherHost)
                    {
                        foreach (var host in room.Room.Seats)
                        {
                            if (host.Account != null)
                            {
                                host.State = SeatState.Host;
                                break;
                            }
                        }
                    }
                    if (room != null)
                    {
                        _NotifyRoomLayoutChanged(room.Room);
                    }
                    return(RoomOperationResult.Success);
                }
            }
        }