2021-09-09 05:47:34 +02:00
|
|
|
import { codingContractTypesMetadata, DescriptionFunc, GeneratorFunc, SolverFunc } from "./data/codingcontracttypes";
|
2019-04-11 10:37:40 +02:00
|
|
|
|
|
|
|
import { IMap } from "./types";
|
|
|
|
|
2021-09-25 20:42:57 +02:00
|
|
|
import { Generic_fromJSON, Generic_toJSON, Reviver } from "./utils/JSONReviver";
|
2021-10-01 19:08:37 +02:00
|
|
|
import { CodingContractEvent } from "./ui/React/CodingContractModal";
|
2021-05-04 01:46:04 +02:00
|
|
|
|
2018-09-14 23:03:31 +02:00
|
|
|
/* tslint:disable:no-magic-numbers completed-docs max-classes-per-file no-console */
|
|
|
|
|
|
|
|
/* Represents different types of problems that a Coding Contract can have */
|
2018-09-16 10:55:54 +02:00
|
|
|
export class CodingContractType {
|
2021-09-05 01:09:30 +02:00
|
|
|
/**
|
|
|
|
* Function that generates a description of the problem
|
|
|
|
*/
|
|
|
|
desc: DescriptionFunc;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Number that generally represents the problem's difficulty. Bigger numbers = harder
|
|
|
|
*/
|
|
|
|
difficulty: number;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A function that randomly generates a valid 'data' for the problem
|
|
|
|
*/
|
|
|
|
generate: GeneratorFunc;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Name of the type of problem
|
|
|
|
*/
|
|
|
|
name: string;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The maximum number of tries the player gets on this kind of problem before it self-destructs
|
|
|
|
*/
|
|
|
|
numTries: number;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stores a function that checks if the provided answer is correct
|
|
|
|
*/
|
|
|
|
solver: SolverFunc;
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
name: string,
|
|
|
|
desc: DescriptionFunc,
|
|
|
|
gen: GeneratorFunc,
|
|
|
|
solver: SolverFunc,
|
|
|
|
diff: number,
|
|
|
|
numTries: number,
|
|
|
|
) {
|
|
|
|
this.name = name;
|
|
|
|
this.desc = desc;
|
|
|
|
this.generate = gen;
|
|
|
|
this.solver = solver;
|
|
|
|
this.difficulty = diff;
|
|
|
|
this.numTries = numTries;
|
|
|
|
}
|
2018-09-14 23:03:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Contract Types */
|
|
|
|
// tslint:disable-next-line
|
2018-09-16 10:55:54 +02:00
|
|
|
export const CodingContractTypes: IMap<CodingContractType> = {};
|
2018-09-14 23:03:31 +02:00
|
|
|
|
|
|
|
for (const md of codingContractTypesMetadata) {
|
2021-09-05 01:09:30 +02:00
|
|
|
// tslint:disable-next-line
|
|
|
|
CodingContractTypes[md.name] = new CodingContractType(
|
|
|
|
md.name,
|
|
|
|
md.desc,
|
|
|
|
md.gen,
|
|
|
|
md.solver,
|
|
|
|
md.difficulty,
|
|
|
|
md.numTries,
|
|
|
|
);
|
2018-09-14 23:03:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enum representing the different types of rewards a Coding Contract can give
|
|
|
|
*/
|
|
|
|
export enum CodingContractRewardType {
|
2021-09-05 01:09:30 +02:00
|
|
|
FactionReputation,
|
|
|
|
FactionReputationAll,
|
|
|
|
CompanyReputation,
|
|
|
|
Money, // This must always be the last reward type
|
2018-09-14 23:03:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enum representing the result when trying to solve the Contract
|
|
|
|
*/
|
|
|
|
export enum CodingContractResult {
|
2021-09-05 01:09:30 +02:00
|
|
|
Success,
|
|
|
|
Failure,
|
|
|
|
Cancelled,
|
2018-09-14 23:03:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A class that represents the type of reward a contract gives
|
|
|
|
*/
|
|
|
|
export interface ICodingContractReward {
|
2021-09-05 01:09:30 +02:00
|
|
|
/* Name of Company/Faction name for reward, if applicable */
|
|
|
|
name?: string;
|
|
|
|
type: CodingContractRewardType;
|
2018-09-14 23:03:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A Coding Contract is a file that poses a programming-related problem to the Player.
|
|
|
|
* The player receives a reward if the problem is solved correctly
|
|
|
|
*/
|
|
|
|
export class CodingContract {
|
2021-09-05 01:09:30 +02:00
|
|
|
/* Relevant data for the contract's problem */
|
|
|
|
data: any;
|
2018-09-14 23:03:31 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
/* Contract's filename */
|
|
|
|
fn: string;
|
2018-09-14 23:03:31 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
/* Describes the reward given if this Contract is solved. The reward is actually
|
2018-09-14 23:03:31 +02:00
|
|
|
processed outside of this file */
|
2021-09-05 01:09:30 +02:00
|
|
|
reward: ICodingContractReward | null;
|
|
|
|
|
|
|
|
/* Number of times the Contract has been attempted */
|
|
|
|
tries = 0;
|
|
|
|
|
|
|
|
/* String representing the contract's type. Must match type in ContractTypes */
|
|
|
|
type: string;
|
|
|
|
|
2021-09-09 05:47:34 +02:00
|
|
|
constructor(fn = "", type = "Find Largest Prime Factor", reward: ICodingContractReward | null = null) {
|
2021-09-05 01:09:30 +02:00
|
|
|
this.fn = fn;
|
|
|
|
if (!this.fn.endsWith(".cct")) {
|
|
|
|
this.fn += ".cct";
|
2018-09-14 23:03:31 +02:00
|
|
|
}
|
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
// tslint:disable-next-line
|
|
|
|
if (CodingContractTypes[type] == null) {
|
2021-09-09 05:47:34 +02:00
|
|
|
throw new Error(`Error: invalid contract type: ${type} please contact developer`);
|
2018-09-14 23:03:31 +02:00
|
|
|
}
|
2021-05-01 09:17:31 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
this.type = type;
|
|
|
|
this.data = CodingContractTypes[type].generate();
|
|
|
|
this.reward = reward;
|
|
|
|
}
|
|
|
|
|
|
|
|
getData(): any {
|
|
|
|
return this.data;
|
|
|
|
}
|
|
|
|
|
|
|
|
getDescription(): string {
|
|
|
|
return CodingContractTypes[this.type].desc(this.data);
|
|
|
|
}
|
|
|
|
|
|
|
|
getDifficulty(): number {
|
|
|
|
return CodingContractTypes[this.type].difficulty;
|
|
|
|
}
|
|
|
|
|
|
|
|
getMaxNumTries(): number {
|
|
|
|
return CodingContractTypes[this.type].numTries;
|
|
|
|
}
|
|
|
|
|
|
|
|
getType(): string {
|
|
|
|
return CodingContractTypes[this.type].name;
|
|
|
|
}
|
|
|
|
|
|
|
|
isSolution(solution: string): boolean {
|
|
|
|
return CodingContractTypes[this.type].solver(this.data, solution);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a popup to prompt the player to solve the problem
|
|
|
|
*/
|
|
|
|
async prompt(): Promise<CodingContractResult> {
|
|
|
|
return new Promise<CodingContractResult>((resolve) => {
|
2021-10-01 19:08:37 +02:00
|
|
|
const props = {
|
|
|
|
c: this,
|
|
|
|
onClose: () => {
|
|
|
|
resolve(CodingContractResult.Cancelled);
|
2021-09-05 01:09:30 +02:00
|
|
|
},
|
2021-10-01 19:08:37 +02:00
|
|
|
onAttempt: (val: string) => {
|
|
|
|
if (this.isSolution(val)) {
|
|
|
|
resolve(CodingContractResult.Success);
|
|
|
|
} else {
|
|
|
|
resolve(CodingContractResult.Failure);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
CodingContractEvent.emit(props);
|
2021-09-05 01:09:30 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Serialize the current file to a JSON save state.
|
|
|
|
*/
|
|
|
|
toJSON(): any {
|
|
|
|
return Generic_toJSON("CodingContract", this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initiatizes a CodingContract from a JSON save state.
|
|
|
|
*/
|
|
|
|
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
|
|
|
static fromJSON(value: any): CodingContract {
|
|
|
|
return Generic_fromJSON(CodingContract, value.data);
|
|
|
|
}
|
2018-09-14 23:03:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Reviver.constructors.CodingContract = CodingContract;
|