s3si.ts/initRank.ts

86 lines
2.3 KiB
TypeScript
Raw Normal View History

2022-10-28 05:07:18 -04:00
/**
* If rankState in profile.json is not defined, it will be initialized.
*/
import { flags } from "./deps.ts";
import { Splatnet3 } from "./src/splatnet3.ts";
2022-11-18 07:32:30 -05:00
import { gameId, readline } from "./src/utils.ts";
import { FileStateBackend, Profile } from "./src/state.ts";
2022-10-28 05:07:18 -04:00
import { BattleListType } from "./src/types.ts";
import { RANK_PARAMS } from "./src/RankTracker.ts";
import { DEFAULT_ENV } from "./src/env.ts";
2022-10-28 05:07:18 -04:00
const parseArgs = (args: string[]) => {
const parsed = flags.parse(args, {
string: ["profilePath"],
alias: {
"help": "h",
"profilePath": ["p", "profile-path"],
},
});
return parsed;
};
const opts = parseArgs(Deno.args);
if (opts.help) {
console.log(
`Usage: deno run -A ${Deno.mainModule} [options]
Options:
--profile-path <path>, -p Path to config file (default: ./profile.json)
--help Show this help message and exit`,
);
Deno.exit(0);
}
const env = DEFAULT_ENV;
2022-10-28 05:07:18 -04:00
const stateBackend = new FileStateBackend(opts.profilePath ?? "./profile.json");
const profile = new Profile({ stateBackend, env });
await profile.readState();
2022-10-28 05:07:18 -04:00
if (profile.state.rankState) {
2022-10-28 05:07:18 -04:00
console.log("rankState is already initialized.");
Deno.exit(0);
}
const splatnet = new Splatnet3({ profile, env });
2022-10-28 05:07:18 -04:00
const battleList = await splatnet.getBattleList(BattleListType.Bankara);
2022-10-28 05:07:18 -04:00
if (battleList.length === 0) {
console.log("No anarchy battle found. Did you play anarchy?");
Deno.exit(0);
}
const { vsHistoryDetail: detail } = await splatnet.getBattleDetail(
battleList[0],
);
2022-10-28 05:07:18 -04:00
console.log(
2022-10-28 08:45:56 -04:00
`Your latest anarchy battle is played at ${
2022-10-28 05:07:18 -04:00
new Date(detail.playedTime).toLocaleString()
}. Please enter your rank after this battle(format: RANK,POINT. S+0,300):`,
);
while (true) {
2022-11-18 07:32:30 -05:00
const userInput = await readline();
2022-10-28 05:07:18 -04:00
const [rank, point] = userInput.split(",");
const pointNumber = parseInt(point);
if (!RANK_PARAMS.find((i) => i.rank === rank)) {
console.log("Invalid rank. Please enter again:");
} else if (isNaN(pointNumber)) {
console.log("Invalid point. Please enter again:");
} else {
profile.writeState({
...profile.state,
2022-10-28 05:07:18 -04:00
rankState: {
gameId: await gameId(detail.id),
rank,
rankPoint: pointNumber,
},
});
2022-10-28 05:07:18 -04:00
break;
}
}
console.log("rankState is initialized.");