using System; using System.Data; using System.Linq; using System.Data.SqlClient; using Filter.Networking.Instances; using Filter.Networking.Zone; using Filter.Utilities; namespace Filter.Handlers.Zone { internal class Zone12Handle { [ZonePacketHandler(Zone12TypeClient.BuyItem)] public static void HandleBuyItem(ZoneClient ClientSocket, Packet ClientPacket) { if (ClientSocket.IsConnected != 0) { ClientPacket.Dispose(); return; } else if (Program.Config.CCEnabled && ClientSocket.InNPC) { UInt16 ItemID; Byte Amount; if (!ClientPacket.ReadUInt16(out ItemID)) { } else if (!ClientPacket.ReadByte(out Amount)) { } else { using (var Connection = new SqlConnection()) { Connection.ConnectionString = Program.ConnectionBuilder.ConnectionString; try { Connection.Open(); } catch { } if (Connection.State == ConnectionState.Open) { using (var Command = Connection.CreateCommand()) { Item CurrencyItem; if ((CurrencyItem = Program.CustomCurrency.BuyItems.Where(FindItemID => FindItemID.ItemID == ItemID).FirstOrDefault()) != null) { Currency CurrencyType = Program.CustomCurrency.Currencys[CurrencyItem.CurrencyID]; Int32 CharacterID = 0; Int64 CurrencyAmount = 0; if (CurrencyType.Type == 1) { //Character Command.CommandText = String.Format("SELECT TOP 1 nCharNo FROM {0}..tCharacter WHERE sID = '{1}'", Program.Config.SQLCharacter, ClientSocket.Manager.CharacterName); CharacterID = Convert.ToInt32(Command.ExecuteScalar()); Command.CommandText = String.Format("SELECT TOP 1 Amount FROM {0}..{1} WHERE nCharNo = '{2}'", Program.Config.SQLCustomCurrency, CurrencyType.Table, CharacterID); CurrencyAmount = Convert.ToInt64(Command.ExecuteScalar()); } else if (CurrencyType.Type == 2) { //Account Command.CommandText = String.Format("SELECT TOP 1 Amount FROM {0}..{1} WHERE nUserNo = '{2}'", Program.Config.SQLCustomCurrency, CurrencyType.Table, ClientSocket.Manager.AccountID); CurrencyAmount = Convert.ToInt64(Command.ExecuteScalar()); } if (CurrencyAmount == -1) { Echo.SendWhisper(ClientSocket.Manager, "You cannot buy this because you do not have anything of this currency ({0}).", CurrencyType.Name); } else if (CurrencyAmount < CurrencyItem.CurrencyWorth * Amount) { Echo.SendWhisper(ClientSocket.Manager, "You do not have enough of {0} to buy {1}. Have {2}. Need: {3}.", CurrencyType.Name, CurrencyItem.ItemName, CurrencyAmount, (CurrencyItem.CurrencyWorth * Amount) - CurrencyAmount); } else { if (CurrencyType.Type == 1) { Command.CommandText = String.Format("UPDATE {0}..{1} SET Amount = (Amount - {2}) WHERE nCharNo = '{3}'", Program.Config.SQLCustomCurrency, CurrencyType.Table, CurrencyItem.CurrencyWorth * Amount, CharacterID); } else { Command.CommandText = String.Format("UPDATE {0}..{1} SET Amount = (Amount - {2}) WHERE nUserNo = '{3}'", Program.Config.SQLCustomCurrency, CurrencyType.Table, CurrencyItem.CurrencyWorth * Amount, ClientSocket.Manager.AccountID); } Command.ExecuteNonQuery(); ClientSocket.Server.SendPacket(ClientPacket); CurrencyAmount = CurrencyAmount - (CurrencyItem.CurrencyWorth * Amount); Echo.SendWhisper(ClientSocket.Manager, "You bought {0} for {1} {2} you are left with {3}.", CurrencyItem.ItemName, CurrencyItem.CurrencyWorth * Amount, CurrencyType.Name, CurrencyAmount); } } else { ClientSocket.Server.SendPacket(ClientPacket); } } } Connection.Close(); } } } else { ClientSocket.Server.SendPacket(ClientPacket); } ClientPacket.Dispose(); } [ZonePacketHandler(Zone12TypeClient.SellItem)] public static void HandleSellItem(ZoneClient ClientSocket, Packet ClientPacket) { if (ClientSocket.IsConnected != 0) { ClientPacket.Dispose(); return; } else if (Program.Config.CCEnabled && ClientSocket.InNPC) { Byte SlotID; Byte Amount; if (!ClientPacket.ReadByte(out SlotID)) { } else if (!ClientPacket.ReadByte(out Amount)) { } else { using (var Connection = new SqlConnection()) { Connection.ConnectionString = Program.ConnectionBuilder.ConnectionString; try { Connection.Open(); } catch { } if (Connection.State == ConnectionState.Open) { using (var Command = Connection.CreateCommand()) { Int32 CharacterID = 0; Int32 ItemID = 0; Command.CommandText = String.Format("SELECT TOP 1 nCharNo FROM {0}..tCharacter WHERE sID = '{1}'", Program.Config.SQLCharacter, ClientSocket.Manager.CharacterName); CharacterID = Convert.ToInt32(Command.ExecuteScalar()); Command.CommandText = String.Format("SELECT TOP 1 nItemID FROM {0}..tItem WHERE nStorageType = '9' AND nOwner = '{1}' AND nStorage = '{2}'", Program.Config.SQLCharacter, CharacterID, SlotID); ItemID = Convert.ToInt32(Command.ExecuteScalar()); Item CurrencyItem; if ((CurrencyItem = Program.CustomCurrency.SellItems.Where(FindItemID => FindItemID.ItemID == ItemID).FirstOrDefault()) != null) { Currency CurrencyType = Program.CustomCurrency.Currencys[CurrencyItem.CurrencyID]; if (CurrencyType.Type == 1) { Command.CommandText = String.Format("SELECT TOP 1 Amount FROM {0}..{1} WHERE nCharNo = '{2}'", Program.Config.SQLCustomCurrency, CurrencyType.Table, CharacterID); } else if (CurrencyType.Type == 2) { Command.CommandText = String.Format("SELECT TOP 1 Amount FROM {0}..{1} WHERE nUserNo = '{2}'", Program.Config.SQLCustomCurrency, CurrencyType.Table, ClientSocket.Manager.AccountID); } using (var Reader = Command.ExecuteReader()) { Reader.Read(); if (Reader.HasRows) { Reader.Close(); if (CurrencyType.Type == 1) { Command.CommandText = String.Format("UPDATE {0}..{1} SET Amount = (Amount + {2}) WHERE nCharNo = '{3}'", Program.Config.SQLCustomCurrency, CurrencyType.Table, CurrencyItem.CurrencyWorth * Amount, CharacterID); } else if (CurrencyType.Type == 2) { Command.CommandText = String.Format("UPDATE {0}..{1} SET Amount = (Amount + {2}) WHERE nUserNo = '{3}'", Program.Config.SQLCustomCurrency, CurrencyType.Table, CurrencyItem.CurrencyWorth * Amount, ClientSocket.Manager.AccountID); } Command.ExecuteNonQuery(); } else { Reader.Close(); if (CurrencyType.Type == 1) { Command.CommandText = String.Format("INSERT INTO {0}..{1} (nCharNo, Amount) VALUES ('{2}', '{3}')", Program.Config.SQLCustomCurrency, CurrencyType.Table, CharacterID, CurrencyItem.CurrencyWorth * Amount); } else if (CurrencyType.Type == 2) { Command.CommandText = String.Format("INSERT INTO {0}..{1} (nUserNo, Amount) VALUES ('{2}', '{3}')", Program.Config.SQLCustomCurrency, CurrencyType.Table, ClientSocket.Manager.AccountID, CurrencyItem.CurrencyWorth * Amount); } Command.ExecuteNonQuery(); } if (CurrencyType.Type == 1) { Command.CommandText = String.Format("SELECT TOP 1 Amount FROM {0}..{1} WHERE nCharNo = '{2}'", Program.Config.SQLCustomCurrency, CurrencyType.Table, CharacterID); } else if (CurrencyType.Type == 2) { Command.CommandText = String.Format("SELECT TOP 1 Amount FROM {0}..{1} WHERE nUserNo = '{2}'", Program.Config.SQLCustomCurrency, CurrencyType.Table, ClientSocket.Manager.AccountID); } Int64 CurrencyAmount = Convert.ToInt64(Command.ExecuteScalar()); Echo.SendWhisper(ClientSocket.Manager, "You have gained {0} {1}! New Balance: {2}.", CurrencyItem.CurrencyWorth * Amount, CurrencyType.Name, CurrencyAmount); ClientSocket.Server.SendPacket(ClientPacket); } } else { ClientSocket.Server.SendPacket(ClientPacket); } } } else { ClientSocket.Server.SendPacket(ClientPacket); } Connection.Close(); } } } else { ClientSocket.Server.SendPacket(ClientPacket); } ClientPacket.Dispose(); } } }