diff --git a/src/logger.ts b/src/logger.ts index bd41186bd03..ba7e38d52d4 100644 --- a/src/logger.ts +++ b/src/logger.ts @@ -11,7 +11,7 @@ import Battle from "./battle"; import { getBiomeName, PokemonPools, SpeciesTree } from "./data/biomes"; import { trainerConfigs } from "./data/trainer-config"; import { Mode } from "./ui/ui"; -import { TitlePhase } from "./phases"; +import { LoginPhase, TitlePhase } from "./phases"; import { Item } from "pokenode-ts"; import Trainer from "./field/trainer"; import { Species } from "./enums/species"; @@ -117,7 +117,7 @@ export function enemyPokeName(scene: BattleScene, index: integer | Pokemon | Ene // LoggerTools.logActions(this.scene, this.scene.currentBattle.waveIndex, "") export const rarities = [] -export const rarityslot = [0] +export const rarityslot = [0, ""] export const isPreSwitch: Utils.BooleanHolder = new Utils.BooleanHolder(false); @@ -376,6 +376,47 @@ export function generateOption(i: integer, saves: any): OptionSelectItem { } return op; } +/** + * Generates a UI option to save a log to your device. + * @param i The slot number. Corresponds to an index in `logs`. + * @param saves Your session data. Used to label logs if they match one of your save slots. + * @returns A UI option. + */ +export function generateEditOption(scene: BattleScene, i: integer, saves: any, phase: TitlePhase): OptionSelectItem { + var filename: string = (JSON.parse(localStorage.getItem(logs[i][1])) as DRPD).title + var op: OptionSelectItem = { + label: `Export ${filename} (${getSize(printDRPD("", "", JSON.parse(localStorage.getItem(logs[i][1])) as DRPD))})`, + handler: () => { + rarityslot[1] = logs[i][1] + //scene.phaseQueue[0].end() + scene.ui.setMode(Mode.NAME_LOG, { + autofillfields: [ + (JSON.parse(localStorage.getItem(logs[i][1])) as DRPD).title, + (JSON.parse(localStorage.getItem(logs[i][1])) as DRPD).authors.join(", ") + ], + buttonActions: [ + () => { + scene.ui.playSelect(); + console.log("Ending UI phase thingy"); + phase.callEnd() + } + ] + }); + return false; + } + } + for (var j = 0; j < saves.length; j++) { + console.log(saves[j].seed, logs[i][2], saves[j].seed == logs[i][2]) + if (saves[j].seed == logs[i][2]) { + op.label = "[Slot " + (saves[j].slot + 1) + "]" + op.label.substring(6) + } + } + if (logs[i][4] != "") { + op.label = " " + op.label + op.item = logs[i][4] + } + return op; +} /** * Generates an option to create a new log. * @@ -424,6 +465,27 @@ export function appendLog(keyword: string, data: string) { export function clearLog(keyword: string) { localStorage.setItem(logs[logKeys.indexOf(keyword)][1], "---- " + logs[logKeys.indexOf(keyword)][3] + " ----" + logs[logKeys.indexOf(keyword)][5]) } +export function setFileInfo(title: string, authors: string[]) { + var fileID = rarityslot[1] as string + var drpd = JSON.parse(localStorage.getItem(fileID)) as DRPD; + drpd.title = title; + for (var i = 0; i < authors.length; i++) { + while (authors[i][0] == " ") { + authors[i] = authors[i].substring(1) + } + while (authors[i][authors[i].length - 1] == " ") { + authors[i] = authors[i].substring(0, authors[i].length - 1) + } + } + for (var i = 0; i < authors.length; i++) { + if (authors[i] == "") { + authors.splice(i, 1) + i--; + } + } + drpd.authors = authors; + localStorage.setItem(fileID, JSON.stringify(drpd)) +} /** * Saves a log to your device. * @param keyword The identifier key for the log you want to save. diff --git a/src/ui/log-name-form-ui-handler.ts b/src/ui/log-name-form-ui-handler.ts index 83a883c73ab..b4b69e93aee 100644 --- a/src/ui/log-name-form-ui-handler.ts +++ b/src/ui/log-name-form-ui-handler.ts @@ -3,6 +3,8 @@ import { ModalConfig } from "./modal-ui-handler"; import * as Utils from "../utils"; import { Mode } from "./ui"; import i18next from "i18next"; +import * as LoggerTools from "../logger"; +import { addTextObject, TextStyle } from "./text"; export default class LogNameFormUiHandler extends FormModalUiHandler { getModalTitle(config?: ModalConfig): string { @@ -44,38 +46,36 @@ export default class LogNameFormUiHandler extends FormModalUiHandler { return super.getReadableErrorMessage(error); } + setup(): void { + super.setup(); + + //const label = addTextObject(this.scene, 10, 87, "Text", TextStyle.TOOLTIP_CONTENT, { fontSize: "42px" }); + + //this.modalContainer.add(label); + } + show(args: any[]): boolean { + console.error("Shown") if (super.show(args)) { const config = args[0] as ModalConfig; const originalLoginAction = this.submitAction; + this.inputs[0].setText(args[0].autofillfields[0]) + this.inputs[1].setText(args[0].autofillfields[1]) this.submitAction = (_) => { // Prevent overlapping overrides on action modification this.submitAction = originalLoginAction; this.sanitizeInputs(); this.scene.ui.setMode(Mode.LOADING, { buttonActions: [] }); const onFail = error => { - this.scene.ui.setMode(Mode.LOGIN_FORM, Object.assign(config, { errorMessage: error?.trim() })); + this.scene.ui.setMode(Mode.NAME_LOG, Object.assign(config, { errorMessage: error?.trim() })); this.scene.ui.playError(); }; if (!this.inputs[0].text) { - return onFail(i18next.t("menu:emptyUsername")); + //return onFail(i18next.t("menu:emptyUsername")); } - Utils.apiPost("account/login", `username=${encodeURIComponent(this.inputs[0].text)}&password=${encodeURIComponent(this.inputs[1].text)}`, "application/x-www-form-urlencoded") - .then(response => { - if (!response.ok) { - return response.text(); - } - return response.json(); - }) - .then(response => { - if (response.hasOwnProperty("token")) { - Utils.setCookie(Utils.sessionIdKey, response.token); - originalLoginAction(); - } else { - onFail(response); - } - }); + LoggerTools.setFileInfo(this.inputs[0].text, this.inputs[1].text.split(",")) + originalLoginAction() }; return true; diff --git a/src/ui/ui.ts b/src/ui/ui.ts index eb3be06fed8..d4dddab5922 100644 --- a/src/ui/ui.ts +++ b/src/ui/ui.ts @@ -97,7 +97,8 @@ const transitionModes = [ Mode.EGG_HATCH_SCENE, Mode.EGG_LIST, Mode.EGG_GACHA, - Mode.CHALLENGE_SELECT + Mode.CHALLENGE_SELECT, + Mode.NAME_LOG ]; const noTransitionModes = [ @@ -121,8 +122,7 @@ const noTransitionModes = [ Mode.LOADING, Mode.SESSION_RELOAD, Mode.UNAVAILABLE, - Mode.OUTDATED, - Mode.NAME_LOG + Mode.OUTDATED ]; export default class UI extends Phaser.GameObjects.Container {