-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathvisualize.ts
306 lines (254 loc) · 10.9 KB
/
visualize.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
const goButton = document.getElementById("goButton") as HTMLButtonElement;
const personIcon = new Image();
personIcon.src = "images/person-icon.png";
const IMAGE_WIDTH = 25;
const IMAGE_HEIGHT = 25;
const SELLER_WIDTH = 22;
const SCANNER_WIDTH = 10;
// -------------------------
// DEFINITIONS
// -------------------------
type Coords = { x: number; y: number };
// Format of the JSON event data
interface ReplayData {
sellerLines: number;
scannerLines: number;
events: Array<SimEvent>;
}
interface SimEvent {
event: "BUS_ARRIVAL" | "WALK_TO_SELLER" | "WAIT_IN_SELLER_LINE" | "BUY_TICKETS" | "WALK_TO_SCANNER" | "WAIT_IN_SCANNER_LINE" | "SCAN_TICKETS";
time: number;
// BUS_ARRIVAL
busId?: number;
peopleCreated?: Array<number>;
// All of the following seller and scanner events specify a duration
duration?: number;
// WALK_TO_SELLER, WAIT_IN_SELLER_LINE, BUY_TICKETS
people?: Array<number>;
sellerLine?: number;
// WALK_TO_SCANNER, WAIT_IN_SCANNER_LINE, SCAN_TICKETS
person?: number;
scannerLine?: number;
}
interface SimEventWithId extends SimEvent {
id: number;
}
// Events always take place in this order
const EventTypeWeight = {
BUS_ARRIVAL: 0,
WALK_TO_SELLER: 1,
WAIT_IN_SELLER_LINE: 2,
BUY_TICKETS: 3,
WALK_TO_SCANNER: 4,
WAIT_IN_SCANNER_LINE: 5,
SCAN_TICKETS: 6,
};
const SortAndInjectSequentialIDs = (events: Array<SimEvent>): Array<SimEventWithId> => {
events = events.sort((a, b) => (a.time === b.time ? EventTypeWeight[a.event] - EventTypeWeight[b.event] : a.time - b.time));
let id = 0;
return events.map((e) => Object.assign({ id: id++ }, e) as SimEventWithId);
};
type PeopleInLine = {
begin: number;
end: number;
};
// -------------------------
// CANVAS OBJECTS
// -------------------------
class Queue {
static DISTANCE: number = 20;
private waiting: { [key: string]: PeopleInLine } = {};
constructor(private queueType: string, private num: number, private xy: Coords, private width: number) {}
draw(ctx: CanvasRenderingContext2D, now: number) {
ctx.fillText(`${this.queueType} #${this.num}`, this.xy.x, this.xy.y);
}
registerPeople(people: Array<number>, peopleInLine: PeopleInLine) {
this.waiting[people.sort().join(",")] = peopleInLine;
}
removePeople(people: Array<number>) {
delete this.waiting[people.sort().join(",")];
}
peopleWaiting(now: number): Array<string> {
return Object.keys(this.waiting).filter((k) => this.waiting[k].end < now);
}
positionFromFrontOfLine(now: number, people: Array<number>): number {
const keys = Object.keys(this.waiting).filter((k) => this.waiting[k].end < now);
const ordered = keys.sort((a, b) => this.waiting[b].end - this.waiting[a].end);
return ordered.length - ordered.indexOf(people.sort().join(","));
}
queueBegin(now: number): Coords {
const keys = Object.keys(this.waiting).filter((k) => this.waiting[k].end < now);
return { x: this.xy.x - 5 - keys.length * this.width - this.width, y: this.xy.y + 15 };
}
get queueTerminus(): Coords {
return { x: this.xy.x - 5, y: this.xy.y + 15 };
}
get serviceLocation(): Coords {
return { x: this.xy.x + 5, y: this.xy.y + 15 };
}
}
class Person {
static START_AT: Coords = { x: 100, y: 700 };
static GROUP_SPACING = 6;
private events: Array<SimEvent> = [];
private positionInGroup: number = 0;
private lastWalkingStart: Coords = { x: Person.START_AT.x, y: Person.START_AT.y };
constructor(private id: number, private sellerQueues: Array<Queue>, private scannerQueues: Array<Queue>) {}
registerEvent(event: SimEvent) {
this.events.push(event);
if (event.people) {
this.positionInGroup = event.people!.indexOf(this.id);
}
}
draw(ctx: CanvasRenderingContext2D, now: number) {
const topEvent = this.events[this.events.length - 1];
if (!topEvent || topEvent.event === "BUS_ARRIVAL") {
ctx.drawImage(personIcon, this.lastWalkingStart.x, this.lastWalkingStart.y, IMAGE_WIDTH, IMAGE_HEIGHT);
return;
}
const wabble = Math.random() * 3;
const queue = (() => {
if (topEvent.scannerLine) {
return this.scannerQueues[topEvent.scannerLine - 1];
} else if (topEvent.sellerLine) {
return this.sellerQueues[topEvent.sellerLine - 1];
} else {
console.error("No queue found");
throw new Error("Invalid state of affairs.");
}
})();
switch (topEvent.event) {
case "WALK_TO_SELLER":
queue.registerPeople(topEvent.people!, { begin: topEvent.time, end: topEvent.time + topEvent.duration! });
{
const durationElapsed = now - topEvent.time;
const durationRatio = durationElapsed / topEvent.duration!;
const start = this.lastWalkingStart;
const destination = queue.queueBegin(now);
const x = start.x + (destination.x - start.x) * durationRatio + this.positionInGroup * Person.GROUP_SPACING + wabble;
const y = start.y + (destination.y - start.y) * durationRatio + wabble;
ctx.drawImage(personIcon, x, y, IMAGE_WIDTH, IMAGE_HEIGHT);
}
break;
case "WAIT_IN_SELLER_LINE":
{
const pos = queue.positionFromFrontOfLine(now, topEvent.people!);
const terminus = queue.queueTerminus;
const x = terminus.x - pos * SELLER_WIDTH + this.positionInGroup * Person.GROUP_SPACING;
const y = terminus.y;
ctx.drawImage(personIcon, x, y, IMAGE_WIDTH, IMAGE_HEIGHT);
}
break;
case "BUY_TICKETS":
queue.removePeople(topEvent.people!);
{
const seqNo = topEvent.people!.indexOf(this.id);
const serviceLocation = queue.serviceLocation;
this.lastWalkingStart = serviceLocation;
ctx.drawImage(personIcon, serviceLocation.x + seqNo * Person.GROUP_SPACING, serviceLocation.y, IMAGE_WIDTH, IMAGE_HEIGHT);
}
break;
case "WALK_TO_SCANNER":
queue.registerPeople([topEvent.person!], { begin: topEvent.time, end: topEvent.time + topEvent.duration! });
{
const durationElapsed = now - topEvent.time;
const durationRatio = durationElapsed / topEvent.duration!;
const start = this.lastWalkingStart;
const destination = queue.queueBegin(now);
const x = start.x + (destination.x - start.x) * durationRatio + wabble + this.positionInGroup * Person.GROUP_SPACING;
const y = start.y + (destination.y - start.y) * durationRatio + wabble;
ctx.drawImage(personIcon, x, y, IMAGE_WIDTH, IMAGE_HEIGHT);
}
break;
case "WAIT_IN_SCANNER_LINE":
{
const pos = queue.positionFromFrontOfLine(now, [topEvent.person!]);
const terminus = queue.queueTerminus;
const x = terminus.x - pos * SCANNER_WIDTH;
const y = terminus.y;
ctx.drawImage(personIcon, x, y, IMAGE_WIDTH, IMAGE_HEIGHT);
}
break;
case "SCAN_TICKETS":
queue.removePeople([topEvent.person!]);
{
if (now < topEvent.time + topEvent.duration!) {
const serviceLocation = queue.serviceLocation;
ctx.drawImage(personIcon, serviceLocation.x, serviceLocation.y, IMAGE_WIDTH, IMAGE_HEIGHT);
} else {
// TODO: Self destruct
}
}
break;
default:
console.warn("Unknown event: " + topEvent.event);
}
}
}
// -------------------------
// CANVAS LOGIC
// -------------------------
let stopTriggered = false;
const Run = async (sourceFile: string, speed: number) => {
const canvas = document.getElementById("animate") as HTMLCanvasElement;
const ctx = canvas.getContext("2d");
if (!ctx) {
console.error("no context?");
return;
}
const data = (await (await fetch(sourceFile)).json()) as ReplayData;
const events = SortAndInjectSequentialIDs(data.events);
const sellerQueues = Array.from(Array(data.sellerLines).keys()).map((i) => new Queue("Seller", i + 1, { x: 500, y: 75 + i * 75 }, SELLER_WIDTH));
const scannerQueues = Array.from(Array(data.scannerLines).keys()).map(
(i) => new Queue("Scanner", i + 1, { x: 999, y: 75 + i * 75 }, SCANNER_WIDTH)
);
const personMap: { [personId: number]: Person } = {};
const begin = new Date().getTime();
const Draw = () => {
const now = (new Date().getTime() - begin) / 1000 / speed;
// Parse events until we run out or until we find an event from the future
let e: SimEventWithId | null | undefined = null;
while (true) {
e = events.shift();
if (!e) {
break;
} else if (e.time > now) {
events.unshift(e);
break;
}
if (e.event === "BUS_ARRIVAL") {
(e.peopleCreated || []).forEach((id) => (personMap[id] = new Person(id, sellerQueues, scannerQueues)));
} else if (e.people !== undefined) {
e.people.forEach((id) => personMap[id].registerEvent(e as SimEvent));
} else if (e.person !== undefined) {
personMap[e.person].registerEvent(e);
} else {
console.warn("Invalid event received", e);
}
}
ctx.globalCompositeOperation = "destination-over";
ctx.clearRect(0, 0, 1100, 800);
sellerQueues.forEach((q) => q.draw(ctx, now));
scannerQueues.forEach((q) => q.draw(ctx, now));
Object.keys(personMap).forEach((id) => personMap[id].draw(ctx, now));
ctx.fillText(`T = ${now.toFixed(2)} minutes`, 5, 15);
if (now < 30 && !stopTriggered) {
window.requestAnimationFrame(Draw);
} else {
goButton.textContent = "Start";
stopTriggered = false;
}
};
window.requestAnimationFrame(Draw);
};
goButton.addEventListener("click", function() {
if (this.textContent === "Start") {
this.textContent = "Stop";
const file = (document.getElementById("file") as HTMLSelectElement).value;
const speed = parseInt((document.getElementById("speed") as HTMLInputElement).value, 10);
Run(file, speed);
} else {
this.textContent = "Start";
stopTriggered = true;
}
});