feat(活动): 小游戏

This commit is contained in:
luying
2023-03-17 16:48:23 +08:00
parent 04cecc2d5d
commit 3669d557a7
14 changed files with 565 additions and 10 deletions

View File

@@ -267,6 +267,7 @@ export enum REDIS_KEY {
GVG_HISTORY_CITY ='gvgHisCity', // gvg激战期玩家进入的城池
GVG_SEND_REWARD ='gvgSendReward', // gvg发放奖励
GVG_SPINE_CNT ='gvgSpineCnt', // gvg spine的下发数量
ACTIVITY_MINI_GAME ='miniGame', // 活动小游戏排行榜
}
// 各排行榜对应hash的key
@@ -329,7 +330,8 @@ export function getInfoKeyByRedisKey(redisKey: REDIS_KEY) {
return { infoKey: REDIS_KEY.LEAGUE_INFO, extraKey: [] };
case REDIS_KEY.GVG_BATTLE_LEAGUE_RANK_BY_CITY: // 激战期联军排行榜
return { infoKey: REDIS_KEY.LEAGUE_INFO, extraKey: [] };
case REDIS_KEY.ACTIVITY_MINI_GAME:
return { infoKey: REDIS_KEY.USER_INFO, extraKey: [] };
default:
return { infoKey: REDIS_KEY.USER_INFO, extraKey: [] };
}
@@ -431,7 +433,8 @@ export const KEY_TO_COMPOSE_FIELD = new Map([
[REDIS_KEY.LEAGUE_INFO, COMPOSE_FIELD_TYPE.LEAGUE],
[REDIS_KEY.GVG_BATTLE_RANK, COMPOSE_FIELD_TYPE.ROLE],
[REDIS_KEY.GVG_BATTLE_LEAGUE_RANK, COMPOSE_FIELD_TYPE.LEAGUE],
[REDIS_KEY.GVG_BATTLE_LEAGUE_RANK_BY_CITY, COMPOSE_FIELD_TYPE.LEAGUE]
[REDIS_KEY.GVG_BATTLE_LEAGUE_RANK_BY_CITY, COMPOSE_FIELD_TYPE.LEAGUE],
[REDIS_KEY.ACTIVITY_MINI_GAME, COMPOSE_FIELD_TYPE.ROLE]
]);
@@ -1140,8 +1143,10 @@ export enum ITEM_CHANGE_REASON {
GVG_REVIVE = 174, // gvg复活队伍
GVG_USE_ITEM = 175, // gvg使用连弩
ARTIFACT_LV_RETURN = 176, // 宝物继承等级返还
ACT_FORGE_BUILD = 177, // 火神祭祀锻造
ACT_FORGE_HELP = 178, // 火神祭祀失败补助
ACT_FORGE_BUILD = 177, // 火神祭祀锻造
ACT_FORGE_HELP = 178, // 火神祭祀失败补助
ACT_MINI_GAME_REWARD = 179, // 小游戏单局奖励
ACT_MINI_GAME_BUY_CNT = 180, // 小游戏花元宝买
}
export enum TA_EVENT {

View File

@@ -649,6 +649,12 @@ export const STATUS = {
ACTIVITY_BUILD_COUNT: { code: 50047, simStr: '铸造次数已满' },
ACTIVITY_BUY_CNT_MAX: { code: 50048, simStr: '该图谱购买次数已达上限' },
ACTIVITY_MATERIAL_COUNT_NOT_ZERO: { code: 50049, simStr: '材料数量不可为0' },
ACTIVITY_MINI_GAME_COUNT_LACK: { code: 50050, simStr: '次数不足' },
ACTIVITY_MINI_GAME_RECORD_NOT_FOUND: { code: 50051, simStr: '未找到这条记录' },
ACTIVITY_MINI_GAME_BUY_COUNT_MAX: { code: 50052, simStr: '购买次数不足' },
ACTIVITY_MINI_GAME_BOX_NOT_FOUND: { code: 50053, simStr: '未找到该宝箱' },
ACTIVITY_MINI_GAME_BOX_HAS_RECEIVED: { code: 50054, simStr: '该宝箱已领取' },
ACTIVITY_MINI_GAME_SCORE_NOT_ENOUGH: { code: 50055, simStr: '积分不足' },
// GM后台相关状态 60000 - 69999
GM_ERR_PASSWORD: { code: 60001, simStr: '账号或密码错误' },

View File

@@ -0,0 +1,72 @@
import BaseModel from './BaseModel';
import { index, getModelForClass, prop, DocumentType } from '@typegoose/typegoose';
/**
* 小游戏记录
*/
@index({ roleId: 1, activityId: 1 })
@index({ score: -1 })
export default class Activity_MiniGame extends BaseModel {
@prop({ required: true })
serverId: number; // 服Id
@prop({ required: true })
activityId: number; // 活动Id
@prop({ required: true })
roundIndex: number; // 第几轮
@prop({ required: true })
nextRefreshTime: number; // 活动结束时间
@prop({ required: true })
roleId: string; // 用户Id
@prop({ required: true })
buyCnt: number; // 小游戏购买次数
@prop({ required: true, type: Number })
receivedBox: number[]; // 领取宝箱
@prop({ required: true })
score: number; // 积分
public static async findData(serverId: number, activityId: number, roundIndex: number, roleId: string) {
let result: ActivityMiniGameModelType = await ActivityMiniGameModel.findOne({ serverId, roleId, activityId, roundIndex }).lean();
return result;
}
public static async incScore(serverId: number, activityId: number, roundIndex: number, roleId: string, score: number, nextRefreshTime: number) {
let result: ActivityMiniGameModelType = await ActivityMiniGameModel.findOneAndUpdate({ serverId, roleId, activityId, roundIndex }, { $inc: { score }, $setOnInsert: { buyCnt: 0, receivedBox: [] }, $set: { nextRefreshTime } }, { new: true, upsert: true }).lean();
return result;
}
public static async buyCnt(serverId: number, activityId: number, roundIndex: number, roleId: string, count: number, nextRefreshTime: number) {
let result: ActivityMiniGameModelType = await ActivityMiniGameModel.findOneAndUpdate({ serverId, roleId, activityId, roundIndex }, { $inc: { buyCnt: count }, $setOnInsert: { score: 0, receivedBox: [] }, $set: { nextRefreshTime } }, { new: true, upsert: true }).lean();
return result;
}
public static async receiveBox(serverId: number, activityId: number, roundIndex: number, roleId: string, boxId: number) {
let result: ActivityMiniGameModelType = await ActivityMiniGameModel.findOneAndUpdate({ serverId, roleId, activityId, roundIndex, receivedBox: { $ne: boxId } }, { $push: { receivedBox: boxId } }, { new: true }).lean();
return result;
}
public static async findRank() {
let result: { _id: { activityId: number, roundIndex: number }, roleIds: string[], arr: { roleId: string, score: number, updatedAt: Date }[] }[]
= await ActivityMiniGameModel.aggregate([
{ $match: { nextRefreshTime: { $gt: Date.now() } } },
{ $group: {
_id: { activityId: '$activityId', roundIndex: '$roundIndex' },
roleIds: { $push: '$roleId' }, arr: { $push: { roleId: '$roleId', score: '$score', updatedAt: '$updatedAt' } } }
}
]);
return result
}
}
export const ActivityMiniGameModel = getModelForClass(Activity_MiniGame);
export interface ActivityMiniGameModelType extends Pick<DocumentType<Activity_MiniGame>, keyof Activity_MiniGame> { }
export type ActivityMiniGameModelTypeParam = Partial<ActivityMiniGameModelType>; // 将所有字段变成可选项

View File

@@ -0,0 +1,62 @@
import BaseModel from './BaseModel';
import { index, getModelForClass, prop, DocumentType } from '@typegoose/typegoose';
import { genCode } from '../pubUtils/util';
/**
* 小游戏记录
*/
@index({ roleId: 1, activityId: 1 })
@index({ gameCode: 1 })
export default class Activity_MiniGame_Rec extends BaseModel {
@prop({ required: true })
serverId: number; // 服Id
@prop({ required: true })
activityId: number; // 活动Id
@prop({ required: true })
roundIndex: number; // 第几轮
@prop({ required: true })
todayIndex: number; // 今天第几天
@prop({ required: true })
roleId: string; // 用户Id
@prop({ required: true })
gameCode: string; // 唯一id
@prop({ required: true })
score: number; // 这局游戏的得分
@prop({ required: true })
isComplete: boolean; // 是否结束
public static async findRecords(serverId: number, activityId: number, roundIndex: number, roleId: string) {
let result: ActivityMiniGameRecModelType[] = await ActivityMiniGameRecModel.find({ serverId, roleId, activityId, roundIndex, isComplete: true }).lean();
return result;
}
public static async findByCode(activityId: number, roleId: string, gameCode: string) {
let result: ActivityMiniGameRecModelType = await ActivityMiniGameRecModel.findOne({ activityId, roleId, gameCode }).lean();
return result;
}
public static async gameStart(serverId: number, activityId: number, roundIndex: number, todayIndex: number, roleId: string) {
let gameCode = genCode(10);
let result: ActivityMiniGameRecModelType = await ActivityMiniGameRecModel.findOneAndUpdate({ gameCode }, { $set: { serverId, activityId, roundIndex, todayIndex, roleId, score: 0, isComplete: false } }, { new: true, upsert: true }).lean();
return result;
}
public static async gameEnd(activityId: number, roleId: string, gameCode: string, score: number) {
let result: ActivityMiniGameRecModelType = await ActivityMiniGameRecModel.findOneAndUpdate({ activityId, roleId, gameCode, isComplete: false }, { $set: { score, isComplete: true } }, { new: true }).lean();
return result;
}
}
export const ActivityMiniGameRecModel = getModelForClass(Activity_MiniGame_Rec);
export interface ActivityMiniGameRecModelType extends Pick<DocumentType<Activity_MiniGame_Rec>, keyof Activity_MiniGame_Rec> { }
export type ActivityMiniGameRecModelTypeParam = Partial<ActivityMiniGameRecModelType>; // 将所有字段变成可选项

View File

@@ -62,17 +62,18 @@ export class ForgeManual {
this.quality = data.quality;
}
public setPlayerData(playerData: ActivityForgeModelType, todayIndex: number) {
public setPlayerData(playerData: ActivityForgeModelType, todayIndex: number, hintDic?: ForgeHintInDb[]) {
this.buildCnt = playerData.buildCnt||0;
this.buyCnt = playerData.buyCnt||0;
let todayFailRecord = playerData.record?.filter(cur => cur.todayIndex == todayIndex && cur.isSuccess == false)||[];
this.failCnt = todayFailRecord.length;
this.failCnt = todayFailRecord.length;
if(hintDic) this.calHintType(hintDic);
}
public calHintType(hintDic: ForgeHintInDb[]) {
for(let { failCnt, hintType } of hintDic) {
if(this.failCnt < failCnt) {
this.hintType = hintType; break;
if(this.failCnt >= failCnt) {
this.hintType = hintType;
}
}
}

View File

@@ -0,0 +1,109 @@
// 节日活动 - 小游戏
import { pick } from 'underscore';
import { ActivityModelType } from '../../db/Activity';
import { ActivityMiniGameModelType } from '../../db/ActivityMiniGame';
import { ActivityMiniGameRecModelType } from '../../db/ActivityMiniGameRec';
import { ActivityBase } from './activityField';
// 后台格式
interface MiniGameBuyCountInDb {
day: number; // 第几天
buyCnt: number; // 可以购买的次数(累积)
freeCnt: number; // 免费次数(每日刷新)
}
interface MiniGameBoxInDb {
id: number; // 宝箱id
score: number; // 分数
reward: string; // type&id&count
}
interface MiniGameDataInDb {
gameType: number; // 小游戏类型 1-消消乐 2-射箭
buyCounts: MiniGameBuyCountInDb[]; // 可购买次数
consume: string; // 购买次数的花费 type&id&count
reward: string; // 每局的参与奖励 type&id&count
box: MiniGameBoxInDb[]; // 宝箱
}
class MiniGameBox {
id: number; // 宝箱id
score: number; // 分数/成功次数
reward: string; // type&id&count
hasReceived: boolean = false; // 是否已领取
constructor(data: MiniGameBoxInDb) {
this.id = data.id;
this.score = data.score;
this.reward = data.reward;
}
public setReceive(box: number[]) {
if(box.indexOf(this.id) != -1) this.hasReceived = true;
}
}
export class MiniGameData extends ActivityBase {
gameType: number; // 小游戏类型 1-消消乐 2-射箭
consume: string; // 购买次数的消耗 type&id&count
reward: string; // 单局游戏的参与奖励 type&id&count
freeCnt: number = 0; // 今天的免费次数
maxBuyCnt: number = 0; // 今天可以购买的次数
box: MiniGameBox[] = []; // 宝箱
playCnt: number = 0; // 已玩次数,当 playCnt < freeCnt + buyCnt 时可以玩
buyCnt: number = 0; // 已购买次数
score: number = 0; // 当前总分
constructor(activityData: ActivityModelType, createTime: number, serverTime: number) {
super(activityData, createTime, serverTime)
this.initData(activityData.data)
}
public initData(data: string): void {
let dataObj: MiniGameDataInDb = JSON.parse(data);
if(!dataObj) return;
this.gameType = dataObj.gameType;
this.consume = dataObj.consume;
this.reward = dataObj.reward;
for(let { day, buyCnt, freeCnt } of (dataObj.buyCounts||[])) {
if(day == this.todayIndex) this.freeCnt = freeCnt;
if(day <= this.todayIndex) this.maxBuyCnt += buyCnt;
}
for(let data of (dataObj.box||[])) {
this.box.push(new MiniGameBox(data));
}
}
public setPlayerData(playerData: ActivityMiniGameModelType) {
if(!playerData) return;
this.buyCnt = playerData.buyCnt||0;
this.score = playerData.score||0;
for(let box of this.box) {
box.setReceive(playerData.receivedBox||[]);
}
}
public setPlayerRecords(records: ActivityMiniGameRecModelType[]) {
this.playCnt = records.length;
}
public incPlayerCnt() {
this.playCnt++;
}
public findBox(boxId: number) {
return this.box.find(cur => cur.id == boxId);
}
public getShowResult() {
return {
...this.getBaseKeys(),
...pick(this, ['gameType', 'consume', 'reward', 'freeCnt', 'maxBuyCnt', 'box', 'playCnt', 'buyCnt', 'score'])
}
}
}

View File

@@ -361,6 +361,7 @@ export class KeyName {
hid?: number;
seasonNum?: number;
activityId?: number;
roundIndex?: number;
index?: number; // 军团活动第几期
vestigeId?: number;
groupKey?: string;
@@ -380,6 +381,7 @@ export class KeyName {
if(param.groupKey) this.groupKey = param.groupKey;
if(param.day) this.day = param.day;
if(param.configId) this.configId = param.configId;
if(param.roundIndex) this.roundIndex = param.roundIndex;
}
public getName() {
@@ -424,6 +426,8 @@ export class KeyName {
return `${this.key}:${this.configId}:${this.groupKey}`;
case REDIS_KEY.GVG_BATTLE_LEAGUE_RANK_BY_CITY:
return `${this.key}:${this.configId}:${this.groupKey}:${this.cityId}`;
case REDIS_KEY.ACTIVITY_MINI_GAME:
return `${this.key}:${this.activityId}:${this.roundIndex}`;
default:
return this.key;
}