132 lines
4.1 KiB
TypeScript
132 lines
4.1 KiB
TypeScript
import BaseController from '../common/base.controller'
|
|
import { router } from '../decorators/router'
|
|
import { BagItem } from '../models/BagItem'
|
|
import { ZError } from '../common/ZError'
|
|
import { ItemInfo } from '../logic/ItemDef'
|
|
import ItemCtrl from '../logic/ItemCtrl'
|
|
import { BaseConst } from '../constants/BaseConst'
|
|
import { error } from '../common/Debug'
|
|
|
|
export default class ItemController extends BaseController {
|
|
@router('post /api/:accountid/items')
|
|
async itemList(req: any) {
|
|
let { accountid, type, items } = req.params
|
|
let queryData: any = { accountid }
|
|
if (type != undefined) {
|
|
queryData.itemtype = type << 0
|
|
}
|
|
if (items && items.length > 0) {
|
|
queryData.itemid = {$in: items}
|
|
}
|
|
let results = await BagItem.find(queryData)
|
|
return results.map(o => o.toJson())
|
|
}
|
|
|
|
@router('post /svr/:accountid/useitem')
|
|
@router('post /api/:accountid/useitem')
|
|
async userItem(req: any) {
|
|
let { accountid, itemid, count } = req.params
|
|
let record = await BagItem.findOne({
|
|
accountid,
|
|
itemid
|
|
})
|
|
if (!record) {
|
|
throw new ZError(11, 'item not found')
|
|
}
|
|
if (record.count < count) {
|
|
throw new ZError(12, 'not enough item')
|
|
}
|
|
record.count -= count
|
|
let data: ItemInfo[] = ItemCtrl.useItem(itemid, count)
|
|
await BagItem.addItems(accountid, data)
|
|
await record.save()
|
|
return data
|
|
}
|
|
|
|
@router('post /api/:accountid/buyitem')
|
|
async buyItem(req: any) {
|
|
let { itemid, count, accountid } = req.params
|
|
let pinfo = ItemCtrl.getItemPrice(itemid << 0)
|
|
if (pinfo.id == 0) {
|
|
throw new ZError(10, 'item can`t buy')
|
|
}
|
|
let record = await BagItem.findOne({
|
|
accountid,
|
|
itemid: pinfo.id
|
|
})
|
|
if (!record || record.count < pinfo.count * count) {
|
|
throw new ZError(11, 'not enough money')
|
|
}
|
|
record.count -= pinfo.count * count
|
|
let newItem = (await BagItem.findOrCreate({ accountid, itemid }))
|
|
.doc
|
|
newItem.count += count
|
|
await newItem.save()
|
|
await record.save()
|
|
return {
|
|
itemid,
|
|
buynum: count,
|
|
totalnum: newItem.count,
|
|
priceid: pinfo.id,
|
|
pricenum: pinfo.count*count
|
|
}
|
|
}
|
|
|
|
@router('post /svr/:accountid/additem')
|
|
async addItem(req: any) {
|
|
let { itemid, count, accountid } = req.params
|
|
let itemInfo = ItemCtrl.findItem(itemid)
|
|
if (!itemInfo) {
|
|
throw new ZError(10, 'no item found')
|
|
}
|
|
let record = (await BagItem.findOrCreate({ accountid, itemid }))
|
|
.doc
|
|
record.count += count
|
|
record.itemtype = itemInfo._type
|
|
await record.save()
|
|
return record.toJson()
|
|
}
|
|
|
|
@router('post /api/:accountid/randomhero')
|
|
async randomHero(req: any) {
|
|
let { accountid } = req.params
|
|
const cfg = global.$cfg.get(BaseConst.ITEMFUNC).get(90003)
|
|
const itemInfos: ItemInfo[] = ItemCtrl.getItemsByInfo(cfg.consume1)
|
|
for (let item of itemInfos) {
|
|
let record = await BagItem.findOne({ accountid, itemid: item.id })
|
|
if (!record || record.count < item.count) {
|
|
throw new ZError(10, 'not enough item' + item.id)
|
|
}
|
|
record.count -= item.count
|
|
await record.save()
|
|
}
|
|
return {}
|
|
}
|
|
|
|
@router('post /api/:accountid/puzzle')
|
|
async puzzleMap(req: any) {
|
|
let { accountid } = req.params
|
|
const cfg = global.$cfg.get(BaseConst.ITEMFUNC).get(90004)
|
|
const itemInfos: ItemInfo[] = ItemCtrl.getItemsByInfo(cfg.consume1)
|
|
for (let item of itemInfos) {
|
|
let record = await BagItem.findOne({ accountid, itemid: item.id })
|
|
if (!record || record.count < item.count) {
|
|
throw new ZError(10, 'not enough item' + item.id)
|
|
}
|
|
record.count -= item.count
|
|
await record.save()
|
|
}
|
|
const resultItems = ItemCtrl.getItemsByInfo(cfg.get)
|
|
if (!resultItems || resultItems.length == 0) {
|
|
error(`拼藏宝图, 未获取到正确的目标物品配置`)
|
|
throw new ZError(11, 'no target item found')
|
|
}
|
|
let resultItem = resultItems[0]
|
|
let newItem = (await BagItem.findOrCreate({ accountid, itemid: resultItem.id, itemtype: resultItem.type })).doc
|
|
newItem.count += resultItem.count
|
|
await newItem.save()
|
|
return newItem.toJson()
|
|
}
|
|
|
|
}
|