好友:赠送礼物

This commit is contained in:
luying
2021-02-03 20:17:55 +08:00
parent fe85f0e247
commit 41bf3d96b5
10 changed files with 725 additions and 624 deletions

View File

@@ -1,6 +1,6 @@
import { Application, BackendSession } from "pinus";
import { resResult, getRandEelm, getResStr, shouldRefresh } from "../../../pubUtils/util";
import { STATUS, ROLE, FRIEND_DROP_TYPE, FRIEND_RELATION_TYPE, POPULATE_TYPE, BLOCK_OPEATE } from "../../../consts";
import { STATUS, ROLE, FRIEND_DROP_TYPE, FRIEND_RELATION_TYPE, POPULATE_TYPE, BLOCK_OPEATE, CONSUME_TYPE, ITID } from "../../../consts";
import { RoleModel, RoleType } from "../../../db/Role";
import { getBeforeDaySeconds } from "../../../pubUtils/timeUtil";
import { FriendApplyModel } from "../../../db/FriendApply";
@@ -11,8 +11,10 @@ import { isRoleOnline } from "../../../services/redisService";
import { increaseFrdCnt, getRecommendType, sortByBeSentHeart } from "../../../services/friendService";
import { FriendPointModel } from "../../../db/FriendPoint";
import { gameData } from "../../../pubUtils/data";
import { addItems } from "../../../services/rewardService";
import { addItems, handleCost } from "../../../services/rewardService";
import { getFriendPointObject } from "../../../pubUtils/itemUtils";
import { RewardInter } from "../../../pubUtils/interface";
import { FriendPresentLogModel } from '../../../db/FriendPresentLog';
export default function (app: Application) {
@@ -448,4 +450,45 @@ export class FriendHandler {
})
}
// 赠送礼物
public async sendPresent(msg: { roleId: string, items: RewardInter[] }, session: BackendSession) {
let roleId: string = session.get('roleId');
let sid: string = session.get('sid');
let { roleId: hisRoleId, items } = msg;
let myRelation = await FriendRelationModel.findFriendByRole(roleId, POPULATE_TYPE.NOT);
let friends = myRelation?myRelation.friends: [];
let curFriend = friends.find(cur => cur.roleId == hisRoleId);
if(!curFriend) return resResult(STATUS.FRIEND_NOT_FOUND);
let friendValueInc = 0;
for(let {id, count} of items ) {
let dicGood = gameData.goods.get(id);
if(!dicGood) {
return resResult(STATUS.DIC_DATA_NOT_FOUND);
}
let dicItid = ITID.get(dicGood.itid);
if (dicItid.type == CONSUME_TYPE.FRIEND_FAVOUR) {
friendValueInc += dicGood.value * count;
} else {
return resResult(STATUS.WRONG_PARMS);
}
}
let costResult = await handleCost(roleId, sid, items);
if(!costResult) return resResult(STATUS.ROLE_MATERIAL_NOT_ENOUGH);
let result = await FriendShipModel.addFriendValue(roleId, hisRoleId, friendValueInc);
if(!result) return resResult(STATUS.FRIEND_NOT_FOUND);
// TODO 日志可以转到log服
await FriendPresentLogModel.createRecord(roleId, hisRoleId, items);
return resResult(STATUS.SUCCESS, {
roleId: hisRoleId,
...result
})
}
}

View File

@@ -27,6 +27,7 @@ export const CONSUME_TYPE = {
SKIN: 7, // 时装
PIECE: 8, // 装备碎片
JEWEL: 9, //宝石
FRIEND_FAVOUR: 10, // 好友道具
};
export enum ROLE_TERAPH {
@@ -125,7 +126,8 @@ const itid_array = [
{ id: 45, name: '足具宝石', table: 'item', type: CONSUME_TYPE.JEWEL },
{ id: 46, name: '礼器宝石', table: 'item', type: CONSUME_TYPE.JEWEL },
{ id: 47, name: '典籍宝石', table: 'item', type: CONSUME_TYPE.JEWEL },
{ id: 48, name: '灵玄石', table: 'item', type: CONSUME_TYPE.JEWEL }
{ id: 48, name: '灵玄石', table: 'item', type: CONSUME_TYPE.JEWEL },
{ id: 49, name: '玩家好感道具', table: 'item', type: CONSUME_TYPE.FRIEND_FAVOUR }
];
export const ITID = new Map<number, {id: number, name: string, table: string, type?: number, isCurrency?: boolean, equipJewel?: number}>();

View File

@@ -6,6 +6,9 @@ export enum ROLE {
HANDLE_APPLY = 'roleId friendCnt lv',
GET_LV = 'lv',
GET_ROLE_ID = 'lv'
GET_ROLE_ID = 'roleId'
};
export enum FRIEND {
SEND_PRESENT = 'friendValue friendLv'
}

View File

@@ -31,14 +31,14 @@ export default class FriendPoint extends BaseModel {
public static async updatePointToday(roleId: string, roleName: string, cntInc: number, maxPerDay: number, type: number, lean = true) {
const curTime = new Date((new Date()).setHours(0, 0, 0, 0));
// 当 oldCnt + cntInc > maxPerDay 时需计算实际可以获得的点数cntInc - (newCnt - maxPerDay)
const rec: FriendPointType = await FriendPointModel.findOneAndUpdate({roleId, roleName, createdAt: {$gte: curTime}, cnt: {$lt: maxPerDay}, type}, {$inc: {cnt: cntInc}}, {upsert: true, new: true}).lean(lean);
const rec: FriendPointType = await FriendPointModel.findOneAndUpdate({roleId, roleName, createdAt: {$gte: curTime}, cnt: {$lt: maxPerDay}, type}, {$inc: {cnt: cntInc, sendCnt: 0}}, {upsert: true, new: true}).lean(lean);
return rec;
}
public static async updateSendCntToday(roleId: string, roleName: string, cntInc: number, maxPerDay: number, type: number, lean = true) {
const curTime = new Date((new Date()).setHours(0, 0, 0, 0));
// 当 oldCnt + cntInc > maxPerDay 时需计算实际可以获得的点数cntInc - (newCnt - maxPerDay)
const rec: FriendPointType = await FriendPointModel.findOneAndUpdate({roleId, roleName, createdAt: {$gte: curTime}, sendCnt: {$lt: maxPerDay}, type}, {$inc: {sendCnt: cntInc}}, {upsert: true, new: true}).lean(lean);
const rec: FriendPointType = await FriendPointModel.findOneAndUpdate({roleId, roleName, createdAt: {$gte: curTime}, sendCnt: {$lt: maxPerDay}, type}, {$inc: {sendCnt: cntInc, cnt: 0}}, {upsert: true, new: true}).lean(lean);
return rec;
}

View File

@@ -0,0 +1,31 @@
import BaseModel from './BaseModel';
import { index, getModelForClass, prop, DocumentType } from '@typegoose/typegoose';
import { ItemReward } from './generalField';
/**
* GM用户组接口
*/
@index({ uid: 1 })
@index({ api: 1 })
export default class FriendPresentLog extends BaseModel {
@prop({ required: true })
roleId: string;
@prop({ required: true })
hisRoleId: string;
@prop({ required: true, type: ItemReward, default: [] })
items: ItemReward[];
public static async createRecord(roleId: string, hisRoleId: string, items: ItemReward[]) {
const r = await FriendPresentLogModel.insertMany({roleId, hisRoleId, items});
return r;
}
}
export const FriendPresentLogModel = getModelForClass(FriendPresentLog);
export interface FriendPresentLogType extends Pick<DocumentType<FriendPresentLog>, keyof FriendPresentLog>{};

View File

@@ -3,6 +3,7 @@ import { index, getModelForClass, prop, DocumentType } from '@typegoose/typegoos
import { getFriendLvByExp } from '../pubUtils/data';
import * as util from '../pubUtils/friendUtil';
import { nowSeconds } from '../pubUtils/timeUtil';
import { FRIEND } from '../consts';
/**
* 好友直接的亲密值
@@ -144,6 +145,15 @@ export default class FriendShip extends BaseModel {
return result;
}
// 增加亲密度
public static async addFriendValue(roleId: string, hisRoleId: string, inc: number) {
let { roleIds } = util.getRoleIds([roleId, hisRoleId]);
let result: FriendShipType = await FriendShipModel.findOneAndUpdate({ roleIds }, { $inc: { friendValue: inc } }, { new: true })
.select(FRIEND.SEND_PRESENT)
.lean({ virtuals: true });
return result;
}
}
export const FriendShipModel = getModelForClass(FriendShip);

View File

@@ -255,4 +255,11 @@ export class PvpOtherHeroes extends PvpHeroInfo {
super();
this.score = score;
}
}
export class ItemReward {
@prop({ required: true })
id: number;
@prop({ required: true })
count: number;
}

View File

@@ -406,9 +406,9 @@ export function getArmyWishPoolBaseByLv(lv: number) {
export function getFriendLvByExp(exp: number) {
let resultLv = 1;
for(let [lv, {value}] of gameData.roleFriendLv) {
for(let [lv, {sum}] of gameData.roleFriendLv) {
resultLv = lv;
if(exp < value) break;
if(exp < sum) break;
}
return resultLv;
}

View File

@@ -10,6 +10,8 @@ export interface DicRoleFriendLv {
readonly lv: number;
// 亲密度值
readonly value: number;
// 亲密度总值
readonly sum: number;
// 寻宝加成
readonly comBattleAdd: number;
@@ -20,8 +22,11 @@ const str = readJsonFile(FILENAME.DIC_ROLE_FRIEND_LEVEL);
let arr = JSON.parse(str);
export const dicRoleFriendLv = new Map<number, DicRoleFriendLv>();
let sum = 0;
arr.forEach(o => {
sum += o.value;
o.sum = sum;
dicRoleFriendLv.set(o.lv, o);
});

1232
shared/resource/jsons/dic_goods.json Executable file → Normal file

File diff suppressed because it is too large Load Diff