Skip to content

Commit

Permalink
refactor(frontend) fix template searching, update database images
Browse files Browse the repository at this point in the history
Signed-off-by: Rachel Powers <508861+Ryex@users.noreply.github.com>
  • Loading branch information
Ryex committed Aug 26, 2024
1 parent 6e503f9 commit f40437e
Show file tree
Hide file tree
Showing 65 changed files with 187 additions and 96 deletions.
140 changes: 113 additions & 27 deletions www/assets_finding.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,20 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"%matplotlib widget\n",
"import json\n",
"\n",
"database = {}\n",
"\n",
"with open(\"data/database.json\", \"r\") as f:\n",
" database = json.load(f)\n",
"with open(\"src/ts/virtualMachine/prefabDatabase.ts\", \"r\") as f:\n",
" contents = f.read().removeprefix(\"export default \").removesuffix(\" as const\")\n",
" database = json.loads(contents)\n",
"\n",
"db = database[\"db\"]"
"db = database[\"prefabs\"]"
]
},
{
Expand All @@ -38,7 +40,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -58,7 +60,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -80,7 +82,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 35,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -105,9 +107,9 @@
" better_matches = []\n",
" for can in filtered_matches:\n",
" name, match, mapping = can\n",
" if mapping.startswith(\"Item\") and mapping in name:\n",
" if mapping.startswith(\"Item\") and mapping in name or mapping.lower() in name:\n",
" better_matches.append((name, match, mapping))\n",
" elif mapping.startswith(\"Structure\") and mapping in name:\n",
" elif mapping.startswith(\"Structure\") and mapping in name or mapping.lower() in name:\n",
" better_matches.append((name, match, mapping))\n",
" if len(better_matches) > 0:\n",
" filtered_matches = better_matches\n",
Expand All @@ -127,7 +129,7 @@
" direct = []\n",
" for can in filtered_matches:\n",
" name, match, mapping = can\n",
" if f\"{match}-\" in name:\n",
" if f\"{match}-\" in name or f\"{match.lower()}-\" in name:\n",
" direct.append((name, match, mapping))\n",
" if len(direct) > 0:\n",
" filtered_matches = direct\n",
Expand All @@ -154,7 +156,7 @@
" continue\n",
" elif name.startswith(\"Kit\") and not mapping.startswith(\"Kit\"):\n",
" continue\n",
" elif not name.startswith(match):\n",
" elif not (name.startswith(match) or name.startswith(match.lower())):\n",
" continue\n",
" not_worse.append((name, match, mapping))\n",
" if len(not_worse) > 0:\n",
Expand All @@ -171,14 +173,15 @@
"\n",
"for entry in db.values():\n",
" candidates = []\n",
" entry_name = entry[\"prefab\"][\"prefab_name\"]\n",
" for name in names:\n",
" if entry[\"name\"] in name:\n",
" candidates.append((name, entry[\"name\"], entry[\"name\"]))\n",
" if entry[\"name\"].removeprefix(\"Item\") in name:\n",
" candidates.append((name, entry[\"name\"].removeprefix(\"Item\"), entry[\"name\"]))\n",
" if entry[\"name\"].removeprefix(\"Structure\") in name:\n",
" candidates.append((name, entry[\"name\"].removeprefix(\"Structure\"), entry[\"name\"]))\n",
" image_candidates[entry[\"name\"]] = filter_candidates(candidates)"
" if entry_name in name or entry_name.lower() in name:\n",
" candidates.append((name, entry_name, entry_name))\n",
" if entry_name.removeprefix(\"Item\") in name or entry_name.removeprefix(\"Item\").lower() in name:\n",
" candidates.append((name, entry_name.removeprefix(\"Item\"), entry_name))\n",
" if entry_name.removeprefix(\"Structure\") in name or entry_name.removeprefix(\"Structure\").lower() in name:\n",
" candidates.append((name, entry_name.removeprefix(\"Structure\"), entry_name))\n",
" image_candidates[entry_name] = filter_candidates(candidates)"
]
},
{
Expand All @@ -190,7 +193,7 @@
},
{
"cell_type": "code",
"execution_count": 17,
"execution_count": 36,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -206,12 +209,12 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Prepare out List of file copies. at this point a few items will never have a match. and one or two will have two choices but those choices will be arbitrary."
"Prepare out List of file copies. at this point a few items will never have a match. and one or two will have two choices but those choices will be filtered again by passing them through some extra function."
]
},
{
"cell_type": "code",
"execution_count": 18,
"execution_count": 40,
"metadata": {},
"outputs": [
{
Expand All @@ -227,33 +230,95 @@
"ItemHorticultureBelt []\n",
"ItemKitLiquidRegulator []\n",
"ItemKitPortablesConnector []\n",
"ItemMushroom ['ItemMushroom-resources.assets-3022.png', 'ItemMushroom-resources.assets-9304.png']\n",
"ItemPlantEndothermic_Creative []\n",
"ItemPlantThermogenic_Creative []\n",
"ItemSuitModCryogenicUpgrade []\n",
"Landingpad_GasConnectorInwardPiece []\n",
"Landingpad_LiquidConnectorInwardPiece []\n",
"StructureBlocker []\n",
"StructureElevatorLevelIndustrial []\n",
"StructureLogicSorter []\n",
"StructurePlinth []\n"
]
}
],
"source": [
"%matplotlib widget\n",
"to_copy = []\n",
"\n",
"from IPython.display import Image, display\n",
"\n",
"gases = [(\"Oxygen\", \"White\"), (\"Nitrogen\", \"Black\"), (\"CarbonDioxide\", \"Yellow\"), (\"Fuel\", \"Orange\")]\n",
"\n",
"colors = [\"White\", \"Black\", \"Gray\", \"Khaki\", \"Brown\", \"Orange\", \"Yellow\", \"Red\", \"Green\", \"Blue\", \"Purple\"]\n",
"\n",
"def split_gas(name):\n",
" for gas, color in gases:\n",
" if name.endswith(gas):\n",
" return (name.removesuffix(gas), gas, color)\n",
" elif name.lower().endswith(gas):\n",
" return (name.lower().removesuffix(gas), gas, color)\n",
" elif name.lower().endswith(gas.lower()):\n",
" return (name.lower().removesuffix(gas.lower()), gas.lower(), color.lower())\n",
" return [name, None, None]\n",
"\n",
"def match_gas_color(name, candidates):\n",
" mat, gas, color = split_gas(name)\n",
" seek = f\"{mat}_{color}\"\n",
" if gas is not None:\n",
" for candidate in candidates:\n",
" if seek in candidate or seek.lower() in candidate:\n",
" return candidate\n",
" return None\n",
"\n",
"def prefer_direct(name, candidates):\n",
" for candidate in candidates:\n",
" if f\"{name}-\" in candidate or f\"{name.lower()}-\" in candidate:\n",
" return candidate\n",
" return None\n",
"\n",
"def prefer_uncolored(name, candidates):\n",
" for candidate in candidates:\n",
" for color in colors:\n",
" if f\"_{color}\" not in candidate and f\"_{color.lower()}\" not in candidate:\n",
" return candidate\n",
" return None\n",
"\n",
"def prefer_lower_match(name, candidates):\n",
" for candidate in candidates:\n",
" if f\"{name.lower()}-\" in candidate:\n",
" return candidate\n",
" return None\n",
"\n",
"filter_funcs = [prefer_lower_match, prefer_direct, match_gas_color, prefer_uncolored]\n",
"\n",
"for name, candidates in image_candidates.items():\n",
" if len(candidates) != 1:\n",
" found = False\n",
" for func in filter_funcs:\n",
" candidate = func(name, candidates)\n",
" if candidate is not None:\n",
" to_copy.append((name, candidate))\n",
" found = True\n",
" if found:\n",
" continue\n",
" print(name, candidates)\n",
" if len(candidates) > 1:\n",
" for candidate in candidates:\n",
" print(candidate)\n",
" display(Image(datapath / candidate))\n",
" \n",
" #take first as fallback\n",
" to_copy.append((name, candidates[0]))\n",
" # to_copy.append((name, candidates[0]))\n",
" raise StopExecution\n",
" else:\n",
" # print(name, candidates)\n",
" to_copy.append((name, candidates[0]))\n"
]
},
{
"cell_type": "code",
"execution_count": 21,
"execution_count": 41,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -334,14 +399,28 @@
},
{
"cell_type": "code",
"execution_count": 22,
"execution_count": 42,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "3497a8d33d6a45879586d4c7441e3f60",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"IntProgress(value=0, max=1288)"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"1266 of 1266 | 100.00% \n",
"1288 of 1288 | 100.00% \n",
"Done\n"
]
}
Expand All @@ -352,13 +431,20 @@
"destpath = Path(\"img/stationpedia\")\n",
"total_files = len(to_copy)\n",
"\n",
"from ipywidgets import IntProgress\n",
"from IPython.display import display\n",
"import time\n",
"\n",
"f = IntProgress(min=0, max=total_files)\n",
"display(f)\n",
"count = 0\n",
"print ( f\"{count} of {total_files} | { count / total_files * 100}\", end=\"\\r\")\n",
"for name, file in to_copy:\n",
" source = datapath / file\n",
" dest = destpath / f\"{name}.png\"\n",
" shutil.copy(source, dest)\n",
" count += 1\n",
" f.value = count\n",
" print ( f\"{count} of {total_files} | { (count / total_files) * 100 :.2f}% \", end=\"\\r\")\n",
"print()\n",
"print(\"Done\")\n",
Expand All @@ -382,7 +468,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.2"
"version": "3.12.5"
}
},
"nbformat": 4,
Expand Down
Binary file modified www/img/stationpedia/DynamicAirConditioner.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified www/img/stationpedia/FlareGun.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified www/img/stationpedia/H2Combustor.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified www/img/stationpedia/ItemCannedPowderedEggs.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified www/img/stationpedia/ItemCerealBar.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added www/img/stationpedia/ItemCerealBarBag.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added www/img/stationpedia/ItemCerealBarBox.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added www/img/stationpedia/ItemChocolateBar.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added www/img/stationpedia/ItemChocolateCake.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added www/img/stationpedia/ItemChocolateCerealBar.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added www/img/stationpedia/ItemCocoaPowder.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added www/img/stationpedia/ItemCocoaTree.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified www/img/stationpedia/ItemCookedCorn.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified www/img/stationpedia/ItemCookedTomato.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added www/img/stationpedia/ItemEmergencySuppliesBox.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added www/img/stationpedia/ItemKitLinearRail.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified www/img/stationpedia/ItemKitRegulator.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added www/img/stationpedia/ItemKitRobotArmDoor.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added www/img/stationpedia/ItemKitRoboticArm.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added www/img/stationpedia/ItemMiningPackage.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added www/img/stationpedia/ItemPlainCake.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added www/img/stationpedia/ItemPortablesPackage.png
Binary file added www/img/stationpedia/ItemResidentialPackage.png
Binary file added www/img/stationpedia/ItemSugar.png
Binary file added www/img/stationpedia/ItemSugarCane.png
Binary file added www/img/stationpedia/ItemWaterBottleBag.png
Binary file added www/img/stationpedia/ItemWaterBottlePackage.png
Binary file added www/img/stationpedia/SeedBag_Cocoa.png
Binary file added www/img/stationpedia/SeedBag_SugarCane.png
Binary file added www/img/stationpedia/StructureChuteExportBin.png
Binary file added www/img/stationpedia/StructureComputerUpright.png
Binary file modified www/img/stationpedia/StructureCrateMount.png
Binary file modified www/img/stationpedia/StructureElevatorShaftIndustrial.png
Binary file modified www/img/stationpedia/StructureLiquidValve.png
Binary file added www/img/stationpedia/StructureReinforcedWall.png
Binary file added www/img/stationpedia/StructureRobotArmDoor.png
Binary file added www/img/stationpedia/StructureRoboticArmDock.png
Binary file modified www/img/stationpedia/StructureValve.png
2 changes: 1 addition & 1 deletion www/src/ts/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export function crc32(str: string): number {

export function numberToString(n: number): string {
if (isZeroNegative(n)) return "-0";
return n.toString();
return n?.toString() ?? `${n}`;
}
export function displayNumber(n: number): string {
return numberToString(n).replace("Infinity", "∞");
Expand Down
2 changes: 1 addition & 1 deletion www/src/ts/virtualMachine/controls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export class VMICControls extends VMObjectMixin(SignalWatcher(BaseElement)) {
}
this.selectUpdateTimeout = setTimeout(() => {
if (this.activeICSelect.value != null) {
this.activeICSelect.value.value = this.activeIC.value.toString();
this.activeICSelect.value.value = this.activeIC.value?.toString() ?? "";
this.activeICSelect.value.handleValueChange();
}
}, 100);
Expand Down
4 changes: 2 additions & 2 deletions www/src/ts/virtualMachine/device/addDevice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export class VMAddDeviceButton extends VMObjectMixin(BaseElement) {
let last: Map<string, LogicableStructureTemplate> = null
return computed(() => {
const next = new Map(
Array.from(Object.values(this.templateDB.value ?? {})).flatMap((template) => {
Array.from(this.templateDB.value?.values() ?? []).flatMap((template) => {
if ("structure" in template && "logic" in template) {
return [[template.prefab.prefab_name, template]] as [
string,
Expand Down Expand Up @@ -244,7 +244,7 @@ export class VMAddDeviceButton extends VMObjectMixin(BaseElement) {
(result) =>
html`
<vm-device-template
prefab_name=${result.entry.prefab.prefab_name}
prefabName=${result.entry.prefab.prefab_name}
class="card"
@add-device-template=${this._handleDeviceAdd}
>
Expand Down
2 changes: 1 addition & 1 deletion www/src/ts/virtualMachine/device/pins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export class VMDevicePins extends VMObjectMixin(BaseElement) {
}
);
});
return pinsHtml;
return html`${watch(pinsHtml)}`;
}

_handleChangePin(e: CustomEvent) {
Expand Down
15 changes: 6 additions & 9 deletions www/src/ts/virtualMachine/device/slotAddDialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
ItemInfo,
ObjectInfo,
ObjectTemplate,
TemplateDatabase,
} from "ic10emu_wasm";
import { computed, ReadonlySignal, signal, Signal, watch } from "@lit-labs/preact-signals";
import { repeat } from "lit/directives/repeat.js";
Expand Down Expand Up @@ -47,15 +48,15 @@ export class VMSlotAddDialog extends VMObjectMixin(BaseElement) {
this._filter.value = val;
}

templateDB = computed(() => {
templateDB = computed((): TemplateDatabase => {
return this.vm.value?.state.templateDB.value ?? null;
});

items = (() => {
let last: { [k: string]: SlotableItemTemplate } = null;
return computed(() => {
const next = Object.fromEntries(
Array.from(Object.values(this.templateDB.value ?? {})).flatMap((template) => {
Array.from(this.templateDB.value?.values() ?? []).flatMap((template) => {
if ("item" in template) {
return [[template.prefab.prefab_name, template]] as [
string,
Expand All @@ -82,7 +83,7 @@ export class VMSlotAddDialog extends VMObjectMixin(BaseElement) {
if (isSome(obj)) {
const template = obj.template;
const slot = "slots" in template ? template.slots[this.slotIndex.value] : null;
const typ = slot.typ;
const typ = slot?.typ;

if (typeof typ === "string" && typ !== "None") {
filtered = Array.from(Object.values(this.items.value)).filter(
Expand Down Expand Up @@ -238,10 +239,6 @@ export class VMSlotAddDialog extends VMObjectMixin(BaseElement) {
const div = e.currentTarget as HTMLDivElement;
const key = parseInt(div.getAttribute("key"));
const entry = this.templateDB.value.get(key) as SlotableItemTemplate;
const obj = window.VM.vm.state.getObject(this.objectID);
const dbTemplate = obj.peek().template;
console.log("using entry", dbTemplate);

const template: FrozenObject = {
obj_info: {
prefab: entry.prefab.prefab_name,
Expand Down Expand Up @@ -301,8 +298,8 @@ export class VMSlotAddDialog extends VMObjectMixin(BaseElement) {
}

_handleDialogHide() {
this.objectID = undefined;
this.slotIndex = undefined;
this.objectIDSignal.value = null;
this.slotIndex.value = null;
}

private slotIndex: Signal<number> = signal(0);
Expand Down
Loading

0 comments on commit f40437e

Please sign in to comment.