-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathcontract_seasons.ts
177 lines (163 loc) · 6.76 KB
/
contract_seasons.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
175
176
177
import { ei } from './proto';
export interface ContractSeasonProgress {
id: string;
name: string;
startTime: number;
availablePE: number;
completedPE: number;
startingGrade: ei.Contract.PlayerGrade;
totalCxp: number;
cxpLastRewardGiven: number;
goals: ei.IContractSeasonGoal[];
}
// Convert a season ID into a number: winter_2025 => 2025.0, spring_2025 = 2025.25, etc.
function parseSeasonId(seasonId: string): number {
// We expect format season_year
const split = seasonId.split('_');
if (split.length == 2) {
let year = parseInt(split[1]);
if (isNaN(year)) return -1;
switch (split[0].toLowerCase()) {
case 'winter': break;
case 'spring': year += 0.25; break;
case 'summer': year += 0.50; break;
case 'fall': year += 0.75; break;
}
return year;
}
return -1;
}
// Construct a dummy season contract grade goal with a PE and a season max reward
function singlePEGradeGoal(
grade: ei.Contract.PlayerGrade,
peCxp: number,
maxCxp: number
): ei.ContractSeasonInfo.IGoalSet {
return {
grade,
goals: [
{ cxp: peCxp, rewardType: ei.RewardType.EGGS_OF_PROPHECY, rewardAmount: 1 },
{ cxp: maxCxp, rewardType: ei.RewardType.UNKNOWN_REWARD, rewardAmount: 0 },
],
};
}
// Estimate each season is 13 weeks. Compute from the Winter 2025 start time.
const winter2025StartTime = 1734969600;
const secondsIn52Weeks = 52 * 7 * 24 * 60 * 60;
function estimateSeasonStartTime(year: number): number {
if (year <= 0) {
// We couldn't parse a year to use
return 0;
}
const yearsAfterWinter2025 = year - 2025;
return winter2025StartTime + yearsAfterWinter2025 * secondsIn52Weeks;
}
// Default contract season info to use if we're missing one, based on Winter 2025.
// PE goal only - we'll assume each season has one PE.
const defaultContractSeason: ei.IContractSeasonInfo = {
startTime: winter2025StartTime,
gradeGoals: [
singlePEGradeGoal(ei.Contract.PlayerGrade.GRADE_C, 15000, 30000),
singlePEGradeGoal(ei.Contract.PlayerGrade.GRADE_B, 40000, 80000),
singlePEGradeGoal(ei.Contract.PlayerGrade.GRADE_A, 98000, 196000),
singlePEGradeGoal(ei.Contract.PlayerGrade.GRADE_AA, 175000, 300000),
singlePEGradeGoal(ei.Contract.PlayerGrade.GRADE_AAA, 315000, 630000),
],
};
function getDefaultSeasonForYear(year: number): ei.IContractSeasonInfo {
if (year < 2025) {
// No rewards for pre-2025 seasons
return {};
}
return {
...defaultContractSeason,
startTime: estimateSeasonStartTime(year),
};
}
export function getContractSeasonProgressData(
backup: ei.IBackup,
contractSeasons?: ei.IContractSeasonInfo[]
): ContractSeasonProgress[] {
const rewardSeasonIds = new Set<string>();
// Collect the player's progress per season.
// If we have a cxpLastRewardGiven value then it's a contract season with rewards.
// Track the best contract grade we've seen too, in case we need it later.
let pastStartingGrade: ei.Contract.PlayerGrade = ei.Contract.PlayerGrade.GRADE_C;
const seasonProgressById: { [ id: string ]: ei.ContractPlayerInfo.ISeasonProgress } = {};
const seasonProgressData = backup.contracts?.lastCpi?.seasonProgress || [];
for (const seasonProgress of seasonProgressData) {
if (seasonProgress.seasonId != null) {
seasonProgressById[seasonProgress.seasonId] = seasonProgress;
if (seasonProgress.cxpLastRewardGiven != null) {
rewardSeasonIds.add(seasonProgress.seasonId);
}
if (seasonProgress.startingGrade != null && seasonProgress.startingGrade > pastStartingGrade) {
pastStartingGrade = seasonProgress.startingGrade;
}
}
}
// Contract seasons with rewards by ID
const contractSeasonsById: { [ id: string ]: ei.IContractSeasonInfo } = {};
for (const season of contractSeasons || []) {
if (season.id != null) {
contractSeasonsById[season.id] = season;
// Do we have a non-empty list of goals for any grade?
if (season.gradeGoals != null && season.gradeGoals.some(
gradeGoal => Array.isArray(gradeGoal.goals) && gradeGoal.goals.length > 0)) {
rewardSeasonIds.add(season.id);
}
}
}
// Combine the reward seasons from both lists.
// - If we have both season progress and a season definition, count available & completed eggs from the goals.
// Take the max number of PEs from any grade's goals (although we'd expect them to all be the same).
// - If we have a season definition but no progress, count available eggs but assume none completed. Use the
// highest contract grade we saw in other season progress, else grade C.
// - If we have progress for a season after Winter 2025 but no season definition, assume it has a single PE
// with the same CXP requirements as Winter 2025. If it's before Winter 2025 assume no PEs.
const result: ContractSeasonProgress[] = [];
for (const seasonId of rewardSeasonIds) {
const seasonYear = parseSeasonId(seasonId);
const contractSeason = contractSeasonsById[seasonId] ?? getDefaultSeasonForYear(seasonYear);
const seasonProgress = seasonProgressById[seasonId];
let availablePE = 0;
let completedPE = 0;
let goals: ei.IContractSeasonGoal[] = [];
let startingGrade: ei.Contract.PlayerGrade = ei.Contract.PlayerGrade.GRADE_UNSET;
if (contractSeason.gradeGoals != null) {
// Maximum number of PEs available for any grade
const allGradesPECounts = contractSeason.gradeGoals.map(gradeGoal =>
(gradeGoal.goals || [])
.filter(goal => goal.rewardType === ei.RewardType.EGGS_OF_PROPHECY)
.reduce((total, goal) => total + (goal.rewardAmount ?? 1), 0)
) ?? [0];
availablePE = Math.max(...allGradesPECounts);
startingGrade = seasonProgress?.startingGrade ?? pastStartingGrade;
goals = contractSeason.gradeGoals.find(gradeGoal => gradeGoal?.grade === startingGrade)?.goals ?? [];
// PEs obtained
if (seasonProgress != null) {
const cxpLastRewardGiven = seasonProgress.cxpLastRewardGiven ?? seasonProgress.totalCxp ?? 0;
if (cxpLastRewardGiven > 0) {
completedPE = goals
.filter(goal => goal.cxp != null && goal.cxp <= cxpLastRewardGiven &&
goal.rewardType === ei.RewardType.EGGS_OF_PROPHECY)
.reduce((total, goal) => total + (goal.rewardAmount ?? 1), 0);
}
}
}
const contractSeasonProgress: ContractSeasonProgress = {
id: seasonId,
name: contractSeason.name ?? seasonId,
startTime: contractSeason.startTime ?? estimateSeasonStartTime(seasonYear),
availablePE,
completedPE,
startingGrade,
totalCxp: seasonProgress?.totalCxp ?? 0,
cxpLastRewardGiven: seasonProgress?.cxpLastRewardGiven ?? 0,
goals,
};
result.push(contractSeasonProgress);
}
result.sort((a, b) => (a.startTime - b.startTime));
return result;
}