1
0
forked from a/lifeto-shop
lifeto-shop/src/lib/lifeto/stateful.ts

50 lines
1.4 KiB
TypeScript
Raw Normal View History

2022-07-06 07:48:11 +00:00
import { RefStore } from "../../state/state";
2022-07-07 07:54:36 +00:00
import { bank_endpoint, Session } from "../session";
2022-07-06 07:48:11 +00:00
import { TricksterAccount, TricksterInventory } from "../trickster";
2022-07-07 07:54:36 +00:00
import { BankEndpoint, LTOApi } from "./api";
2022-07-06 07:48:11 +00:00
export interface SessionBinding {
new(s:Session):LTOApi
}
export const getLTOState = <A extends LTOApi>(c: new (s:Session) => A,s:Session, r:RefStore): LTOApi => {
return new StatefulLTOApi(new c(s),r);
}
export class StatefulLTOApi implements LTOApi {
u: LTOApi
r: RefStore
constructor(s:LTOApi, r:RefStore){
this.u = s
this.r=r
}
2022-07-07 07:54:36 +00:00
BankAction = <T,D>(e: BankEndpoint, t: T):Promise<D> => {
return this.u.BankAction(e,t)
}
2022-07-06 07:48:11 +00:00
GetInventory = async (path:string):Promise<TricksterInventory>=>{
const inv = await this.u.GetInventory(path)
2022-07-07 07:54:36 +00:00
if(this.r.invs.value.get(inv.path)){
this.r.invs.value.get(inv.path)!.items = inv.items
}else{
this.r.invs.value.set(inv.path,inv)
}
if(inv.galders) {
this.r.invs.value.get(inv.path)!.galders = inv.galders
}
2022-07-06 07:48:11 +00:00
this.r.dirty.value = this.r.dirty.value + 1
return inv
}
GetAccounts = async ():Promise<TricksterAccount[]> => {
const xs = await this.u.GetAccounts()
xs.forEach((x)=>{
x.characters.forEach((ch)=>{
this.r.chars.value.set(ch.path,ch)
})
})
return xs
}
GetLoggedin= async ():Promise<boolean>=>{
return this.u.GetLoggedin()
}
}