forked from a/lifeto-shop
40 lines
1.1 KiB
TypeScript
40 lines
1.1 KiB
TypeScript
|
|
import { RefStore } from "../../state/state";
|
||
|
|
import { Session } from "../session";
|
||
|
|
import { TricksterAccount, TricksterInventory } from "../trickster";
|
||
|
|
import { LTOApi } from "./api";
|
||
|
|
|
||
|
|
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
|
||
|
|
}
|
||
|
|
GetInventory = async (path:string):Promise<TricksterInventory>=>{
|
||
|
|
const inv = await this.u.GetInventory(path)
|
||
|
|
this.r.invs.value.set(inv.path,inv)
|
||
|
|
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()
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|