Skip to content

Commit

Permalink
Support editing legacy industries (#210)
Browse files Browse the repository at this point in the history
* refactor industries

* npm update

* sort

* Editor legacy industry support
  • Loading branch information
scottanderson authored Sep 18, 2024
1 parent 46a4c85 commit 6a074f7
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 19 deletions.
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 9 additions & 7 deletions ts/Studio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ import {
getIndustryName,
industryInputLabels,
industryOutputLabels,
isIndustryName,
} from './industries';
import {VegetationUtil} from './VegetationUtil';

Expand Down Expand Up @@ -1498,13 +1497,16 @@ export class Studio {
if (typeof industry.type === 'number') {
const setIndustryType = (type: IndustryType) => industry.type = type;
td.replaceChildren(editIndustryType(this, industry.type, setIndustryType));
} else if (isIndustryName(industry.type)) {
const setIndustryName = (name: IndustryName) => industry.type = name;
td.replaceChildren(editIndustryName(this, industry.type, setIndustryName));
} else {
const setIndustryName = (name: GvasString) => industry.type = name;
td.replaceChildren(editString(this, industry.type, setIndustryName));
td.classList.add('table-warning');
const industryName = getIndustryName(industry);
if (industryName !== null) {
const setIndustryName = (name: IndustryName) => industry.type = name;
td.replaceChildren(editIndustryName(this, industryName, setIndustryName));
} else {
const setIndustryName = (name: GvasString) => industry.type = name;
td.replaceChildren(editString(this, industry.type, setIndustryName));
td.classList.add('table-warning');
}
}
tr.appendChild(td);
// Inputs
Expand Down
10 changes: 6 additions & 4 deletions ts/StudioEditor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {GvasString, GvasText, gvasToString} from './Gvas';
import {IndustryName, IndustryNames, IndustryType, industryNames, isIndustryName} from './industries';
import {IndustryName, IndustryType, industryNames, isIndustryName, legacyIndustryNames} from './industries';
import {Permission, permissionEqual, permissionLabels, permissionToString} from './Permission';
import {Quaternion} from './Quaternion';
import {Quadruplet} from './Railroad';
Expand Down Expand Up @@ -457,9 +457,11 @@ export function editIndustryType(
type: IndustryType,
saveValue: (value: IndustryType) => void,
): Node {
const options: {[key: string]: string} = {};
for (const key of IndustryNames) {
options[key] = isIndustryName(key) ? industryNames[key] : key;
const options: {[key: number]: string} = {};
for (const [key, value] of Object.entries(legacyIndustryNames)) {
const type: IndustryType = Number(key);
const name = value;
options[type] = isIndustryName(name) ? industryNames[name] : name;
}
const save = (value: string) => saveValue(Number(value) as IndustryType);
return editDropdown(studio, String(type), options, save);
Expand Down
25 changes: 20 additions & 5 deletions ts/industries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,20 @@ export const IndustryNames = [
'engineshed_style4',
'firewooddepot',
'freightdepot',
'GoldDredge',
'GoldMine',
'GoldSmelter',
'ironoremine',
'ironworks',
'logcamp',
'MeatPackingPlant',
'oilfield',
'RailExpressAgency',
'Refinery',
'Sandhouse',
'sawmill',
'smelter',
'StampMill',
'telegraphoffice',
'watertower_1870_style1',
'watertower_1870_style2',
Expand All @@ -79,11 +84,6 @@ export const IndustryNames = [
'WaterWell',
'WheatFarm',
'Woodrick',
'GoldDredge',
'GoldMine',
'StampMill',
'GoldSmelter',
'RailExpressAgency',
] as const satisfies readonly string[];

/**
Expand Down Expand Up @@ -121,6 +121,18 @@ export const legacyIndustryNames: Record<IndustryType, IndustryName> = {
[IndustryType.wood_rick]: 'Woodrick',
};

/**
* Lookup table for legacy industry names.
*/
const legacyIndustryMap: Record<string, IndustryName> = {
'enginehouse_alpine_blue': 'enginehouse_alpine',
'enginehouse_aspen_gold': 'enginehouse_aspen',
'enginehouse_barn_red': 'enginehouse_barn',
'enginehouse_princes_mineral_brown': 'enginehouse_princess',
'SandHouse': 'Sandhouse',
'Waterwell': 'WaterWell',
};

export const isIndustryName = (name: IndustryType | GvasString): name is IndustryName =>
!!name && (typeof name === 'string') && IndustryNames.includes(name);

Expand All @@ -129,6 +141,9 @@ export type IndustryName = typeof IndustryNames[number];
export function getIndustryName(industry: Industry): IndustryName | null {
if (typeof industry.type === 'number') return legacyIndustryNames[industry.type];
if (isIndustryName(industry.type)) return industry.type;
if (industry.type === null) return null;
if (industry.type in legacyIndustryMap) return legacyIndustryMap[industry.type];
console.warn(`Unrecognized industry type ${industry.type}`);
return null;
}

Expand Down

0 comments on commit 6a074f7

Please sign in to comment.