Skip to content

Commit a87754f

Browse files
committed
Refactor player arguments initialization to improve readability and maintainability
1 parent a8aebc8 commit a87754f

File tree

1 file changed

+41
-51
lines changed

1 file changed

+41
-51
lines changed

src/stores/game.ts

Lines changed: 41 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { observable, action, computed, makeAutoObservable } from "mobx";
22
import { ipcRenderer, IpcRendererEvent } from "electron";
33
import { userConfigStore, get as getConfig } from "src/config";
44
import { RootStore } from "src/utils/useStore";
5+
import { PlayerArguments } from "../interfaces/config";
56

67
export default class GameStore {
78
@observable
@@ -56,63 +57,52 @@ export default class GameStore {
5657
const awsSinkGuid: string = ipcRenderer.sendSync(
5758
"get-aws-sink-cloudwatch-guid",
5859
);
59-
const dataProviderUrl = getConfig("DataProviderUrl");
60-
const portalUrl = getConfig("OnboardingPortalUrl");
61-
const unitySentrySampleRate = getConfig("UnitySentrySampleRate", 0);
62-
const marketServiceUrl = getConfig("MarketServiceUrl");
63-
const patrolRewardServiceUrl = getConfig("PatrolRewardServiceUrl");
64-
const seasonPassServiceUrl = getConfig("SeasonPassServiceUrl");
65-
const meadPledgePortalUrl = getConfig("MeadPledgePortalUrl");
6660
const genesisBlockPath = getConfig("GenesisBlockPath");
6761
const appProtocolVersion = getConfig("AppProtocolVersion");
68-
const IAPServiceHostUrl = getConfig("IAPServiceHostUrl");
69-
const appleMarketUrl = getConfig("AppleMarketUrl");
70-
const googleMarketUrl = getConfig("GoogleMarketUrl");
71-
const guildServiceUrl = getConfig("GuildServiceUrl");
72-
const guildIconBucket = getConfig("GuildIconBucket");
73-
const maintenance = getConfig("Maintenance", false);
7462
const planetRegistryUrl = getConfig("PlanetRegistryUrl");
75-
const arenaUrl = getConfig("ArenaServiceUrl");
76-
77-
const playerArgs = [
78-
`--private-key=${privateKey}`,
79-
`--rpc-client=true`,
80-
`--rpc-server-host=${host}`,
81-
`--rpc-server-port=${port}`,
82-
`--selected-planet-id=${planetId}`,
83-
`--genesis-block-path=${genesisBlockPath}`,
84-
`--language=${this._language}`,
85-
`--app-protocol-version=${appProtocolVersion}`,
86-
`--aws-sink-guid=${awsSinkGuid}`,
87-
`--on-boarding-host=${portalUrl}`,
88-
`--sentry-sample-rate=${unitySentrySampleRate}`,
89-
`--market-service-host=${marketServiceUrl}`,
90-
`--patrol-reward-service-host=${patrolRewardServiceUrl}`,
91-
`--season-pass-service-host=${seasonPassServiceUrl}`,
92-
`--mead-pledge-portal-url=${meadPledgePortalUrl}`,
93-
`--iap-service-host=${IAPServiceHostUrl}`,
94-
`--apple-market-url=${appleMarketUrl}`,
95-
`--google-market-url=${googleMarketUrl}`,
96-
`--arena-service-host=${arenaUrl}`,
97-
];
98-
99-
const appendIfDefined = (value: string | undefined, label: string) => {
100-
if (value !== undefined) {
101-
playerArgs.push(`--${label}=${value}`);
63+
const playerArguments = getConfig("PlayerConfig");
64+
65+
const initializePlayerArgs = (): string[] => {
66+
const args: string[] = [
67+
`--private-key=${privateKey}`,
68+
`--rpc-client`,
69+
`--rpc-server-host=${host}`,
70+
`--rpc-server-port=${port}`,
71+
`--selected-planet-id=${planetId}`,
72+
`--genesis-block-path=${genesisBlockPath}`,
73+
`--language=${this._language}`,
74+
`--app-protocol-version=${appProtocolVersion}`,
75+
`--planet-registry-url=${planetRegistryUrl}`,
76+
];
77+
78+
const appendIfDefined = (value: string | undefined, label: string) => {
79+
if (value !== undefined) {
80+
args.push(`--${label}=${value}`);
81+
}
82+
};
83+
84+
const pascalToKebabCase = (input: string): string => {
85+
return input
86+
.replace(/([a-z])([A-Z])/g, "$1-$2") // Add hyphen between lowercase and uppercase
87+
.replace(/([A-Z])([A-Z][a-z])/g, "$1-$2") // Handle consecutive uppercase letters
88+
.toLowerCase(); // Convert entire string to lowercase
89+
};
90+
91+
for (const [key, value] of Object.entries(playerArguments)) {
92+
const pascalKey = pascalToKebabCase(key);
93+
if (typeof value === "boolean") {
94+
if (value) {
95+
args.push(`--${pascalKey}`);
96+
}
97+
} else {
98+
appendIfDefined(value, pascalKey);
99+
}
102100
}
103-
};
104-
105-
appendIfDefined(dataProviderUrl, "api-server-host");
106-
if (planetId !== "0x000000000000" && planetId !== "0x100000000000") {
107-
appendIfDefined(guildServiceUrl, "guild-service-url");
108-
appendIfDefined(guildIconBucket, "guild-icon-bucket");
109-
}
110101

111-
if (maintenance) {
112-
playerArgs.push(`--maintenance=${maintenance}`);
113-
}
102+
return args;
103+
};
114104

115-
appendIfDefined(planetRegistryUrl, "planet-registry-url");
105+
const playerArgs = initializePlayerArgs();
116106

117107
ipcRenderer.send("launch game", {
118108
args: playerArgs,

0 commit comments

Comments
 (0)