This commit is contained in:
Olivier Gagnon 2021-09-02 00:36:33 -04:00
parent d3aeda8ad5
commit 8d17495e85
10 changed files with 215 additions and 153 deletions

@ -4,8 +4,12 @@ import { IndustryStartingCosts } from "./IndustryData";
import { Industry } from "./Industry"; import { Industry } from "./Industry";
import { CorporationConstants } from "./data/Constants"; import { CorporationConstants } from "./data/Constants";
import { OfficeSpace } from "./OfficeSpace"; import { OfficeSpace } from "./OfficeSpace";
import { Material } from "./Material";
import { Product } from "./Product";
import { Warehouse } from "./Warehouse";
import { CorporationUnlockUpgrade } from "./data/CorporationUnlockUpgrades"; import { CorporationUnlockUpgrade } from "./data/CorporationUnlockUpgrades";
import { CorporationUpgrade } from "./data/CorporationUpgrades"; import { CorporationUpgrade } from "./data/CorporationUpgrades";
import { Cities } from "../Locations/Cities";
export function NewIndustry(corporation: ICorporation, industry: string, name: string): void { export function NewIndustry(corporation: ICorporation, industry: string, name: string): void {
for (let i = 0; i < corporation.divisions.length; ++i) { for (let i = 0; i < corporation.divisions.length; ++i) {
@ -70,4 +74,148 @@ export function IssueDividends(corporation: ICorporation, percent: number): void
} }
corporation.dividendPercentage = percent*100; corporation.dividendPercentage = percent*100;
}
export function SellMaterial(mat: Material, amt: string, price: string): void {
if(price === '') price = '0';
if(amt === '') amt = '0';
let cost = price.replace(/\s+/g, '');
cost = cost.replace(/[^-()\d/*+.MP]/g, ''); //Sanitize cost
let temp = cost.replace(/MP/g, mat.bCost+'');
try {
temp = eval(temp);
} catch(e) {
throw new Error("Invalid value or expression for sell price field: " + e);
}
if (temp == null || isNaN(parseFloat(temp))) {
throw new Error("Invalid value or expression for sell price field");
}
if (cost.includes("MP")) {
mat.sCost = cost; //Dynamically evaluated
} else {
mat.sCost = temp;
}
//Parse quantity
if (amt.includes("MAX") || amt.includes("PROD")) {
let q = amt.replace(/\s+/g, '');
q = q.replace(/[^-()\d/*+.MAXPROD]/g, '');
let tempQty = q.replace(/MAX/g, '1');
tempQty = tempQty.replace(/PROD/g, '1');
try {
tempQty = eval(tempQty);
} catch(e) {
throw new Error("Invalid value or expression for sell price field: " + e);
}
if (tempQty == null || isNaN(parseFloat(tempQty))) {
throw new Error("Invalid value or expression for sell price field");
}
mat.sllman[0] = true;
mat.sllman[1] = q; //Use sanitized input
} else if (isNaN(parseFloat(amt))) {
throw new Error("Invalid value for sell quantity field! Must be numeric or 'MAX'");
} else {
let q = parseFloat(amt);
if (isNaN(q)) {q = 0;}
if (q === 0) {
mat.sllman[0] = false;
mat.sllman[1] = 0;
} else {
mat.sllman[0] = true;
mat.sllman[1] = q;
}
}
}
export function SellProduct(product: Product, city: string, amt: string, price: string, all: boolean): void {
//Parse price
if (price.includes("MP")) {
//Dynamically evaluated quantity. First test to make sure its valid
//Sanitize input, then replace dynamic variables with arbitrary numbers
price = price.replace(/\s+/g, '');
price = price.replace(/[^-()\d/*+.MP]/g, '');
let temp = price.replace(/MP/g, '1');
try {
temp = eval(temp);
} catch(e) {
throw new Error("Invalid value or expression for sell quantity field: " + e);
}
if (temp == null || isNaN(parseFloat(temp))) {
throw new Error("Invalid value or expression for sell quantity field.");
}
product.sCost = price; //Use sanitized price
} else {
const cost = parseFloat(price);
if (isNaN(cost)) {
throw new Error("Invalid value for sell price field");
}
product.sCost = cost;
}
// Array of all cities. Used later
const cities = Object.keys(Cities);
// Parse quantity
if (amt.includes("MAX") || amt.includes("PROD")) {
//Dynamically evaluated quantity. First test to make sure its valid
let qty = amt.replace(/\s+/g, '');
qty = qty.replace(/[^-()\d/*+.MAXPROD]/g, '');
let temp = qty.replace(/MAX/g, '1');
temp = temp.replace(/PROD/g, '1');
try {
temp = eval(temp);
} catch(e) {
throw new Error("Invalid value or expression for sell price field: " + e);
}
if (temp == null || isNaN(parseFloat(temp))) {
throw new Error("Invalid value or expression for sell price field");
}
if (all) {
for (let i = 0; i < cities.length; ++i) {
const tempCity = cities[i];
product.sllman[tempCity][0] = true;
product.sllman[tempCity][1] = qty; //Use sanitized input
}
} else {
product.sllman[city][0] = true;
product.sllman[city][1] = qty; //Use sanitized input
}
} else if (isNaN(parseFloat(amt))) {
throw new Error("Invalid value for sell quantity field! Must be numeric");
} else {
let qty = parseFloat(amt);
if (isNaN(qty)) {qty = 0;}
if (qty === 0) {
if (all) {
for (let i = 0; i < cities.length; ++i) {
const tempCity = cities[i];
product.sllman[tempCity][0] = false;
product.sllman[tempCity][1] = '';
}
} else {
product.sllman[city][0] = false;
product.sllman[city][1] = '';
}
} else {
if (all) {
for (let i = 0; i < cities.length; ++i) {
const tempCity = cities[i];
product.sllman[tempCity][0] = true;
product.sllman[tempCity][1] = qty;
}
} else {
product.sllman[city][0] = true;
product.sllman[city][1] = qty;
}
}
}
}
export function SetSmartSupply(warehouse: Warehouse, smartSupply: boolean): void {
warehouse.smartSupplyEnabled = smartSupply;
} }

@ -23,7 +23,7 @@ export function ExpandNewCityPopup(props: IProps): React.ReactElement {
try { try {
NewCity(props.corp, props.division, dropdown.current.value); NewCity(props.corp, props.division, dropdown.current.value);
} catch(err) { } catch(err) {
dialogBoxCreate(err); dialogBoxCreate(err+'');
return; return;
} }

@ -25,6 +25,8 @@ import { ICorporation } from "../ICorporation";
import { IIndustry } from "../IIndustry"; import { IIndustry } from "../IIndustry";
import { CorporationRouting } from "./Routing"; import { CorporationRouting } from "./Routing";
import { IPlayer } from "../../PersonObjects/IPlayer"; import { IPlayer } from "../../PersonObjects/IPlayer";
import { SetSmartSupply } from "../Actions";
interface IProductProps { interface IProductProps {
corp: ICorporation; corp: ICorporation;
@ -514,7 +516,7 @@ export function IndustryWarehouse(props: IProps): React.ReactElement {
const smartSupplyCheckboxId = "cmpy-mgmt-smart-supply-checkbox"; const smartSupplyCheckboxId = "cmpy-mgmt-smart-supply-checkbox";
function smartSupplyOnChange(e: React.ChangeEvent<HTMLInputElement>): void { function smartSupplyOnChange(e: React.ChangeEvent<HTMLInputElement>): void {
if(warehouse === 0) return; if(warehouse === 0) return;
warehouse.smartSupplyEnabled = e.target.checked; SetSmartSupply(warehouse, e.target.checked);
corp.rerender(props.player); corp.rerender(props.player);
} }

@ -20,7 +20,7 @@ export function IssueDividendsPopup(props: IProps): React.ReactElement {
try { try {
IssueDividends(props.corp, percent/100); IssueDividends(props.corp, percent/100);
} catch(err) { } catch(err) {
dialogBoxCreate(err); dialogBoxCreate(err+'');
} }
removePopup(props.popupId); removePopup(props.popupId);

@ -29,7 +29,7 @@ export function LevelableUpgrade(props: IProps): React.ReactElement {
try { try {
LevelUpgrade(props.corp, props.upgrade); LevelUpgrade(props.corp, props.upgrade);
} catch(err) { } catch(err) {
dialogBoxCreate(err); dialogBoxCreate(err+'');
} }
props.corp.rerender(props.player); props.corp.rerender(props.player);
} }

@ -28,7 +28,7 @@ export function NewIndustryPopup(props: IProps): React.ReactElement {
try { try {
NewIndustry(props.corp, industry, name); NewIndustry(props.corp, industry, name);
} catch(err) { } catch(err) {
dialogBoxCreate(err); dialogBoxCreate(err+'');
return; return;
} }

@ -3,6 +3,7 @@ import { dialogBoxCreate } from "../../../utils/DialogBox";
import { removePopup } from "../../ui/React/createPopup"; import { removePopup } from "../../ui/React/createPopup";
import { ICorporation } from "../ICorporation"; import { ICorporation } from "../ICorporation";
import { Material } from "../Material"; import { Material } from "../Material";
import { SellMaterial } from "../Actions";
function initialPrice(mat: Material): string { function initialPrice(mat: Material): string {
let val = mat.sCost ? mat.sCost+'' : ''; let val = mat.sCost ? mat.sCost+'' : '';
@ -26,64 +27,10 @@ export function SellMaterialPopup(props: IProps): React.ReactElement {
const [price, setPrice] = useState<string>(initialPrice(props.mat)); const [price, setPrice] = useState<string>(initialPrice(props.mat));
function sellMaterial(): void { function sellMaterial(): void {
let p = price;
let qty = amt;
if(p === '') p = '0';
if(qty === '') qty = '0';
let cost = p.replace(/\s+/g, '');
cost = cost.replace(/[^-()\d/*+.MP]/g, ''); //Sanitize cost
let temp = cost.replace(/MP/g, props.mat.bCost+'');
try { try {
temp = eval(temp); SellMaterial(props.mat, amt, price);
} catch(e) { } catch(err) {
dialogBoxCreate("Invalid value or expression for sell price field: " + e); dialogBoxCreate(err+'');
return;
}
if (temp == null || isNaN(parseFloat(temp))) {
dialogBoxCreate("Invalid value or expression for sell price field");
return;
}
if (cost.includes("MP")) {
props.mat.sCost = cost; //Dynamically evaluated
} else {
props.mat.sCost = temp;
}
//Parse quantity
if (qty.includes("MAX") || qty.includes("PROD")) {
let q = qty.replace(/\s+/g, '');
q = q.replace(/[^-()\d/*+.MAXPROD]/g, '');
let tempQty = q.replace(/MAX/g, '1');
tempQty = tempQty.replace(/PROD/g, '1');
try {
tempQty = eval(tempQty);
} catch(e) {
dialogBoxCreate("Invalid value or expression for sell price field: " + e);
return;
}
if (tempQty == null || isNaN(parseFloat(tempQty))) {
dialogBoxCreate("Invalid value or expression for sell price field");
return;
}
props.mat.sllman[0] = true;
props.mat.sllman[1] = q; //Use sanitized input
} else if (isNaN(parseFloat(qty))) {
dialogBoxCreate("Invalid value for sell quantity field! Must be numeric or 'MAX'");
return;
} else {
let q = parseFloat(qty);
if (isNaN(q)) {q = 0;}
if (q === 0) {
props.mat.sllman[0] = false;
props.mat.sllman[1] = 0;
} else {
props.mat.sllman[0] = true;
props.mat.sllman[1] = q;
}
} }
removePopup(props.popupId); removePopup(props.popupId);

@ -3,6 +3,7 @@ import { dialogBoxCreate } from "../../../utils/DialogBox";
import { removePopup } from "../../ui/React/createPopup"; import { removePopup } from "../../ui/React/createPopup";
import { Cities } from "../../Locations/Cities"; import { Cities } from "../../Locations/Cities";
import { Product } from "../Product"; import { Product } from "../Product";
import { SellProduct } from "../Actions";
function initialPrice(product: Product): string { function initialPrice(product: Product): string {
let val = product.sCost ? product.sCost+'' : ''; let val = product.sCost ? product.sCost+'' : '';
@ -31,93 +32,10 @@ export function SellProductPopup(props: IProps): React.ReactElement {
} }
function sellProduct(): void { function sellProduct(): void {
//Parse price try {
if (px.includes("MP")) { SellProduct(props.product, props.city, iQty, px, checked);
//Dynamically evaluated quantity. First test to make sure its valid } catch(err) {
//Sanitize input, then replace dynamic variables with arbitrary numbers dialogBoxCreate(err+'');
let price = px.replace(/\s+/g, '');
price = price.replace(/[^-()\d/*+.MP]/g, '');
let temp = price.replace(/MP/g, '1');
try {
temp = eval(temp);
} catch(e) {
dialogBoxCreate("Invalid value or expression for sell quantity field: " + e);
return;
}
if (temp == null || isNaN(parseFloat(temp))) {
dialogBoxCreate("Invalid value or expression for sell quantity field.");
return;
}
props.product.sCost = price; //Use sanitized price
} else {
const cost = parseFloat(px);
if (isNaN(cost)) {
dialogBoxCreate("Invalid value for sell price field");
return;
}
props.product.sCost = cost;
}
// Array of all cities. Used later
const cities = Object.keys(Cities);
// Parse quantity
if (iQty.includes("MAX") || iQty.includes("PROD")) {
//Dynamically evaluated quantity. First test to make sure its valid
let qty = iQty.replace(/\s+/g, '');
qty = qty.replace(/[^-()\d/*+.MAXPROD]/g, '');
let temp = qty.replace(/MAX/g, '1');
temp = temp.replace(/PROD/g, '1');
try {
temp = eval(temp);
} catch(e) {
dialogBoxCreate("Invalid value or expression for sell price field: " + e);
return;
}
if (temp == null || isNaN(parseFloat(temp))) {
dialogBoxCreate("Invalid value or expression for sell price field");
return;
}
if (checked) {
for (let i = 0; i < cities.length; ++i) {
const tempCity = cities[i];
props.product.sllman[tempCity][0] = true;
props.product.sllman[tempCity][1] = qty; //Use sanitized input
}
} else {
props.product.sllman[props.city][0] = true;
props.product.sllman[props.city][1] = qty; //Use sanitized input
}
} else if (isNaN(parseFloat(iQty))) {
dialogBoxCreate("Invalid value for sell quantity field! Must be numeric");
return;
} else {
let qty = parseFloat(iQty);
if (isNaN(qty)) {qty = 0;}
if (qty === 0) {
if (checked) {
for (let i = 0; i < cities.length; ++i) {
const tempCity = cities[i];
props.product.sllman[tempCity][0] = false;
props.product.sllman[tempCity][1] = '';
}
} else {
props.product.sllman[props.city][0] = false;
props.product.sllman[props.city][1] = '';
}
} else {
if (checked) {
for (let i = 0; i < cities.length; ++i) {
const tempCity = cities[i];
props.product.sllman[tempCity][0] = true;
props.product.sllman[tempCity][1] = qty;
}
} else {
props.product.sllman[props.city][0] = true;
props.product.sllman[props.city][1] = qty;
}
}
} }
removePopup(props.popupId); removePopup(props.popupId);

@ -23,7 +23,7 @@ export function UnlockUpgrade(props: IProps): React.ReactElement {
try { try {
UU(props.corp, props.upgradeData); UU(props.corp, props.upgradeData);
} catch(err) { } catch(err) {
dialogBoxCreate(err); dialogBoxCreate(err+'');
} }
props.corp.rerender(props.player); props.corp.rerender(props.player);
} }

@ -25,7 +25,10 @@ import {
NewCity, NewCity,
UnlockUpgrade, UnlockUpgrade,
LevelUpgrade, LevelUpgrade,
IssueDividends } from "./Corporation/Actions"; IssueDividends,
SellMaterial,
SellProduct,
SetSmartSupply } from "./Corporation/Actions";
import { CorporationUnlockUpgrades } from "./Corporation/data/CorporationUnlockUpgrades"; import { CorporationUnlockUpgrades } from "./Corporation/data/CorporationUnlockUpgrades";
import { CorporationUpgrades } from "./Corporation/data/CorporationUpgrades"; import { CorporationUpgrades } from "./Corporation/data/CorporationUpgrades";
import { import {
@ -563,6 +566,39 @@ function NetscriptFunctions(workerScript) {
return Augmentations[name]; return Augmentations[name];
} }
function getDivision(divisionName) {
const division = Player.corporation.divisions.find(div => div.name === divisionName);
if(division === undefined)
throw new Error(`No division named '${divisionName}'`);
return division;
}
function getWarehouse(divisionName, cityName) {
const division = getDivision(divisionName);
if(!(cityName in division.warehouses))
throw new Error(`Invalid city name '${cityName}'`);
const warehouse = division.warehouses[cityName];
if(warehouse === 0)
throw new Error(`${division.name} has not expanded to '${cityName}'`);
return warehouse;
}
function getMaterial(divisionName, cityName, materialName) {
const warehouse = getWarehouse(divisionName, cityName);
const material = warehouse.materials[materialName];
if(material === undefined)
throw new Error(`Invalid material name: '${materialName}'`);
return material;
}
function getProduct(divisionName, productName) {
const division = getDivision(divisionName);
const product = division.products[productName];
if(product === undefined)
throw new Error(`Invalid product name: '${productName}'`);
return product;
}
const runAfterReset = function(cbScript=null) { const runAfterReset = function(cbScript=null) {
//Run a script after reset //Run a script after reset
if (cbScript && isString(cbScript)) { if (cbScript && isString(cbScript)) {
@ -4110,8 +4146,7 @@ function NetscriptFunctions(workerScript) {
NewIndustry(Player.corporation, industryName, divisionName); NewIndustry(Player.corporation, industryName, divisionName);
}, },
expandCity: function(divisionName, cityName) { expandCity: function(divisionName, cityName) {
const division = Player.corporation.divisions.find(div => div.name === divisionName); const division = getDivision(divisionName);
if(division === undefined) throw new Error("No division named '${divisionName}'");
NewCity(Player.corporation, division, cityName); NewCity(Player.corporation, division, cityName);
}, },
unlockUpgrade: function(upgradeName) { unlockUpgrade: function(upgradeName) {
@ -4129,6 +4164,18 @@ function NetscriptFunctions(workerScript) {
issueDividends: function(percent) { issueDividends: function(percent) {
IssueDividends(Player.corporation, percent); IssueDividends(Player.corporation, percent);
}, },
sellMaterial: function(divisionName, cityName, materialName, amt, price) {
const material = getMaterial(divisionName, cityName, materialName);
SellMaterial(material, amt, price);
},
sellProduct: function(divisionName, cityName, productName, amt, price, all) {
const product = getProduct(divisionName, productName);
SellProduct(product, cityName, amt, price, all);
},
setSmartSupply: function(divisionName, cityName, enabled) {
const warehouse = getWarehouse(divisionName, cityName);
SetSmartSupply(warehouse, enabled);
},
}, // End Corporation API }, // End Corporation API
// Coding Contract API // Coding Contract API