// public void verify(){
    //     loginControllerScript.ShowSuccessfulLogin();
    // }
    /// <summary>
    /// Function which is used to create a starting account in the database with default information and items
    /// </summary>
    /// <param name="verified"></param>
    /// <param name="username"></param>
    /// <param name="email"></param>
    private void PostToDatabase(bool verified = false, string username = null, string email = null)
    {
        UserData userData = new UserData();

        Debug.Log("1");
        if (verified)
        {
            userData.email         = email;
            userData.localId       = localId;
            userData.userName      = username;
            userData.level         = 1;
            userData.experience    = 0;
            userData.maxExperience = 100;
            userData.hp            = 100;
            userData.coin          = 0;
            userData.verified      = true;
        }
        Debug.Log("2");
        //verify();

        RestClient.Put(databaseURL + "/" + localId + ".json?auth=" + idToken, userData);
        Item          item         = new Item("Bronze Daggger", "Damage", 1, username);
        EquippedItems equippedItem = new EquippedItems();

        equippedItem.weapon = item;
        InventoryDBHandler.PutEquippedItem(username, equippedItem);
    }
示例#2
0
    /// <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>
    /// Fetch required data from database and set the values on the UI.
    /// </summary>
    public void Init()
    {
        // StartCoroutine(inventoryInt.getInventoryDetails((success, allResults) => {
        //     if (success)
        //     {
        //         this.inBagList = allResults;
        //         this.populateInBagItems();
        //     }
        // }));

        // StartCoroutine(inventoryInt.getEquippedItemsDetails((success, allResults) => {
        //     if (success)
        //     {
        //         this.equippedItems = allResults;
        //     }
        // }));

        // get userdata from mainmenu
        this.userData = this.mainMenuControllerScript.getUserData();

        // incase server have issues and never fetch the user data entity
        if (this.userData == null)
        {
            return;
        }

        // get items list
        InventoryDBHandler.GetInventory(userData.getName(), itemsDict => {
            // set to local dictionary of items in inventory
            this.inBagList = itemsDict;
            // display list of items in inventory on the UI
            this.populateInBagItems();
        });

        InventoryDBHandler.GetEquippedItem(userData.getName(), equippedObj => {
            // set to local object of equipped items
            this.equippedItems = equippedObj;

            // set weapon name on the UI
            this.equippedWeapon.GetComponent <Text>().text = equippedObj.weapon.name;
        });

        // set the gold amount to the UI
        goldAmount.GetComponent <Text>().text = this.userData.getCoin().ToString();
    }
    /// <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);
    }