-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspeedrun.suite.js
148 lines (145 loc) · 5.74 KB
/
speedrun.suite.js
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
'use strict';
const speedrunSuite = {
run: function (args) {
runSuite(args);
}
}
const runSuite = function (args) {
args = getArgs(args);
const speedrunData = getSpeedrunData();
if (speedrunData.roomName == undefined)
return;
const rclData = getRclData(speedrunData.roomName);
trackRclProgress(speedrunData.roomName, args);
const data = prepareData(rclData);
displayData(args, data);
}
const getSpeedrunData = function () {
if (!global.speedrunData) {
global.speedrunData = {};
for (const roomName of Object.keys(Game.rooms)) {
if (Game.rooms[roomName].controller && Game.rooms[roomName].controller.my) {
global.speedrunData.roomName = roomName;
break;
}
}
global.setup = true;
}
return global.speedrunData;
}
const getRclData = function (roomName) {
const data = global.rclData || Memory.rclData && JSON.parse(Memory.rclData) || {},
room = Game.rooms[roomName], controller = room.controller;
let update = false;
if (data[1] == undefined) {
for (let x = 1; x <= controller.level; x++) {
data[x] = Game.time;
}
update = true;
}
if (data[controller.level] == undefined) {
data[controller.level] = Game.time;
update = true;
}
if (update) {
global.rclData = data;
Memory.rclData = JSON.stringify(data);
}
return data;
}
const trackRclProgress = function (roomName, args) {
const controller = Game.rooms[roomName].controller;
if (!global.rclProgress || global.rclProgress.level != controller.level) {
global.rclProgress = {
level: controller.level,
progress: [],
last: controller.progress,
avg: 0
};
}
global.rclProgress.progress.push(controller.progress - global.rclProgress.last);
global.rclProgress.last = controller.progress;
while (global.rclProgress.progress.length > args.avgDuration)
global.rclProgress.progress.splice(0, 1);
let avg = 0;
for (let x = 0; x < global.rclProgress.progress.length; x++) {
avg += global.rclProgress.progress[x];
}
global.rclProgress.avg = avg / (global.rclProgress.progress.length || 1);
}
const prepareData = function (rclData) {
const data = {}, max = Object.keys(rclData).length;
data.spawned = rclData[1];
data.max = max;
for (let x = 1; x <= max; x++) {
data[x] = {
tick: rclData[x],
time: rclData[x] - rclData[x - 1],
};
}
return data;
}
const displayData = function (args, data) {
const visual = new RoomVisual(), x = args.position.x, y = args.position.y, stop = Math.min(args.showPastMax ? Infinity : args.maxRCL, data.max);
let line = y;
visual.text("Speedrun Statistics:", x, line++, { align: "left", color: "#00CCFF" });
for (let c = 2; c <= stop; c++) {
let string = "RCL " + c + ": " + delimit(data[c].time) + " (" + delimit(data[c].tick - data.spawned) + ")";
string += " - Avg. CP: " + (Math.floor(CONTROLLER_LEVELS[c - 1] / data[c].time * 10) / 10) + "e/t";
visual.text(string, x, line++, { align: "left" });
}
// Current (to next) RCL
if (data.max + 1 < args.maxRCL) {
const nextRCL = data.max + 1, controller = Game.rooms[global.speedrunData.roomName].controller, toRcl = controller.progressTotal - controller.progress,
ept = Math.floor(global.rclProgress.avg * 10) / 10, tickEstimate = Game.time + Math.floor(toRcl / ept), tPlusEstimate = tickEstimate - Game.time;
visual.text("Next RCL: " + nextRCL, x, line++, { align: "left", color: "#AACCFF" });
visual.text("Progress: " + delimit(controller.progress) + " / " + delimit(controller.progressTotal) + " (" + (Math.floor(controller.progress / controller.progressTotal * 1000) / 10) + "%)", x, line++, { align: "left" });
visual.text("Elapsed: " + delimit(Game.time - data[data.max].tick) + " (" + delimit(Game.time - data.spawned) + ")", x, line++, { align: "left" });
visual.text("Estimated RCL: " + (tPlusEstimate == Infinity ? "No Data" : "T+" + delimit(tPlusEstimate) +
" (" + delimit(tickEstimate - data.spawned) + ")"), x, line++, { align: "left" });
visual.text("Avg. Control Points: " + ept + "e/t", x, line++, { align: "left" });
}
// GOAL
// visual.text("GOAL (RCL " + args.maxRCL + "):", x, line++, { align: "left", color: "#99FFFF" });
// visual.text("RCL")
}
const getArgs = function (args = {}) {
args.startTime = args.startTime || Memory.startTime || Game.time;
args.position = args.position || { x: 1, y: 1 };
args.avgDuration = args.avgDuration || 100;
args.maxRCL = args.maxRCL || 8;
args.showPastMax = args.showPastMax || false;
return args;
}
const delimit = function (number, digit = 3) {
const dec = number % 1;
number = number.toString();
let newNumber = "", post = "";
if (dec) {
const pre = number;
number = "";
for (const x in pre) {
if (pre[x] == ".") {
for (let y = +x + 1; y < pre.length; y++) {
post += pre[y];
}
break;
}
else {
number += pre[x];
}
}
}
const offset = number.length % digit;
for (const x in number) {
const num = number[x];
newNumber += (x > 0 && x % digit == offset ? "," : "") + num;
}
if (post)
newNumber += "." + post;
return newNumber;
}
const tickToClocktime = function (ticks) {
return ticks;
}
module.exports = speedrunSuite;