示例#1
0
    // Gets called when the buy button in the buy menu is pressed
    // Takes the items from the menu and then resolve what was bought and what actions should be taken
    public void ResolveBuy(GameObject menu)
    {
        string locationId = FindLocationIdOfCurrentLocation();

        int[] rowChildIndex = FindRowsWithinMenu(menu);

        for (int i = 0; i < rowChildIndex.Length; i++)
        {
            GameObject tempRow     = menu.transform.GetChild(rowChildIndex[i]).gameObject;
            string[]   rowElements = ReadRow(tempRow);

            string itemName         = rowElements[0];
            float  itemPrice        = float.Parse(rowElements[1]);
            int    itemStockInShop  = int.Parse(rowElements[2]);
            int    itemToBuy        = 0;
            string currentProductId = FindProductIdOfInputString(itemName);

            // If the input field was empty then itemToSell is assigned 0
            // if it wasn't empty then it takes whatever value was in it
            itemToBuy = rowElements[3] == "" ? 0 : int.Parse(rowElements[3]);

            // Checks to make sure that certain conditions are met before activating the buying of stock
            if (itemToBuy < 0) // if the number of items to buy is negative then an error message is displayed
            {
                _errorGui.enabled      = true;
                _errorGui.errorMessage = "Cannot buy a negative amount";
            }
            else if (itemToBuy > itemStockInShop) // If there are more items to buy than in inventory an error message is displayed
            {
                _errorGui.enabled      = true;
                _errorGui.errorMessage = "Not enough stock in shop to buy";
            }
            else if (itemToBuy == 0) // If the items to sell was 0 then it is skipped
            {
            }
            else // If it passes all the conditions then the correct changes are made
            {
                // The total cost is calculated, so is the new amount of stock that is to be inputed
                int cost = Convert.ToInt32(Math.Round(itemToBuy * itemPrice));
                itemStockInShop -= itemToBuy;

                // The data displayed on the rows are updated and the input field are wiped
                WriteRow(tempRow, itemName, itemPrice, itemStockInShop);
                WipeInput(tempRow);

                // The money decrease by the cost value
                _gameMoneyHandler.MoneyChange(cost, false);

                // The correct amount of items are added to the player inventory
                // The correct amount of items are removed from the locations stock in productLocations table
                // The change is recorded in the ProductChanges Table
                _dataBaseConnector.DataBasePlayerInventoryInput(int.Parse(FindProductIdOfInputString(itemName)), itemPrice, itemToBuy, true, false);
                _dataBaseConnector.DataBaseProductLocationUpdate("Stock", itemStockInShop.ToString(), "ProductID", FindProductIdOfInputString(itemName), "LocationID", locationId);
                _dataBaseConnector.DataBaseProductChangesInsert(int.Parse(currentProductId), int.Parse(locationId), itemPrice, itemToBuy);
            }

            WipeInput(tempRow);
        }
    }
示例#2
0
    void EventLocationPriceUpdate() // Should check for any changes in ProductChanges in the last 24 hrs and then update the prices of diffrent products at a location
    {
        int indexCounter = 0;

        string[,] allRelevantDataForLocations = new string[24, 6]; // ProductId, LocationId, CurrentPrice, CurrentStock, ChangeInStock, PED

        int timeSinceLastCheck = _gameTimeHandler.GetTimeInHours() - 24;

        string[,] selectResultFromChanges = _dataBaseConnector.DataBaseProductChangesSelectWithinLast24Hours(timeSinceLastCheck.ToString());

        // Goes through ProductLocation database and copies all relevant data to a 2d Array
        // These items include the productId, LocationId, CurrentPrice, CurrentStock and PED here
        for (int x = 0; x < 6; x++)
        {
            string[,] resultsFromProductLocationForLocation = _dataBaseConnector.DataBaseProductLocationSelect("LocationID", x.ToString());

            for (int z = 0; z < 4; z++)
            {
                string[,] tempProduct = _dataBaseConnector.DataBaseProductsSelect("ProductID", resultsFromProductLocationForLocation[z, 0]);
                allRelevantDataForLocations[indexCounter, 0] = resultsFromProductLocationForLocation[z, 0];
                allRelevantDataForLocations[indexCounter, 1] = resultsFromProductLocationForLocation[z, 1];
                allRelevantDataForLocations[indexCounter, 2] = resultsFromProductLocationForLocation[z, 3];
                allRelevantDataForLocations[indexCounter, 3] = resultsFromProductLocationForLocation[z, 2];
                allRelevantDataForLocations[indexCounter, 4] = 0.ToString();
                allRelevantDataForLocations[indexCounter, 5] = tempProduct[0, 5];
                indexCounter += 1;
            }
        }

        // Finds the correct index where the changes will be inputted to
        for (int i = 0; i < selectResultFromChanges.Length / 3; i++)
        {
            // if there is nothing in the specified index then this index is skipped
            if (selectResultFromChanges[i, 0] is null)
            {
                continue;
            }

            int  locationStartIndex        = int.Parse(selectResultFromChanges[i, 1]) * 4;
            int  productLocationIndex      = locationStartIndex;
            bool productLocationIndexFound = false;

            // The location indexes increase every 4 indexes therefore we only need the check the products for the next 4 indexes
            for (int c = locationStartIndex; c < locationStartIndex + 4; c++)
            {
                // If the correct index is found then the correct indexes are found, if its not advance the index
                if (allRelevantDataForLocations[locationStartIndex, 0] == selectResultFromChanges[i, 0])
                {
                    productLocationIndexFound = true;
                }
                else if (productLocationIndexFound != true)
                {
                    productLocationIndex += 1;
                }
            }

            // Adds the changed stock amount to the already existing amount of changed stock
            allRelevantDataForLocations[productLocationIndex, 4] = Convert.ToString(int.Parse(allRelevantDataForLocations[productLocationIndex, 4]) + int.Parse(selectResultFromChanges[i, 2]));
        }

        // Check through all indexes in the array
        for (int v = 0; v < allRelevantDataForLocations.Length / 6; v++)
        {
            // If the changed stock is 0 then skip the index
            if (allRelevantDataForLocations[v, 4] == "0")
            {
                continue;
            }

            // Get all the relevant data and assign then to temporary variables to make reading easier
            string tempProductId         = allRelevantDataForLocations[v, 0];
            string tempLocationId        = allRelevantDataForLocations[v, 1];
            float  tempPed               = float.Parse(allRelevantDataForLocations[v, 5]);
            bool   hasCurrentPriceFailed = float.TryParse(allRelevantDataForLocations[v, 2], out float tempCurrentPrice);
            int    tempLastStock         = int.Parse(allRelevantDataForLocations[v, 3]) + int.Parse(allRelevantDataForLocations[v, 4]);
            int    tempCurrentStock      = int.Parse(allRelevantDataForLocations[v, 3]);

            // Calculate the change in price
            float newPrice = _economics.CalcChangeInPrice(tempPed, tempCurrentPrice, tempLastStock, tempCurrentStock);

            // Update the entry in the productLocation table and Insert a new entry in the productChanges table
            _dataBaseConnector.DataBaseProductLocationUpdate("LocalPrice", newPrice.ToString(), "ProductID", tempProductId, "LocationID", tempLocationId);
            _dataBaseConnector.DataBaseProductChangesInsert(int.Parse(tempProductId), int.Parse(tempLocationId), newPrice, tempCurrentStock);
        }
    }