Convert CodingContractGenerator to typescript

This commit is contained in:
Olivier Gagnon 2021-03-14 01:31:23 -05:00
parent 6c57d548ec
commit 62c571ef3a

@ -1,20 +1,23 @@
import { import {
CodingContract, CodingContract,
CodingContractRewardType, CodingContractRewardType,
CodingContractTypes CodingContractTypes,
ICodingContractReward
} from "./CodingContracts"; } from "./CodingContracts";
import { Factions } from "./Faction/Factions"; import { Factions } from "./Faction/Factions";
import { Player } from "./Player"; import { Player } from "./Player";
import { AllServers } from "./Server/AllServers"; import { AllServers } from "./Server/AllServers";
import { GetServerByHostname } from "./Server/ServerHelpers"; import { GetServerByHostname } from "./Server/ServerHelpers";
import { SpecialServerNames } from "./Server/SpecialServerIps"; import { SpecialServerNames } from "./Server/SpecialServerIps";
import { Server } from "./Server/Server";
import { HacknetServer } from "./Hacknet/HacknetServer";
import { getRandomInt } from "../utils/helpers/getRandomInt"; import { getRandomInt } from "../utils/helpers/getRandomInt";
export function generateRandomContract() { export function generateRandomContract() {
// First select a random problem type // First select a random problem type
let problemType = getRandomProblemType(); const problemType = getRandomProblemType();
// Then select a random reward type. 'Money' will always be the last reward type // Then select a random reward type. 'Money' will always be the last reward type
const reward = getRandomReward(); const reward = getRandomReward();
@ -22,15 +25,15 @@ export function generateRandomContract() {
// Choose random server // Choose random server
const randServer = getRandomServer(); const randServer = getRandomServer();
let contractFn = getRandomFilename(randServer, reward); const contractFn = getRandomFilename(randServer, reward);
let contract = new CodingContract(contractFn, problemType, reward); const contract = new CodingContract(contractFn, problemType, reward);
randServer.addContract(contract); randServer.addContract(contract);
} }
export function generateRandomContractOnHome() { export function generateRandomContractOnHome() {
// First select a random problem type // First select a random problem type
let problemType = getRandomProblemType(); const problemType = getRandomProblemType();
// Then select a random reward type. 'Money' will always be the last reward type // Then select a random reward type. 'Money' will always be the last reward type
const reward = getRandomReward(); const reward = getRandomReward();
@ -38,13 +41,19 @@ export function generateRandomContractOnHome() {
// Choose random server // Choose random server
const serv = Player.getHomeComputer(); const serv = Player.getHomeComputer();
let contractFn = getRandomFilename(serv, reward); const contractFn = getRandomFilename(serv, reward);
let contract = new CodingContract(contractFn, problemType, reward); const contract = new CodingContract(contractFn, problemType, reward);
serv.addContract(contract); serv.addContract(contract);
} }
export function generateContract(params) { export interface IGenerateContractParams {
problemType?: string;
server?: string;
fn?: string;
}
export function generateContract(params: IGenerateContractParams) {
// Problem Type // Problem Type
let problemType; let problemType;
const problemTypes = Object.keys(CodingContractTypes); const problemTypes = Object.keys(CodingContractTypes);
@ -62,7 +71,7 @@ export function generateContract(params) {
if (params.server != null) { if (params.server != null) {
server = GetServerByHostname(params.server); server = GetServerByHostname(params.server);
if (server == null) { if (server == null) {
server = AllServers[param.server]; server = AllServers[params.server];
} }
if (server == null) { if (server == null) {
server = getRandomServer(); server = getRandomServer();
@ -84,7 +93,7 @@ export function generateContract(params) {
} }
// Ensures that a contract's reward type is valid // Ensures that a contract's reward type is valid
function sanitizeRewardType(rewardType) { function sanitizeRewardType(rewardType: CodingContractRewardType): CodingContractRewardType {
let type = rewardType; // Create copy let type = rewardType; // Create copy
const factionsThatAllowHacking = Player.factions.filter((fac) => { const factionsThatAllowHacking = Player.factions.filter((fac) => {
@ -115,9 +124,11 @@ function getRandomProblemType() {
return problemTypes[randIndex]; return problemTypes[randIndex];
} }
function getRandomReward() { function getRandomReward(): ICodingContractReward {
let reward = {}; let reward: ICodingContractReward = {
reward.type = getRandomInt(0, CodingContractRewardType.Money); name: "",
type: getRandomInt(0, CodingContractRewardType.Money),
};
reward.type = sanitizeRewardType(reward.type); reward.type = sanitizeRewardType(reward.type);
// Add additional information based on the reward type // Add additional information based on the reward type
@ -155,7 +166,7 @@ function getRandomReward() {
return reward; return reward;
} }
function getRandomServer() { function getRandomServer(): Server | HacknetServer {
const servers = Object.keys(AllServers); const servers = Object.keys(AllServers);
let randIndex = getRandomInt(0, servers.length - 1); let randIndex = getRandomInt(0, servers.length - 1);
let randServer = AllServers[servers[randIndex]]; let randServer = AllServers[servers[randIndex]];
@ -163,7 +174,7 @@ function getRandomServer() {
// An infinite loop shouldn't ever happen, but to be safe we'll use // An infinite loop shouldn't ever happen, but to be safe we'll use
// a for loop with a limited number of tries // a for loop with a limited number of tries
for (let i = 0; i < 200; ++i) { for (let i = 0; i < 200; ++i) {
if (!randServer.purchasedByPlayer && randServer.hostname !== SpecialServerNames.WorldDaemon) { if (randServer instanceof Server && !randServer.purchasedByPlayer && randServer.hostname !== SpecialServerNames.WorldDaemon) {
break; break;
} }
randIndex = getRandomInt(0, servers.length - 1); randIndex = getRandomInt(0, servers.length - 1);
@ -173,11 +184,11 @@ function getRandomServer() {
return randServer; return randServer;
} }
function getRandomFilename(server, reward) { function getRandomFilename(server: Server | HacknetServer, reward: ICodingContractReward): string {
let contractFn = `contract-${getRandomInt(0, 1e6)}`; let contractFn = `contract-${getRandomInt(0, 1e6)}`;
for (let i = 0; i < 1000; ++i) { for (let i = 0; i < 1000; ++i) {
if (server.contracts.filter((c) => {return c.fn === contractFn}).length <= 0) { break; } if (server.contracts.filter((c: CodingContract) => {return c.fn === contractFn}).length <= 0) { break; }
contractFn = `contract-${getRandomInt(0, 1e6)}`; contractFn = `contract-${getRandomInt(0, 1e6)}`;
} }