Пример #1
0
        public virtual void Load(bool serverRequiresAuth)
        {
            if (!serverRequiresAuth)
            {
                // no authorization means no accounts, which means no profiles.
                return;
            }

            if (Username == null || Username.Length < 1)
            {
                return;
            }

            lock (m_Lock)
            {
                UserRoles = Roles.GetRolesForUser(Username);

                // request auth ticket from game server
                ProfileBase profile = ProfileBase.Create(Username, true);

                int    maxCharacters      = 1;
                string loginHistoryIP     = "";
                string loginHistoryTime   = "";
                string logoffHistoryTime  = "";
                long   totalTimeOnAccount = 0;
                string alias      = "";
                byte[] profilePic = new byte[0];

                foreach (SettingsProperty prop in ProfileBase.Properties)
                {
                    switch (prop.Name)
                    {
                    case "MaxCharacters":
                        maxCharacters = (int)profile.GetPropertyValue("MaxCharacters");
                        break;

                    case "LoginHistoryIP":
                        loginHistoryIP = (string)profile.GetPropertyValue("LoginHistoryIP");
                        break;

                    case "LoginHistoryTime":
                        loginHistoryTime = (string)profile.GetPropertyValue("LoginHistoryTime");
                        break;

                    case "LogoffHistoryTime":
                        logoffHistoryTime = (string)profile.GetPropertyValue("LogoffHistoryTime");
                        break;

                    case "TotalTimeOnAccount":
                        totalTimeOnAccount = (long)profile.GetPropertyValue("TotalTimeOnAccount");
                        break;

                    case "Alias":
                        alias = (string)profile.GetPropertyValue("Alias");
                        break;

                    default:
                        switch (prop.PropertyType.FullName)
                        {
                        case "System.Int32":
                            AddedProperties.SetProperty(prop.Name, (int)profile.GetPropertyValue(prop.Name));
                            break;

                        case "System.Int16":
                            AddedProperties.SetProperty(prop.Name, (short)profile.GetPropertyValue(prop.Name));
                            break;

                        case "System.Bool":
                            AddedProperties.SetProperty(prop.Name, (bool)profile.GetPropertyValue(prop.Name));
                            break;

                        case "System.Single":
                            AddedProperties.SetProperty(prop.Name, (float)profile.GetPropertyValue(prop.Name));
                            break;

                        case "System.Double":
                            AddedProperties.SetProperty(prop.Name, (double)profile.GetPropertyValue(prop.Name));
                            break;

                        case "System.Byte":
                            AddedProperties.SetProperty(prop.Name, (byte)profile.GetPropertyValue(prop.Name));
                            break;

                        case "System.DateTime":
                            AddedProperties.SetProperty(prop.Name, (DateTime)profile.GetPropertyValue(prop.Name));
                            break;

                        case "System.Int64":
                            AddedProperties.SetProperty(prop.Name, (long)profile.GetPropertyValue(prop.Name));
                            break;

                        case "System.Guid":
                            AddedProperties.SetProperty(prop.Name, (Guid)profile.GetPropertyValue(prop.Name));
                            break;

                        case "System.Byte[]":
                            AddedProperties.SetProperty(prop.Name, (byte[])profile.GetPropertyValue(prop.Name));
                            break;

                        default:
                            if (profile.GetPropertyValue(prop.Name) == null)
                            {
                                AddedProperties.SetProperty(prop.Name, "");
                            }
                            else
                            {
                                AddedProperties.SetProperty(prop.Name, (string)profile.GetPropertyValue(prop.Name).ToString());
                            }
                            break;
                        }
                        break;
                    }
                }

                MaxCharacters      = maxCharacters;
                Alias              = alias;
                TotalTimeOnAccount = totalTimeOnAccount;

                if (loginHistoryIP == null || loginHistoryTime == null || logoffHistoryTime == null)
                {
                    Log1.Logger("Server").Warn("Failed to read login history for user [" + Username + "]. If this is the first login, it's not unusual.");
                    return;
                }
                else
                {
                    string[] ips     = loginHistoryIP.Trim().Split(char.Parse("|"));
                    string[] logins  = loginHistoryTime.Trim().Split(char.Parse("|"));
                    string[] logouts = logoffHistoryTime.Trim().Split(char.Parse("|"));

                    if (ips.Length != logins.Length || logins.Length != logouts.Length)
                    {
                        Log1.Logger("Server").Error("Failed to read login history for user [" + Username + "]. Login history arrays are of inconsistent length.");
                        return;
                    }

                    LoginHistoryIP    = new Queue <string>();
                    LogoffHistoryTime = new Queue <DateTime>();
                    LoginHistoryTime  = new Queue <DateTime>();

                    Log1.Logger("Server").Debug("Loading [" + ips.Length.ToString() + "] login sessions for profile of [" + Username + "].");
                    for (int i = 0; i < ips.Length; i++)
                    {
                        string ip     = ips[i];
                        string login  = logins[i];
                        string logout = logouts[i];

                        if (ip.Trim().Length < 1 || login.Trim().Length < 1 || logout.Trim().Length < 1)
                        {
                            continue;
                        }

                        LoginHistoryIP.Enqueue(ip);
                        LogoffHistoryTime.Enqueue(DateTime.Parse(logout));
                        LoginHistoryTime.Enqueue(DateTime.Parse(login));
                    }
                }
            }
        }
Пример #2
0
        public virtual bool Save(bool serverRequiresAuth)
        {
            if (!serverRequiresAuth)
            {
                // no authorization means no accounts, which means no profiles.
                return(true);
            }

            if (LoginHistoryIP.Count != LoginHistoryTime.Count || LoginHistoryTime.Count != LogoffHistoryTime.Count)
            {
                Log1.Logger("Server").Error("Unable to save user's [" + Username + "] profile.  Login History variables are not the same size.");
                return(false);
            }

            try
            {
                lock (m_Lock)
                {
                    ProfileBase profile = ProfileBase.Create(Username, true);
                    profile.SetPropertyValue("MaxCharacters", MaxCharacters);
                    string[] curRoles = Roles.GetRolesForUser(Username);
                    foreach (string role in UserRoles)
                    {
                        try
                        {
                            if (Array.IndexOf(curRoles, role) == -1)
                            {
                                Roles.AddUserToRole(Username, role);
                            }
                        }
                        catch (Exception e)
                        {
                            Roles.CreateRole(role);
                            Roles.AddUserToRole(Username, role);
                        }
                    }

                    if (LoginHistoryIP.Count != LoginHistoryTime.Count || LoginHistoryTime.Count != LogoffHistoryTime.Count)
                    {
                        Log1.Logger("Server").Error("Unable to save user's [" + Username + "] login history to profile.  Login History arrays are not the same size.");
                    }
                    else
                    {
                        string ips     = "";
                        string logins  = "";
                        string logouts = "";

                        for (int i = 0; i < LoginHistoryIP.Count; i++)
                        {
                            ips     += LoginHistoryIP.ElementAt(i) + "|";
                            logins  += LoginHistoryTime.ElementAt(i) + "|";
                            logouts += LogoffHistoryTime.ElementAt(i) + "|";
                        }

                        ips     = ips.Trim(char.Parse("|"));
                        logins  = logins.Trim(char.Parse("|"));
                        logouts = logouts.Trim(char.Parse("|"));

                        profile.SetPropertyValue("LoginHistoryIP", ips);
                        profile.SetPropertyValue("LoginHistoryTime", logins);
                        profile.SetPropertyValue("LogoffHistoryTime", logouts);

                        long currentTimeSpent = (long)profile.GetPropertyValue("TotalTimeOnAccount");

                        Int64Property pl = AddedProperties.GetProperty("TotalTimeOnAccount") as Int64Property;
                        if (pl != null)
                        {
                            pl.Value = currentTimeSpent + m_TotalSessionTime;
                            Log1.Logger("Account").Info("Updating total account login time for [" + Username + "] from " + new TimeSpan(currentTimeSpent).ToString("g") + " to " + new TimeSpan(currentTimeSpent + m_TotalSessionTime).ToString("g"));
                        }
                        m_TotalSessionTime = 0;

                        foreach (Property p in AddedProperties.AllProperties)
                        {
                            switch (p.Name)
                            {
                            case "LoginHistoryIP":
                            case "LoginHistoryTime":
                            case "LogoffHistoryTime":
                            case "":
                                continue;

                            default:
                                profile.SetPropertyValue(p.Name, p.PropertyValue());
                                break;
                            }
                        }

                        Log1.Logger("Server").Debug("Saving [" + LoginHistoryIP.Count + "] login sessions for profile of [" + Username + "].");
                    }
                    //profile.SetPropertyValue("Alias", Alias);

                    profile.Save();
                    Log1.Logger("Server").Info("Saved profile info [" + Username + "].");
                }
            }
            catch (Exception e)
            {
                Log1.Logger("Server").Error("Failed to save profile for user [" + Username + "]. " + e.Message);
                return(false);
            }

            return(true);
        }
Пример #3
0
 protected virtual void OnAddedProperties(AddedPropertyEventArgs e)
 {
     AddedProperties?.Invoke(this, e);
 }