-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathvoyagers.ts
174 lines (158 loc) · 5.82 KB
/
voyagers.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
DataCore(<VoyageTool>): input from UI =>
VoyagersAssemble(): lineups =>
EstimateLineups(): estimates =>
SortLineups(): lineups, estimates =>
DataCore(<VoyageTool>) { updateUI } : void
*/
import { IVoyageCrew, IVoyageInputConfig } from '../model/voyage';
import { Estimate, JohnJayBest, Refill } from '../model/worker';
import { ILineupEstimate, ISkillAggregate } from './voyagers/model';
import { VoyagersLineup } from './voyagers/lineup';
import { voyagersAssemble } from './voyagers/assembler';
import { estimateLineups } from './voyagers/estimator';
import { sortLineups } from './voyagers/sorter';
const DEBUGGING: boolean = false;
type InputType = {
voyage_description: IVoyageInputConfig;
roster: IVoyageCrew[];
bestShip: {
score: number;
};
options: {
assembler: string;
strategy: string;
scanDepth: number;
maxYield: number;
};
};
type OutputType = (result: (JohnJayBest[] | { error: string }), inProgress?: boolean) => void;
type ChewableType = (config: any, reportProgress?: () => boolean) => Estimate;
const VoyagersWorker = (input: InputType, output: OutputType, chewable: ChewableType) => {
const { voyage_description, roster, bestShip, options: { assembler, strategy } } = input;
const debugCallback: ((message: string) => void) | undefined = DEBUGGING ? (message: string) => console.log(message) : undefined;
// Generate lots of unique lineups of potential voyagers
voyagersAssemble(assembler, voyage_description, roster, { strategy, debugCallback })
.then(lineups => {
// Estimate only as many lineups as necessary
const scanDepth: number = assembler === 'idic' ? 30 : 5;
estimateLineups(datacoreEstimator, lineups, voyage_description, bestShip.score, { strategy, scanDepth, debugCallback })
.then(estimates => {
// Return only the best lineups by requested strategy
let methods: string[] = ['estimate', 'minimum', 'moonshot'];
if (strategy === 'estimate')
methods = ['estimate'];
else if (strategy === 'minimum')
methods = ['minimum'];
else if (strategy === 'moonshot')
methods = ['moonshot'];
// Either get 1 best lineup for each method, or the 3 best lineups for a single method
const limit: number = strategy === 'any' ? 1 : 3;
sortLineups(datacoreSorter, lineups, estimates, methods)
.then(sorted => {
output(JSON.parse(JSON.stringify(sorted)), false);
});
});
})
.catch(error => {
output({ error: `${error}` });
});
function datacoreEstimator(lineup: VoyagersLineup): Promise<ILineupEstimate> {
const SKILLS: string[] = [
'command_skill',
'science_skill',
'security_skill',
'engineering_skill',
'diplomacy_skill',
'medicine_skill'
];
let ps: ISkillAggregate | undefined = undefined;
let ss: ISkillAggregate | undefined = undefined;
const others: ISkillAggregate[] = [];
for (let iSkill = 0; iSkill < SKILLS.length; iSkill++) {
const aggregate: ISkillAggregate = lineup.skills[SKILLS[iSkill]];
if (SKILLS[iSkill] === voyage_description.skills.primary_skill)
ps = aggregate;
else if (SKILLS[iSkill] === voyage_description.skills.secondary_skill)
ss = aggregate;
else
others.push(aggregate);
}
const chewableConfig = {
ps, ss, others,
startAm: input.bestShip.score + lineup.antimatter,
prof: lineup.proficiency,
noExtends: false, // Set to true to show estimate with no refills
numSims: 5000
};
// Increase confidence of estimates for thorough, marginal strategies
if (['thorough', 'minimum', 'moonshot'].includes(strategy))
chewableConfig.numSims = 10000;
return new Promise((resolve, reject) => {
const estimate: Estimate = chewable(chewableConfig, () => false);
// Add antimatter prop here to allow for post-sorting by AM
estimate.antimatter = input.bestShip.score + lineup.antimatter;
resolve({ estimate, key: lineup.key });
});
}
function datacoreSorter(a: ILineupEstimate, b: ILineupEstimate, method: string = 'estimate'): number {
const DIFFERENCE: number = 0.02; // ~1 minute
const aEstimate: Refill = a.estimate.refills[0];
const bEstimate: Refill = b.estimate.refills[0];
// Best Median Runtime by default
let aScore: number = aEstimate.result;
let bScore: number = bEstimate.result;
let compareCloseTimes: boolean = false;
// Best Guaranteed Minimum
// Compare 99% worst case times (saferResult)
if (method === 'minimum') {
aScore = aEstimate.saferResult;
bScore = bEstimate.saferResult;
}
// Best Moonshot
// Compare best case times (moonshotResult)
else if (method === 'moonshot') {
compareCloseTimes = true;
aScore = aEstimate.moonshotResult;
bScore = bEstimate.moonshotResult;
// If times are close enough, use the one with the better median result
if (Math.abs(bScore - aScore) <= DIFFERENCE) {
aScore = aEstimate.result;
bScore = bEstimate.result;
}
}
// Best Dilemma Chance
else if (method === 'dilemma') {
aScore = aEstimate.lastDil;
bScore = bEstimate.lastDil;
if (aScore === bScore) {
aScore = aEstimate.dilChance;
bScore = bEstimate.dilChance;
}
// If dilemma chance is the same, use the one with the better median result
if (aScore === bScore) {
compareCloseTimes = true;
aScore = aEstimate.result;
bScore = bEstimate.result;
}
}
// Highest Antimatter
else if (method === 'antimatter') {
aScore = a.estimate.antimatter ?? 0;
bScore = b.estimate.antimatter ?? 0;
// If antimatter is the same, use the one with the better median result
if (aScore === bScore) {
compareCloseTimes = true;
aScore = aEstimate.result;
bScore = bEstimate.result;
}
}
// If times are close enough, use the one with the better safer result
if (compareCloseTimes && Math.abs(bScore - aScore) <= DIFFERENCE) {
aScore = aEstimate.saferResult;
bScore = bEstimate.saferResult;
}
return bScore - aScore;
}
};
export default VoyagersWorker;