// Gets called when the sell button in the sell menu is pressed // Takes the items from the menu and then resolve what was sold and what actions should be taken public void ResolveSell(GameObject menu) { string locationId = FindLocationIdOfCurrentLocation(); int[] rowChildIndex = FindRowsWithinMenu(menu); // Goes through each row in the 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 itemStockInInventory = int.Parse(rowElements[2]); int itemToSell = 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 itemToSell = rowElements[3] == "" ? 0 : int.Parse(rowElements[3]); // Checks to make sure that certain conditions are met before activating the sell if (itemToSell < 0) // if the number of items to sell is negative then an error message is displayed { _errorGui.enabled = true; _errorGui.errorMessage = "To Sell is negative"; } else if (itemToSell > itemStockInInventory) // If there are more items to sell than in inventory an error message is displayed { _errorGui.enabled = true; _errorGui.errorMessage = "Not enough stock in inventory"; } else if (itemToSell == 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 revenue is calculated, so is the new amount of stock that is to be inputed int revenue = Convert.ToInt32(Math.Round(itemToSell * itemPrice)); itemStockInInventory -= itemToSell; // The data displayed on the rows are updated and the input field are wiped WriteRow(tempRow, itemName, itemPrice, itemStockInInventory); WipeInput(tempRow); // The money increases by the revenue value _gameMoneyHandler.MoneyChange(revenue, true); // The stock changes in PlayerInventory and the change is recorded in the Changes table _dataBaseConnector.DataBasePlayerInventoryInput(int.Parse(locationId), itemPrice, itemToSell, true, true); _dataBaseConnector.DataBaseProductChangesInsert(int.Parse(currentProductId), int.Parse(locationId), itemPrice, itemToSell); } // The Input field in the row is wiped weather or not the sale succeeds WipeInput(tempRow); } }
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); } }