forked from quisquous/cactbot
-
Notifications
You must be signed in to change notification settings - Fork 45
/
mistake_collector.ts
175 lines (154 loc) · 5.28 KB
/
mistake_collector.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
import { callOverlayHandler } from '../../resources/overlay_plugin_api';
import { EventResponses } from '../../types/event';
import { OopsyMistake } from '../../types/oopsy';
import { MistakeObserver, ViewEvent } from './mistake_observer';
import { OopsyOptions } from './oopsy_options';
const broadcastSource = 'oopsyraidsy';
const msgSyncRequestType = 'SyncRequest';
const msgSyncResponseType = 'SyncResponse';
// MistakeForwarder forwards observer calls to all observers.
// It also collects all events in case a broadcast sync is requested.
export class MistakeCollector implements MistakeObserver {
private observers: MistakeObserver[] = [];
private events: ViewEvent[] = [];
private creationTime = Date.now();
private latestSyncTimestamp?: number;
constructor(private options: OopsyOptions, private shouldSync: boolean) {
this.AddObserver(this);
this.RequestSync();
}
private DebugPrint(str: string): void {
if (this.options.Debug)
console.error(str);
}
private RequestSync(): void {
if (!this.shouldSync)
return;
this.DebugPrint(`RequestSync: ${this.creationTime}`);
void callOverlayHandler({
call: 'broadcast',
source: broadcastSource,
msg: {
type: msgSyncRequestType,
id: this.creationTime,
timestamp: this.creationTime,
},
});
}
private SendSyncResponse(): void {
if (!this.shouldSync)
return;
this.DebugPrint(`SendSyncResponse: ${this.creationTime}`);
void callOverlayHandler({
call: 'broadcast',
source: broadcastSource,
msg: {
type: msgSyncResponseType,
id: this.creationTime,
timestamp: this.creationTime,
data: JSON.stringify(this.events),
},
});
}
private ReceiveSyncResponse(timestamp: number, data: string): void {
this.DebugPrint(`ReceiveSyncResponse: ${timestamp} (prev: ${this.latestSyncTimestamp ?? ''})`);
this.latestSyncTimestamp = timestamp;
try {
const parsed = JSON.parse(data) as unknown;
if (!Array.isArray(parsed)) {
console.error('Malformed sync response');
return;
}
// TODO: giant hacky type assertion here because type guarding this seems complicated.
// TODO: maybe there's some automated tooling we could use for this?
const events: ViewEvent[] = parsed as ViewEvent[];
for (const observer of this.observers)
observer.OnSyncEvents(events);
} catch (e) {
console.error(e);
}
}
OnBroadcastMessage(e: EventResponses['BroadcastMessage']): void {
if (!this.shouldSync)
return;
if (e.source !== broadcastSource)
return;
const msg = e.msg;
if (msg === null || typeof msg !== 'object')
return;
// Turn an unknown into an indexable object.
// TODO: is there some better way to do this?
const obj: { [key: string]: unknown } = {};
for (const [key, value] of Object.entries(msg ?? {}))
obj[key] = value;
// Ignore messages from ourselves.
// TODO: do we actually receive broadcast messages from ourselves, if subscribed?
if (obj.id === this.creationTime || obj.id === undefined)
return;
if (obj.type === msgSyncRequestType) {
// If this collector was created after this timestamp request, ignore it.
if (typeof obj.timestamp !== 'number' || obj.timestamp < this.creationTime) {
this.DebugPrint(
`OnBroadcastMessage: ignoring: (past creation): ${obj.timestamp as string}`,
);
return;
}
this.SendSyncResponse();
} else if (obj.type === msgSyncResponseType) {
if (typeof obj.timestamp !== 'number')
return;
// If we have data from further in the past, don't overwrite with partial future data.
if (this.latestSyncTimestamp && this.latestSyncTimestamp <= obj.timestamp) {
this.DebugPrint(`OnBroadcastMessage: ignoring (past data): ${obj.timestamp}`);
return;
}
const data = obj.data;
if (typeof data === 'string')
this.ReceiveSyncResponse(obj.timestamp, data);
}
}
OnEvent(event: ViewEvent): void {
this.events.push(event);
}
OnSyncEvents(events: ViewEvent[]): void {
// Clobber our current set of events with synced events.
//
// TODO: there could be some raciness here where if you open up the summary
// mid-fight, then an event could get dropped that occurred after the sync
// request but before the sync response was received. This is not worth
// solving at the moment though.
this.events = events;
}
AddObserver(observer: MistakeObserver): void {
this.observers.push(observer);
}
OnMistakeObj(timestamp: number, m?: OopsyMistake): void {
if (!m)
return;
for (const observer of this.observers) {
observer.OnEvent({
timestamp: timestamp,
type: 'Mistake',
mistake: m,
});
}
}
StartEncounter(timestamp: number): void {
for (const observer of this.observers) {
observer.OnEvent({
timestamp: timestamp,
type: 'StartEncounter',
});
}
}
OnChangeZone(timestamp: number, zoneName: string, zoneId: number): void {
for (const observer of this.observers) {
observer.OnEvent({
timestamp: timestamp,
type: 'ChangeZone',
zoneName: zoneName,
zoneId: zoneId,
});
}
}
}