using MySql.Data.MySqlClient; using MySqlX.XDevAPI.Relational; using Newtonsoft.Json; using PoEco.Net.DB; using PoEco.Net.Web; using System; using System.Collections.Generic; using System.Data.SqlClient; using System.Diagnostics.Metrics; using System.IO; using System.Linq; using System.Security.Cryptography; using System.Text; using System.Threading.Tasks; namespace PoEco.Net.JSON { internal class Stash { public static void GenStashHash(string user) { List itemsID = new List(); StringBuilder stashHash = new StringBuilder(); StashRoot stash = JsonConvert.DeserializeObject(File.ReadAllText($@"data/stash/{user}.json")); foreach (var i in stash.items) { itemsID.Add(i.id); } stashHash.Append(string.Join(":", itemsID)); string sb64 = Convert.ToBase64String(Encoding.ASCII.GetBytes(stashHash.ToString())); DB.User.SetUserStashHash(user, sb64); } public static string GetStashHash(string user) { List itemsID = new List(); StringBuilder stashHash = new StringBuilder(); StashRoot stash = JsonConvert.DeserializeObject(File.ReadAllText($@"data/stash/{user}.json")); foreach (var i in stash.items) { itemsID.Add(i.id); } stashHash.Append(string.Join(":", itemsID)); string sb64 = Convert.ToBase64String(Encoding.ASCII.GetBytes(stashHash.ToString())); return sb64; } public static int GetUserItemCount(string user, int tid) { StashRoot stash = JsonConvert.DeserializeObject(File.ReadAllText($@"data/stash/{user}.rare.{tid}.json")); return stash.items.Count; } public static int GetUserProfitCount(string user, int tid) { StashRoot stash = JsonConvert.DeserializeObject(File.ReadAllText($@"data/stash/{user}.profit.{tid}.json")); return stash.items.Count; } public static void UpdateUserTabRGB(string user, int tabindex) { StashRoot stash = JsonConvert.DeserializeObject(File.ReadAllText($@"data/stash/{user}.json")); MySqlConnection con = DB.Stash.MxPoEDB(); string cmdText = $"UPDATE poeco_user SET tabrgb = '{stash.tabs[tabindex].colour.r}:{stash.tabs[tabindex].colour.g}:{stash.tabs[tabindex].colour.b}' WHERE account = '{user}'"; MySqlCommand cmd = new MySqlCommand(cmdText, con); con.Open(); cmd.ExecuteNonQuery(); con.Close(); } public static void UpdateUserTabName(string user, int tabindex) { StashRoot stash = JsonConvert.DeserializeObject(File.ReadAllText($"data/stash/{user}.tab{tabindex}.json")); MySqlConnection con = DB.Stash.MxPoEDB(); string cmdText = $"UPDATE poeco_user SET tabname = '{stash.tabs[tabindex].n}' WHERE account = '{user}'"; MySqlCommand cmd = new MySqlCommand(cmdText, con); con.Open(); cmd.ExecuteNonQuery(); con.Close(); } public static void CleanDB(string user, int userid) { MySqlConnection con = DB.Stash.MxPoEDB(); string cmdText = $"SELECT * FROM poeco_items WHERE uid = '{userid}'"; MySqlCommand cmd = new MySqlCommand(cmdText, con); con.Open(); MySqlDataReader mdata = cmd.ExecuteReader(); while (mdata.Read()) { //if (!CheckItemExistsStash(user, mdata.GetString(6))) //{ // DB.Stash.DeleteSpecificItem(userid, mdata.GetString(6)); //} } } public static bool CheckItemExistsStash(string user, int tid, string uuid) { bool exists = false; List items = new List(); StashRoot stash = JsonConvert.DeserializeObject(File.ReadAllText($"data/stash/{user}.rare.{tid}.json")); foreach (var s in stash.items) { items.Add(s.id); } if (items.Contains(uuid)) { exists = true; } return exists; } public static void GenItemTable(string user) { List stringItemTxt = new List(); List stringItemMod = new List(); StashRoot stash = JsonConvert.DeserializeObject(File.ReadAllText($@"data/stash/{user}.json")); int curitem = 1; double chaosValue = 0; int guid = 0; //DB.Stash.DeleteItems(DB.User.GetUserID(user)); var tmpfiles = Directory.GetFiles("tmp"); StringBuilder sCommand = new StringBuilder("INSERT INTO poeco_items(uid, tabindex, tabname, tabrgb, cid, bid, uiid, imod, var, img, txt, b64, ctype, min, max) VALUES "); List Rows = new List(); int itemcount = stash.items.Count(); foreach ( var file in tmpfiles ) { File.Delete(file); } foreach (Item obj in stash.items) { if (!DB.Stash.CheckItemExistsDB(obj.id)) { itemcount--; } if (obj.typeLine != "" && obj.frameType == 2 && obj.identified && !DB.Stash.CheckItemExistsDB(obj.id)) { //Message.drawProgress(curitem, itemcount); int icid = DB.Stash.GetClassIDByBase(obj.typeLine); string itemclass = DB.Stash.GetClassByID(icid); stringItemTxt.Add($"Item Class: {char.ToUpper(itemclass[0]) + itemclass.Substring(1)}"); stringItemTxt.Add("Rarity: Rare"); stringItemTxt.Add(obj.name ?? ""); stringItemTxt.Add(obj.typeLine ?? ""); stringItemTxt.Add("--------"); if (obj.properties != null && obj.properties.Count() > 0) { foreach (Property property in obj.properties) { if (property.name == "Quality") stringItemTxt.Add(string.Format("Quality: {0}", property.values[0][0])); if (property.name == "Armour") stringItemTxt.Add(string.Format("Armour: {0}", property.values[0][0])); if (property.name == "Energy Shield") stringItemTxt.Add(string.Format("Energy Shield: {0}", property.values[0][0])); if (property.name == "Evasion Rating") stringItemTxt.Add(string.Format("Evasion Rating: {0}", property.values[0][0])); if (property.values.Count>() == 0) stringItemTxt.Add(property.name ?? ""); if (property.name == "Physical Damage") stringItemTxt.Add(string.Format("Physical Damage: {0}", property.values[0][0])); if (property.name == "Elemental Damage") stringItemTxt.Add(string.Format("Elemental Damage: {0}", property.values[0][0])); if (property.name == "Critical Strike Chance") stringItemTxt.Add(string.Format("Critical Strike Chance: {0}", property.values[0][0])); if (property.name == "Attacks per Second") stringItemTxt.Add(string.Format("Attacks per Second: {0}", property.values[0][0])); if (property.name == "Weapon Range") stringItemTxt.Add(string.Format("Weapon Range: {0}", property.values[0][0])); } stringItemTxt.Add("--------"); } stringItemTxt.Add("Requirements:"); if (obj.requirements != null) { foreach (Requirement requirement in obj.requirements) { if (requirement.name == "Level") stringItemTxt.Add(string.Format("Level: {0}", requirement.values[0][0])); if (requirement.name == "Str") stringItemTxt.Add(string.Format("Str: {0}", requirement.values[0][0])); if (requirement.name == "Dex") stringItemTxt.Add(string.Format("Dex: {0}", requirement.values[0][0])); if (requirement.name == "Int") stringItemTxt.Add(string.Format("Int: {0}", requirement.values[0][0])); } } stringItemTxt.Add("--------"); if (obj.sockets != null) { List stringList2 = new List(); List stringList3 = new List(); List stringList4 = new List(); List stringList5 = new List(); List stringList6 = new List(); List stringList7 = new List(); foreach (Socket socket in obj.sockets) { if (socket.group == 0) stringList2.Add(socket.sColour.ToString()); if (socket.group == 1) stringList3.Add(socket.sColour.ToString()); if (socket.group == 2) stringList4.Add(socket.sColour.ToString()); if (socket.group == 3) stringList5.Add(socket.sColour.ToString()); if (socket.group == 4) stringList6.Add(socket.sColour.ToString()); if (socket.group == 5) stringList7.Add(socket.sColour.ToString()); } string str1 = string.Join("-", (IEnumerable)stringList2); string str2 = string.Join("-", (IEnumerable)stringList3); string str3 = string.Join("-", (IEnumerable)stringList4); string str4 = string.Join("-", (IEnumerable)stringList5); string str5 = string.Join("-", (IEnumerable)stringList6); string str6 = string.Join("-", (IEnumerable)stringList7); stringItemTxt.Add("Sockets: " + str1 + " " + str2 + " " + str3 + " " + str4 + " " + str5 + " " + str6); } stringItemTxt.Add("--------"); stringItemTxt.Add(string.Format("Item Level: {0}", (object)obj.ilvl)); stringItemTxt.Add("--------"); if (obj.implicitMods != null) { foreach (string implicitMod in obj.implicitMods) { stringItemTxt.Add(implicitMod ?? ""); stringItemMod.Add(implicitMod + "/n"); } stringItemTxt.Add("--------"); stringItemMod.Add("--------/n"); } if (obj.explicitMods != null) { foreach (string explicitMod in obj.explicitMods) { stringItemTxt.Add(explicitMod ?? ""); stringItemMod.Add(explicitMod + "/n"); } } File.WriteAllLines("tmp/" + obj.id + ".txt", (IEnumerable)stringItemTxt); File.WriteAllLines("tmp/" + obj.id + ".mod.txt", (IEnumerable)stringItemMod); string ib64 = Convert.ToBase64String(File.ReadAllBytes("tmp/" + obj.id + ".txt")); Web.PoEPrices.GetItemJson(obj.id, ib64); int tabindex = DB.User.GetUserTabIndex(user); Rows.Add( $"(" + $"'{DB.User.GetUserID(user)}'," + $"'{stash.tabs[tabindex].i}'," + $"'{stash.tabs[tabindex].n}'," + $"'{stash.tabs[tabindex].colour.r}:{stash.tabs[tabindex].colour.g}:{stash.tabs[tabindex].colour.b}'," + $"'{icid}'," + $"'{DB.Stash.GetBaseIDByName(obj.typeLine)}'," + $"'{obj.id}'," + $"'{MySql.Data.MySqlClient.MySqlHelper.EscapeString(File.ReadAllText("tmp/" + obj.id + ".mod.txt"))}'," + $"'{DB.Stash.GetClassByID(icid)}:{MySql.Data.MySqlClient.MySqlHelper.EscapeString(obj.typeLine)}:{MySql.Data.MySqlClient.MySqlHelper.EscapeString(obj.name)}:{obj.ilvl}'," + $"'{obj.icon}'," + $"'{MySql.Data.MySqlClient.MySqlHelper.EscapeString(File.ReadAllText("tmp/" + obj.id + ".txt"))}'," + $"'{ib64}'," + $"'{Web.PoEPrices.GetCurType(obj.id)}'," + $"'{Web.PoEPrices.GetMin(obj.id)}'," + $"'{Web.PoEPrices.GetMax(obj.id)}'" + $")"); stringItemTxt.Clear(); stringItemMod.Clear(); curitem++; Utilities.Message.CMW(chaosValue.ToString(),true,1); } } sCommand.Append(string.Join(",", Rows)); //Message.CMW(sCommand.ToString(),true,3); sCommand.Append(";"); if (sCommand.ToString() != "INSERT INTO poeco_items(uid, tabindex, tabname, tabrgb, cid, bid, uiid, imod, var, img, txt, b64, ctype, min, max) VALUES ;") { //DB.Stash.AddItemTable(sCommand.ToString()); } } public class Colour { public int r { get; set; } public int g { get; set; } public int b { get; set; } } public class Tab { public string n { get; set; } public int i { get; set; } public string id { get; set; } public string type { get; set; } public bool selected { get; set; } public Colour colour { get; set; } public string srcL { get; set; } public string srcC { get; set; } public string srcR { get; set; } } public class Socket { public int group { get; set; } public string attr { get; set; } public string sColour { get; set; } } public class Property { public string name { get; set; } public List> values { get; set; } public int displayMode { get; set; } public int? type { get; set; } } public class Requirement { public string name { get; set; } public List> values { get; set; } public int displayMode { get; set; } public string suffix { get; set; } } public class AdditionalProperty { public string name { get; set; } public List> values { get; set; } public int displayMode { get; set; } public double progress { get; set; } public int type { get; set; } } public class NextLevelRequirement { public string name { get; set; } public List> values { get; set; } public int displayMode { get; set; } } public class SocketedItem { public bool verified { get; set; } public int w { get; set; } public int h { get; set; } public string icon { get; set; } public bool support { get; set; } public string id { get; set; } public string name { get; set; } public string typeLine { get; set; } public bool identified { get; set; } public int ilvl { get; set; } public List properties { get; set; } public List requirements { get; set; } public List additionalProperties { get; set; } public List nextLevelRequirements { get; set; } public string secDescrText { get; set; } public List explicitMods { get; set; } public string descrText { get; set; } public int frameType { get; set; } public int socket { get; set; } public string colour { get; set; } } public class Item { public bool verified { get; set; } public int w { get; set; } public int h { get; set; } public string icon { get; set; } public int stackSize { get; set; } public int maxStackSize { get; set; } public string league { get; set; } public string id { get; set; } public List sockets { get; set; } public string name { get; set; } public string typeLine { get; set; } public bool identified { get; set; } public int ilvl { get; set; } public bool corrupted { get; set; } public int frameType { get; set; } public int x { get; set; } public int y { get; set; } public string inventoryId { get; set; } public List socketedItems { get; set; } public List properties { get; set; } public List requirements { get; set; } public List implicitMods { get; set; } public List explicitMods { get; set; } public List craftedMods { get; set; } public bool? support { get; set; } public List additionalProperties { get; set; } public string secDescrText { get; set; } public string descrText { get; set; } public List flavourText { get; set; } public bool? fractured { get; set; } public List fracturedMods { get; set; } public List enchantMods { get; set; } public int? talismanTier { get; set; } } public class StashRoot { public int numTabs { get; set; } public List tabs { get; set; } public bool quadLayout { get; set; } public List items { get; set; } } } }