2022-07-07 07:54:36 +00:00
|
|
|
import { RefStore } from "../../state/state";
|
2022-08-10 01:39:44 +00:00
|
|
|
import { Serializable } from "../storage";
|
2022-07-07 07:54:36 +00:00
|
|
|
import { LTOApi } from "./api";
|
|
|
|
import { pathIsBank, splitPath } from "./lifeto";
|
2022-08-10 01:39:44 +00:00
|
|
|
import { BankItem, InternalXfer, InvalidOrder, MarketMove, Order,MarketMoveToChar, TxnDetails } from "./order";
|
2022-07-07 07:54:36 +00:00
|
|
|
|
|
|
|
export interface OrderDetails {
|
|
|
|
item_uid: string | "galders"
|
|
|
|
count:number
|
|
|
|
origin_path:string
|
|
|
|
target_path:string
|
|
|
|
}
|
|
|
|
|
|
|
|
const notSupported = new InvalidOrder("not supported yet")
|
|
|
|
const notFound = new InvalidOrder("character not found")
|
|
|
|
|
|
|
|
export class OrderTracker implements Serializable<OrderTracker> {
|
|
|
|
orders: {[key:string]:Order} = {}
|
|
|
|
|
|
|
|
async tick(r:RefStore, api:LTOApi):Promise<any> {
|
|
|
|
let hasDirty = false
|
|
|
|
console.log("ticking")
|
|
|
|
for(const [id, order] of Object.entries(this.orders)) {
|
2022-08-10 01:39:44 +00:00
|
|
|
if(order.status() == "SUCCESS" || order.status() == "ERROR") {
|
2022-07-07 07:54:36 +00:00
|
|
|
console.log("finished order", order)
|
|
|
|
hasDirty = true
|
|
|
|
delete this.orders[id]
|
|
|
|
}
|
|
|
|
order.tick(r,api)
|
|
|
|
}
|
|
|
|
if(hasDirty){
|
|
|
|
r.dirty.value++
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
parse(s: any): OrderTracker {
|
|
|
|
if(s == undefined) {
|
|
|
|
return new OrderTracker()
|
|
|
|
}
|
|
|
|
if(s.orders == undefined) {
|
|
|
|
return new OrderTracker()
|
|
|
|
}
|
|
|
|
this.orders = {}
|
|
|
|
const raw: Order[] = Object.values(s.orders)
|
|
|
|
for(const o of raw) {
|
|
|
|
let newOrder:Order | undefined = undefined
|
|
|
|
console.log("loading", o)
|
|
|
|
if(o.details){
|
2022-08-10 01:39:44 +00:00
|
|
|
if(o.status() == "SUCCESS" || o.status() == "ERROR") {
|
2022-07-07 07:54:36 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
switch(o.order_type) {
|
|
|
|
case "InternalXfer":
|
|
|
|
newOrder = new InternalXfer(o.details).parse(o)
|
|
|
|
break;
|
|
|
|
case "BankItem":
|
|
|
|
newOrder = new BankItem(o.details).parse(o)
|
|
|
|
break;
|
2022-08-10 01:39:44 +00:00
|
|
|
case "MarketMove":
|
|
|
|
newOrder = new MarketMove(o.details).parse(o)
|
|
|
|
case "MarketMoveToChar":
|
|
|
|
newOrder = new MarketMoveToChar(o.details).parse(o)
|
|
|
|
break;
|
2022-07-07 07:54:36 +00:00
|
|
|
case "InvalidOrder":
|
|
|
|
newOrder = new InvalidOrder("").parse(o)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(newOrder) {
|
|
|
|
this.orders[newOrder.action_id] = newOrder
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return this
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class OrderSender {
|
|
|
|
r: RefStore
|
|
|
|
constructor(r:RefStore) {
|
|
|
|
this.r = r
|
|
|
|
}
|
|
|
|
|
|
|
|
send(o:OrderDetails):Order {
|
|
|
|
const formed = this.form(o)
|
|
|
|
this.r.orders.value.orders[formed.action_id] = formed
|
|
|
|
return formed
|
|
|
|
}
|
|
|
|
|
|
|
|
form(o:OrderDetails):Order {
|
|
|
|
// bank to bank
|
|
|
|
if(pathIsBank(o.origin_path) && pathIsBank(o.target_path)) {
|
|
|
|
return this.bank_to_bank(o)
|
|
|
|
}
|
|
|
|
// bank to user
|
|
|
|
if(pathIsBank(o.origin_path) && !pathIsBank(o.target_path)) {
|
|
|
|
return this.bank_to_user(o)
|
|
|
|
}
|
|
|
|
// user to bank
|
|
|
|
if(!pathIsBank(o.origin_path) && pathIsBank(o.target_path)) {
|
|
|
|
return this.user_to_bank(o)
|
|
|
|
}
|
|
|
|
// user to user
|
|
|
|
if(!pathIsBank(o.origin_path) && !pathIsBank(o.target_path)) {
|
|
|
|
return this.user_to_user(o)
|
|
|
|
}
|
|
|
|
return notSupported
|
|
|
|
}
|
|
|
|
bank_to_bank(o:OrderDetails): Order{
|
|
|
|
const origin = this.r.chars.value.get(o.origin_path)
|
|
|
|
const target = this.r.chars.value.get(o.target_path)
|
2022-08-10 01:39:44 +00:00
|
|
|
if(!(origin && target)) {
|
|
|
|
return notFound
|
|
|
|
}
|
|
|
|
return new MarketMove(this.transformInternalOrder(o))
|
2022-07-07 07:54:36 +00:00
|
|
|
}
|
|
|
|
bank_to_user(o:OrderDetails): Order{
|
|
|
|
// get the uid of the bank
|
|
|
|
const origin = this.r.chars.value.get(o.origin_path)
|
|
|
|
const target = this.r.chars.value.get(o.target_path)
|
|
|
|
if(!(origin && target)) {
|
|
|
|
return notFound
|
|
|
|
}
|
|
|
|
const [account, name] = splitPath(target.path)
|
2024-04-09 03:42:34 +00:00
|
|
|
/*if(account != origin.path) {
|
2022-08-10 01:39:44 +00:00
|
|
|
return new MarketMoveToChar(this.transformInternalOrder(o))
|
2024-04-09 03:42:34 +00:00
|
|
|
}*/
|
2022-08-10 01:39:44 +00:00
|
|
|
return new InternalXfer(this.transformInternalOrder(o))
|
2022-07-07 07:54:36 +00:00
|
|
|
}
|
|
|
|
user_to_bank(o:OrderDetails): Order{
|
|
|
|
const origin = this.r.chars.value.get(o.origin_path)
|
|
|
|
const target = this.r.chars.value.get(o.target_path)
|
|
|
|
if(!(origin && target)) {
|
|
|
|
return notFound
|
|
|
|
}
|
|
|
|
const [account, name] = splitPath(origin.path)
|
2024-04-09 03:42:34 +00:00
|
|
|
/*if(account != target.path) {
|
2022-08-10 01:39:44 +00:00
|
|
|
return new MarketMove(this.transformInternalOrder(o))
|
2024-04-09 03:42:34 +00:00
|
|
|
}*/
|
2022-08-10 01:39:44 +00:00
|
|
|
return new BankItem(this.transformInternalOrder(o))
|
2022-07-07 07:54:36 +00:00
|
|
|
}
|
|
|
|
user_to_user(o:OrderDetails): Order{
|
|
|
|
const origin = this.r.chars.value.get(o.origin_path)
|
|
|
|
const target = this.r.chars.value.get(o.target_path)
|
2022-08-10 01:39:44 +00:00
|
|
|
if(!(origin && target)) {
|
|
|
|
return notFound
|
|
|
|
}
|
2024-04-09 03:42:34 +00:00
|
|
|
// return new MarketMoveToChar(this.transformInternalOrder(o))
|
|
|
|
return new InternalXfer(this.transformInternalOrder(o))
|
2022-07-07 07:54:36 +00:00
|
|
|
}
|
|
|
|
|
2022-08-10 01:39:44 +00:00
|
|
|
private transformInternalOrder(o:OrderDetails):TxnDetails {
|
2022-07-07 07:54:36 +00:00
|
|
|
const origin = this.r.chars.value.get(o.origin_path)!
|
|
|
|
const target = this.r.chars.value.get(o.target_path)!
|
|
|
|
return {
|
|
|
|
origin: origin.id.toString(),
|
|
|
|
target: target.id.toString(),
|
|
|
|
item_uid: o.item_uid,
|
|
|
|
count: o.count,
|
|
|
|
origin_path: o.origin_path,
|
|
|
|
target_path: o.target_path,
|
2022-08-10 01:39:44 +00:00
|
|
|
origin_account: origin.account_id.toString(),
|
|
|
|
target_account: target.account_id.toString(),
|
2022-07-07 07:54:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|