/// <summary> /// Gets the range. /// </summary> /// <param name="Offset">The offset.</param> /// <param name="Limit">The limit.</param> internal List <Clan> GetRange(int Offset = 0, int Limit = 50, bool Store = true) { List <Clan> Clans = new List <Clan>(Limit - Offset); DbSqlQuery <Core.Database.Models.Clans> SqlClans; using (GBS_MySQL MySQL = new GBS_MySQL()) { SqlClans = MySQL.Clans.SqlQuery("SELECT * FROM Clans LIMIT " + Offset + ", " + Limit + ";"); if (SqlClans != null) { if (SqlClans.Any()) { foreach (Core.Database.Models.Clans Data in SqlClans) { if (!string.IsNullOrEmpty(Data.Data)) { Clan Clan = new Clan(Data.HighID, Data.LowID); JsonConvert.PopulateObject(Data.Data, Clan, this.Settings); if (Store && Clan.Members.Entries.Count > 0) { this.Add(Clan); } Clans.Add(Clan); } } } } } return(Clans); }
/// <summary> /// Saves the specified player in the specified database. /// </summary> /// <param name="Player">The player.</param> /// <param name="DBMS">The DBMS.</param> internal void Save(Player Player, DBMS DBMS = Constants.Database) { switch (DBMS) { case DBMS.MySQL: { using (GBS_MySQL Database = new GBS_MySQL()) { var Data = Database.Players.Find(Player.HighID, Player.LowID); if (Data != null) { Data.HighID = Player.HighID; Data.LowID = Player.LowID; Data.Data = JsonConvert.SerializeObject(Player, this.Settings); } else { Logging.Error(this.GetType(), "The database returned a null value when we tried to get a player."); } Database.SaveChangesAsync(); } break; } case DBMS.Redis: { Redis.Players.StringSetAsync(Player.ToString(), JsonConvert.SerializeObject(Player, this.Settings), TimeSpan.FromMinutes(30)); break; } case DBMS.Both: { this.Save(Player, DBMS.MySQL); this.Save(Player, DBMS.Redis); break; } case DBMS.File: { File.WriteAllText(Directory.GetCurrentDirectory() + "\\Saves\\Players\\" + Player + ".json", JsonConvert.SerializeObject(Player, this.Settings)); break; } case DBMS.Mongo: { Mongo.Players.UpdateOneAsync(T => T.HighID == Player.HighID && T.LowID == Player.LowID, Builders <Core.Database.Models.Mongo.Players> .Update.Set(T => T.Player, BsonDocument.Parse(JsonConvert.SerializeObject(Player, this.Settings)))); break; } } }
/// <summary> /// Creates a clan with the specified identifier in the specified database. /// </summary> /// <param name="Clan">The clan.</param> /// <param name="DBMS">The DBMS.</param> /// <param name="Store">if set to <c>true</c> [store].</param> internal Clan New(Clan Clan, DBMS DBMS = Constants.Database, bool Store = true) { if (Clan.LowID == 0) { Clan.LowID = Interlocked.Increment(ref this.Seed); } switch (DBMS) { case DBMS.MySQL: { using (GBS_MySQL Database = new GBS_MySQL()) { Database.Clans.Add(new Core.Database.Models.Clans { HighID = Clan.HighID, LowID = Clan.LowID, Data = JsonConvert.SerializeObject(Clan, this.Settings) }); Database.SaveChangesAsync(); } if (Store) { this.Add(Clan); } break; } case DBMS.Redis: { Redis.Clans.StringSetAsync(Clan.ToString(), JsonConvert.SerializeObject(Clan, this.Settings), TimeSpan.FromMinutes(15)); if (Store) { this.Add(Clan); } break; } case DBMS.Both: { this.New(Clan, DBMS.Redis, Store); this.New(Clan, DBMS.MySQL, Store); break; } } return(Clan); }
/// <summary> /// Deletes the specified player in the specified database. /// </summary> /// <param name="HighID">The high identifier.</param> /// <param name="LowID">The low identifier.</param> /// <param name="DBMS">The DBMS.</param> internal void Delete(int HighID, int LowID, DBMS DBMS = Constants.Database) { switch (DBMS) { case DBMS.MySQL: { using (GBS_MySQL Database = new GBS_MySQL()) { Core.Database.Models.MySQL.Players Data = Database.Players.Find(HighID, LowID); if (Data != null) { Database.Players.Remove(Data); } else { Logging.Error(this.GetType(), "Data was null when deleting a player in the MySQL Database."); } Database.SaveChangesAsync(); } break; } case DBMS.Redis: { Redis.Players.KeyDeleteAsync(HighID + "-" + LowID); break; } case DBMS.Both: { this.Delete(HighID, LowID, DBMS.MySQL); this.Delete(HighID, LowID, DBMS.Redis); break; } case DBMS.File: { File.Delete(Directory.GetCurrentDirectory() + "\\Saves\\Players\\" + HighID + "-" + LowID + ".json"); break; } case DBMS.Mongo: { Mongo.Players.DeleteOneAsync(T => T.HighID == HighID && T.LowID == LowID); break; } } }
/// <summary> /// Saves the specified clan in the specified database. /// </summary> /// <param name="Clan">The clan.</param> /// <param name="DBMS">The DBMS.</param> internal void Save(Alliance Clan, DBMS DBMS = Constants.Database) { switch (DBMS) { case DBMS.MySQL: { using (GBS_MySQL Database = new GBS_MySQL()) { Core.Database.Models.MySQL.Clans Data = Database.Clans.Find(Clan.HighID, Clan.LowID); if (Data != null) { Data.Data = JsonConvert.SerializeObject(Clan, this.Settings); } Database.SaveChangesAsync(); } break; } case DBMS.Redis: { Redis.Clans.StringSetAsync(Clan.ToString(), JsonConvert.SerializeObject(Clan, this.Settings), TimeSpan.FromMinutes(15)); break; } case DBMS.Both: { this.Save(Clan, DBMS.MySQL); this.Save(Clan, DBMS.Redis); break; } case DBMS.Mongo: { Mongo.Clans.UpdateOneAsync(T => T.HighID == Clan.HighID && T.LowID == Clan.LowID, Builders <Core.Database.Models.Mongo.Clans> .Update.Set(T => T.Data, BsonDocument.Parse(JsonConvert.SerializeObject(Clan, this.Settings)))); break; } } }
/// <summary> /// Saves the specified clan in the specified database. /// </summary> /// <param name="Clan">The clan.</param> /// <param name="DBMS">The DBMS.</param> internal void Save(Clan Clan, DBMS DBMS = Constants.Database) { switch (DBMS) { case DBMS.MySQL: { using (GBS_MySQL Database = new GBS_MySQL()) { Core.Database.Models.Clans Data = Database.Clans.Find(Clan.HighID, Clan.LowID); if (Data != null) { Data.Data = JsonConvert.SerializeObject(Clan, this.Settings); } Database.SaveChangesAsync(); } break; } case DBMS.Redis: { Redis.Clans.StringSetAsync(Clan.ToString(), JsonConvert.SerializeObject(Clan, this.Settings), TimeSpan.FromMinutes(15)); break; } case DBMS.Both: { this.Save(Clan, DBMS.MySQL); this.Save(Clan, DBMS.Redis); break; } } }
/// <summary> /// Gets the clan using the specified identifier in the specified database. /// </summary> /// <param name="HighID">The high identifier.</param> /// <param name="LowID">The low identifier.</param> /// <param name="DBMS">The DBMS.</param> /// <param name="Store">if set to <c>true</c> [store].</param> internal Clan Get(int HighID, int LowID, DBMS DBMS = Constants.Database, bool Store = true) { if (!this.ContainsKey((long)HighID << 32 | (uint)LowID)) { Clan Clan = null; switch (DBMS) { case DBMS.MySQL: { using (GBS_MySQL Database = new GBS_MySQL()) { var Data = Database.Clans.Find(HighID, LowID); if (Data != null) { if (!string.IsNullOrEmpty(Data.Data)) { Clan = new Clan(HighID, LowID); JsonConvert.PopulateObject(Data.Data, Clan, this.Settings); if (Store) { this.Add(Clan); } } else { Logging.Error(this.GetType(), "The data returned wasn't null but empty, at Get(" + HighID + ", " + LowID + ", MySQL, " + Store + ")."); } } } break; } case DBMS.Redis: { string Data = Redis.Clans.StringGet(HighID + "-" + LowID).ToString(); if (!string.IsNullOrEmpty(Data)) { Clan = new Clan(HighID, LowID); JsonConvert.PopulateObject(Data, Clan, this.Settings); if (Store) { this.Add(Clan); } } break; } case DBMS.Both: { Clan = this.Get(HighID, LowID, DBMS.Redis, Store); if (Clan == null) { Clan = this.Get(HighID, LowID, DBMS.MySQL, Store); if (Clan != null) { this.Save(Clan, DBMS.Redis); } } break; } } return(Clan); } return(this[LowID]); }
/// <summary> /// Creates a clan with the specified identifier in the specified database. /// </summary> /// <param name="HighID">The high identifier.</param> /// <param name="LowID">The low identifier.</param> /// <param name="DBMS">The DBMS.</param> /// <param name="Store">if set to <c>true</c> [store].</param> internal Alliance New(int HighID = Constants.ServerID, int LowID = 0, DBMS DBMS = Constants.Database, bool Store = true) { Alliance Clan = new Alliance(HighID, Interlocked.Increment(ref this.Seed)); switch (DBMS) { case DBMS.MySQL: { using (GBS_MySQL Database = new GBS_MySQL()) { Database.Clans.Add(new Core.Database.Models.MySQL.Clans { HighID = Clan.HighID, LowID = Clan.LowID, Data = JsonConvert.SerializeObject(Clan, this.Settings) }); Database.SaveChangesAsync(); } if (Store) { this.Add(Clan); } break; } case DBMS.Redis: { Redis.Clans.StringSetAsync(Clan.ToString(), JsonConvert.SerializeObject(Clan, this.Settings), TimeSpan.FromMinutes(15)); if (Store) { this.Add(Clan); } break; } case DBMS.Both: { this.New(Clan.HighID, Clan.LowID, DBMS.MySQL, Store); this.New(Clan.HighID, Clan.LowID, DBMS.Redis, Store); break; } case DBMS.Mongo: { Mongo.Clans.InsertOneAsync(new Core.Database.Models.Mongo.Clans { HighID = Clan.HighID, LowID = Clan.LowID, Data = BsonDocument.Parse(JsonConvert.SerializeObject(Clan, this.Settings)) }); if (Store) { this.Add(Clan); } break; } } return(Clan); }
/// <summary> /// Gets the clan using the specified identifier in the specified database. /// </summary> /// <param name="HighID">The high identifier.</param> /// <param name="LowID">The low identifier.</param> /// <param name="DBMS">The DBMS.</param> /// <param name="Store">if set to <c>true</c> [store].</param> internal Alliance Get(int HighID, int LowID, DBMS DBMS = Constants.Database, bool Store = true) { long Id = (long)HighID << 32 | (uint)LowID; if (!this.TryGetValue(Id, out Alliance Clan)) { switch (DBMS) { case DBMS.MySQL: { using (GBS_MySQL Database = new GBS_MySQL()) { var Data = Database.Clans.Find(HighID, LowID); if (Data != null) { if (!string.IsNullOrEmpty(Data.Data)) { Clan = new Alliance(HighID, LowID); JsonConvert.PopulateObject(Data.Data, Clan, this.Settings); if (Store) { this.Add(Clan); } } else { Logging.Error(this.GetType(), "The data returned wasn't null but empty, at Get(" + HighID + ", " + LowID + ", MySQL, " + Store + ")."); } } } break; } case DBMS.Redis: { string Data = Redis.Clans.StringGet(HighID + "-" + LowID).ToString(); if (!string.IsNullOrEmpty(Data)) { Clan = new Alliance(HighID, LowID); JsonConvert.PopulateObject(Data, Clan, this.Settings); if (Store) { this.Add(Clan); } } break; } case DBMS.Both: { Clan = this.Get(HighID, LowID, DBMS.Redis, Store); if (Clan == null) { Clan = this.Get(HighID, LowID, DBMS.MySQL, Store); if (Clan != null) { this.Save(Clan, DBMS.Redis); } } break; } case DBMS.Mongo: { Core.Database.Models.Mongo.Clans Save = Mongo.Clans.Find(T => T.HighID == HighID && T.LowID == LowID).Limit(1).SingleOrDefault(); if (Save != null) { Clan = new Alliance(HighID, LowID); JsonConvert.PopulateObject(Save.Data.ToJson(), Clan, this.Settings); if (Store) { this.Add(Clan); } } break; } } } return(Clan); }
/// <summary> /// Creates a new player using the specified identifier in the specified database. /// </summary> /// <param name="Device">The device.</param> /// <param name="HighID">The high identifier.</param> /// <param name="LowID">The low identifier.</param> /// <param name="DBMS">The DBMS.</param> /// <param name="Store">if set to <c>true</c> [store].</param> internal Player New(Device Device, int HighID = Constants.ServerID, int LowID = 0, DBMS DBMS = Constants.Database, bool Store = true) { Player Player = new Player(Device, HighID, Interlocked.Increment(ref this.Seed)); for (int i = 0; i < 40; i++) { char Letter = (char)SResources.Random.Next('A', 'Z'); Player.Token = Player.Token + Letter; } switch (DBMS) { case DBMS.MySQL: { using (GBS_MySQL Database = new GBS_MySQL()) { Database.Players.Add(new Core.Database.Models.MySQL.Players { HighID = Player.HighID, LowID = Player.LowID, Data = JsonConvert.SerializeObject(Player, this.Settings) }); Database.SaveChangesAsync(); } if (Store) { this.Add(Player); } break; } case DBMS.Redis: { this.Save(Player, DBMS); if (Store) { this.Add(Player); } break; } case DBMS.Both: { this.Save(Player, DBMS); using (GBS_MySQL Database = new GBS_MySQL()) { Database.Players.Add(new Core.Database.Models.MySQL.Players { HighID = Player.HighID, LowID = Player.LowID, Data = JsonConvert.SerializeObject(Player, this.Settings) }); Database.SaveChangesAsync(); } if (Store) { this.Add(Player); } break; } case DBMS.File: { if (!File.Exists(Directory.GetCurrentDirectory() + "\\Saves\\Players\\" + Player + ".json")) { File.WriteAllText(Directory.GetCurrentDirectory() + "\\Saves\\Players\\" + Player + ".json", JsonConvert.SerializeObject(Player, this.Settings)); } break; } case DBMS.Mongo: { Mongo.Players.InsertOne(new Core.Database.Models.Mongo.Players { HighID = Player.HighID, LowID = Player.LowID, Player = BsonDocument.Parse(JsonConvert.SerializeObject(Player, this.Settings)) }); if (Store) { this.Add(Player); } break; } } return(Player); }
/// <summary> /// Gets the player using the specified identifier in the specified database. /// </summary> /// <param name="Device">The device.</param> /// <param name="HighID">The high identifier.</param> /// <param name="LowID">The low identifier.</param> /// <param name="DBMS">The DBMS.</param> /// <param name="Store">if set to <c>true</c> [store].</param> internal async Task <Player> Get(Device Device, int HighID, int LowID, DBMS DBMS = Constants.Database, bool Store = true) { long Id = (long)HighID << 32 | (uint)LowID; if (!this.TryGetValue(Id, out Player Player)) { switch (DBMS) { case DBMS.MySQL: { using (GBS_MySQL Database = new GBS_MySQL()) { var Data = await Database.Players.FindAsync(HighID, LowID); if (Data != null) { if (!string.IsNullOrEmpty(Data.Data)) { Player = JsonConvert.DeserializeObject <Player>(Data.Data, this.Settings); Player.Device = Device; Device.Player = Player; if (Store) { this.Add(Player); } } else { Logging.Error(this.GetType(), "The data returned wasn't null but empty, at Get(" + HighID + ", " + LowID + ", MySQL, " + Store + ")."); } } } break; } case DBMS.Redis: { string Data = await Redis.Players.StringGetAsync(HighID + "-" + LowID); if (!string.IsNullOrEmpty(Data)) { Player = JsonConvert.DeserializeObject <Player>(Data, this.Settings); Player.Device = Device; Device.Player = Player; if (Store) { this.Add(Player); } } break; } case DBMS.Both: { Player = await this.Get(Device, HighID, LowID, DBMS.Redis, Store); if (Player == null) { Player = await this.Get(Device, HighID, LowID, DBMS.MySQL, Store); if (Player != null) { this.Save(Player, DBMS.Redis); } } break; } case DBMS.File: { if (File.Exists(Directory.GetCurrentDirectory() + "\\Saves\\Players\\" + HighID + "-" + LowID + ".json")) { string JSON = File.ReadAllText(Directory.GetCurrentDirectory() + "\\Saves\\Players\\" + HighID + "-" + LowID + ".json"); if (!string.IsNullOrWhiteSpace(JSON)) { Player = JsonConvert.DeserializeObject <Player>(JSON, this.Settings); Player.Device = Device; Device.Player = Player; } else { Logging.Error(this.GetType(), "The data returned wasn't null but empty, at Get(" + HighID + ", " + LowID + ", File, " + Store + ")."); } } break; } case DBMS.Mongo: { Core.Database.Models.Mongo.Players Save = await Mongo.Players.Find(T => T.HighID == HighID && T.LowID == LowID).Limit(1).SingleOrDefaultAsync(); if (Save != null) { Player = JsonConvert.DeserializeObject <Player>(Save.Player.ToJson(), this.Settings); Player.Device = Device; Device.Player = Player; if (Store) { this.Add(Player); } } break; } } } return(Player); }
/// <summary> /// Gets the player using the specified identifier in the specified database. /// </summary> /// <param name="Device">The device.</param> /// <param name="HighID">The high identifier.</param> /// <param name="LowID">The low identifier.</param> /// <param name="DBMS">The DBMS.</param> /// <param name="Store">if set to <c>true</c> [store].</param> internal Player Get(Device Device, int HighID, int LowID, DBMS DBMS = Constants.Database, bool Store = true) { if (!this.ContainsKey((long)HighID << 32 | (uint)LowID)) { Player Player = null; switch (DBMS) { case DBMS.MySQL: { using (GBS_MySQL Database = new GBS_MySQL()) { var Data = Database.Players.Find(HighID, LowID); if (Data != null) { if (!string.IsNullOrEmpty(Data.Data)) { Player = JsonConvert.DeserializeObject <Player>(Data.Data, this.Settings); Player.Device = Device; Device.Player = Player; if (Store) { this.Add(Player); } } else { Logging.Error(this.GetType(), "The data returned wasn't null but empty, at Get(" + HighID + ", " + LowID + ", MySQL, " + Store + ")."); } } } break; } case DBMS.Redis: { string Data = Redis.Players.StringGet(HighID + "-" + LowID).ToString(); if (!string.IsNullOrEmpty(Data)) { Player = JsonConvert.DeserializeObject <Player>(Data, this.Settings); Player.Device = Device; Device.Player = Player; if (Store) { this.Add(Player); } } break; } case DBMS.Both: { Player = this.Get(Device, HighID, LowID, DBMS.Redis, Store); if (Player == null) { Player = this.Get(Device, HighID, LowID, DBMS.MySQL, Store); if (Player != null) { this.Save(Player, DBMS.Redis); } } break; } case DBMS.File: { if (File.Exists(Directory.GetCurrentDirectory() + "\\Saves\\Players\\" + HighID + "-" + LowID + ".json")) { string JSON = File.ReadAllText(Directory.GetCurrentDirectory() + "\\Saves\\Players\\" + HighID + "-" + LowID + ".json"); if (!string.IsNullOrWhiteSpace(JSON)) { Player = JsonConvert.DeserializeObject <Player>(JSON, this.Settings); Player.Device = Device; Device.Player = Player; } else { Logging.Error(this.GetType(), "The data returned wasn't null but empty, at Get(" + HighID + ", " + LowID + ", File, " + Store + ")."); } } break; } } return(Player); } return(this[LowID]); }