Newer
Older
taehui / qwilight-fe / src / wow / setWowStore.ts
@Taehui Taehui on 6 Nov 6 KB 2023-11-06 오후 10:13
import { TFunction } from "i18next";
import { runInAction } from "mobx";
import { sprintf } from "sprintf-js";
import { getMillis } from "taehui-ts/date";

import { formatText } from "src/Utility";
import { Avatar } from "src/wow/Wow";
import { wwwAXIOS } from "src/Www";
import { GetWowAPI, WowAPIAvatar } from "src/wwwAPI";

export default function setLevelStore() {
  let lastMillis = getMillis();

  const formatAvatar = (
    avatars: WowAPIAvatar[],
    toValue: (value: number) => string,
    format: string,
  ): Avatar[] => {
    return avatars.map((avatar) => ({
      ...avatar,
      text: sprintf(format, toValue(avatar.value)),
    }));
  };

  return {
    isLoading: false,
    totalAvatars: [] as Avatar[],
    highestAvatars: [] as Avatar[],
    standAvatars: [] as Avatar[],
    bandAvatars: [] as Avatar[],
    totalAvatarsAt: [] as Avatar[],
    highestAvatarsAt: [] as Avatar[],
    standAvatarsAt: [] as Avatar[],
    bandAvatarsAt: [] as Avatar[],
    ability5KAvatars0: [] as Avatar[],
    ability5KAvatars1: [] as Avatar[],
    ability5KAvatars2: [] as Avatar[],
    ability5KAvatars3: [] as Avatar[],
    ability5KAvatars4: [] as Avatar[],
    ability7KAvatars0: [] as Avatar[],
    ability7KAvatars1: [] as Avatar[],
    ability7KAvatars2: [] as Avatar[],
    ability7KAvatars3: [] as Avatar[],
    ability7KAvatars4: [] as Avatar[],
    ability9KAvatars0: [] as Avatar[],
    ability9KAvatars1: [] as Avatar[],
    ability9KAvatars2: [] as Avatar[],
    ability9KAvatars3: [] as Avatar[],
    ability9KAvatars4: [] as Avatar[],
    levelAvatars0: [] as Avatar[],
    levelAvatars1: [] as Avatar[],
    levelAvatars2: [] as Avatar[],
    levelAvatars3: [] as Avatar[],
    levelAvatars4: [] as Avatar[],

    async getWow(t: TFunction) {
      const millis = getMillis();
      lastMillis = millis;
      this.isLoading = true;
      (async () => {
        const { status, data } = await wwwAXIOS.get<GetWowAPI>("/wow");
        if (millis >= lastMillis) {
          if (status === 200) {
            runInAction(() => {
              this.totalAvatars = formatAvatar(
                data.totalAvatars,
                formatText,
                t("handledUnit"),
              );
              this.highestAvatars = formatAvatar(
                data.highestAvatars,
                formatText,
                t("unitCount"),
              );
              this.standAvatars = formatAvatar(
                data.standAvatars,
                formatText,
                t("standUnit"),
              );
              this.bandAvatars = formatAvatar(
                data.bandAvatars,
                (value) => value.toString(),
                t("bandUnit"),
              );
              this.totalAvatarsAt = formatAvatar(
                data.totalAvatarsAt,
                formatText,
                t("handledUnit"),
              );
              this.highestAvatarsAt = formatAvatar(
                data.highestAvatarsAt,
                formatText,
                t("unitCount"),
              );
              this.standAvatarsAt = formatAvatar(
                data.standAvatarsAt,
                formatText,
                t("standUnit"),
              );
              this.bandAvatarsAt = formatAvatar(
                data.bandAvatarsAt,
                (value) => value.toString(),
                t("bandUnit"),
              );
              this.ability5KAvatars0 = formatAvatar(
                data.ability5KAvatars.slice(0, 10),
                formatText,
                "%s Point",
              );
              this.ability5KAvatars1 = formatAvatar(
                data.ability5KAvatars.slice(10, 20),
                formatText,
                "%s Point",
              );
              this.ability5KAvatars2 = formatAvatar(
                data.ability5KAvatars.slice(20, 30),
                formatText,
                "%s Point",
              );
              this.ability5KAvatars3 = formatAvatar(
                data.ability5KAvatars.slice(30, 40),
                formatText,
                "%s Point",
              );
              this.ability5KAvatars4 = formatAvatar(
                data.ability5KAvatars.slice(40, 50),
                formatText,
                "%s Point",
              );
              this.ability7KAvatars0 = formatAvatar(
                data.ability7KAvatars.slice(0, 10),
                formatText,
                "%s Point",
              );
              this.ability7KAvatars1 = formatAvatar(
                data.ability7KAvatars.slice(10, 20),
                formatText,
                "%s Point",
              );
              this.ability7KAvatars2 = formatAvatar(
                data.ability7KAvatars.slice(20, 30),
                formatText,
                "%s Point",
              );
              this.ability7KAvatars3 = formatAvatar(
                data.ability7KAvatars.slice(30, 40),
                formatText,
                "%s Point",
              );
              this.ability7KAvatars4 = formatAvatar(
                data.ability7KAvatars.slice(40, 50),
                formatText,
                "%s Point",
              );
              this.ability9KAvatars0 = formatAvatar(
                data.ability9KAvatars.slice(0, 10),
                formatText,
                "%s Point",
              );
              this.ability9KAvatars1 = formatAvatar(
                data.ability9KAvatars.slice(10, 20),
                formatText,
                "%s Point",
              );
              this.ability9KAvatars2 = formatAvatar(
                data.ability9KAvatars.slice(20, 30),
                formatText,
                "%s Point",
              );
              this.ability9KAvatars3 = formatAvatar(
                data.ability9KAvatars.slice(30, 40),
                formatText,
                "%s Point",
              );
              this.ability9KAvatars4 = formatAvatar(
                data.ability9KAvatars.slice(40, 50),
                formatText,
                "%s Point",
              );
              this.levelAvatars0 = formatAvatar(
                data.levelAvatars.slice(0, 10),
                (value) => value.toString(),
                "LV. %s",
              );
              this.levelAvatars1 = formatAvatar(
                data.levelAvatars.slice(10, 20),
                (value) => value.toString(),
                "LV. %s",
              );
              this.levelAvatars2 = formatAvatar(
                data.levelAvatars.slice(20, 30),
                (value) => value.toString(),
                "LV. %s",
              );
              this.levelAvatars3 = formatAvatar(
                data.levelAvatars.slice(30, 40),
                (value) => value.toString(),
                "LV. %s",
              );
              this.levelAvatars4 = formatAvatar(
                data.levelAvatars.slice(40, 50),
                (value) => value.toString(),
                "LV. %s",
              );
              this.isLoading = false;
            });
          }
        }
      })();
    },
  };
}