public static bool addChest(InfChest _chest) { string query = $"SELECT * FROM InfChests WHERE X = {_chest.x} AND Y = {_chest.y} AND WorldID = {Main.worldID}"; using (var reader = db.QueryReader(query)) { if (reader.Read()) { return(false); } } query = $"INSERT INTO InfChests (UserID, X, Y, Name, Public, Refill, Users, Groups, WorldID) VALUES ({_chest.userid}, {_chest.x}, {_chest.y}, '', {0}, {-1}, '', '', {Main.worldID})"; int result = db.Query(query); query = $"SELECT MAX(ID) AS lastchestid FROM InfChests"; int lastid = -1; using (var reader = db.QueryReader(query)) { if (reader.Read()) { try { lastid = reader.Get <int>("lastchestid"); } catch { TSPlayer.Server.SendErrorMessage("Error getting last inserted row id."); return(false); } } } //Reduce 40 transactions to 1. Massive speed increase on initial chest conversion. query = "INSERT INTO ChestItems (Slot, Type, Stack, Prefix, WorldID, ChestID) VALUES"; for (int i = 0; i < _chest.items.Length; i++) { query += $" ({i}, {_chest.items[i].type}, {_chest.items[i].stack}, {_chest.items[i].prefix}, {Main.worldID}, {lastid}),"; } query = query.TrimEnd(','); result = db.Query(query); return(result == 40); //for (int i = 0; i < _chest.items.Length; i++) //{ // query = $"INSERT INTO ChestItems (Slot, Type, Stack, Prefix, WorldID, ChestID) VALUES ({i}, {_chest.items[i].type}, {_chest.items[i].stack}, {_chest.items[i].prefix}, {Main.worldID}, {lastid})"; // result += db.Query(query); //} // //if (result == 41) // return true; //else // return false; }
public static void restoreChests() { List <InfChest> chestList = new List <InfChest>(); InfChests.lockChests = true; string query = $"SELECT * FROM InfChests WHERE WorldID = {Main.worldID}"; using (var reader = db.QueryReader(query)) { while (reader.Read()) { InfChest chest = new InfChest(); chest.id = reader.Get <int>("ID"); chest.x = reader.Get <int>("X"); chest.y = reader.Get <int>("Y"); chest.items = new Item[40]; chestList.Add(chest); } } foreach (InfChest chest in chestList) { query = $"SELECT * FROM ChestItems WHERE ChestID = {chest.id}"; using (var reader = db.QueryReader(query)) { while (reader.Read()) { int slot = reader.Get <int>("Slot"); int type = reader.Get <int>("Type"); int stack = reader.Get <int>("Stack"); int prefix = reader.Get <int>("Prefix"); Item item = new Item(); item.SetDefaults(type); item.stack = stack; item.prefix = (byte)prefix; chest.items[slot] = item; } } } for (int i = 0; i < chestList.Count; i++) { Main.chest[i] = new Chest(); Main.chest[i].x = chestList[i].x; Main.chest[i].y = chestList[i].y; Main.chest[i].item = chestList[i].items; } query = $"DELETE FROM InfChests WHERE WorldID = {Main.worldID}"; db.Query(query); query = $"DELETE FROM ChestItems WHERE WorldID = {Main.worldID}"; db.Query(query); TShock.Utils.SaveWorld(); InfChests.lockChests = false; InfChests.notInfChests = true; }
public static InfChest getChest(string name) { string query = $"SELECT * FROM InfChests WHERE Name = '{name}'"; InfChest chest = new InfChest(); using (var reader = db.QueryReader(query)) { if (reader.Read()) { string _users = reader.Get <string>("Users"); string _groups = reader.Get <string>("Groups"); chest.id = reader.Get <int>("ID"); chest.userid = reader.Get <int>("UserID"); chest.x = reader.Get <int>("X"); chest.y = reader.Get <int>("Y"); chest.isPublic = reader.Get <int>("Public") == 1 ? true : false; chest.refillTime = reader.Get <int>("Refill"); chest.users = string.IsNullOrEmpty(_users) ? new List <int>() : _users.Split(',').ToList().ConvertAll <int>(p => int.Parse(p)); chest.groups = string.IsNullOrEmpty(_groups) ? new List <string>() : _groups.Split(',').ToList(); chest.items = new Item[40]; } else { return(null); } } query = $"SELECT * FROM ChestItems WHERE ChestID = {chest.id}"; using (var reader = db.QueryReader(query)) { while (reader.Read()) { int slot = reader.Get <int>("Slot"); int type = reader.Get <int>("Type"); int stack = reader.Get <int>("Stack"); int prefix = reader.Get <int>("Prefix"); Item item = new Item(); item.SetDefaults(type); item.stack = stack; item.prefix = (byte)prefix; chest.items[slot] = item; } } return(chest); }
public static bool setAll(int id, InfChest chest) { int ispublic = chest.isPublic ? 1 : 0; int result = 0; string query = $"UPDATE InfChests SET UserID = {chest.userid}, Public = {ispublic}, Users = '{string.Join(",", chest.users.Select(p => p.ToString()))}', Groups = '{string.Join(",", chest.groups)}', Refill = {chest.refillTime} WHERE ID = {id}"; result += db.Query(query); for (int i = 0; i < chest.items.Length; i++) { query = $"UPDATE ChestItems SET Type = {chest.items[i].type}, Stack = {chest.items[i].stack}, Prefix = {chest.items[i].prefix} WHERE ChestID = {id} AND Slot = {i}"; result += db.Query(query); } if (result == 41) { return(true); } else { return(false); } }