lifeto-shop/src/lib/lifeto/item_mover.ts
2025-06-23 01:33:03 -05:00

122 lines
2.8 KiB
TypeScript

import { LTOApi } from './api'
export interface InternalXferParams {
itemUid: string | 'galders'
count: number
targetCharId: string
}
export interface BankItemParams {
itemUid: string | 'galders'
count: number
targetAccount: string
}
export interface MoveResult {
success: boolean
error?: string
data?: any
}
export class ItemMover {
constructor(private api: LTOApi) {}
/**
* Transfer items between characters
* Uses internal-xfer-item API
*/
async internalXfer(params: InternalXferParams): Promise<MoveResult> {
try {
const request = {
item_uid: params.itemUid,
qty: params.count.toString(),
new_char: params.targetCharId,
}
const response = await this.api.BankAction<any, any>('internal-xfer-item', request)
if (response.status !== 'success') {
return {
success: false,
error: response.message || 'Failed to transfer item',
}
}
return {
success: true,
data: response.data,
}
} catch (error) {
return {
success: false,
error: error instanceof Error ? error.message : 'Unknown error in internalXfer',
}
}
}
/**
* Move items to bank
* Uses bank-item API
*/
async bankItem(params: BankItemParams): Promise<MoveResult> {
try {
const request = {
item_uid: params.itemUid,
qty: params.count.toString(),
account: params.targetAccount,
}
const response = await this.api.BankAction<any, any>('bank-item', request)
if (response.status !== 'success') {
return {
success: false,
error: response.message || 'Failed to bank item',
}
}
return {
success: true,
data: response.data,
}
} catch (error) {
return {
success: false,
error: error instanceof Error ? error.message : 'Unknown error in bankItem',
}
}
}
/**
* High-level function that determines whether to use bankItem or internalXfer
* based on whether targetAccountId is provided (bank) or targetCharId (character)
*/
async moveItem(
itemUid: string | 'galders',
count: number,
targetCharId?: string,
targetAccountId?: string,
): Promise<MoveResult> {
if (targetAccountId) {
// Use bank-item when moving to bank (targetAccountId is provided)
return this.bankItem({
itemUid,
count,
targetAccount: targetAccountId,
})
}
if (targetCharId) {
// Use internal-xfer when moving between characters
return this.internalXfer({
itemUid,
count,
targetCharId,
})
}
return {
success: false,
error: 'Either targetCharId or targetAccountId must be provided',
}
}
}