2025-01-12 15:33:05 -08:00
|
|
|
import type { InputFieldConfig } from "./form-modal-ui-handler";
|
|
|
|
import { FormModalUiHandler } from "./form-modal-ui-handler";
|
|
|
|
import type { ModalConfig } from "./modal-ui-handler";
|
2023-12-30 18:41:25 -05:00
|
|
|
import { Mode } from "./ui";
|
2024-04-18 19:08:53 -04:00
|
|
|
import { TextStyle, addTextObject } from "./text";
|
2024-06-17 17:05:33 -04:00
|
|
|
import i18next from "i18next";
|
2024-11-04 12:57:21 -08:00
|
|
|
import { pokerogueApi } from "#app/plugins/api/pokerogue-api";
|
2025-01-12 15:33:05 -08:00
|
|
|
import { globalScene } from "#app/global-scene";
|
2023-12-30 18:41:25 -05:00
|
|
|
|
2024-09-05 18:52:48 +09:00
|
|
|
|
|
|
|
interface LanguageSetting {
|
|
|
|
inputFieldFontSize?: string,
|
|
|
|
warningMessageFontSize?: string,
|
|
|
|
errorMessageFontSize?: string,
|
|
|
|
}
|
|
|
|
|
|
|
|
const languageSettings: { [key: string]: LanguageSetting } = {
|
2024-10-31 21:54:05 +01:00
|
|
|
"es-ES": {
|
2024-09-05 18:52:48 +09:00
|
|
|
inputFieldFontSize: "50px",
|
|
|
|
errorMessageFontSize: "40px",
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-12-30 18:41:25 -05:00
|
|
|
export default class RegistrationFormUiHandler extends FormModalUiHandler {
|
|
|
|
getModalTitle(config?: ModalConfig): string {
|
2024-05-23 17:03:10 +02:00
|
|
|
return i18next.t("menu:register");
|
2023-12-30 18:41:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
getWidth(config?: ModalConfig): number {
|
|
|
|
return 160;
|
|
|
|
}
|
|
|
|
|
|
|
|
getMargin(config?: ModalConfig): [number, number, number, number] {
|
|
|
|
return [ 0, 0, 48, 0 ];
|
|
|
|
}
|
|
|
|
|
2024-04-18 19:08:53 -04:00
|
|
|
getButtonTopMargin(): number {
|
|
|
|
return 8;
|
|
|
|
}
|
|
|
|
|
2023-12-30 18:41:25 -05:00
|
|
|
getButtonLabels(config?: ModalConfig): string[] {
|
2024-05-23 17:03:10 +02:00
|
|
|
return [ i18next.t("menu:register"), i18next.t("menu:backToLogin") ];
|
2023-12-30 18:41:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
getReadableErrorMessage(error: string): string {
|
2024-05-23 17:03:10 +02:00
|
|
|
const colonIndex = error?.indexOf(":");
|
|
|
|
if (colonIndex > 0) {
|
2023-12-30 18:41:25 -05:00
|
|
|
error = error.slice(0, colonIndex);
|
2024-05-23 17:03:10 +02:00
|
|
|
}
|
2023-12-30 18:41:25 -05:00
|
|
|
switch (error) {
|
2024-10-19 18:44:36 -07:00
|
|
|
case "invalid username":
|
|
|
|
return i18next.t("menu:invalidRegisterUsername");
|
|
|
|
case "invalid password":
|
|
|
|
return i18next.t("menu:invalidRegisterPassword");
|
|
|
|
case "failed to add account record":
|
|
|
|
return i18next.t("menu:usernameAlreadyUsed");
|
2023-12-30 18:41:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return super.getReadableErrorMessage(error);
|
|
|
|
}
|
|
|
|
|
2024-10-24 06:00:07 +10:00
|
|
|
override getInputFieldConfigs(): InputFieldConfig[] {
|
|
|
|
const inputFieldConfigs: InputFieldConfig[] = [];
|
|
|
|
inputFieldConfigs.push({ label: i18next.t("menu:username") });
|
|
|
|
inputFieldConfigs.push({ label: i18next.t("menu:password"), isPassword: true });
|
|
|
|
inputFieldConfigs.push({ label: i18next.t("menu:confirmPassword"), isPassword: true });
|
|
|
|
return inputFieldConfigs;
|
|
|
|
}
|
|
|
|
|
2024-04-18 19:08:53 -04:00
|
|
|
setup(): void {
|
|
|
|
super.setup();
|
|
|
|
|
2024-09-05 18:52:48 +09:00
|
|
|
this.modalContainer.list.forEach((child: Phaser.GameObjects.GameObject) => {
|
|
|
|
if (child instanceof Phaser.GameObjects.Text && child !== this.titleText) {
|
|
|
|
const inputFieldFontSize = languageSettings[i18next.resolvedLanguage!]?.inputFieldFontSize;
|
|
|
|
if (inputFieldFontSize) {
|
|
|
|
child.setFontSize(inputFieldFontSize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const warningMessageFontSize = languageSettings[i18next.resolvedLanguage!]?.warningMessageFontSize ?? "42px";
|
2025-01-12 15:33:05 -08:00
|
|
|
const label = addTextObject(10, 87, i18next.t("menu:registrationAgeWarning"), TextStyle.TOOLTIP_CONTENT, { fontSize: warningMessageFontSize });
|
2024-04-18 19:08:53 -04:00
|
|
|
|
|
|
|
this.modalContainer.add(label);
|
|
|
|
}
|
|
|
|
|
2023-12-30 18:41:25 -05:00
|
|
|
show(args: any[]): boolean {
|
|
|
|
if (super.show(args)) {
|
|
|
|
const config = args[0] as ModalConfig;
|
|
|
|
|
|
|
|
const originalRegistrationAction = this.submitAction;
|
|
|
|
this.submitAction = (_) => {
|
|
|
|
// Prevent overlapping overrides on action modification
|
|
|
|
this.submitAction = originalRegistrationAction;
|
|
|
|
this.sanitizeInputs();
|
2025-01-12 15:33:05 -08:00
|
|
|
globalScene.ui.setMode(Mode.LOADING, { buttonActions: []});
|
2023-12-30 18:41:25 -05:00
|
|
|
const onFail = error => {
|
2025-01-12 15:33:05 -08:00
|
|
|
globalScene.ui.setMode(Mode.REGISTRATION_FORM, Object.assign(config, { errorMessage: error?.trim() }));
|
|
|
|
globalScene.ui.playError();
|
2024-09-05 18:52:48 +09:00
|
|
|
const errorMessageFontSize = languageSettings[i18next.resolvedLanguage!]?.errorMessageFontSize;
|
|
|
|
if (errorMessageFontSize) {
|
|
|
|
this.errorMessage.setFontSize(errorMessageFontSize);
|
|
|
|
}
|
2023-12-30 18:41:25 -05:00
|
|
|
};
|
2024-05-23 17:03:10 +02:00
|
|
|
if (!this.inputs[0].text) {
|
|
|
|
return onFail(i18next.t("menu:emptyUsername"));
|
|
|
|
}
|
|
|
|
if (!this.inputs[1].text) {
|
|
|
|
return onFail(this.getReadableErrorMessage("invalid password"));
|
|
|
|
}
|
|
|
|
if (this.inputs[1].text !== this.inputs[2].text) {
|
|
|
|
return onFail(i18next.t("menu:passwordNotMatchingConfirmPassword"));
|
|
|
|
}
|
2024-11-04 12:57:21 -08:00
|
|
|
const [ usernameInput, passwordInput ] = this.inputs;
|
|
|
|
pokerogueApi.account.register({ username: usernameInput.text, password: passwordInput.text })
|
|
|
|
.then(registerError => {
|
|
|
|
if (!registerError) {
|
|
|
|
pokerogueApi.account.login({ username: usernameInput.text, password: passwordInput.text })
|
|
|
|
.then(loginError => {
|
|
|
|
if (!loginError) {
|
2024-08-07 09:23:12 -07:00
|
|
|
originalRegistrationAction && originalRegistrationAction();
|
2024-05-23 17:03:10 +02:00
|
|
|
} else {
|
2024-11-04 12:57:21 -08:00
|
|
|
onFail(loginError);
|
2024-05-23 17:03:10 +02:00
|
|
|
}
|
2023-12-30 18:41:25 -05:00
|
|
|
});
|
2024-05-23 17:03:10 +02:00
|
|
|
} else {
|
2024-11-04 12:57:21 -08:00
|
|
|
onFail(registerError);
|
2024-05-23 17:03:10 +02:00
|
|
|
}
|
2023-12-30 18:41:25 -05:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2024-05-23 17:03:10 +02:00
|
|
|
}
|