Skip to content

Commit 0d4d1c1

Browse files
committed
Search: Convert to typescript
1 parent da620dc commit 0d4d1c1

File tree

6 files changed

+737
-71
lines changed

6 files changed

+737
-71
lines changed

index.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
</div>
2424
<?php require "components/commonjs.html"; ?>
2525
<script src="https://cdn.jsdelivr.net/npm/showdown@2.1.0/dist/showdown.min.js"></script>
26-
<script src="index.js"></script>
26+
<script src="typestripped/index.js"></script>
2727
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>
2828
<script src="https://censorcanary.org/censorcanary.js" defer></script>
2929
</body>

index.js renamed to index.ts

Lines changed: 131 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,75 @@
1+
import type { IAbility, IArcane, ICustom, IExportEnemies, IFlavourItem, IGear, IPowersuit, IRecipe, IRegion, IRelic, IResource, ISentinel, IUpgrade, IWeapon, TMissionDeck } from "warframe-public-export-plus";
2+
3+
interface IRewardSource {
4+
name: string[];
5+
rotation?: number;
6+
itemCount: number;
7+
probability: number;
8+
probabilityWorstCase?: number;
9+
}
10+
11+
// common.js
12+
declare let onLanguageUpdate: () => void;
13+
declare function getDictPromise(): Promise<Record<string, string>>;
14+
declare function resolveTextIcons(text: string): string;
15+
declare function setImageSource(img: HTMLImageElement, icon: string): void;
16+
17+
// fetch
18+
declare const dict: Record<string, string>;
19+
declare const ExportRegions: Record<string, IRegion>;
20+
declare const ExportRelics: Record<string, IRelic>;
21+
declare const ExportEnemies: IExportEnemies;
22+
declare const supplementalGlyphData: Record<string, {
23+
promo_code?: string;
24+
twitch?: string;
25+
youtube?: string;
26+
discord?: string;
27+
twitter?: string;
28+
mixer?: string;
29+
"other-site"?: string;
30+
markdown?: string;
31+
}>;
32+
declare global {
33+
interface Window {
34+
dict: Record<string, string>;
35+
dict_entries: [string, string][];
36+
ExportWarframes_entries: [string, IPowersuit][];
37+
ExportWeapons_entries: [string, IWeapon][];
38+
ExportUpgrades_entries: [string, IUpgrade][];
39+
ExportArcanes_entries: [string, IArcane][];
40+
ExportResources_entries: [string, IResource][];
41+
ExportFlavour_entries: [string, IFlavourItem][];
42+
ExportCustoms_entries: [string, ICustom][];
43+
ExportGear_entries: [string, IGear][];
44+
ExportSentinels_entries: [string, ISentinel][];
45+
ExportAbilities_entries: [string, IAbility][];
46+
ExportRewards_entries: [string, TMissionDeck][];
47+
meta_entries: {
48+
warframe: [string, IPowersuit][];
49+
weapon: [string, IWeapon][];
50+
upgrade: [string, IUpgrade][];
51+
arcane: [string, IArcane][];
52+
resource: [string, IResource][];
53+
flavour: [string, IFlavourItem][];
54+
custom: [string, ICustom][];
55+
gear: [string, IGear][];
56+
sentinel: [string, ISentinel][];
57+
ability: [string, IAbility][];
58+
};
59+
itemToRecipeMap: Record<string, string>;
60+
missionDeckNames: Record<string, string[]>;
61+
droptableNames: Record<string, string[]>;
62+
}
63+
}
64+
165
const params = new URLSearchParams(location.hash.replace("#", ""));
266
if (params.has("q"))
367
{
4-
document.getElementById("query").value = params.get("q");
68+
(document.getElementById("query") as HTMLInputElement).value = params.get("q");
569
document.getElementById("results-status").textContent = "Loading...";
670
}
771

8-
document.getElementById("query").oninput = function()
72+
(document.getElementById("query") as HTMLInputElement).oninput = function(this: HTMLInputElement)
973
{
1074
if (this.value == "")
1175
{
@@ -62,25 +126,25 @@ Promise.all([
62126
{
63127
window.dict = dict;
64128
window.dict_entries = Object.entries(window.dict).sort(([key1, value1], [key2, value2]) => value1.length - value2.length);
65-
window.ExportWarframes = ExportWarframes;
66-
window.ExportWeapons = ExportWeapons;
67-
window.ExportUpgrades = ExportUpgrades;
68-
window.ExportArcanes = ExportArcanes;
69-
window.ExportResources = ExportResources;
70-
window.ExportFlavour = ExportFlavour;
71-
window.ExportCustoms = ExportCustoms;
72-
window.ExportGear = ExportGear;
73-
window.ExportSentinels = ExportSentinels
74-
window.ExportRewards = ExportRewards;
75-
window.ExportRegions = ExportRegions;
76-
window.ExportEnemies = ExportEnemies;
77-
window.ExportImages = ExportImages;
78-
window.ExportTextIcons = ExportTextIcons;
79-
window.ExportRelics = ExportRelics;
80-
window.ExportAbilities = ExportAbilities;
81-
window.supplementalGlyphData = supplementalGlyphData;
82-
83-
for (const suit of Object.values(ExportWarframes))
129+
//(window as any).ExportWarframes = ExportWarframes;
130+
//(window as any).ExportWeapons = ExportWeapons;
131+
//(window as any).ExportUpgrades = ExportUpgrades;
132+
//(window as any).ExportArcanes = ExportArcanes;
133+
//(window as any).ExportResources = ExportResources;
134+
//(window as any).ExportFlavour = ExportFlavour;
135+
//(window as any).ExportCustoms = ExportCustoms;
136+
//(window as any).ExportGear = ExportGear;
137+
//(window as any).ExportSentinels = ExportSentinels;
138+
//(window as any).ExportRewards = ExportRewards;
139+
(window as any).ExportRegions = ExportRegions;
140+
(window as any).ExportEnemies = ExportEnemies;
141+
(window as any).ExportImages = ExportImages;
142+
(window as any).ExportTextIcons = ExportTextIcons;
143+
(window as any).ExportRelics = ExportRelics;
144+
//(window as any).ExportAbilities = ExportAbilities;
145+
(window as any).supplementalGlyphData = supplementalGlyphData;
146+
147+
for (const suit of Object.values(ExportWarframes as Record<string, IPowersuit>))
84148
{
85149
for (const ability of suit.abilities)
86150
{
@@ -89,44 +153,45 @@ Promise.all([
89153
}
90154
}
91155

92-
window.ExportWarframes_entries = Object.entries(ExportWarframes);
93-
window.ExportWeapons_entries = Object.entries(ExportWeapons).filter(([uniqueName, item]) => item.totalDamage != 0);
94-
window.ExportUpgrades_entries = Object.entries(ExportUpgrades);
95-
window.ExportArcanes_entries = Object.entries(ExportArcanes);
96-
window.ExportResources_entries = Object.entries(ExportResources);
97-
window.ExportFlavour_entries = Object.entries(ExportFlavour);
98-
window.ExportCustoms_entries = Object.entries(ExportCustoms);
99-
window.ExportGear_entries = Object.entries(ExportGear);
100-
window.ExportSentinels_entries = Object.entries(ExportSentinels)
101-
window.ExportRewards_entries = Object.entries(ExportRewards);
102-
window.ExportAbilities_entries = Object.entries(ExportAbilities);
156+
window.ExportWarframes_entries = Object.entries(ExportWarframes as Record<string, IPowersuit>);
157+
window.ExportWeapons_entries = Object.entries(ExportWeapons as Record<string, IWeapon>).filter(([uniqueName, item]) => item.totalDamage != 0);
158+
window.ExportUpgrades_entries = Object.entries(ExportUpgrades as Record<string, IUpgrade>);
159+
window.ExportArcanes_entries = Object.entries(ExportArcanes as Record<string, IArcane>);
160+
window.ExportResources_entries = Object.entries(ExportResources as Record<string, IResource>);
161+
window.ExportFlavour_entries = Object.entries(ExportFlavour as Record<string, IFlavourItem>);
162+
window.ExportCustoms_entries = Object.entries(ExportCustoms as Record<string, ICustom>);
163+
window.ExportGear_entries = Object.entries(ExportGear as Record<string, IGear>);
164+
window.ExportSentinels_entries = Object.entries(ExportSentinels as Record<string, ISentinel>);
165+
window.ExportAbilities_entries = Object.entries(ExportAbilities as Record<string, IAbility>);
166+
167+
window.ExportRewards_entries = Object.entries(ExportRewards as Record<string, TMissionDeck>);
103168

104169
window.meta_entries = {
105-
warframe: ExportWarframes_entries,
106-
weapon: ExportWeapons_entries,
107-
upgrade: ExportUpgrades_entries,
108-
arcane: ExportArcanes_entries,
109-
resource: ExportResources_entries,
110-
flavour: ExportFlavour_entries,
111-
custom: ExportCustoms_entries,
112-
gear: ExportGear_entries,
113-
sentinel: ExportSentinels_entries,
114-
ability: ExportAbilities_entries,
170+
warframe: window.ExportWarframes_entries,
171+
weapon: window.ExportWeapons_entries,
172+
upgrade: window.ExportUpgrades_entries,
173+
arcane: window.ExportArcanes_entries,
174+
resource: window.ExportResources_entries,
175+
flavour: window.ExportFlavour_entries,
176+
custom: window.ExportCustoms_entries,
177+
gear: window.ExportGear_entries,
178+
sentinel: window.ExportSentinels_entries,
179+
ability: window.ExportAbilities_entries,
115180
};
116181

117182
window.itemToRecipeMap = {};
118-
Object.entries(ExportRecipes).forEach(([uniqueName, recipe]) => {
119-
itemToRecipeMap[recipe.resultType] = uniqueName;
183+
Object.entries(ExportRecipes as Record<string, IRecipe>).forEach(([uniqueName, recipe]) => {
184+
window.itemToRecipeMap[recipe.resultType] = uniqueName;
120185
});
121186

122187
updateMissionDeckNames();
123188

124-
if (document.getElementById("query").value)
189+
if ((document.getElementById("query") as HTMLInputElement).value)
125190
{
126-
doQuery(document.getElementById("query").value);
191+
doQuery((document.getElementById("query") as HTMLInputElement).value);
127192
}
128193

129-
document.getElementById("query").oninput = function()
194+
document.getElementById("query").oninput = function(this: HTMLInputElement)
130195
{
131196
if (this.value == "")
132197
{
@@ -194,7 +259,7 @@ function updateMissionDeckNames()
194259
});
195260
}
196261

197-
function doQuery(query)
262+
function doQuery(query: string)
198263
{
199264
console.time("Input to language tags");
200265
let results = getDictEntriesFromQuery(query).reduce((arr, [key, value]) =>
@@ -251,7 +316,7 @@ function doQuery(query)
251316
return 1;
252317
}
253318
// Tags last
254-
return (a.type == "tag") - (b.type == "tag");
319+
return Number(a.type == "tag") - Number(b.type == "tag");
255320
});
256321
console.timeEnd("Sort results");
257322

@@ -510,7 +575,7 @@ function doQuery(query)
510575
}
511576

512577
{
513-
var converter = new showdown.Converter();
578+
var converter = new window.showdown.Converter();
514579
converter.setOption("openLinksInNewWindow", true);
515580
converter.setOption("strikethrough", true);
516581
let div = document.createElement("div");
@@ -584,32 +649,28 @@ function doQuery(query)
584649
|| result.type == "sentinel"
585650
)
586651
{
587-
const dropType = itemToRecipeMap[result.key] ?? result.key;
588-
const dropIsBlueprint = !!itemToRecipeMap[result.key];
652+
const dropType = window.itemToRecipeMap[result.key] ?? result.key;
653+
const dropIsBlueprint = !!window.itemToRecipeMap[result.key];
589654
const storeItem = "/Lotus/StoreItems/" + dropType.substring(7);
590-
const sources = [];
591-
ExportRewards_entries.forEach(([deckName, tiers]) =>
655+
const sources: IRewardSource[] = [];
656+
window.ExportRewards_entries.forEach(([deckName, tiers]) =>
592657
{
593658
for (let i = 0; i != tiers.length; ++i)
594659
{
595660
for (const reward of tiers[i])
596661
{
597662
if (reward.type == storeItem)
598663
{
599-
if (deckName in missionDeckNames)
664+
if (deckName in window.missionDeckNames)
600665
{
601-
const source = {
602-
name: missionDeckNames[deckName] ?? [deckName],
666+
const source: IRewardSource = {
667+
name: window.missionDeckNames[deckName] ?? [deckName],
603668
rotation: tiers.length > 1 ? i : undefined,
604-
itemCount: reward.itemCount
669+
itemCount: reward.itemCount,
670+
probability: reward.probability ?? { COMMON: 0.76, UNCOMMON: 0.40, RARE: 0.10 }[reward.rarity],
605671
};
606-
if (reward.probability)
607-
{
608-
source.probability = reward.probability
609-
}
610-
else
672+
if (!reward.probability)
611673
{
612-
source.probability = { COMMON: 0.76, UNCOMMON: 0.40, RARE: 0.10 }[reward.rarity];
613674
source.probabilityWorstCase = { COMMON: 0.50, UNCOMMON: 0.22, RARE: 0.02 }[reward.rarity];
614675
}
615676
sources.push(source);
@@ -631,7 +692,7 @@ function doQuery(query)
631692
if (reward.type == dropType)
632693
{
633694
sources.push({
634-
name: droptableNames[droptableName] ?? [droptableName],
695+
name: window.droptableNames[droptableName] ?? [droptableName],
635696
itemCount: 1,
636697
probability: reward.probability * pool.chance
637698
});
@@ -701,16 +762,16 @@ function doQuery(query)
701762
console.timeEnd("Commit to DOM");
702763
}
703764

704-
function getDictEntriesFromQuery(query)
765+
function getDictEntriesFromQuery(query: string): [string, string][]
705766
{
706767
let num_results = 0;
707768
query = query.toLowerCase();
708-
return dict_entries.filter(([key, value]) =>
769+
return window.dict_entries.filter(([key, value]) =>
709770
{
710771
value = value.toLowerCase();
711772
return value == query
712773
|| (
713-
(value.substr(0, query.length) == query
774+
(value.substring(0, query.length) == query
714775
|| value.indexOf(" " + query) !== -1
715776
)
716777
&& ++num_results < 100
@@ -726,7 +787,7 @@ function resolveTagsToUses(results)
726787
{
727788
if (result.type == "tag")
728789
{
729-
for (const [type, entries] of Object.entries(meta_entries))
790+
for (const [type, entries] of Object.entries(window.meta_entries))
730791
{
731792
const entry = entries.find(([uniqueName, item]) => item.name == result.key);
732793
if (entry)
@@ -744,7 +805,7 @@ function resolveTagsToUses(results)
744805
function addUniqueNameResults(res, query)
745806
{
746807
query = query.toLowerCase();
747-
for (const [type, entries] of Object.entries(meta_entries))
808+
for (const [type, entries] of Object.entries(window.meta_entries))
748809
{
749810
for (const [uniqueName, item] of entries)
750811
{

package-lock.json

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
},
66
"devDependencies": {
77
"@types/bootstrap": "^5.2.10",
8+
"@types/showdown": "^2.0.6",
89
"@typescript-eslint/eslint-plugin": "^8.43.0",
910
"@typescript-eslint/parser": "^8.43.0",
1011
"eslint": "^8",

0 commit comments

Comments
 (0)