From f979259e49e7fd3eab4cb41483757313e773d29f Mon Sep 17 00:00:00 2001 From: zhl Date: Mon, 11 Jan 2021 16:50:44 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=B8=8A=E6=8A=A5=E5=92=8C?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E6=B8=B8=E6=88=8F=E8=AE=B0=E5=BD=95=E7=9A=84?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/RecordController.ts | 26 ++++++++++++++++++ src/models/GameRecord.ts | 42 ++++++++++++++++++++++++----- 2 files changed, 62 insertions(+), 6 deletions(-) create mode 100644 src/controllers/RecordController.ts diff --git a/src/controllers/RecordController.ts b/src/controllers/RecordController.ts new file mode 100644 index 0000000..a03de80 --- /dev/null +++ b/src/controllers/RecordController.ts @@ -0,0 +1,26 @@ +import {GameRecord} from "../models/GameRecord"; +import BaseController from "../common/base.controller"; +import {role, router} from "../decorators/router"; + + +export default class RecordController extends BaseController { + @role('anon') + @router('post /api/:accountid/records') + async recordList(req: any) { + let {accountid} = req.params; + let records = await GameRecord.find({'players.accountid': accountid}); + let results: any = []; + for (let record of records) { + results.push(record.toJSON()); + } + return results; + } + + @role('anon') + @router('post /api/record/save') + async upload(req: any) { + let record = new GameRecord(req.params); + await record.save(); + return {}; + } +} diff --git a/src/models/GameRecord.ts b/src/models/GameRecord.ts index 5172ef0..a9031d4 100644 --- a/src/models/GameRecord.ts +++ b/src/models/GameRecord.ts @@ -7,26 +7,56 @@ import { } from "@typegoose/typegoose"; import {Base, TimeStamps} from "@typegoose/typegoose/lib/defaultClasses"; -interface GameRecordClass extends Base, TimeStamps { +interface GameRecordClass extends TimeStamps { } + +class GamePlayer { + @prop() + public playerid: string; + @prop() + public accountid: string + @prop() + public index: number; + @prop() + public team: number; + @prop() + public heroid: number; + @prop() + public cardgroup: string; + @prop() + public cards: [number]; + @prop() + public statdata: {}; +} /** * 对战记录 */ @dbconn() -@index({accountid: 1}, {unique: false}) +@index({'players.accountid': 1}, {unique: false}) @modelOptions({ schemaOptions: {collection: "game_record", timestamps: true} }) -class GameRecordClass { +class GameRecordClass extends Base{ @prop() - public accountid: string; + public roomid: string; + @prop() + public round: number; + @prop() + public winner: number; - toJson() { + @prop({_id: false, type: () => [GamePlayer]}) + public players: GamePlayer[]; + + public toJson() { return { - accountid: this.accountid, + roomid: this.roomid, + round: this.round, + winner: this.winner, + players: '', } + } }