增加上报和获取游戏记录的接口

This commit is contained in:
zhl 2021-01-11 16:50:44 +08:00
parent b39bb08048
commit f979259e49
2 changed files with 62 additions and 6 deletions

View File

@ -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 {};
}
}

View File

@ -7,26 +7,56 @@ import {
} from "@typegoose/typegoose"; } from "@typegoose/typegoose";
import {Base, TimeStamps} from "@typegoose/typegoose/lib/defaultClasses"; import {Base, TimeStamps} from "@typegoose/typegoose/lib/defaultClasses";
interface GameRecordClass extends Base<string>, 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() @dbconn()
@index({accountid: 1}, {unique: false}) @index({'players.accountid': 1}, {unique: false})
@modelOptions({ @modelOptions({
schemaOptions: schemaOptions:
{collection: "game_record", timestamps: true} {collection: "game_record", timestamps: true}
}) })
class GameRecordClass { class GameRecordClass extends Base<string>{
@prop() @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 { return {
accountid: this.accountid, roomid: this.roomid,
round: this.round,
winner: this.winner,
players: '',
} }
} }
} }