/// <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 (GCS_MySQL MySQL = new GCS_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.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 (GCS_MySQL Database = new GCS_MySQL()) { Core.Database.Models.Players 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); } 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; } } }
/// <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 (GCS_MySQL Database = new GCS_MySQL()) { Core.Database.Models.Clans Data = Database.Clans.Find(Clan.HighID, Clan.LowID); if (Data != null) { // lock (Clan.Members.Gate) { Data.Data = JsonConvert.SerializeObject(Clan, this.Settings); } } Database.SaveChangesAsync(); } break; } case DBMS.Redis: { // lock (Clan.Members.Gate) { Redis.Clans.StringSetAsync(Clan.ToString(), JsonConvert.SerializeObject(Clan, this.Settings), TimeSpan.FromMinutes(30)); } break; } case DBMS.Both: { this.Save(Clan, DBMS.MySQL); this.Save(Clan, DBMS.Redis); break; } } }
/// <summary> /// Gets the random <see cref="Player"/>. /// </summary> /// <param name="Offset">The offset.</param> /// <param name="Limit">The limit.</param> internal List <Player> GetRandom(int Limit = 50) { List <Player> Players = new List <Player>(Limit); DbSqlQuery <Core.Database.Models.Players> SqlPlayers; using (GCS_MySQL MySQL = new GCS_MySQL()) { SqlPlayers = MySQL.Players.SqlQuery("SELECT * FROM Players ORDER BY rand() LIMIT " + Limit); if (SqlPlayers != null) { if (SqlPlayers.Any()) { foreach (Core.Database.Models.Players Data in SqlPlayers) { if (!this.ContainsKey((long)Data.HighID << 32 | (long)(uint)Data.LowID)) { if (!string.IsNullOrEmpty(Data.Data)) { Player Player = new Player(); JsonConvert.PopulateObject(Data.Data, Player, this.Settings); if (Player.NameSetted) { Players.Add(Player); this.Add(Player); } } } } } } } return(Players); }
/// <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(LowID)) { Clan Clan = null; switch (DBMS) { case DBMS.MySQL: { using (GCS_MySQL Database = new GCS_MySQL()) { Core.Database.Models.Clans 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); } } } } 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="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) { lock (this.Gate) { Clan.LowID = this.Seed++; } } switch (DBMS) { case DBMS.MySQL: { using (GCS_MySQL Database = new GCS_MySQL()) { // lock (Clan.Members.Gate) { 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: { // lock (Clan.Members.Gate) { Redis.Clans.StringSetAsync(Clan.ToString(), JsonConvert.SerializeObject(Clan, this.Settings), TimeSpan.FromMinutes(30)); } 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> /// Creates a new player 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 Player New(int HighID = Constants.ServerID, int LowID = 0, DBMS DBMS = Constants.Database, bool Store = true) { Player Player = null; if (LowID == 0) { lock (this.Gate) { Player = new Player(null, HighID, this.Seed++); } } else { Player = new Player(null, HighID, LowID); } JsonConvert.PopulateObject(Files.Home.Starting_Home, Player.Objects, this.Settings); for (int i = 0; i < 20; i++) { char Letter = (char)Core.Resources.Random.Next('A', 'Z'); Player.Token = Player.Token + Letter; } switch (DBMS) { case DBMS.MySQL: { using (GCS_MySQL Database = new GCS_MySQL()) { Database.Players.Add(new Core.Database.Models.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 (GCS_MySQL Database = new GCS_MySQL()) { Database.Players.Add(new Core.Database.Models.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; } } return(Player); }
/// <summary> /// Gets the player 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 Player Get(int HighID, int LowID, DBMS DBMS = Constants.Database, bool Store = true) { if (!this.ContainsKey((long)HighID << 32 | (long)(uint)LowID)) { Player Player = null; switch (DBMS) { case DBMS.MySQL: { using (GCS_MySQL Database = new GCS_MySQL()) { Core.Database.Models.Players Data = Database.Players.Find(HighID, LowID); if (Data != null) { if (!string.IsNullOrEmpty(Data.Data)) { Player = JsonConvert.DeserializeObject <Player>(Data.Data, this.Settings); if (Store) { this.Add(Player); } } } } break; } case DBMS.Redis: { string Data = Redis.Players.StringGet(HighID + "-" + LowID).ToString(); Console.WriteLine(Data); if (!string.IsNullOrEmpty(Data)) { Player = JsonConvert.DeserializeObject <Player>(Data, this.Settings); if (Store) { this.Add(Player); } } break; } case DBMS.Both: { Player = this.Get(HighID, LowID, DBMS.Redis, Store); if (Player == null) { Player = this.Get(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); } } break; } } return(Player); } return(this[LowID]); }