剧情弹幕:添加接口
This commit is contained in:
@@ -0,0 +1,50 @@
|
|||||||
|
import { Application, BackendSession } from "pinus";
|
||||||
|
import { ScriptBarrageModel } from "../../../db/ScriptBarrage";
|
||||||
|
import { BarrageReturn } from '../../../domain/roleField/barrage';
|
||||||
|
import { resResult, getRandEelm } from "../../../pubUtils/util";
|
||||||
|
import { STATUS } from "../../../consts";
|
||||||
|
|
||||||
|
export default function(app: Application) {
|
||||||
|
return new BarrageHandler(app);
|
||||||
|
}
|
||||||
|
|
||||||
|
export class BarrageHandler {
|
||||||
|
constructor(private app: Application) {
|
||||||
|
}
|
||||||
|
|
||||||
|
public async sendBarrage(msg: { warId: number, rid: string, index: string, content: string }, session: BackendSession) {
|
||||||
|
const roleId: string = session.get('roleId');
|
||||||
|
const { warId, rid, index, content } = msg;
|
||||||
|
const result = await ScriptBarrageModel.createBarrage(roleId, warId, rid, index, content);
|
||||||
|
let barrageList: BarrageReturn[] = [];
|
||||||
|
let bs = new BarrageReturn(result);
|
||||||
|
barrageList.push(bs);
|
||||||
|
return resResult(STATUS.SUCCESS, { barrageList });
|
||||||
|
}
|
||||||
|
|
||||||
|
public async getBarrageList(msg: {rid: string}, session: BackendSession) {
|
||||||
|
// const roleId: string = session.get('roleId');
|
||||||
|
const { rid } = msg;
|
||||||
|
const result = await ScriptBarrageModel.getBarrageList(rid, 1000);
|
||||||
|
const perMax = 10, max = 50; // 每个对话最大数量,每个剧本最大数量
|
||||||
|
let map = new Map<string, BarrageReturn[]>(); // index => BarrageReturn[]
|
||||||
|
for(let barrage of result) {
|
||||||
|
let { index } = barrage;
|
||||||
|
if(!map.has(index)) {
|
||||||
|
map.set(index, []);
|
||||||
|
}
|
||||||
|
map.get(index).push(new BarrageReturn(barrage));
|
||||||
|
}
|
||||||
|
let ratio = result.length > max? max/result.length: 1;
|
||||||
|
|
||||||
|
let barrageList: BarrageReturn[] = [];
|
||||||
|
for(let [_, barrages] of map) {
|
||||||
|
let count = Math.ceil(barrages.length * ratio);
|
||||||
|
if(count > perMax) count = perMax;
|
||||||
|
|
||||||
|
let curBarrages = getRandEelm(barrages, count);
|
||||||
|
barrageList = barrageList.concat(curBarrages);
|
||||||
|
}
|
||||||
|
return resResult(STATUS.SUCCESS, { barrageList });
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
import BaseModel from './BaseModel';
|
||||||
|
import { index, getModelForClass, prop, DocumentType } from '@typegoose/typegoose';
|
||||||
|
import { genCode } from '../pubUtils/util';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 派遣任务记录表
|
||||||
|
*/
|
||||||
|
@index({ roleId: 1, type: 1 })
|
||||||
|
|
||||||
|
export default class ScriptBarrage extends BaseModel {
|
||||||
|
@prop({ required: true })
|
||||||
|
code: string; // 弹幕唯一标识
|
||||||
|
@prop({ required: true })
|
||||||
|
roleId: string; // 角色 id
|
||||||
|
@prop({ required: true })
|
||||||
|
warId: number; // 关卡id
|
||||||
|
@prop({ required: true })
|
||||||
|
rid: string; // 剧本id
|
||||||
|
@prop({ required: true })
|
||||||
|
index: string; // 对话索引
|
||||||
|
@prop({ required: true })
|
||||||
|
content: string; // 弹幕内容
|
||||||
|
|
||||||
|
|
||||||
|
public static async createBarrage(roleId: string, warId: number, rid: string, index: string, content: string) {
|
||||||
|
const code = genCode(10);
|
||||||
|
let result: ScriptBarrageType = await ScriptBarrageModel.findOneAndUpdate({ code }, { $setOnInsert: { roleId, warId, rid, index, content } }, { new: true, upsert: true }).lean();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static async getBarrageList(rid: string, limit: number) {
|
||||||
|
let result: ScriptBarrageType[] = await ScriptBarrageModel.find({ rid }, { _id: 0 }).limit(limit).sort({ index: 1, createdAt: -1 }).lean();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
export const ScriptBarrageModel = getModelForClass(ScriptBarrage);
|
||||||
|
|
||||||
|
export interface ScriptBarrageType extends Pick<DocumentType<ScriptBarrage>, keyof ScriptBarrage>{};
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
import { ScriptBarrageType } from '../../db/ScriptBarrage';
|
||||||
|
import { getSeconds } from '../../pubUtils/timeUtil';
|
||||||
|
|
||||||
|
export class BarrageReturn {
|
||||||
|
code: string; // code
|
||||||
|
warId: number; // 关卡id
|
||||||
|
rid: string; // 剧本id
|
||||||
|
index: string; // 对话索引id
|
||||||
|
content: string; // 弹幕内容
|
||||||
|
createTime: number; // 发送时间
|
||||||
|
|
||||||
|
constructor(barrage: ScriptBarrageType) {
|
||||||
|
if(barrage) {
|
||||||
|
this.code = barrage.code;
|
||||||
|
this.warId = barrage.warId;
|
||||||
|
this.rid = barrage.rid;
|
||||||
|
this.index = barrage.index;
|
||||||
|
this.content = barrage.content;
|
||||||
|
this.createTime = getSeconds(barrage.createdAt);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user