73 lines
2.6 KiB
TypeScript
73 lines
2.6 KiB
TypeScript
import BaseController from '../common/base.controller'
|
|
import { router } from '../decorators/router'
|
|
import { BaseConst } from '../constants/BaseConst'
|
|
import { ZError } from '../common/ZError'
|
|
import { MatchCfg } from '../cfg/parsers/MatchCfg'
|
|
import { BagItem } from '../models/BagItem'
|
|
import { checkGameing, setGameEnd, updateRank } from '../service/rank'
|
|
import { error } from '../common/Debug'
|
|
import { ItemInfo } from '../logic/ItemDef'
|
|
import ItemCtrl from '../logic/ItemCtrl'
|
|
import { User } from '../models/User'
|
|
|
|
export default class MatchController extends BaseController {
|
|
|
|
@router('post /svr/:accountid/beginmatch')
|
|
async beginMatch(req: any) {
|
|
let { accountid, matchid } = req.params
|
|
let cfg: MatchCfg = global.$cfg.get(BaseConst.MATCH).get(matchid << 0)
|
|
if (!cfg || !cfg.consume) {
|
|
throw new ZError(10, 'match not found')
|
|
}
|
|
let gameing = await checkGameing(accountid)
|
|
if (gameing) {
|
|
throw new ZError(12, 'gameing')
|
|
}
|
|
let itemObj = cfg.consume.split(':')
|
|
let itemid: number = parseInt(itemObj[0])
|
|
let count: number = parseInt(itemObj[1])
|
|
let record = await BagItem.findOne({
|
|
accountid,
|
|
itemid
|
|
})
|
|
if (!record || record.count < count) {
|
|
throw new ZError(11, 'not enough ticket')
|
|
}
|
|
await BagItem.useItem(accountid, itemid, count, 'match_ticket', matchid)
|
|
return {}
|
|
}
|
|
|
|
@router('post /svr/:accountid/leftgame')
|
|
async userLeft(req: any) {
|
|
let { accountid, roomid, dead, matchid, scoreChange } = req.params
|
|
console.log(`${roomid} player ${accountid} left game manul, isdead: ${dead}`)
|
|
await setGameEnd(accountid)
|
|
if (!dead && matchid) {
|
|
let cfg: MatchCfg = global.$cfg.get(BaseConst.MATCH).get(parseInt(matchid))
|
|
if (!cfg) {
|
|
error(`match cfg not found: ${matchid}`)
|
|
}
|
|
let user = await User.findById(accountid)
|
|
if (!user) {
|
|
error(`save game record, account not found: ${ accountid }`)
|
|
}
|
|
let items: ItemInfo[] = []
|
|
if (cfg.escapeget) {
|
|
items = ItemCtrl.getItemsByInfo(cfg.escapeget);
|
|
}
|
|
await BagItem.addItems(accountid, items, 'game_left', roomid)
|
|
// 更新逃跑次数
|
|
user.season_data.inc('escape', 1)
|
|
user.season_data.inc('loss', 1)
|
|
const fc = global.$cfg.get(BaseConst.FORMULA)
|
|
let oldScore = user.season_score
|
|
user.season_score = Math.max((user.season_score + scoreChange) | 0, fc.get(70002).number)
|
|
await updateRank(user._id, user.season_score)
|
|
await user.save()
|
|
return {scoreChange: user.season_score - oldScore, score: user.season_score, items}
|
|
}
|
|
return {}
|
|
}
|
|
|
|
}
|