From 3f9641ad28508c7d90dd7d78cf5ffd975d979861 Mon Sep 17 00:00:00 2001 From: zhl Date: Fri, 22 Jan 2021 13:17:38 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=BC=80=E5=A7=8B?= =?UTF-8?q?=E5=8C=B9=E9=85=8D=E7=9A=84=E6=8E=A5=E5=8F=A3,=20=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E8=8E=B7=E5=8F=96item=E7=9A=84=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/api.md | 4 +++- src/controllers/AccountController.ts | 14 +++----------- src/controllers/MatchController.ts | 2 +- src/controllers/RecordController.ts | 29 ++++++++++++++++++++++++---- src/models/BagItem.ts | 18 +++++++++++++++-- src/models/GameRecord.ts | 4 ++++ 6 files changed, 52 insertions(+), 19 deletions(-) diff --git a/docs/api.md b/docs/api.md index 8513c59..9341fb0 100644 --- a/docs/api.md +++ b/docs/api.md @@ -497,7 +497,9 @@ 3. Response: JSON ```js - +{ + ticket: '123123123' //匹配的时候须带上此ticket +} ``` diff --git a/src/controllers/AccountController.ts b/src/controllers/AccountController.ts index 4d4e82e..7aee02f 100644 --- a/src/controllers/AccountController.ts +++ b/src/controllers/AccountController.ts @@ -8,6 +8,7 @@ import { Hero } from '../models/subdoc/Hero' import { CardGroup } from '../models/CardGroup' import { BagItem, ItemType } from '../models/BagItem' import ItemCtrl from '../logic/ItemCtrl' +import { ItemInfo } from '../logic/ItemDef' export default class AccountController extends BaseController { @role('anon') @@ -126,17 +127,8 @@ export default class AccountController extends BaseController { throw new ZError(12, 'not enough item') } record.count -= count - let data = ItemCtrl.useItem(itemid, count) - // let data = [{ itemid: 10001, itemnum: 1 }] - for (let obj of data) { - let item = (await BagItem.findOrCreate({ - accountid, - itemid: obj.id, - itemtype: obj.type - })).doc - item.count += obj.count - await item.save() - } + let data: ItemInfo[] = ItemCtrl.useItem(itemid, count) + await BagItem.addItems(accountid, data) await record.save() return data } diff --git a/src/controllers/MatchController.ts b/src/controllers/MatchController.ts index 0cb7778..9a5f292 100644 --- a/src/controllers/MatchController.ts +++ b/src/controllers/MatchController.ts @@ -26,6 +26,6 @@ export default class MatchController extends BaseController { } record.count -= count await record.save() - return {} + return {ticket: '123123123123'} } } diff --git a/src/controllers/RecordController.ts b/src/controllers/RecordController.ts index 5137a9d..9681d0b 100644 --- a/src/controllers/RecordController.ts +++ b/src/controllers/RecordController.ts @@ -5,6 +5,10 @@ import { RecordInfo, User } from '../models/User' import { error } from '../common/Debug' import { timeBeforeDay } from '../utils/time.util' import { BaseConst } from '../constants/BaseConst' +import { MatchCfg } from '../cfg/parsers/MatchCfg' +import ItemCtrl from '../logic/ItemCtrl' +import { ItemInfo } from '../logic/ItemDef' +import { BagItem } from '../models/BagItem' export default class RecordController extends BaseController { @role('anon') @@ -24,9 +28,13 @@ export default class RecordController extends BaseController { async upload(req: any) { let record = new GameRecord(req.params) await record.save() - if (!record.season) { + if (!record.matchid) { return } + let cfg: MatchCfg = global.$cfg.get(BaseConst.MATCH).get(parseInt(record.matchid)) + if (cfg) { + error(`match cfg not found: ${record.matchid}`) + } const fc = global.$cfg.get(BaseConst.FORMULA) const scores = [ fc.get(70043).number, @@ -36,7 +44,8 @@ export default class RecordController extends BaseController { fc.get(70047).number, fc.get(70048).number ] - let seasonData = {} + let seasonData: any = {} + let itemData: any = {} for (let player of record.players) { if (player.accountid.startsWith('robot')) { continue @@ -115,9 +124,21 @@ export default class RecordController extends BaseController { * end of 处理比赛统计信息 */ await user.save() - // @ts-ignore seasonData[player.playerid] = user.season_data + /** + * 获取胜利失败的物品 + */ + let items: ItemInfo[] = [] + if (cfg) { + if (record.winner == player.team && cfg.winget) { + items = ItemCtrl.getItemsByInfo(cfg.winget); + } else if (record.winner !== player.team && cfg.failget) { + items = ItemCtrl.getItemsByInfo(cfg.failget); + } + } + itemData[player.playerid] = items + await BagItem.addItems(player.accountid, items) } - return seasonData + return {seasonData, itemData} } } diff --git a/src/models/BagItem.ts b/src/models/BagItem.ts index 5ec811f..fc8cf3d 100644 --- a/src/models/BagItem.ts +++ b/src/models/BagItem.ts @@ -2,7 +2,8 @@ import { dbconn } from '../decorators/dbconn' import { getModelForClass, index, - modelOptions, plugin, + modelOptions, + plugin, prop, Severity } from '@typegoose/typegoose' @@ -16,6 +17,7 @@ import findOrCreate from 'mongoose-findorcreate' import { BaseConst } from '../constants/BaseConst' import { DropItemCfg } from '../cfg/parsers/DropItemCfg' import { ItemCardCfg } from '../cfg/parsers/ItemCardCfg' +import { ItemInfo } from '../logic/ItemDef' export enum ItemType { UNKNOW = 0, // 未知 @@ -71,7 +73,19 @@ class BagItemClass extends FindOrCreate { public position: number @prop() - public souce?: string + public source?: string + + public static async addItems(accountid: string, items: ItemInfo[]) { + for (const item of items) { + let record = (await BagItem.findOrCreate({ + accountid, + itemid: item.id, + itemtype: item.type + })).doc + record.count += item.count + await record.save() + } + } public toJson() { return { diff --git a/src/models/GameRecord.ts b/src/models/GameRecord.ts index 93567c4..3e915b7 100644 --- a/src/models/GameRecord.ts +++ b/src/models/GameRecord.ts @@ -63,6 +63,9 @@ class GameRecordClass extends Base{ @prop() public season: number; + @prop() + public matchid: string; + @prop({_id: false, type: () => [GamePlayer]}) public players: GamePlayer[]; @@ -73,6 +76,7 @@ class GameRecordClass extends Base{ round: this.round, winner: this.winner, season: this.season, + match: this.matchid, players: '', } From 14087f02db9192dbe3bd648cc0dd8ce36d5ac785 Mon Sep 17 00:00:00 2001 From: zhl Date: Fri, 22 Jan 2021 13:22:15 +0800 Subject: [PATCH 2/5] =?UTF-8?q?=E9=87=8D=E6=9E=84=E4=BB=A3=E7=A0=81,=20?= =?UTF-8?q?=E5=B0=86item=E7=9B=B8=E5=85=B3=E6=8E=A5=E5=8F=A3=E7=8B=AC?= =?UTF-8?q?=E7=AB=8B=E5=87=BA=E6=9D=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/AccountController.ts | 74 ------------------------ src/controllers/ItemController.ts | 84 ++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 74 deletions(-) create mode 100644 src/controllers/ItemController.ts diff --git a/src/controllers/AccountController.ts b/src/controllers/AccountController.ts index 7aee02f..12024f7 100644 --- a/src/controllers/AccountController.ts +++ b/src/controllers/AccountController.ts @@ -101,80 +101,6 @@ export default class AccountController extends BaseController { return result } - @router('post /api/:accountid/items') - async itemList(req: any) { - let { accountid, type } = req.params - let queryData: any = { accountid } - if (type != undefined) { - queryData.itemtype = type << 0 - } - let items = await BagItem.find(queryData) - return items.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 - } - } - - @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() - } } diff --git a/src/controllers/ItemController.ts b/src/controllers/ItemController.ts new file mode 100644 index 0000000..9e12550 --- /dev/null +++ b/src/controllers/ItemController.ts @@ -0,0 +1,84 @@ +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' + +export default class ItemController extends BaseController { + @router('post /api/:accountid/items') + async itemList(req: any) { + let { accountid, type } = req.params + let queryData: any = { accountid } + if (type != undefined) { + queryData.itemtype = type << 0 + } + let items = await BagItem.find(queryData) + return items.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 + } + } + + @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() + } +} From f92af1cd1f16c5bd1d9edc05f337b224088dc6fb Mon Sep 17 00:00:00 2001 From: zhl Date: Fri, 22 Jan 2021 13:26:38 +0800 Subject: [PATCH 3/5] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E4=BF=A1=E6=81=AF=E6=8E=A5=E5=8F=A3,=20=E8=B0=83=E6=95=B4?= =?UTF-8?q?=E4=BB=A3=E5=B8=81=E7=9A=84=E6=95=B0=E6=8D=AE=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/api.md | 15 ++++++--------- src/controllers/AccountController.ts | 5 ++--- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/docs/api.md b/docs/api.md index 9341fb0..0d28329 100644 --- a/docs/api.md +++ b/docs/api.md @@ -40,15 +40,12 @@ trial: false, // 是否是试用 trial_expire: 1609919293, // 试用到期时间, 0: 说明是永久 }], - moneys: { - 'coin': 0, // 金币 - 'diamond': 0, // 钻石 - 'hero_shard': 0, // 通用英雄碎片 - 'hero_exp': 0, // 通用英雄经验 - 'hero_shard_heroid': 0, // 英雄专用碎片 - 'hero_exp_heroid': 0, // 英雄专用经验 - 'card_scroll': 0, //抽卡卷轴 - }, + moneys: [ + { + "itemid": 80012, //代币的id + "itemnum": 140 // 数量 + } + ], normal_stat: [0, 0, 0, 0] //匹配: 胜利场数, 失败场数, 平局场数, 掉线场数 season_rank: 1 // 当前赛季排名 match_score: 1000 //当前匹配分, 用于匹配时上传 diff --git a/src/controllers/AccountController.ts b/src/controllers/AccountController.ts index 12024f7..6959489 100644 --- a/src/controllers/AccountController.ts +++ b/src/controllers/AccountController.ts @@ -7,8 +7,6 @@ import { BaseConst } from '../constants/BaseConst' import { Hero } from '../models/subdoc/Hero' import { CardGroup } from '../models/CardGroup' import { BagItem, ItemType } from '../models/BagItem' -import ItemCtrl from '../logic/ItemCtrl' -import { ItemInfo } from '../logic/ItemDef' export default class AccountController extends BaseController { @role('anon') @@ -72,7 +70,8 @@ export default class AccountController extends BaseController { result.heros = heros await account.save() - result.moneys = account.moneys + const moneyList = await BagItem.find({accountid, itemtype: ItemType.MONEY}) + result.moneys = moneyList.map(o => o.toJson()) result.normal_stat = account.normal_stat result.extinfo = account.extinfo result.season_score = account.season_score From 5baf4ddeb561e7db5ca3ebeb3a008b61ed4b85b3 Mon Sep 17 00:00:00 2001 From: zhl Date: Fri, 22 Jan 2021 13:39:05 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=E7=BB=9F=E4=B8=80=E7=89=A9=E5=93=81?= =?UTF-8?q?=E7=9A=84=E8=BF=94=E5=9B=9E=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/api.md | 22 +++++++++++++--------- src/controllers/MailController.ts | 21 +++++++++++---------- src/models/BagItem.ts | 5 +++-- 3 files changed, 27 insertions(+), 21 deletions(-) diff --git a/docs/api.md b/docs/api.md index 0d28329..dc3c754 100644 --- a/docs/api.md +++ b/docs/api.md @@ -338,8 +338,9 @@ ```js [ //附件列表 { - "itemid": 0, //道具id - "itemnum": 0, //道具数量 + "id": 0, //道具id + "count": 0, //道具数量 + "type": 1 //道具类型 }, ] @@ -365,9 +366,10 @@ ```js [ //物品列表 { - "itemid": 0, //道具id - "itemnum": 0, //道具数量 - }, + "id": 0, //道具id + "count": 0, //道具数量 + "type": 1 //道具类型 + } ] ``` @@ -392,8 +394,9 @@ ```js [ //物品列表 { - "itemid": 0, //道具id - "itemnum": 0, //道具数量 + "id": 0, //道具id + "count": 0, //道具数量 + "type": 1 //道具类型 }, ] @@ -468,8 +471,9 @@ ```js [ //物品列表 { - "itemid": 0, //道具id - "itemnum": 0, //道具数量 + "id": 0, //道具id + "count": 0, //道具数量 + "type": 1 //道具类型 }, ] diff --git a/src/controllers/MailController.ts b/src/controllers/MailController.ts index 8eae6f6..414ee0d 100644 --- a/src/controllers/MailController.ts +++ b/src/controllers/MailController.ts @@ -3,6 +3,8 @@ import { router } from '../decorators/router' import { getAttachment } from '../service/mail' import { ZError } from '../common/ZError' import { BagItem, getItemType } from '../models/BagItem' +import ItemCtrl from '../logic/ItemCtrl' +import { ItemInfo } from '../logic/ItemDef' export default class MailController extends BaseController { @@ -13,19 +15,18 @@ export default class MailController extends BaseController { if (data.errcode) { throw new ZError(data.errcode, data.errmsg) } + let items: ItemInfo[] = [] if (data.attachments && data.attachments.length > 0) { - for (let obj of data.attachments) { - let itemType = getItemType(obj.itemid << 0) - let item = (await BagItem.findOrCreate({ - accountid, - itemid: obj.itemid, - itemtype: itemType - })).doc - item.count += obj.itemnum - await item.save() + let itemStr = '' + for (let i = 0; i < data.attachments.length; i++) { + const obj = data.attachments[i] + if (i > 0) itemStr += '|' + itemStr += `${obj.itemid}:${obj.itemnum}` } + items = ItemCtrl.getItemsByInfo(itemStr) + await BagItem.addItems(accountid, items) } - return data.attachments + return items } } diff --git a/src/models/BagItem.ts b/src/models/BagItem.ts index fc8cf3d..c242643 100644 --- a/src/models/BagItem.ts +++ b/src/models/BagItem.ts @@ -89,8 +89,9 @@ class BagItemClass extends FindOrCreate { public toJson() { return { - itemid: this.itemid, - itemnum: this.count + id: this.itemid, + count: this.count, + type: this.itemtype } } } From 32f91d29cd3f59e2a7584907d87d190f2577871a Mon Sep 17 00:00:00 2001 From: zhl Date: Fri, 22 Jan 2021 14:08:11 +0800 Subject: [PATCH 5/5] =?UTF-8?q?=E5=B0=86=E5=BC=80=E5=A7=8B=E5=8C=B9?= =?UTF-8?q?=E9=85=8D=E6=8E=A5=E5=8F=A3=E6=94=B9=E4=B8=BA=E6=9C=8D=E5=8A=A1?= =?UTF-8?q?=E7=AB=AF=E8=B0=83=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/api.md | 48 ++++++++++++++---------------- src/controllers/MatchController.ts | 4 +-- 2 files changed, 24 insertions(+), 28 deletions(-) diff --git a/docs/api.md b/docs/api.md index dc3c754..d3b1ce6 100644 --- a/docs/api.md +++ b/docs/api.md @@ -479,32 +479,6 @@ ``` -### 18. 开始比赛 -1. Method: POST -2. URI: /api/:accountid/beginmatch - -| 字段 | 说明 | -| -------- | -------------------------------------- | -| accountid | 帐号id | - -> POST参数 - - -| 字段 | 说明 | -| -------- | -------------------------------------- | -| matchid |10_match.xlsx里的id | - - -3. Response: JSON - -```js -{ - ticket: '123123123' //匹配的时候须带上此ticket -} - -``` - - @@ -525,6 +499,28 @@ ``` +### 2. 开始比赛 +1. Method: POST +2. URI: /svr/:accountid/beginmatch + +| 字段 | 说明 | +| -------- | -------------------------------------- | +| accountid | 帐号id | + +> POST参数 + + +| 字段 | 说明 | +| -------- | -------------------------------------- | +| matchid |10_match.xlsx里的id | + + +3. Response: JSON + +```js + +``` + diff --git a/src/controllers/MatchController.ts b/src/controllers/MatchController.ts index 9a5f292..15d56ad 100644 --- a/src/controllers/MatchController.ts +++ b/src/controllers/MatchController.ts @@ -7,7 +7,7 @@ import { BagItem } from '../models/BagItem' export default class MatchController extends BaseController { - @router('post /api/:accountid/beginmatch') + @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) @@ -26,6 +26,6 @@ export default class MatchController extends BaseController { } record.count -= count await record.save() - return {ticket: '123123123123'} + return {} } }