/// <summary> /// When a student buy an item ,the method will check if the student have sufficient coins /// if the item bought exist in the student inventory, quantity of that item will be increased. /// if the item bought does not exist in the student inventory, the item will be created in the inventory. /// </summary> //Buy public void Buy() { UserData player = mainMenuController.getUserData(); Debug.Log("coin" + player.getCoin().ToString()); coinTxt.text = player.getCoin().ToString(); bool notExist = true; string coin = coinTxt.text; buyCost = buyCost * qty; int coinBalance = int.Parse(coin, System.Globalization.NumberStyles.Integer); if (coinBalance >= buyCost) { //ADD IN INVENTORY (update inventory db) foreach (var key in this.itemdict.Keys) { var item = this.itemdict[key]; if (this.itemtobepurchased.Equals(item.name)) { item.quantity += (uint)this.qty; notExist = false; break; } } if (notExist) { Item newItem = new Item(); newItem.name = this.itemtobepurchased; newItem.quantity = (uint)this.qty; newItem.studentUsername = player.userName; this.itemdict.Add(player.userName + this.itemtobepurchased, newItem); } InventoryDBHandler.PutInventory(this.itemdict); coinBalance = coinBalance - buyCost; //calculate coin left player.coin = coinBalance; //update user coin balance DatabaseShopHandler.PutUser(player.localId, player, () => { }); //Update coin in User DB coinTxt.text = coinBalance.ToString(); //update coin text in UI } else { messageBox.SetActive(true); messageBox.transform.GetChild(1).GetComponent <Text>().text = "Inefficient coins."; } buyCost = 0; //reset total purchase cost inside form create qty = 1; //reset qty }
/// <summary> /// Triggers when user select which item to equip from the list of items in inventory. /// </summary> /// <param name="itemClicked">The button object that was clicked by the user.</param> public void onInBagItemClick(Button itemClicked) { bool toAppendItem = true; Item previousEquippedItem = this.equippedItems.weapon; Item item; // search for the item that is being clicked foreach (string key in this.inBagList.Keys) { item = this.inBagList[key]; if (item.name.Equals(itemClicked.name)) { // check if clicked item to be equipped is a weapon as can only equip weaponm not potions if (item.property.Equals(Item.WEAPON)) { Debug.Log("Equipping item..."); // update the newly equipped weapon to the UI this.equippedWeapon.GetComponent <Text>().text = item.name; // set Model of EquippedItems to the newly equipped item equippedItems.weapon = item; // remove item from the inventory since quantity of it becomes 0 after adding it to the item list // if (item.quantity - 1 == 0) // this.inBagList.Remove(key); // // reduce quantity of that equipped item from the inventory // else item.quantity -= 1; // found the item so don't need to continue to search break; } else { // can exit loop because only can equip weapons, not any of the potions. return; } } } // just incase there isn't anything if (previousEquippedItem != null) { // search item in inventory that contains the same item as the previously equipped item so can increase its quantity in the inventory foreach (string key in this.inBagList.Keys) { item = this.inBagList[key]; // check if found the inventory item to increase the quantity if (item.name.Equals(previousEquippedItem.name)) { // increment item's quantity by 1 since adding previous equipped item back into the inventory ++item.quantity; // since increased the quantity, no need to append the item to the inventory list toAppendItem = false; // can return since no more task break; } } // check if need to append item into the inventory list if (toAppendItem) { // append previous equipped item since inventory doesn't have that item this.inBagList.Add(userData.getName() + previousEquippedItem.name, previousEquippedItem); } } // update the UI in the list of items in the inventory this.populateInBagItems(); InventoryDBHandler.PutEquippedItem(userData.getName(), this.equippedItems); InventoryDBHandler.PutInventory(this.inBagList); }