2022-08-10 01:39:44 +00:00
|
|
|
import { Axios, AxiosResponse, Method } from "axios"
|
|
|
|
import log, { debug } from "loglevel"
|
|
|
|
import { bank_endpoint, EndpointCreator, market_endpoint, Session } from "../session"
|
2022-07-06 07:48:11 +00:00
|
|
|
import { dummyChar, TricksterAccount, TricksterInventory, TricksterItem, TricksterWallet } from "../trickster"
|
2022-07-07 07:54:36 +00:00
|
|
|
import { BankEndpoint, LTOApi } from "./api"
|
2022-07-06 07:48:11 +00:00
|
|
|
|
2022-07-07 07:54:36 +00:00
|
|
|
export const pathIsBank = (path:string):boolean => {
|
2022-08-10 01:39:44 +00:00
|
|
|
if(path.includes("/")) {
|
|
|
|
return false
|
2022-07-07 07:54:36 +00:00
|
|
|
}
|
2022-08-10 01:39:44 +00:00
|
|
|
return true
|
2022-07-07 07:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const splitPath = (path:string):[string,string]=>{
|
|
|
|
const spl = path.split("/")
|
|
|
|
switch(spl.length) {
|
|
|
|
case 1:
|
|
|
|
return [spl[0], ""]
|
|
|
|
case 2:
|
|
|
|
return [spl[0],spl[1]]
|
|
|
|
}
|
|
|
|
return ["",""]
|
|
|
|
}
|
2022-07-06 07:48:11 +00:00
|
|
|
export class LTOApiv0 implements LTOApi {
|
|
|
|
s: Session
|
|
|
|
constructor(s:Session) {
|
|
|
|
this.s = s
|
|
|
|
}
|
2022-07-07 07:54:36 +00:00
|
|
|
|
|
|
|
BankAction = async <T,D>(e: BankEndpoint, t: T):Promise<D> => {
|
2022-08-10 01:39:44 +00:00
|
|
|
let VERB:Method | "POSTFORM" = "POST"
|
|
|
|
let endpoint:EndpointCreator = bank_endpoint
|
|
|
|
switch(e){
|
|
|
|
case "buy-from-order":
|
|
|
|
case "cancel-order":
|
|
|
|
endpoint = market_endpoint
|
|
|
|
case "sell-item":
|
|
|
|
VERB = "POSTFORM"
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
return this.s.request(VERB as any,e,t,endpoint).then((x)=>{
|
|
|
|
return x.data
|
|
|
|
})
|
2022-07-07 07:54:36 +00:00
|
|
|
}
|
2022-07-06 07:48:11 +00:00
|
|
|
GetInventory = async (char_path: string):Promise<TricksterInventory> =>{
|
|
|
|
if(char_path.startsWith(":")) {
|
|
|
|
char_path = char_path.replace(":","")
|
|
|
|
}
|
2024-04-09 03:42:34 +00:00
|
|
|
let type = char_path.includes("/") ? "char" : "account"
|
|
|
|
return this.s.request("GET", `v2/item-manager/items/${type}/${char_path}`,undefined).then((ans:AxiosResponse)=>{
|
2022-07-06 07:48:11 +00:00
|
|
|
const o = ans.data
|
|
|
|
log.debug("GetInventory", o)
|
|
|
|
let name = "bank"
|
|
|
|
let id = 0
|
|
|
|
let galders = 0
|
2022-08-10 01:39:44 +00:00
|
|
|
if(pathIsBank(char_path)){
|
2022-07-06 07:48:11 +00:00
|
|
|
let [char, val] = Object.entries(o.characters)[0] as [string,any]
|
|
|
|
name = val.name
|
|
|
|
id = Number(char)
|
2022-08-10 01:39:44 +00:00
|
|
|
galders = 0
|
2022-07-06 07:48:11 +00:00
|
|
|
}else {
|
2022-08-10 01:39:44 +00:00
|
|
|
let [char, val] = Object.entries(o.characters)[0] as [string,any]
|
|
|
|
name = val.name
|
|
|
|
id = Number(char)
|
|
|
|
galders = val.galders
|
2022-07-06 07:48:11 +00:00
|
|
|
}
|
2024-08-12 01:13:42 +00:00
|
|
|
let out:TricksterInventory = {
|
|
|
|
account_name: o.account.account_gid,
|
|
|
|
account_id: o.account.account_code,
|
2022-07-06 07:48:11 +00:00
|
|
|
name,
|
|
|
|
id,
|
|
|
|
path: char_path,
|
|
|
|
galders,
|
2022-07-07 07:54:36 +00:00
|
|
|
items: Object.fromEntries((Object.entries(o.items) as any).map(([k, v]: [string, TricksterItem]):[string, TricksterItem]=>{
|
2022-07-06 07:48:11 +00:00
|
|
|
v.unique_id = Number(k)
|
2022-07-07 07:54:36 +00:00
|
|
|
return [k, v]
|
|
|
|
})),
|
2024-08-12 01:13:42 +00:00
|
|
|
}
|
2022-07-06 07:48:11 +00:00
|
|
|
return out
|
|
|
|
})
|
|
|
|
}
|
|
|
|
GetAccounts = async ():Promise<TricksterAccount[]> => {
|
2022-07-07 07:54:36 +00:00
|
|
|
return this.s.request("GET", "characters/list",undefined).then((ans:AxiosResponse)=>{
|
2022-07-06 07:48:11 +00:00
|
|
|
log.debug("GetAccounts", ans.data)
|
2024-08-12 01:13:42 +00:00
|
|
|
return ans.data.map((x:any):TricksterAccount=>{
|
2022-07-06 07:48:11 +00:00
|
|
|
return {
|
|
|
|
name: x.name,
|
2024-08-12 01:13:42 +00:00
|
|
|
characters: [
|
|
|
|
{account_name:x.name, id: x.id,account_id:x.id, path:x.name, name: x.name+'/bank', class:-8, base_job: -8, current_job: -8},...Object.values(x.characters).map((z:any)=>{
|
|
|
|
return {
|
|
|
|
account_name:x.name,
|
|
|
|
account_id: x.id,
|
|
|
|
id: z.id,
|
|
|
|
name: z.name,
|
|
|
|
path: x.name+"/"+z.name,
|
|
|
|
class: z.class,
|
|
|
|
base_job: z.base_job,
|
|
|
|
current_job: z.current_job,
|
|
|
|
}
|
|
|
|
})],
|
|
|
|
}
|
2022-07-06 07:48:11 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
GetLoggedin = async ():Promise<boolean> => {
|
2022-07-07 07:54:36 +00:00
|
|
|
return this.s.request("POST", "accounts/list",undefined).then((ans:AxiosResponse)=>{
|
2022-07-06 07:48:11 +00:00
|
|
|
if(ans.status == 401) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if(ans.status == 200) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|