private void AdminShop(CommandArgs args) { if (args.Parameters.Count < 1) { args.Player.SendValidAdminShopUsage(); return; } switch (args.Parameters[0].ToLower()) { #region populatedb case "populatedb": if (args.Parameters.Count < 2) { args.Player.SendInfoMessage("You are about to populate the shop database with default values!"); args.Player.SendInfoMessage("If you have done this at least once already, it may not be wise to again."); args.Player.SendInfoMessage("To proceed, type \"/ssa populatedb -confirm\""); return; } if (args.Parameters[1].ToLower() == "-confirm") { for (int i = -48; i < 2743; i++) { if (i == 0) continue; int value = 1; int maxstock = 1; Item item = new Item(); if (i > 0) item.SetDefaults(i); else item.SetDefaults(TShock.Utils.GetItemById(i).name); if (item.value != 0) { value = item.value; } if (item.maxStack != 0) { maxstock = item.maxStack; } int stock = (int)Math.Floor((double)maxstock / 2); if (maxstock == 1) { maxstock = -1; } try { db.Query("INSERT INTO Inventory (ID, Price, Stock, MaxStock, RestockTime, RestockType)" + " VALUES (@0, @1, @2, @3, @4, @5)", i, value, stock, maxstock, -1, -1); } catch (Exception ex) { Log.Error(ex.ToString()); } } RestockItems.Clear(); Inventory.Clear(); ReadDb(); args.Player.SendSuccessMessage("Database successfully populated."); Log.Info("ServerShop Inventory successfully populated."); return; } args.Player.SendValidAdminShopUsage(); return; #endregion #region balance case "balance": if (args.Parameters.Count < 2) { args.Player.SendValidBalanceUsage(); return; } Dictionary<int, int> balancedStocks = new Dictionary<int, int>(); switch (args.Parameters[1].ToLower()) { case "-shortage": foreach (ShopItem item in Inventory) { if (item.maxStock == -1) continue; if (item.stock < Math.Floor((double)item.maxStock / 2)) { item.stock = (int)Math.Floor((double)item.maxStock / 2); balancedStocks.Add(item.id, item.stock); } } break; case "-default": foreach (ShopItem item in Inventory) { if (item.maxStock == -1) continue; if (item.stock != Math.Floor((double)item.maxStock / 2)) { item.stock = (int)Math.Floor((double)item.maxStock / 2); balancedStocks.Add(item.id, item.stock); } } break; case "-surplus": foreach (ShopItem item in Inventory) { if (item.maxStock == -1) continue; if (item.stock > Math.Floor((double)item.maxStock / 2)) { item.stock = (int)Math.Floor((double)item.maxStock / 2); balancedStocks.Add(item.id, item.stock); } } break; default: args.Player.SendValidBalanceUsage(); return; } foreach (var item in balancedStocks) db.Query("UPDATE Inventory SET Stock = @1 WHERE ID = @0", item.Key, item.Value); args.Player.SendSuccessMessage("Balanced {0} items in ServerShop Inventory.", balancedStocks.Count); return; #endregion #region modify case "modify": if (args.Parameters.Count < 4) { args.Player.SendValidModifyUsage(); return; } List<Item> matchedItems = new List<Item>(); ShopItem itm = new ShopItem(); string itemName; int number; if (!Int32.TryParse(args.Parameters.Last(), out number)) { args.Player.SendErrorMessage("Invalid number!"); return; } matchedItems = TShock.Utils.GetItemByIdOrName(string.Join(" ", args.Parameters.GetRange(1, args.Parameters.Count - 3))); if (matchedItems.Count == 0) { args.Player.SendErrorMessage("Invalid item!"); return; } else if (matchedItems.Count > 1) { TShock.Utils.SendMultipleMatchError(args.Player, matchedItems.Select(i => i.name)); return; } itm = Inventory.GetShopItem(matchedItems[0].netID); itemName = TShock.Utils.GetItemById(itm.id).name; switch (args.Parameters[2].ToLower()) { case "price": Money money = number; itm.price = number; db.Query("UPDATE Inventory SET Price = @1 WHERE ID = @0", itm.id, itm.price); args.Player.SendSuccessMessage("Changed {0}'s price to {1}.", itemName, money.ToLongString()); return; case "stock": itm.stock = number; db.Query("UPDATE Inventory SET Stock = @1 WHERE ID = @0", itm.id, itm.stock); args.Player.SendSuccessMessage("Changed {0}'s stock to {1}.", itemName, itm.stock); return; case "maxstock": itm.maxStock = number; db.Query("UPDATE Inventory SET MaxStock = @1 WHERE ID = @0", itm.id, itm.maxStock); args.Player.SendSuccessMessage("Changed {0}'s max stock to {1}.", itemName, itm.maxStock); return; case "restocktime": itm.restockTime = number; db.Query("UPDATE Inventory SET RestockTime = @1 WHERE ID = @0", itm.id, itm.restockTime); args.Player.SendSuccessMessage("Changed {0}'s restock time to {1}.", itemName, itm.restockTime); if (itm.restockTime < 1 && RestockItems.ContainsItem(itm.id)) { RestockItems.RemoveShopItem(itm.id); return; } if (!RestockItems.ContainsItem(itm.id)) { RestockItems.Add(new ShopItem(itm.id, itm.price, itm.stock, itm.maxStock, itm.restockTime, itm.restockType)); } else RestockItems.GetShopItem(itm.id).restockTime = itm.restockTime; return; case "restocktype": if (number != -1 || number != 0 || number != 1) { args.Player.SendErrorMessage("Restock type number must be one of the following: -1, 0, 1"); return; } itm.restockType = number; db.Query("UPDATE Inventory SET RestockType = @1 WHERE ID = @0", itm.id, itm.restockType); args.Player.SendSuccessMessage("Changed {0}'s restock type to {1}.", itemName, itm.restockType.RestockTypeToString()); return; default: args.Player.SendValidModifyUsage(); return; } #endregion #region reloaddb case "reloaddb": args.Player.SendInfoMessage("Reloading Inventory and Regions from database...."); RestockItems.Clear(); Inventory.Clear(); Regions.Clear(); ReadDb(); args.Player.SendSuccessMessage("Successfully reloaded Inventory and Regions from database!"); args.Player.SendSuccessMessage("Note, this resets restock timers."); return; #endregion #region region case "region": if (args.Parameters.Count < 3) { args.Player.SendValidRegionUsage(); return; } var rcmd = args.Parameters[1].ToLower(); var region = TShock.Regions.GetRegionByName(args.Parameters[2]).Name; if (region == null) { args.Player.SendErrorMessage("Invalid region!"); return; } if (rcmd == "add") { if (Regions.Contains(region)) { args.Player.SendErrorMessage("{0} is already a ServerShop region!", region); return; } Regions.Add(region); db.Query("INSERT INTO ShopRegions (Region)" + " VALUES (@0)", region); args.Player.SendSuccessMessage("Successfully made {0} a ServerShop region!", region); } else if (rcmd == "del") { if (!Regions.Contains(region)) { args.Player.SendErrorMessage("{0} is not a ServerShop region!", region); return; } Regions.Remove(region); db.Query("DELETE FROM ShopRegions WHERE Region = @0", region); args.Player.SendSuccessMessage("{0} is no longer a ServerShop region!", region); } else { args.Player.SendValidRegionUsage(); return; } return; #endregion default: args.Player.SendValidAdminShopUsage(); return; } }
private void Shop(CommandArgs args) { #region PreSubCmd var account = SEconomyPlugin.Instance.GetBankAccount(args.Player.UserAccountName); Item itm = new Item(); ShopItem item = new ShopItem(); string itemName; List<Item> matchedItems = new List<Item>(); int amount = 1; Money price; Money sellprice; if (args.Parameters.Count < 2) { args.Player.SendValidShopUsage(); return; } if (args.Parameters.Count > 2) matchedItems = TShock.Utils.GetItemByIdOrName(string.Join(" ", args.Parameters.GetRange(1, args.Parameters.Count - 2))); else matchedItems = TShock.Utils.GetItemByIdOrName(args.Parameters[1].ToLower()); if (matchedItems.Count == 0) { args.Player.SendErrorMessage("Invalid item!"); return; } else if (matchedItems.Count > 1) { TShock.Utils.SendMultipleMatchError(args.Player, matchedItems.Select(i => i.name)); return; } item = Inventory.GetShopItem(matchedItems[0].netID); itemName = TShock.Utils.GetItemById(item.id).name; if (args.Parameters.Count > 2) { if (args.Parameters.Last().ToLower() == "all") { amount = item.stock; } else if (!Int32.TryParse(args.Parameters.Last(), out amount)) { args.Player.SendErrorMessage("Invalid amount!"); return; } } #endregion string subcmd = args.Parameters[0].ToLower(); switch (subcmd) { #region buy case "buy": int times; if (!args.Player.IsInShopRegion(Regions)) return; if (!args.Player.InventorySlotAvailable) { args.Player.SendErrorMessage("Insufficient inventory space!"); return; } if (amount > item.stock) { args.Player.SendErrorMessage("There are only {0} {1}(s) left in stock!", item.stock, itemName); return; } price = (item.price * amount < 1) ? 1 * amount : item.price * amount; if (args.Player.Group.HasPermission("ss.discount")) //Checks if the group has a discount perm { foreach (string permission in args.Player.Group.permissions) { Match Match = Regex.Match(permission, @"ss\.discount\.(\d+)"); //Looks for actual discount in ss.discount.xx if (Match.Success && Match.Value == permission) { int discount = Convert.ToInt32(Match.Groups[1].Value); double percent = (100 - (double)discount) / 100; price = (int)(price * percent); break; } } } if (price > account.Balance) { Money shortage = price - account.Balance; args.Player.SendErrorMessage("You are short {0} {1} from buying {2} {3}(s)!", shortage, Money.CurrencyName, amount, itemName); return; } account.TransferTo(SEconomyPlugin.Instance.WorldAccount, price, Wolfje.Plugins.SEconomy.Journal.BankAccountTransferOptions.AnnounceToSender, String.Format("buying {0} {1}(s) from the ServerShop.", amount, itemName), String.Format("ServerShop Purchase: ({0}){1}", amount, itemName) ); Inventory.GetShopItem(item.id).stock -= amount; db.Query("UPDATE Inventory SET Stock = @0 WHERE ID = @1", Inventory.GetShopItem(item.id).stock, item.id); itm.SetDefaults(item.id); times = (int)Math.Ceiling((double)amount / itm.maxStack); if (amount > itm.maxStack) { for (int i = 0; i < times; i++) { args.Player.GiveItem(itm.netID, itm.name, itm.width, itm.height, itm.maxStack); } } args.Player.GiveItem(itm.netID, itm.name, itm.width, itm.height, amount - (itm.maxStack * (times - 1))); return; #endregion #region sell case "sell": if (!args.Player.IsInShopRegion(Regions)) return; var inventory = args.TPlayer.inventory; List<int> itemLocations = new List<int>(); int amt = 0; for (int i = 50; i >= 0; i--) { if (inventory[i].netID == item.id) { amt += inventory[i].stack; itemLocations.Add(i); } } if (args.Parameters[args.Parameters.Count - 1].ToLower() == "all") { amount = 0; foreach (var i in itemLocations) { amount += inventory[i].stack; } } if (amt < amount) { args.Player.SendErrorMessage("You do not own {0} {1}(s)!", amount, itemName); return; } if (item.maxStock != -1 && item.stock + amount > item.maxStock) { args.Player.SendErrorMessage("The maximum you can sell is {0} {1}(s).", item.maxStock - item.stock, itemName); return; } if (item.price <= 5) { price = item.price * amount; } else price = item.price * amount / 5; SEconomyPlugin.Instance.WorldAccount.TransferTo(account, price, Wolfje.Plugins.SEconomy.Journal.BankAccountTransferOptions.AnnounceToReceiver, String.Format("selling {0} {1}(s) from the ServerShop.", amount, itemName), String.Format("ServerShop Purchase: ({0}){1}", amount, itemName) ); Inventory.GetShopItem(item.id).stock += amount; db.Query("UPDATE Inventory SET Stock = @0 WHERE ID = @1", Inventory.GetShopItem(item.id).stock, item.id); foreach (var loc in itemLocations) { if (amount == 0) break; if (inventory[loc].stack < amount) { amount -= inventory[loc].stack; inventory[loc].stack = 0; } else { inventory[loc].stack -= amount; amount = 0; } NetMessage.SendData((int)PacketTypes.PlayerSlot, -1, -1, "", args.Player.Index, loc); } return; #endregion #region search case "search": price = item.price; sellprice = price / 5; if (sellprice == 0) { sellprice = 1; } args.Player.SendInfoMessage("Item: {0}; Buy Price: {1}; Sell Price: {2}; Stock: {3}; MaxStock: {4}; Restock Time: {5}", itemName, price.ToLongString(), sellprice.ToLongString(), item.stock, item.maxStock == -1 ? "Infinity" : item.maxStock.ToString(), item.restockTime == -1 ? "None" : item.restockTime.ToString() ); return; #endregion default: args.Player.SendValidShopUsage(); return; } }