Hunt method times can now be customized by the user.

This commit is contained in:
Daan Vanden Bosch 2019-06-22 01:12:39 +02:00
parent 0eb4cf6b96
commit adaa36031e
3 changed files with 106 additions and 58 deletions

View File

@ -1,5 +1,5 @@
import Logger from 'js-logger';
import { observable } from "mobx";
import { autorun, IReactionDisposer, observable } from "mobx";
import { HuntMethod, NpcType, Server, SimpleQuest } from "../domain";
import { QuestDto } from "../dto";
import { Loadable } from "../Loadable";
@ -12,13 +12,15 @@ class HuntMethodStore {
new Loadable([], () => this.loadHuntMethods(server))
);
private storageDisposer?: IReactionDisposer;
private async loadHuntMethods(server: Server): Promise<HuntMethod[]> {
const response = await fetch(
`${process.env.PUBLIC_URL}/quests.${Server[server].toLowerCase()}.json`
);
const quests = await response.json() as QuestDto[];
return quests.map(quest => {
const methods = quests.map(quest => {
let totalCount = 0;
const enemyCounts = new Map<NpcType, number>();
@ -45,6 +47,54 @@ class HuntMethodStore {
/^\d-\d.*/.test(quest.name) ? 0.75 : (totalCount > 400 ? 0.75 : 0.5)
);
});
this.loadFromLocalStorage(methods, server);
return methods;
}
private loadFromLocalStorage = (methods: HuntMethod[], server: Server) => {
try {
const methodUserTimesJson = localStorage.getItem(
`HuntMethodStore.methodUserTimes.${Server[server]}`
);
if (methodUserTimesJson) {
const userTimes = JSON.parse(methodUserTimesJson);
for (const method of methods) {
method.userTime = userTimes[method.id] as number;
}
}
if (this.storageDisposer) {
this.storageDisposer();
}
this.storageDisposer = autorun(() =>
this.storeInLocalStorage(methods, server)
);
} catch (e) {
logger.error(e);
}
}
private storeInLocalStorage = (methods: HuntMethod[], server: Server) => {
try {
const userTimes: any = {};
for (const method of methods) {
if (method.userTime != null) {
userTimes[method.id] = method.userTime;
}
}
localStorage.setItem(
`HuntMethodStore.methodUserTimes.${Server[server]}`,
JSON.stringify(userTimes)
);
} catch (e) {
logger.error(e);
}
}
}

View File

@ -55,6 +55,7 @@ class HuntOptimizerStore {
);
}
// TODO: wanted items per server.
@observable readonly wantedItems: IObservableArray<WantedItem> = observable.array();
@observable result?: OptimalResult;
@ -62,56 +63,6 @@ class HuntOptimizerStore {
this.initialize();
}
initialize = async () => {
try {
await this.loadFromLocalStorage();
autorun(this.storeInLocalStorage);
} catch (e) {
logger.error(e);
}
}
loadFromLocalStorage = async () => {
const wantedItemsJson = localStorage.getItem(
`HuntOptimizerStore.wantedItems.${Server[applicationStore.currentServer]}`
);
if (wantedItemsJson) {
const itemStore = await itemTypeStores.current.promise;
const wi = JSON.parse(wantedItemsJson);
const wantedItems: WantedItem[] = [];
for (const { itemTypeId, itemKindId, amount } of wi) {
const item = itemTypeId != null
? itemStore.getById(itemTypeId)
: itemStore.getById(itemKindId); // Legacy name.
if (item) {
wantedItems.push(new WantedItem(item, amount));
}
}
this.wantedItems.replace(wantedItems);
}
}
storeInLocalStorage = () => {
try {
localStorage.setItem(
`HuntOptimizerStore.wantedItems.${Server[applicationStore.currentServer]}`,
JSON.stringify(
this.wantedItems.map(({ itemType, amount }) => ({
itemTypeId: itemType.id,
amount
}))
)
);
} catch (e) {
logger.error(e);
}
}
optimize = async () => {
if (!this.wantedItems.length) {
this.result = undefined;
@ -347,6 +298,56 @@ class HuntOptimizerStore {
if (splitPanArms) name += '\tspa';
return name;
}
private initialize = async () => {
try {
await this.loadFromLocalStorage();
autorun(this.storeInLocalStorage);
} catch (e) {
logger.error(e);
}
}
private loadFromLocalStorage = async () => {
const wantedItemsJson = localStorage.getItem(
`HuntOptimizerStore.wantedItems.${Server[applicationStore.currentServer]}`
);
if (wantedItemsJson) {
const itemStore = await itemTypeStores.current.promise;
const wi = JSON.parse(wantedItemsJson);
const wantedItems: WantedItem[] = [];
for (const { itemTypeId, itemKindId, amount } of wi) {
const item = itemTypeId != null
? itemStore.getById(itemTypeId)
: itemStore.getById(itemKindId); // Legacy name.
if (item) {
wantedItems.push(new WantedItem(item, amount));
}
}
this.wantedItems.replace(wantedItems);
}
}
private storeInLocalStorage = () => {
try {
localStorage.setItem(
`HuntOptimizerStore.wantedItems.${Server[applicationStore.currentServer]}`,
JSON.stringify(
this.wantedItems.map(({ itemType, amount }) => ({
itemTypeId: itemType.id,
amount
}))
)
);
} catch (e) {
logger.error(e);
}
}
}
export const huntOptimizerStore = new HuntOptimizerStore();

View File

@ -3,12 +3,11 @@ import { observer } from "mobx-react";
import moment, { Moment } from "moment";
import React from "react";
import { AutoSizer, Index } from "react-virtualized";
import { HuntMethod, Episode } from "../../domain";
import { Episode, HuntMethod } from "../../domain";
import { EnemyNpcTypes } from "../../domain/NpcType";
import { huntMethodStore } from "../../stores/HuntMethodStore";
import { Column, BigTable } from "../BigTable";
import { BigTable, Column } from "../BigTable";
import "./MethodsComponent.css";
import { hoursToString } from "../time";
@observer
export class MethodsComponent extends React.Component {
@ -28,9 +27,7 @@ export class MethodsComponent extends React.Component {
{
name: 'Time',
width: 50,
cellRenderer: (method) => hoursToString(method.time),
// TODO: enable when methods have IDs so edits can be saved.
// cellRenderer: (method) => <TimeComponent method={method} />,
cellRenderer: (method) => <TimeComponent method={method} />,
className: 'integrated',
},
];