Example #1
0
        /// <summary>
        /// Creates a new player using the specified identifier in the specified database.
        /// </summary>
        internal static LogicClientAvatar Create(Connection connection, int highId = 0, int lowId = 0, DBMS database = Settings.Database)
        {
            int low = (lowId == 0) ? Interlocked.Increment(ref Avatars.Seed) : lowId;

            LogicClientAvatar avatar = new LogicClientAvatar(connection, new LogicLong(highId, low))
            {
                Token = Loader.Random.GenerateRandomString()
            };

            switch (database)
            {
            case DBMS.Mongo:
            {
                AvatarDb.Create(avatar).GetAwaiter().GetResult();
                break;
            }

            case DBMS.File:
            {
                FileInfo file = new FileInfo($"{Directory.GetCurrentDirectory()}/Saves/Players/{avatar}.json");

                if (!file.Exists)
                {
                    file.WriteAllText(JsonConvert.SerializeObject(avatar, AvatarDb.JsonSettings));
                }

                break;
            }
            }

            Avatars.Add(avatar);

            return(avatar);
        }
Example #2
0
        /// <summary>
        /// Gets the player using the specified identifier in the specified database.
        /// </summary>
        internal static LogicClientAvatar Get(Connection connection, LogicLong id, DBMS database = Settings.Database)
        {
            int high = id.High;
            int low  = id.Low;

            if (!Avatars.Pool.TryGetValue(id, out LogicClientAvatar avatar))
            {
                switch (database)
                {
                case DBMS.Mongo:
                {
                    AvatarDb save = AvatarDb.Load(high, low).GetAwaiter().GetResult();

                    if (save != null)
                    {
                        avatar = Avatars.Load(save.Profile.ToJson());

                        if (avatar == null)
                        {
                            Debugger.Error($"Unable to load account with the ID {id}.");
                            return(null);
                        }

                        avatar.Connection = connection;
                        connection.Avatar = avatar;
                    }

                    break;
                }

                case DBMS.File:
                {
                    FileInfo file = new FileInfo($"{Directory.GetCurrentDirectory()}/Saves/Players/{id}.json");

                    if (file.Exists)
                    {
                        string json = file.ReadAllText();

                        if (!json.IsNullOrEmptyOrWhitespace())
                        {
                            avatar = JsonConvert.DeserializeObject <LogicClientAvatar>(json, AvatarDb.JsonSettings);

                            avatar.Connection = connection;
                            connection.Avatar = avatar;
                        }
                        else
                        {
                            Debugger.Error($"The data returned was null/empty/whitespace at Get({id}, {database}).");
                        }
                    }

                    break;
                }
                }
            }

            return(avatar);
        }
Example #3
0
        /// <summary>
        /// Gets the player using the specified identifier in the specified database.
        /// </summary>
        internal static LogicClientAvatar Get(LogicLong id, DBMS database = Settings.Database)
        {
            if (!Avatars.Pool.TryGetValue(id, out LogicClientAvatar avatar))
            {
                switch (database)
                {
                case DBMS.Mongo:
                {
                    AvatarDb save = Mongo.Avatars.Find(db => db.HighID == id.High && db.LowID == id.Low).SingleOrDefault();

                    if (save != null)
                    {
                        avatar = Avatars.Load(save.Profile.ToJson());

                        if (avatar == null)
                        {
                            Debugger.Error($"Unable to load account with the ID {id}, recreating...");
                            avatar = Avatars.Create(null, id);
                        }
                    }

                    break;
                }

                case DBMS.File:
                {
                    FileInfo file = new FileInfo($"{Directory.GetCurrentDirectory()}/Saves/Players/{id}.json");

                    if (file.Exists)
                    {
                        string json = file.ReadAllText();

                        if (!json.IsNullOrEmptyOrWhitespace())
                        {
                            avatar = JsonConvert.DeserializeObject <LogicClientAvatar>(json, AvatarDb.JsonSettings);
                        }
                        else
                        {
                            Debugger.Error($"The data returned was null/empty/whitespace at Get({id}, {database}).");
                        }
                    }

                    break;
                }
                }
            }

            return(avatar);
        }
Example #4
0
        /// <summary>
        /// Deletes the specified player in the specified database.
        /// </summary>
        internal static async void Delete(int high, int low, DBMS database = Settings.Database)
        {
            switch (database)
            {
            case DBMS.Mongo:
            {
                await AvatarDb.Delete(high, low);

                break;
            }

            case DBMS.File:
            {
                new FileInfo($"{Directory.GetCurrentDirectory()}/Saves/Players/{high}-{low}.json").DeleteIfExists();
                break;
            }
            }
        }
Example #5
0
        /// <summary>
        /// Deletes the specified player in the specified database.
        /// </summary>
        internal static async void Delete(LogicClientAvatar avatar, DBMS database = Settings.Database)
        {
            switch (database)
            {
            case DBMS.Mongo:
            {
                await AvatarDb.Delete(avatar.HighID, avatar.LowID);

                break;
            }

            case DBMS.File:
            {
                new FileInfo($"{Directory.GetCurrentDirectory()}/Saves/Players/{avatar}.json").DeleteIfExists();
                break;
            }
            }
        }
Example #6
0
        /// <summary>
        /// Saves the specified player in the specified database.
        /// </summary>
        internal static async void Save(LogicClientAvatar avatar, DBMS database = Settings.Database)
        {
            switch (database)
            {
            case DBMS.Mongo:
            {
                await AvatarDb.Save(avatar);

                break;
            }

            case DBMS.File:
            {
                FileInfo file = new FileInfo($"{Directory.GetCurrentDirectory()}/Saves/Players/{avatar}.json");

                if (file.Exists)
                {
                    file.WriteAllText(JsonConvert.SerializeObject(avatar, AvatarDb.JsonSettings));
                }

                break;
            }
            }
        }