2022-07-03 10:25:12 +00:00
|
|
|
import { Axios, AxiosResponse } from "axios"
|
|
|
|
|
import log from "loglevel"
|
2022-07-03 15:50:41 +00:00
|
|
|
import { RefStore } from "../state/state"
|
2022-07-03 10:25:12 +00:00
|
|
|
import { Session } from "./session"
|
2022-07-03 15:50:41 +00:00
|
|
|
import { dummyChar, TricksterInventory, TricksterItem, TricksterWallet } from "./trickster"
|
|
|
|
|
|
2022-07-03 10:25:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
export interface LTOApi {
|
2022-07-03 15:50:41 +00:00
|
|
|
GetAccount:(name:string) =>Promise<[TricksterInventory, Array<string>]>
|
|
|
|
|
GetInventory:(path:string)=>Promise<TricksterInventory>
|
2022-07-03 10:25:12 +00:00
|
|
|
GetAccounts:() =>Promise<Array<string>>
|
|
|
|
|
GetLoggedin: ()=>Promise<boolean>
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-03 15:50:41 +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
|
|
|
|
|
}
|
|
|
|
|
GetAccount = async (name:string):Promise<[TricksterInventory, Array<string>]> =>{
|
|
|
|
|
const account = await this.u.GetAccount(name)
|
|
|
|
|
this.r.invs.value.set(account[0].path,account[0])
|
|
|
|
|
account[1].forEach((s)=>{
|
|
|
|
|
const d = dummyChar(s)
|
|
|
|
|
this.r.invs.value.set(d.path,d)
|
|
|
|
|
})
|
|
|
|
|
this.r.dirty.value = this.r.dirty.value + 1
|
|
|
|
|
return account
|
|
|
|
|
}
|
|
|
|
|
GetInventory = async (path:string):Promise<TricksterInventory>=>{
|
|
|
|
|
if(!path.includes("/")) {
|
|
|
|
|
const a = await this.GetAccount(path)
|
|
|
|
|
return a[0]
|
|
|
|
|
}
|
|
|
|
|
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<Array<string>> =>{
|
|
|
|
|
return this.u.GetAccounts()
|
|
|
|
|
}
|
|
|
|
|
GetLoggedin= async ():Promise<boolean>=>{
|
|
|
|
|
return this.u.GetLoggedin()
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-07-03 10:25:12 +00:00
|
|
|
|
2022-07-03 15:50:41 +00:00
|
|
|
export class LTOApiv0 implements LTOApi {
|
2022-07-03 10:25:12 +00:00
|
|
|
s: Session
|
|
|
|
|
|
|
|
|
|
constructor(s:Session) {
|
|
|
|
|
this.s = s
|
|
|
|
|
}
|
2022-07-03 15:50:41 +00:00
|
|
|
|
|
|
|
|
GetAccount = async (account: string):Promise<[TricksterInventory, Array<string>]> => {
|
2022-07-03 10:25:12 +00:00
|
|
|
return this.s.authed_request("GET", `item-manager/items/account/${account}`,undefined).then(async (ans:AxiosResponse)=>{
|
|
|
|
|
const o = ans.data
|
2022-07-03 15:50:41 +00:00
|
|
|
log.debug("GetAccount", o)
|
|
|
|
|
let out:string[] = []
|
|
|
|
|
Object.entries(o.characters).forEach((x:[string,any])=>{
|
|
|
|
|
out.push(`${account}/${x[1].name}`)
|
|
|
|
|
})
|
|
|
|
|
return [{
|
2022-07-03 10:25:12 +00:00
|
|
|
name: account,
|
2022-07-03 15:50:41 +00:00
|
|
|
id:account,
|
|
|
|
|
wallet: {
|
|
|
|
|
galders: 0,
|
|
|
|
|
state: 0,
|
|
|
|
|
job_img: "bank",
|
|
|
|
|
},
|
2022-07-03 10:25:12 +00:00
|
|
|
path: account,
|
|
|
|
|
items:(Object.entries(o.items) as any).map(([k, v]: [string, TricksterItem]):TricksterItem=>{
|
|
|
|
|
v.unique_id = Number(k)
|
|
|
|
|
return v
|
|
|
|
|
}),
|
2022-07-03 15:50:41 +00:00
|
|
|
},out]
|
2022-07-03 10:25:12 +00:00
|
|
|
})
|
|
|
|
|
}
|
2022-07-03 15:50:41 +00:00
|
|
|
|
2022-07-03 10:25:12 +00:00
|
|
|
GetInventory = async (char_path: string):Promise<TricksterInventory> =>{
|
|
|
|
|
if(char_path.startsWith(":")) {
|
|
|
|
|
char_path = char_path.replace(":","")
|
|
|
|
|
}
|
|
|
|
|
return this.s.authed_request("GET", `item-manager/items/account/${char_path}`,undefined).then((ans:AxiosResponse)=>{
|
|
|
|
|
const o = ans.data
|
|
|
|
|
log.debug("GetInventory", o)
|
|
|
|
|
let name = ""
|
|
|
|
|
let id = ""
|
|
|
|
|
let wallet:TricksterWallet = {galders:0,state:0,job_img:""}
|
|
|
|
|
if(char_path.includes("/")) {
|
|
|
|
|
let [char, val] = Object.entries(o.characters)[0] as [string,any]
|
|
|
|
|
name = val.name
|
|
|
|
|
id = char
|
|
|
|
|
wallet = {
|
|
|
|
|
galders:val.galders as number,
|
|
|
|
|
state:val.state as number,
|
|
|
|
|
job_img: val.job_img as string
|
|
|
|
|
}
|
|
|
|
|
}else {
|
|
|
|
|
name = char_path
|
|
|
|
|
id = char_path
|
|
|
|
|
wallet = {
|
|
|
|
|
galders: 0,
|
|
|
|
|
state: 0,
|
|
|
|
|
job_img: "bank",
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
let out = {
|
|
|
|
|
name,
|
|
|
|
|
id,
|
|
|
|
|
wallet,
|
|
|
|
|
path: char_path,
|
|
|
|
|
items:(Object.entries(o.items) as any).map(([k, v]: [string, TricksterItem]):TricksterItem=>{
|
|
|
|
|
v.unique_id = Number(k)
|
|
|
|
|
return v
|
|
|
|
|
}),
|
|
|
|
|
} as TricksterInventory
|
|
|
|
|
return out
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GetAccounts = async ():Promise<string[]> => {
|
|
|
|
|
return this.s.authed_request("POST", "accounts/list",undefined).then((ans:AxiosResponse)=>{
|
|
|
|
|
log.debug("GetAccounts", ans.data)
|
|
|
|
|
return ans.data.map((x:any)=>x.name)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
GetLoggedin = async ():Promise<boolean> => {
|
|
|
|
|
return this.s.authed_request("POST", "accounts/list",undefined).then((ans:AxiosResponse)=>{
|
|
|
|
|
if(ans.status == 401) {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
if(ans.status == 200) {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|