public static BusinessItemModel GetBusinessItemFromName(String itemName) { BusinessItemModel item = null; foreach (BusinessItemModel businessItem in Constants.BUSINESS_ITEM_LIST) { if (businessItem.description == itemName) { item = businessItem; break; } } return(item); }
public static BusinessItemModel GetBusinessItemFromHash(String itemHash) { BusinessItemModel item = null; foreach (BusinessItemModel businessItem in Constants.BUSINESS_ITEM_LIST) { if (businessItem.hash == itemHash) { item = businessItem; break; } } return(item); }
public void BusinessPurchaseMadeEvent(Client player, String itemName, int amount) { int businessId = NAPI.Data.GetEntityData(player, EntityData.PLAYER_BUSINESS_ENTERED); BusinessModel business = GetBusinessById(businessId); BusinessItemModel businessItem = GetBusinessItemFromName(itemName); if (business.type == Constants.BUSINESS_TYPE_AMMUNATION && businessItem.type == Constants.ITEM_TYPE_WEAPON && NAPI.Data.GetEntityData(player, EntityData.PLAYER_WEAPON_LICENSE) < Globals.GetTotalSeconds()) { NAPI.Chat.SendChatMessageToPlayer(player, Constants.COLOR_ERROR + Messages.ERR_WEAPON_LICENSE_EXPIRED); } else { int hash = 0; int price = (int)Math.Round(businessItem.products * business.multiplier) * amount; int money = NAPI.Data.GetEntitySharedData(player, EntityData.PLAYER_MONEY); if (money < price) { NAPI.Chat.SendChatMessageToPlayer(player, Constants.COLOR_ERROR + Messages.ERR_PLAYER_NOT_ENOUGH_MONEY); } else { String purchaseMessage = String.Format(Messages.INF_BUSINESS_ITEM_PURCHASED, price); int playerId = NAPI.Data.GetEntityData(player, EntityData.PLAYER_SQL_ID); // We look for the item in the inventory ItemModel itemModel = Globals.GetPlayerItemModelFromHash(playerId, businessItem.hash); if (itemModel == null) { // We create the purchased item itemModel = new ItemModel(); itemModel.hash = businessItem.hash; if (businessItem.type == Constants.ITEM_TYPE_WEAPON) { itemModel.ownerEntity = Constants.ITEM_ENTITY_WHEEL; } else { itemModel.ownerEntity = Int32.TryParse(itemModel.hash, out hash) ? Constants.ITEM_ENTITY_RIGHT_HAND : Constants.ITEM_ENTITY_PLAYER; } itemModel.ownerIdentifier = NAPI.Data.GetEntityData(player, EntityData.PLAYER_SQL_ID); itemModel.amount = businessItem.uses * amount; itemModel.position = new Vector3(0.0f, 0.0f, 0.0f); itemModel.dimension = 0; Task.Factory.StartNew(() => { // Adding the item to the list and database itemModel.id = Database.AddNewItem(itemModel); Globals.itemList.Add(itemModel); }); } else { if (Int32.TryParse(itemModel.hash, out hash) == true) { itemModel.ownerEntity = Constants.ITEM_ENTITY_RIGHT_HAND; } itemModel.amount += (businessItem.uses * amount); Task.Factory.StartNew(() => { // Update the item's amount Database.UpdateItem(itemModel); }); } // If the item has a valid hash, we give it in hand if (itemModel.ownerEntity == Constants.ITEM_ENTITY_RIGHT_HAND) { itemModel.objectHandle = NAPI.Object.CreateObject(UInt32.Parse(itemModel.hash), itemModel.position, new Vector3(0.0f, 0.0f, 0.0f), (byte)player.Dimension); NAPI.Entity.AttachEntityToEntity(itemModel.objectHandle, player, "PH_R_Hand", businessItem.position, businessItem.rotation); } else if (businessItem.type == Constants.ITEM_TYPE_WEAPON) { // We give the weapon to the player WeaponHash weaponHash = NAPI.Util.WeaponNameToModel(itemModel.hash); NAPI.Player.GivePlayerWeapon(player, weaponHash, itemModel.amount); // Checking if it's been bought in the Ammu-Nation if (business.type == Constants.BUSINESS_TYPE_AMMUNATION) { Task.Factory.StartNew(() => { // Add a registered weapon Database.AddLicensedWeapon(itemModel.id, player.Name); }); } } // We set the item into the hand variable NAPI.Data.SetEntityData(player, EntityData.PLAYER_RIGHT_HAND, itemModel.id); // If it's a phone, we create a new number if (itemModel.hash == Constants.ITEM_HASH_TELEPHONE) { if (NAPI.Data.GetEntityData(player, EntityData.PLAYER_PHONE) == 0) { Random random = new Random(); int phone = random.Next(100000, 999999); NAPI.Data.SetEntityData(player, EntityData.PLAYER_PHONE, phone); // Sending the message with the new number to the player String message = String.Format(Messages.INF_PLAYER_PHONE, phone); NAPI.Chat.SendChatMessageToPlayer(player, Constants.COLOR_INFO + message); } } // We substract the product and add funds to the business if (business.owner != String.Empty) { business.funds += price; business.products -= businessItem.products; Task.Factory.StartNew(() => { // Update the business Database.UpdateBusiness(business); }); } NAPI.Data.SetEntitySharedData(player, EntityData.PLAYER_MONEY, money - price); NAPI.Chat.SendChatMessageToPlayer(player, Constants.COLOR_INFO + purchaseMessage); } } }
public void BusinessPurchaseMadeEvent(Client player, string itemName, int amount) { int businessId = player.GetData(EntityData.PLAYER_BUSINESS_ENTERED); BusinessModel business = GetBusinessById(businessId); BusinessItemModel businessItem = GetBusinessItemFromName(itemName); if (business.type == Constants.BUSINESS_TYPE_AMMUNATION && businessItem.type == Constants.ITEM_TYPE_WEAPON && player.GetData(EntityData.PLAYER_WEAPON_LICENSE) < Globals.GetTotalSeconds()) { player.SendChatMessage(Constants.COLOR_ERROR + ErrRes.weapon_license_expired); } else { int hash = 0; int price = (int)Math.Round(businessItem.products * business.multiplier) * amount; int money = player.GetSharedData(EntityData.PLAYER_MONEY); if (money < price) { player.SendChatMessage(Constants.COLOR_ERROR + ErrRes.player_not_enough_money); } else { string purchaseMessage = string.Format(InfoRes.business_item_purchased, price); int playerId = player.GetData(EntityData.PLAYER_SQL_ID); Task.Factory.StartNew(() => { NAPI.Task.Run(() => { // We look for the item in the inventory ItemModel itemModel = Globals.GetPlayerItemModelFromHash(playerId, businessItem.hash); if (itemModel == null || businessItem.hash == Constants.ITEM_HASH_TELEPHONE) { // We create the purchased item itemModel = new ItemModel(); { itemModel.hash = businessItem.hash; itemModel.ownerIdentifier = player.GetData(EntityData.PLAYER_SQL_ID); itemModel.amount = businessItem.uses * amount; itemModel.position = new Vector3(0.0f, 0.0f, 0.0f); itemModel.dimension = 0; } if (businessItem.type == Constants.ITEM_TYPE_WEAPON) { itemModel.ownerEntity = Constants.ITEM_ENTITY_WHEEL; } else { itemModel.ownerEntity = int.TryParse(itemModel.hash, out hash) ? Constants.ITEM_ENTITY_RIGHT_HAND : Constants.ITEM_ENTITY_PLAYER; } // Adding the item to the list and database itemModel.id = Database.AddNewItem(itemModel); Globals.itemList.Add(itemModel); } else { itemModel.amount += businessItem.uses * amount; if (int.TryParse(itemModel.hash, out hash) == true) { itemModel.ownerEntity = Constants.ITEM_ENTITY_RIGHT_HAND; } // Update the item's amount Database.UpdateItem(itemModel); } // If the item has a valid hash, we give it in hand if (itemModel.ownerEntity == Constants.ITEM_ENTITY_RIGHT_HAND) { // Remove the previous item if there was any if (player.GetSharedData(EntityData.PLAYER_RIGHT_HAND) != null) { Globals.RemoveItemOnHands(player); } // Give the new item to the player Globals.AttachItemToPlayer(player, itemModel.id, itemModel.hash, "IK_R_Hand", businessItem.position, businessItem.rotation, EntityData.PLAYER_RIGHT_HAND); } else if (businessItem.type == Constants.ITEM_TYPE_WEAPON) { // Remove the previous item if there was any if (player.GetSharedData(EntityData.PLAYER_RIGHT_HAND) != null) { Globals.RemoveItemOnHands(player); } // We give the weapon to the player player.GiveWeapon(NAPI.Util.WeaponNameToModel(itemModel.hash), itemModel.amount); // Add the attachment to the player AttachmentModel attachment = new AttachmentModel(itemModel.id, "IK_R_Hand", itemModel.hash, businessItem.position, businessItem.rotation); player.SetSharedData(EntityData.PLAYER_RIGHT_HAND, NAPI.Util.ToJson(attachment)); // Checking if it's been bought in the Ammu-Nation if (business.type == Constants.BUSINESS_TYPE_AMMUNATION) { // Add a registered weapon Database.AddLicensedWeapon(itemModel.id, player.Name); } } if (itemModel.hash == Constants.ITEM_HASH_TELEPHONE) { Random random = new Random(); PhoneModel phone = new PhoneModel(); { phone.itemId = itemModel.id; phone.contacts = new List <ContactModel>(); } do { // Generate a random number phone.number = random.Next(100000, 999999); }while (Telephone.phoneList.Where(p => p.number == phone.number).FirstOrDefault() != null); // Add the phone to the database Telephone.phoneList.Add(phone); Database.AddPhoneNumber(phone, player.Name); // Sending the message with the new number to the player player.SendChatMessage(Constants.COLOR_INFO + string.Format(InfoRes.player_phone, phone.number)); } // We substract the product and add funds to the business if (business.owner != string.Empty) { business.funds += price; business.products -= businessItem.products; // Update the business Database.UpdateBusiness(business); } player.SetSharedData(EntityData.PLAYER_MONEY, money - price); player.SendChatMessage(Constants.COLOR_INFO + purchaseMessage); }); }); } } }
public void BusinessPurchaseMadeEvent(Client player, params object[] arguments) { int businessId = NAPI.Data.GetEntityData(player, EntityData.PLAYER_BUSINESS_ENTERED); BusinessModel business = GetBusinessById(businessId); BusinessItemModel businessItem = GetBusinessItemFromName((String)arguments[0]); int amount = Int32.Parse(arguments[1].ToString()); if (business.type == Constants.BUSINESS_TYPE_AMMUNATION && businessItem.type == Constants.ITEM_TYPE_WEAPON && NAPI.Data.GetEntityData(player, EntityData.PLAYER_WEAPON_LICENSE) < Globals.GetTotalSeconds()) { NAPI.Chat.SendChatMessageToPlayer(player, Constants.COLOR_ERROR + Messages.ERR_WEAPON_LICENSE_EXPIRED); } else { int hash = 0; int price = (int)Math.Round(businessItem.products * business.multiplier) * amount; int money = NAPI.Data.GetEntitySharedData(player, EntityData.PLAYER_MONEY); String purchaseMessage = String.Format(Messages.INF_BUSINESS_ITEM_PURCHASED, price); int playerId = NAPI.Data.GetEntityData(player, EntityData.PLAYER_SQL_ID); // Miramos si tiene el objeto ya en el inventario ItemModel itemModel = Globals.GetPlayerItemModelFromHash(playerId, businessItem.hash); if (itemModel == null) { // Creamos el objeto que ha comprado itemModel = new ItemModel(); itemModel.hash = businessItem.hash; if (businessItem.type == Constants.ITEM_TYPE_WEAPON) { itemModel.ownerEntity = Constants.ITEM_ENTITY_WHEEL; } else { itemModel.ownerEntity = Int32.TryParse(itemModel.hash, out hash) ? Constants.ITEM_ENTITY_RIGHT_HAND : Constants.ITEM_ENTITY_PLAYER; } itemModel.ownerIdentifier = NAPI.Data.GetEntityData(player, EntityData.PLAYER_SQL_ID); itemModel.amount = businessItem.uses * amount; itemModel.position = new Vector3(0.0f, 0.0f, 0.0f); itemModel.dimension = 0; // Añadimos el objeto a la base de datos y a la lista itemModel.id = Database.AddNewItem(itemModel); Globals.itemList.Add(itemModel); } else { if (Int32.TryParse(itemModel.hash, out hash) == true) { itemModel.ownerEntity = Constants.ITEM_ENTITY_RIGHT_HAND; } itemModel.amount += (businessItem.uses * amount); Database.UpdateItem(itemModel); } // Si tiene hash, le entregamos en mano el objeto if (itemModel.ownerEntity == Constants.ITEM_ENTITY_RIGHT_HAND) { itemModel.objectHandle = NAPI.Object.CreateObject(UInt32.Parse(itemModel.hash), itemModel.position, new Vector3(0.0f, 0.0f, 0.0f), (byte)player.Dimension); NAPI.Entity.AttachEntityToEntity(itemModel.objectHandle, player, "PH_R_Hand", businessItem.position, businessItem.rotation); } else if (businessItem.type == Constants.ITEM_TYPE_WEAPON) { // Damos el arma al jugador WeaponHash weaponHash = NAPI.Util.WeaponNameToModel(itemModel.hash); NAPI.Player.GivePlayerWeapon(player, weaponHash, itemModel.amount); // Miramos si ha comprado en el Ammu-Nation if (business.type == Constants.BUSINESS_TYPE_AMMUNATION) { Database.AddLicensedWeapon(itemModel.id, player.Name); } } // Añadimos el objeto comprado a la mano NAPI.Data.SetEntityData(player, EntityData.PLAYER_RIGHT_HAND, itemModel.id); // Si es un teléfono, le damos el número if (itemModel.hash == Constants.ITEM_HASH_TELEPHONE) { if (NAPI.Data.GetEntityData(player, EntityData.PLAYER_PHONE) == 0) { Random random = new Random(); int phone = random.Next(100000, 999999); NAPI.Data.SetEntityData(player, EntityData.PLAYER_PHONE, phone); // Informamos al jugador de su número String message = String.Format(Messages.INF_PLAYER_PHONE, phone); NAPI.Chat.SendChatMessageToPlayer(player, Constants.COLOR_INFO + message); } } // Si el negocio tiene dueño, le damos el dinero y descontamos los productos if (business.owner != String.Empty) { business.funds += price; business.products -= businessItem.products; Database.UpdateBusiness(business); } NAPI.Data.SetEntitySharedData(player, EntityData.PLAYER_MONEY, money - price); NAPI.Chat.SendChatMessageToPlayer(player, Constants.COLOR_INFO + purchaseMessage); } }