-
-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathlibrary.ts
More file actions
343 lines (307 loc) · 8.83 KB
/
library.ts
File metadata and controls
343 lines (307 loc) · 8.83 KB
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
import {useEffect, useState} from "react";
import libraryIndex from "./Data/Library-index.json";
export const APPS_INDEX = unpackIndexItems(libraryIndex.apps, "apps");
export const GAMES_INDEX = unpackIndexItems(libraryIndex.games, "games");
export type LibraryIndexItem = {
id: number;
type: LibraryItemType;
title: string;
authors: string[];
year?: number;
systems: System[];
architectures: Architecture[];
categories: CategoryIndex[];
perspectives: PerspectiveIndex[];
publishers: string[];
// Private, used as a cache for searching
index?: string[];
};
export type LibraryItemType = "apps" | "games";
const enum System {
SYSTEM_1 = 1,
SYSTEM_6 = 6,
SYSTEM_7 = 7,
MAC_OS_8 = 8,
MAC_OS_8_5 = 8.5,
MAC_OS_9 = 9,
MAC_OS_X = 10,
}
export const SYSTEM_INDEX = {
[System.SYSTEM_1]: "System 1",
[System.SYSTEM_6]: "System 6",
[System.SYSTEM_7]: "System 7",
[System.MAC_OS_8]: "Mac OS 8",
[System.MAC_OS_8_5]: "Mac OS 8.5",
[System.MAC_OS_9]: "Mac OS 9",
[System.MAC_OS_X]: "Mac OS X",
};
const enum Architecture {
M68K = 0,
PPC = 1,
PPC_CARBON = 2,
X86_INTEL_MAC = 3,
X86_INTEL = 4,
X86_WINDOWS = 5,
}
export const ARCHITECTURE_INDEX = {
[Architecture.M68K]: "68K",
[Architecture.PPC]: "PowerPC",
[Architecture.PPC_CARBON]: "PowerPC (Carbon)",
[Architecture.X86_INTEL_MAC]: "x86",
[Architecture.X86_INTEL]: "x86",
[Architecture.X86_WINDOWS]: "x86 (Windows)",
};
type CategoryIndex = number;
export const CATEGORIES_INDEX = [
"3D Rendering & CAD",
"Action",
"Adventure",
"Antivirus",
"Arcade",
"Board Game",
"Books & Multimedia",
"Business & Productivity",
"Card & Casino",
"Clip Art",
"Compression & Archiving",
"Contextual Menu Modules",
"Control Panels & Extensions",
"Creative",
"Database",
"Development Tools",
"Drivers & Hardware Support",
"Early Childhood",
"Educational",
"Emulators",
"Encryption & Security",
"FPS",
"Flight Simulation",
"Fonts",
"Game Compilations",
"Game Editors & Tools",
"HyperCard",
"Icons",
"Imaging & Burning",
"Interactive Fiction",
"Internet & Communications",
"Lisa",
"Mac Info & Literature",
"Music & Sound",
"Novelties & Fun",
"Operating Systems",
"Pinball",
"Plug-ins",
"Puzzle",
"RPG",
"Racing",
"Reference",
"Science & Math",
"Screen Savers",
"Simulation",
"Software Compilations",
"Sports",
"Spreadsheet",
"Strategy",
"Trivia",
"Utilities",
"Video",
"Visual Arts & Graphics",
"Widgets",
"Word Processing & Publishing",
"World Builder",
];
type PerspectiveIndex = number;
export const PERSPECTIVE_INDEX = [
"1st Person",
"1st Person + 3rd Person",
"3rd Person",
"FMV",
"Hexagonal Grid",
"Isometric",
"Platform",
"Point & Click",
"Side Scrolling",
"Text",
"Text + Illustrations",
"Top Down",
"Vertical Scrolling",
];
function unpackIndexItems(
indexItems: any[],
type: LibraryItemType
): LibraryIndexItem[] {
const items: LibraryIndexItem[] = indexItems.map((indexItem, i) => {
const [
title,
authors,
year,
systems,
architectures,
categories,
publishers,
perspectives,
] = indexItem;
return {
id: i,
type,
title: title ?? "",
authors: authors ?? [],
year,
systems: systems ?? [],
architectures: architectures ?? [],
categories: categories ?? [],
publishers: publishers ?? [],
perspectives: perspectives ?? [],
};
});
// Build search index in the background, so that it's ready when the user
// starts typing in the search box.
const itemsToSearchIndex = items.slice();
const searchIndexMoreItems = () => {
const startTime = performance.now();
while (performance.now() - startTime < 8) {
const item = itemsToSearchIndex.pop();
if (!item) {
return;
}
if (!item.index) {
item.index = extractItemIndex(item);
}
}
setTimeout(searchIndexMoreItems, 100);
};
setTimeout(searchIndexMoreItems, 100);
return items;
}
function parseSearchQuery(query: string): Term[] {
const terms: Term[] = [];
const regex = /(\w+):"([^"]+)"|(\w+):(\S+)|"([^"]+)"|(\S+)/g;
let match;
query = query.toLowerCase();
// Ensure we close any unclosed quotes, to be more resilient when we have a
// partial query that the user is in the middle of typing.
const quoteCount = (query.match(/"/g) ?? []).length;
if (quoteCount % 2 !== 0) {
query += '"';
}
while ((match = regex.exec(query)) !== null) {
if (match[1] && match[2]) {
terms.push({operator: match[1], value: match[2]}); // operator with quoted value
} else if (match[3] && match[4]) {
terms.push({operator: match[3], value: match[4]}); // operator with unquoted value
} else if (match[5]) {
terms.push({value: match[5]}); //quoted value without operator
} else if (match[6]) {
terms.push({value: match[6]}); //bare word without operator
}
}
return terms;
}
type Term = {
operator?: string; // missing if there's no operator
value: string;
};
function termMatches(
term: Term,
item: LibraryIndexItem,
index: string[]
): boolean {
if (!term.operator) {
return index.some(i => i.includes(term.value));
}
function matchesIndex(
value: number[],
index: {[key: number]: string}
): boolean {
return value.some(v => index[v].toLowerCase().includes(term.value));
}
switch (term.operator) {
case "year":
// Allow prefix matches for years, so that you can search for things
// from the 80s/90s/2000s
return item.year?.toString().includes(term.value) === true;
case "title":
return item.title.toLowerCase().includes(term.value);
case "author":
return (
item.authors.some(a => a.toLowerCase().includes(term.value)) ||
item.publishers.some(p => p.toLowerCase().includes(term.value))
);
case "category":
return (
matchesIndex(item.categories, CATEGORIES_INDEX) ||
matchesIndex(item.perspectives, PERSPECTIVE_INDEX)
);
case "system":
case "os":
return matchesIndex(item.systems, SYSTEM_INDEX);
case "architecture":
case "arch":
// Support this alias
if (term.value === "ppc") {
term.value = "powerpc";
}
return matchesIndex(item.architectures, ARCHITECTURE_INDEX);
}
return false;
}
export function createSearchPredicate(
search: string
): (item: LibraryIndexItem) => boolean {
const terms = parseSearchQuery(search);
return item => {
let {index} = item;
if (!index) {
index = item.index = extractItemIndex(item);
}
return terms.every(term => termMatches(term, item, index));
};
}
function extractItemIndex(item: LibraryIndexItem): string[] {
return [
item.title,
...item.authors,
...item.publishers,
...item.categories.map(c => CATEGORIES_INDEX[c]),
...(item.year ? [item.year.toString()] : []),
].map(s => s.toLowerCase());
}
// Matches the JSON type returned by `handleDetails` handler in
// `workers-site/library.ts`
export type LibraryDetailsItem = {
url: string;
files: {[url: string]: number};
description?: string;
screenshots: string[];
manuals: {[url: string]: number};
composers: string[];
externalDownloadUrl?: string;
};
export function useLibraryItemDetails(
item: LibraryIndexItem
): LibraryDetailsItem | undefined {
const [detailsItem, setDetailsItem] = useState<
LibraryDetailsItem | undefined
>();
useEffect(() => {
fetchItemDetails(item, setDetailsItem);
}, [item]);
return detailsItem;
}
async function fetchItemDetails(
item: LibraryIndexItem,
setDetailsItem: (item: LibraryDetailsItem | undefined) => void
) {
const url = `/Library/details?type=${item.type}&id=${item.id}&h=${libraryIndex.hash}`;
const cached = itemDetailsCache.get(url);
if (cached) {
setDetailsItem(cached);
return;
}
setDetailsItem(undefined);
const response = await fetch(url);
const details = (await response.json()) as LibraryDetailsItem;
itemDetailsCache.set(url, details);
setDetailsItem(details);
}
const itemDetailsCache = new Map<string, LibraryDetailsItem>();