2019-04-11 10:37:40 +02:00
|
|
|
import {
|
|
|
|
CodingContract,
|
|
|
|
CodingContractRewardType,
|
2021-03-14 07:31:23 +01:00
|
|
|
CodingContractTypes,
|
|
|
|
ICodingContractReward
|
2019-04-11 10:37:40 +02:00
|
|
|
} from "./CodingContracts";
|
|
|
|
import { Factions } from "./Faction/Factions";
|
|
|
|
import { Player } from "./Player";
|
|
|
|
import { AllServers } from "./Server/AllServers";
|
|
|
|
import { GetServerByHostname } from "./Server/ServerHelpers";
|
2019-04-30 05:54:20 +02:00
|
|
|
import { SpecialServerNames } from "./Server/SpecialServerIps";
|
2021-03-14 07:31:23 +01:00
|
|
|
import { Server } from "./Server/Server";
|
|
|
|
import { HacknetServer } from "./Hacknet/HacknetServer";
|
2019-04-11 10:37:40 +02:00
|
|
|
|
|
|
|
import { getRandomInt } from "../utils/helpers/getRandomInt";
|
2018-10-27 21:26:22 +02:00
|
|
|
|
|
|
|
|
|
|
|
export function generateRandomContract() {
|
|
|
|
// First select a random problem type
|
2021-03-14 07:31:23 +01:00
|
|
|
const problemType = getRandomProblemType();
|
2018-10-27 21:26:22 +02:00
|
|
|
|
|
|
|
// Then select a random reward type. 'Money' will always be the last reward type
|
2018-10-27 22:48:33 +02:00
|
|
|
const reward = getRandomReward();
|
|
|
|
|
|
|
|
// Choose random server
|
|
|
|
const randServer = getRandomServer();
|
|
|
|
|
2021-03-14 07:31:23 +01:00
|
|
|
const contractFn = getRandomFilename(randServer, reward);
|
|
|
|
const contract = new CodingContract(contractFn, problemType, reward);
|
2018-10-27 22:48:33 +02:00
|
|
|
|
|
|
|
randServer.addContract(contract);
|
|
|
|
}
|
|
|
|
|
2019-02-01 09:27:24 +01:00
|
|
|
export function generateRandomContractOnHome() {
|
|
|
|
// First select a random problem type
|
2021-03-14 07:31:23 +01:00
|
|
|
const problemType = getRandomProblemType();
|
2019-02-01 09:27:24 +01:00
|
|
|
|
|
|
|
// Then select a random reward type. 'Money' will always be the last reward type
|
|
|
|
const reward = getRandomReward();
|
|
|
|
|
|
|
|
// Choose random server
|
|
|
|
const serv = Player.getHomeComputer();
|
|
|
|
|
2021-03-14 07:31:23 +01:00
|
|
|
const contractFn = getRandomFilename(serv, reward);
|
|
|
|
const contract = new CodingContract(contractFn, problemType, reward);
|
2019-02-01 09:27:24 +01:00
|
|
|
|
|
|
|
serv.addContract(contract);
|
|
|
|
}
|
|
|
|
|
2021-03-14 07:31:23 +01:00
|
|
|
export interface IGenerateContractParams {
|
|
|
|
problemType?: string;
|
|
|
|
server?: string;
|
|
|
|
fn?: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function generateContract(params: IGenerateContractParams) {
|
2018-10-27 22:48:33 +02:00
|
|
|
// Problem Type
|
|
|
|
let problemType;
|
|
|
|
const problemTypes = Object.keys(CodingContractTypes);
|
|
|
|
if (params.problemType != null && problemTypes.includes(params.problemType)) {
|
|
|
|
problemType = params.problemType;
|
|
|
|
} else {
|
2019-02-01 09:27:24 +01:00
|
|
|
problemType = getRandomProblemType();
|
2018-10-27 22:48:33 +02:00
|
|
|
}
|
2018-10-27 21:26:22 +02:00
|
|
|
|
2018-10-27 22:48:33 +02:00
|
|
|
// Reward Type - This is always random for now
|
|
|
|
const reward = getRandomReward();
|
|
|
|
|
|
|
|
// Server
|
|
|
|
let server;
|
|
|
|
if (params.server != null) {
|
|
|
|
server = GetServerByHostname(params.server);
|
|
|
|
if (server == null) {
|
2021-03-14 07:31:23 +01:00
|
|
|
server = AllServers[params.server];
|
2018-10-27 22:48:33 +02:00
|
|
|
}
|
|
|
|
if (server == null) {
|
|
|
|
server = getRandomServer();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
server = getRandomServer();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filename
|
|
|
|
let fn;
|
|
|
|
if (params.fn != null) {
|
|
|
|
fn = params.fn;
|
|
|
|
} else {
|
|
|
|
fn = getRandomFilename(server, reward);
|
|
|
|
}
|
|
|
|
|
|
|
|
let contract = new CodingContract(fn, problemType, reward);
|
|
|
|
server.addContract(contract);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensures that a contract's reward type is valid
|
2021-03-14 07:31:23 +01:00
|
|
|
function sanitizeRewardType(rewardType: CodingContractRewardType): CodingContractRewardType {
|
2018-10-27 22:48:33 +02:00
|
|
|
let type = rewardType; // Create copy
|
|
|
|
|
|
|
|
const factionsThatAllowHacking = Player.factions.filter((fac) => {
|
2018-10-27 21:26:22 +02:00
|
|
|
try {
|
|
|
|
return Factions[fac].getInfo().offerHackingWork;
|
|
|
|
} catch (e) {
|
|
|
|
console.error(`Error when trying to filter Hacking Factions for Coding Contract Generation: ${e}`);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
2018-10-27 22:48:33 +02:00
|
|
|
if (type === CodingContractRewardType.FactionReputation && factionsThatAllowHacking.length === 0) {
|
|
|
|
type = CodingContractRewardType.CompanyReputation;
|
2018-10-27 21:26:22 +02:00
|
|
|
}
|
2018-10-27 22:48:33 +02:00
|
|
|
if (type === CodingContractRewardType.FactionReputationAll && factionsThatAllowHacking.length === 0) {
|
|
|
|
type = CodingContractRewardType.CompanyReputation;
|
2018-10-27 21:26:22 +02:00
|
|
|
}
|
2019-01-15 04:34:04 +01:00
|
|
|
if (type === CodingContractRewardType.CompanyReputation && Object.keys(Player.jobs).length === 0) {
|
2018-10-27 22:48:33 +02:00
|
|
|
type = CodingContractRewardType.Money;
|
2018-10-27 21:26:22 +02:00
|
|
|
}
|
|
|
|
|
2018-10-27 22:48:33 +02:00
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
2019-02-01 09:27:24 +01:00
|
|
|
function getRandomProblemType() {
|
|
|
|
const problemTypes = Object.keys(CodingContractTypes);
|
|
|
|
let randIndex = getRandomInt(0, problemTypes.length - 1);
|
|
|
|
|
|
|
|
return problemTypes[randIndex];
|
|
|
|
}
|
|
|
|
|
2021-03-14 07:31:23 +01:00
|
|
|
function getRandomReward(): ICodingContractReward {
|
|
|
|
let reward: ICodingContractReward = {
|
|
|
|
name: "",
|
|
|
|
type: getRandomInt(0, CodingContractRewardType.Money),
|
|
|
|
};
|
2018-10-27 22:48:33 +02:00
|
|
|
reward.type = sanitizeRewardType(reward.type);
|
|
|
|
|
2018-10-27 21:26:22 +02:00
|
|
|
// Add additional information based on the reward type
|
2018-10-27 22:48:33 +02:00
|
|
|
const factionsThatAllowHacking = Player.factions.filter((fac) => {
|
|
|
|
try {
|
|
|
|
return Factions[fac].getInfo().offerHackingWork;
|
|
|
|
} catch (e) {
|
|
|
|
console.error(`Error when trying to filter Hacking Factions for Coding Contract Generation: ${e}`);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-10-27 21:26:22 +02:00
|
|
|
switch (reward.type) {
|
2019-04-13 03:22:46 +02:00
|
|
|
case CodingContractRewardType.FactionReputation: {
|
2018-10-27 21:26:22 +02:00
|
|
|
// Get a random faction that player is a part of. That
|
|
|
|
// faction must allow hacking contracts
|
|
|
|
var numFactions = factionsThatAllowHacking.length;
|
|
|
|
var randFaction = factionsThatAllowHacking[getRandomInt(0, numFactions - 1)];
|
|
|
|
reward.name = randFaction;
|
|
|
|
break;
|
2019-04-13 03:22:46 +02:00
|
|
|
}
|
|
|
|
case CodingContractRewardType.CompanyReputation: {
|
2019-01-15 04:34:04 +01:00
|
|
|
const allJobs = Object.keys(Player.jobs);
|
|
|
|
if (allJobs.length > 0) {
|
|
|
|
reward.name = allJobs[getRandomInt(0, allJobs.length - 1)];
|
2018-10-27 21:26:22 +02:00
|
|
|
} else {
|
|
|
|
reward.type = CodingContractRewardType.Money;
|
|
|
|
}
|
|
|
|
break;
|
2019-04-13 03:22:46 +02:00
|
|
|
}
|
2018-10-27 21:26:22 +02:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-10-27 22:48:33 +02:00
|
|
|
return reward;
|
|
|
|
}
|
|
|
|
|
2021-03-14 07:31:23 +01:00
|
|
|
function getRandomServer(): Server | HacknetServer {
|
2018-10-27 21:26:22 +02:00
|
|
|
const servers = Object.keys(AllServers);
|
2018-10-27 22:48:33 +02:00
|
|
|
let randIndex = getRandomInt(0, servers.length - 1);
|
|
|
|
let randServer = AllServers[servers[randIndex]];
|
|
|
|
|
|
|
|
// An infinite loop shouldn't ever happen, but to be safe we'll use
|
|
|
|
// a for loop with a limited number of tries
|
|
|
|
for (let i = 0; i < 200; ++i) {
|
2021-03-14 07:31:23 +01:00
|
|
|
if (randServer instanceof Server && !randServer.purchasedByPlayer && randServer.hostname !== SpecialServerNames.WorldDaemon) {
|
2019-04-30 05:54:20 +02:00
|
|
|
break;
|
|
|
|
}
|
2018-10-27 21:26:22 +02:00
|
|
|
randIndex = getRandomInt(0, servers.length - 1);
|
|
|
|
randServer = AllServers[servers[randIndex]];
|
|
|
|
}
|
|
|
|
|
2018-10-27 22:48:33 +02:00
|
|
|
return randServer;
|
|
|
|
}
|
|
|
|
|
2021-03-14 07:31:23 +01:00
|
|
|
function getRandomFilename(server: Server | HacknetServer, reward: ICodingContractReward): string {
|
2018-10-27 21:26:22 +02:00
|
|
|
let contractFn = `contract-${getRandomInt(0, 1e6)}`;
|
2018-10-27 22:48:33 +02:00
|
|
|
|
|
|
|
for (let i = 0; i < 1000; ++i) {
|
2021-03-14 07:31:23 +01:00
|
|
|
if (server.contracts.filter((c: CodingContract) => {return c.fn === contractFn}).length <= 0) { break; }
|
2018-10-27 21:26:22 +02:00
|
|
|
contractFn = `contract-${getRandomInt(0, 1e6)}`;
|
|
|
|
}
|
2018-10-27 22:48:33 +02:00
|
|
|
|
2018-10-27 21:26:22 +02:00
|
|
|
if (reward.name) { contractFn += `-${reward.name.replace(/\s/g, "")}`; }
|
|
|
|
|
2018-10-27 22:48:33 +02:00
|
|
|
return contractFn;
|
2018-10-27 21:26:22 +02:00
|
|
|
}
|