Esempio n. 1
0
 public LoginStatus Login(int version, string username, out LoginToken token, out Account retAccount)
 {
     Console.WriteLine("{0} logged in", username);
     var connection = OperationContext.Current.GetCallbackChannel<IGameClient>();
     OperationContext.Current.Channel.Faulted += new EventHandler(Channel_Faulted);
     OperationContext.Current.Channel.Closed += new EventHandler(Channel_Faulted);
     token = new LoginToken();
     token.token = System.Guid.NewGuid();
     Account account = new Account() { Username = username, Id = newAccountId++ };
     retAccount = account;
     loggedInGuidToAccount.Add(token.token, account);
     loggedInGuidToChannel.Add(token.token, connection);
     loggedInAccountToGuid.Add(account, token.token);
     loggedInChannelsToGuid.Add(connection, token.token);
     return LoginStatus.Success;
 }
Esempio n. 2
0
 public void NotifyChat(Account act, string message)
 {
     Application.Current.Dispatcher.BeginInvoke((ThreadStart)delegate()
     {
         if (_chatCache.Count > 100) _chatCache.RemoveRange(0, 50);
         _chatCache.Add(new KeyValuePair<string, string>(act.UserName, message));
         var handler = chatEventHandler;
         if (handler != null)
         {
             foreach (var cache in _chatCache)
             {
                 handler(cache.Key, cache.Value);
             }
             _chatCache.Clear();
         }
     });
 }
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 LoginStatus Login(int version, string username, out LoginToken token, out Account retAccount, out string reconnectionString)
 {
     var disconnected = accounts.FirstOrDefault(ac => ac.UserName == username);
     if (disconnected != null)
     {
         token = new LoginToken() { token = loggedInAccountToGuid[disconnected] };
         var ping = loggedInGuidToChannel[token.token];
         try
         {
             if (ping.Ping())
             {
                 reconnectionString = null;
                 retAccount = null;
                 return LoginStatus.InvalidUsernameAndPassword;
             }
         }
         catch (Exception)
         {
         }
     }
     else
     {
         token = new LoginToken();
         token.token = System.Guid.NewGuid();
     }
     reconnectionString = null;
     if (version != Misc.ProtocolVersion)
     {
         retAccount = null;
         return LoginStatus.OutdatedVersion;
     }
     Console.WriteLine("{0} logged in", username);
     var connection = OperationContext.Current.GetCallbackChannel<IGameClient>();
     OperationContext.Current.Channel.Faulted += new EventHandler(Channel_Faulted);
     OperationContext.Current.Channel.Closed += new EventHandler(Channel_Faulted);
     Account account = disconnected;
     if (account == null)
     {
         account = new Account() { UserName = username, Id = newAccountId++ };
         accounts.Add(account);
         if (accountContext != null)
         {
             try
             {
                 accountContext.Accounts.Add(account);
                 accountContext.SaveChanges();
             }
             catch (Exception e)
             {
                 Trace.TraceError(e.ToString());
             }
         }
     }
     retAccount = account;
     if (disconnected != null)
     {
         loggedInGuidToAccount.Remove(token.token);
         loggedInAccountToGuid.Remove(disconnected);
         if (loggedInGuidToChannel.ContainsKey(token.token))
         {
             loggedInChannelsToGuid.Remove(loggedInGuidToChannel[token.token]);
             loggedInGuidToChannel.Remove(token.token);
         }
     }
     loggedInGuidToAccount.Add(token.token, account);
     loggedInGuidToChannel.Add(token.token, connection);
     loggedInAccountToGuid.Add(account, token.token);
     loggedInChannelsToGuid.Add(connection, token.token);
     if (disconnected != null && loggedInGuidToRoom.ContainsKey(token.token) && loggedInGuidToRoom[token.token].State == RoomState.Gaming)
     {
         reconnectionString = loggedInGuidToRoom[token.token].IpAddress.ToString() + ":" + loggedInGuidToRoom[token.token].IpPort;
     }
     return LoginStatus.Success;
 }
Esempio n. 5
0
 private Account GetAccount(string username, string hash)
 {
     if (accountContext == null)
     {
         return new Account() { UserName = username };
     }
     var result = from a in accountContext.Accounts where a.UserName.Equals(username) select a;
     if (result.Count() == 0)
     {
         var account = new Account() { UserName = username };
         accountContext.Accounts.Add(account);
         accountContext.SaveChanges();
         return account;
     }
     return result.First();
 }
Esempio n. 6
0
 public LoginStatus Login(int version, string username, string hash, out LoginToken token, out Account retAccount, out string reconnectionString)
 {
     var disconnected = accounts.FirstOrDefault(ac => ac.UserName == username);
     if (!Authenticate(username, hash))
     {
         reconnectionString = null;
         retAccount = null;
         token = new LoginToken();
         token.TokenString = System.Guid.NewGuid();
         return LoginStatus.InvalidUsernameAndPassword;
     }
     if (disconnected != null)
     {
         token = new LoginToken() { TokenString = loggedInAccountToGuid[disconnected] };
         var ping = loggedInGuidToChannel[token.TokenString];
         try
         {
             if (ping.Ping())
             {
                 reconnectionString = null;
                 retAccount = null;
                 return LoginStatus.InvalidUsernameAndPassword;
             }
         }
         catch (Exception)
         {
         }
     }
     else
     {
         token = new LoginToken();
         token.TokenString = System.Guid.NewGuid();
     }
     reconnectionString = null;
     if (version != Misc.ProtocolVersion)
     {
         retAccount = null;
         return LoginStatus.OutdatedVersion;
     }
     Console.WriteLine("{0} logged in", username);
     var connection = OperationContext.Current.GetCallbackChannel<IGameClient>();
     OperationContext.Current.Channel.Faulted += new EventHandler(Channel_Faulted);
     OperationContext.Current.Channel.Closed += new EventHandler(Channel_Faulted);
     Account account = disconnected;
     if (account == null)
     {
         account = GetAccount(username, hash);
         accounts.Add(account);
     }
     retAccount = account;
     if (disconnected != null)
     {
         loggedInGuidToAccount.Remove(token.TokenString);
         loggedInAccountToGuid.Remove(disconnected);
         if (loggedInGuidToChannel.ContainsKey(token.TokenString))
         {
             loggedInChannelsToGuid.Remove(loggedInGuidToChannel[token.TokenString]);
             loggedInGuidToChannel.Remove(token.TokenString);
         }
     }
     loggedInGuidToAccount.Add(token.TokenString, account);
     loggedInGuidToChannel.Add(token.TokenString, connection);
     loggedInAccountToGuid.Add(account, token.TokenString);
     loggedInChannelsToGuid.Add(connection, token.TokenString);
     if (disconnected != null && loggedInGuidToRoom.ContainsKey(token.TokenString))
     {
         if (loggedInGuidToRoom[token.TokenString].State == RoomState.Gaming
             && gamingInfo.ContainsKey(loggedInGuidToRoom[token.TokenString]) && !gamingInfo[loggedInGuidToRoom[token.TokenString]].isDead[gamingInfo[loggedInGuidToRoom[token.TokenString]].Accounts.IndexOf(account)])
         {
             reconnectionString = loggedInGuidToRoom[token.TokenString].IpAddress.ToString() + ":" + loggedInGuidToRoom[token.TokenString].IpPort;
         }
         else
         {
             loggedInGuidToRoom.Remove(token.TokenString);
         }
     }
     return LoginStatus.Success;
 }