Ejemplo n.º 1
0
        /// <summary>
        /// Initializes the <see cref="Alliances"/> class.
        /// </summary>
        internal static void Init()
        {
            if (Alliances.Initialized)
            {
                return;
            }

            Alliances.Pool = new ConcurrentDictionary <long, Alliance>();

            switch (Settings.Database)
            {
            case DBMS.Mongo:
            {
                foreach (AllianceDb dbEntry in Mongo.Alliances.Find(db => true).ToList())
                {
                    if (dbEntry != null)
                    {
                        Alliance alliance = new Alliance(dbEntry.HighID, dbEntry.LowID);

                        JsonConvert.PopulateObject(dbEntry.Profile.ToJson(), alliance, AllianceDb.JsonSettings);

                        Alliances.Add(alliance);
                    }
                }

                Alliances.Seed = Mongo.AllianceSeed;

                break;
            }

            case DBMS.File:
            {
                DirectoryInfo directory = new DirectoryInfo($"{System.IO.Directory.GetCurrentDirectory()}/Saves/Alliances/");

                directory.CreateIfNotExists();
                directory.DeleteIfExists(".json");

                Parallel.ForEach(directory.GetFiles("*.json"), file =>
                    {
                        string[] id = Path.GetFileNameWithoutExtension(file.Name).Split('-');
                        Alliances.Add(Alliances.Get(LogicStringUtil.ConvertToInt(id[0]), LogicStringUtil.ConvertToInt(id[1])));
                    });

                Alliances.Seed = directory.GetFiles("*.json").Length;
                break;
            }
            }

            Console.WriteLine($"Loaded {Avatars.Count} {((Avatars.Count != 1) ? "avatars" : "avatar")} and {Alliances.Count} {((Alliances.Count != 1) ? "alliances" : "alliance")} into memory." + Environment.NewLine);

            Alliances.Initialized = true;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Gets the alliance using the specified identifier in the specified database.
        /// </summary>
        internal static Alliance Get(int highId, int lowId, DBMS database = Settings.Database, bool store = true)
        {
            long id = (long)highId << 32 | (uint)lowId;

            if (!Alliances.Pool.TryGetValue(id, out Alliance alliance))
            {
                switch (database)
                {
                case DBMS.Mongo:
                {
                    AllianceDb save = Mongo.Alliances.Find(db => db.HighID == highId && db.LowID == lowId).SingleOrDefault();

                    if (save != null)
                    {
                        alliance = Alliances.Load(save.Profile.ToJson());

                        if (alliance == null)
                        {
                            Debugger.Error($"Unable to load alliance with the ID {highId}-{lowId}.");
                        }
                    }

                    break;
                }

                case DBMS.File:
                {
                    FileInfo file = new FileInfo($"{Directory.GetCurrentDirectory()}/Saves/Alliances/{highId}-{lowId}.json");

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

                        if (!json.IsNullOrEmptyOrWhitespace())
                        {
                            alliance = JsonConvert.DeserializeObject <Alliance>(json, AllianceDb.JsonSettings);
                        }
                        else
                        {
                            Debugger.Error($"The data returned wasn't null but empty, at Get({highId}, {lowId}, File, {store}).");
                        }
                    }

                    break;
                }
                }
            }

            return(alliance);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Saves this instance.
        /// </summary>
        internal static void Save(DBMS database = Settings.Database)
        {
            Alliances.ForEach(alliance =>
            {
                try
                {
                    Alliances.Save(alliance, database);
                }
                catch (Exception)
                {
                    Debugger.Debug($"Did not succeed in saving alliance [{alliance}].");
                }
            });

            Debugger.Info($"Saved {Alliances.Count} alliances.");
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Removes the specified alliance.
        /// </summary>
        internal static void Remove(Alliance alliance)
        {
            if (Alliances.Pool.ContainsKey(alliance.Identifier))
            {
                if (!Alliances.Pool.TryRemove(alliance.Identifier, out Alliance tmpAlliance))
                {
                    Debugger.Error("Unsuccessfully removed the specified alliance from the dictionary.");
                }
                else
                {
                    if (!tmpAlliance.Equals(alliance))
                    {
                        Debugger.Error("Successfully removed a alliance from the list but the returned alliance was not equal to our alliance.");
                    }
                }
            }

            Alliances.Save(alliance);
        }
Ejemplo n.º 5
0
        internal static void Delete(Alliance alliance, DBMS database = Settings.Database)
        {
            Alliances.Remove(alliance);

            switch (database)
            {
            case DBMS.Mongo:
            {
                AllianceDb.Delete(alliance.HighID, alliance.LowID).GetAwaiter().GetResult();
                break;
            }

            case DBMS.File:
            {
                new FileInfo($"{Directory.GetCurrentDirectory()}/Saves/Alliances/{alliance.HighID}-{alliance.LowID}.json").DeleteIfExists();
                break;
            }
            }

            Alliances.Save();
        }
Ejemplo n.º 6
0
 /// <summary>
 /// Creates a alliance with the specified identifier in the specified database.
 /// </summary>
 internal static Alliance Create(LogicLong id, DBMS database = Settings.Database, bool store = true)
 {
     return(Alliances.Create(id.High, id.Low, database));
 }
Ejemplo n.º 7
0
 /// <summary>
 /// Gets the alliance using the specified identifier in the specified database.
 /// </summary>
 internal static Alliance Get(LogicLong identifier, DBMS database = Settings.Database, bool store = true)
 {
     return(Alliances.Get(identifier.High, identifier.Low, database));
 }