好友:赠送领取爱心
This commit is contained in:
@@ -1,15 +1,19 @@
|
||||
import { Application, BackendSession } from "pinus";
|
||||
import { resResult, getRandEelm, getResStr } from "../../../pubUtils/util";
|
||||
import { resResult, getRandEelm, getResStr, shouldRefresh } from "../../../pubUtils/util";
|
||||
import { STATUS, ROLE, FRIEND_DROP_TYPE, FRIEND_RELATION_TYPE, POPULATE_TYPE, BLOCK_OPEATE } from "../../../consts";
|
||||
import { RoleModel, RoleType } from "../../../db/Role";
|
||||
import { getBeforeDaySeconds } from "../../../pubUtils/timeUtil";
|
||||
import { FriendApplyModel } from "../../../db/FriendApply";
|
||||
import { FriendApplyParams, FriendListParam, FriendRecommendParams, BlackListParam } from "../../../domain/roleField/friend";
|
||||
import { FriendApplyParams, FriendListParam, FriendRecommendParams, BlackListParam, FriendValueListParam } from "../../../domain/roleField/friend";
|
||||
import { FriendShipModel, FriendShipType } from "../../../db/FriendShip";
|
||||
import { FriendRelationModel } from "../../../db/FriendRelation";
|
||||
import { FriendRelationModel, Relation } from "../../../db/FriendRelation";
|
||||
import { isRoleOnline } from "../../../services/redisService";
|
||||
import { increaseFrdCnt, getRecommendType } from "../../../services/friendService";
|
||||
import { increaseFrdCnt, getRecommendType, sortByBeSentHeart } from "../../../services/friendService";
|
||||
import { FriendPointModel } from "../../../db/FriendPoint";
|
||||
import { gameData } from "../../../pubUtils/data";
|
||||
import { addItems } from "../../../services/rewardService";
|
||||
import { getFriendPointObject } from "../../../pubUtils/itemUtils";
|
||||
|
||||
|
||||
export default function (app: Application) {
|
||||
return new FriendHandler(app);
|
||||
@@ -159,7 +163,7 @@ export class FriendHandler {
|
||||
await FriendRelationModel.addFriend(roleId, friend, friendShip);
|
||||
await FriendRelationModel.addFriend(friend.roleId, role, friendShip);
|
||||
|
||||
let param = new FriendListParam(friend, friendShip);
|
||||
let param = new FriendListParam(friend, roleId, friendShip);
|
||||
let isOnline = await isRoleOnline(friend.roleId);
|
||||
param.setOnline(isOnline);
|
||||
|
||||
@@ -193,21 +197,18 @@ export class FriendHandler {
|
||||
let role = <RoleType>friend.role;
|
||||
let friendShip = <FriendShipType>friend.friendShip;
|
||||
if(!role || !friendShip) continue;
|
||||
let param = new FriendListParam(role, friendShip);
|
||||
|
||||
let fs = <FriendShipType>friend.friendShip;
|
||||
let ref = shouldRefresh(fs.refTime, new Date(), 0);
|
||||
if(ref) {
|
||||
friendShip = await FriendShipModel.refreshSendAndReceive(roleId, role.roleId);
|
||||
}
|
||||
|
||||
let param = new FriendListParam(role, roleId, friendShip);
|
||||
let isOnline = await isRoleOnline(role.roleId);
|
||||
param.setOnline(isOnline);
|
||||
|
||||
let frd = FriendShipModel.getRoleSendAndReceive(roleId, [roleId, role.roleId], friendShip);
|
||||
if(!frd) continue;
|
||||
let { sendHeart, receiveHeart, beSentHeart } = frd;
|
||||
if(sendHeart <= 1) {
|
||||
param.setCanSend(true);
|
||||
}
|
||||
if(beSentHeart >= 1 && receiveHeart < beSentHeart) {
|
||||
param.setCanReceive(true);
|
||||
}
|
||||
list.push(param);
|
||||
|
||||
}
|
||||
|
||||
let { friendCnt = 0, blockCnt = 0 } = role;
|
||||
@@ -329,4 +330,122 @@ export class FriendHandler {
|
||||
friendCnt, blockCnt
|
||||
});
|
||||
}
|
||||
|
||||
// 赠送爱心/一键赠送
|
||||
public async sendHeart(msg: { roleId: string }, session: BackendSession) {
|
||||
let roleId: string = session.get('roleId');
|
||||
let roleName: string = session.get('roleName');
|
||||
|
||||
let { roleId: hisRoleId } = msg;
|
||||
|
||||
let myRelation = await FriendRelationModel.findFriendByRole(roleId, POPULATE_TYPE.FRIENDSHIP); // 好友关系,只有friendship被populate了
|
||||
let friends = myRelation?myRelation.friends: [];
|
||||
let arr = new Array<Relation>();
|
||||
if(hisRoleId != '') {
|
||||
let cur = friends.find(cur => cur.roleId == hisRoleId);
|
||||
if(!cur) return resResult(STATUS.FRIEND_NOT_FOUND);
|
||||
|
||||
arr.push(cur);
|
||||
} else {
|
||||
arr = friends;
|
||||
}
|
||||
let canSendList = sortByBeSentHeart(roleId, arr);
|
||||
|
||||
let {lv} = await RoleModel.findByRoleId(roleId, ROLE.GET_LV);
|
||||
let dicFriendLv = gameData.roleFriend.get(lv);
|
||||
if(!dicFriendLv) return resResult(STATUS.DIC_DATA_NOT_FOUND);
|
||||
|
||||
// 情谊值有上限,送到上限为止
|
||||
let max = dicFriendLv.sendMax;
|
||||
let frdPointRec = await FriendPointModel.getFrdPointRecToday(roleId, FRIEND_DROP_TYPE.SEND_GIFT);
|
||||
let { sendCnt: todaySendCnt = 0 } = frdPointRec||{};
|
||||
|
||||
let todaySendInc = 0;
|
||||
let list = new Array<FriendValueListParam>();
|
||||
for(let relation of canSendList) {
|
||||
|
||||
if(todaySendCnt + 1 > max) break;
|
||||
let fs = <FriendShipType>relation.friendShip;
|
||||
let ref = shouldRefresh(fs.refTime, new Date(), 0);
|
||||
let sendHeartRec = await FriendShipModel.sendHeart(roleId, relation.roleId, 1, 1, ref);
|
||||
if(!sendHeartRec) continue;
|
||||
|
||||
let param = new FriendValueListParam(relation.roleId, roleId, sendHeartRec);
|
||||
list.push(param);
|
||||
|
||||
todaySendCnt += 1;
|
||||
todaySendInc += 1;
|
||||
}
|
||||
if(todaySendInc <= 0) return resResult(STATUS.FRIEND_HAS_SENT);
|
||||
|
||||
// 更新情谊值
|
||||
frdPointRec = await FriendPointModel.updateSendCntToday(roleId, roleName, todaySendInc, max, FRIEND_DROP_TYPE.SEND_GIFT);
|
||||
|
||||
return resResult(STATUS.SUCCESS, {
|
||||
todaySendCnt, list
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
// 领取爱心/一键领取
|
||||
public async receiveHeart(msg: { roleId: string }, session: BackendSession) {
|
||||
let roleId: string = session.get('roleId');
|
||||
let roleName: string = session.get('roleName');
|
||||
let sid: string = session.get('sid');
|
||||
|
||||
let { roleId: hisRoleId } = msg;
|
||||
|
||||
let myRelation = await FriendRelationModel.findFriendByRole(roleId, POPULATE_TYPE.FRIENDSHIP); // 好友关系,只有friendship被populate了
|
||||
let friends = myRelation?myRelation.friends: [];
|
||||
let arr = new Array<Relation>();
|
||||
if(hisRoleId != '') {
|
||||
let cur = friends.find(cur => cur.roleId == hisRoleId);
|
||||
if(!cur) return resResult(STATUS.FRIEND_NOT_FOUND);
|
||||
|
||||
arr.push(cur);
|
||||
} else {
|
||||
arr = friends;
|
||||
}
|
||||
let canReceiveList = sortByBeSentHeart(roleId, arr);
|
||||
|
||||
let {lv} = await RoleModel.findByRoleId(roleId, ROLE.GET_LV);
|
||||
let dicFriendLv = gameData.roleFriend.get(lv);
|
||||
if(!dicFriendLv) return resResult(STATUS.DIC_DATA_NOT_FOUND);
|
||||
|
||||
// 情谊值有上限,到上限为止
|
||||
let max = dicFriendLv.receiveMax;
|
||||
let frdPointRec = await FriendPointModel.getFrdPointRecToday(roleId, FRIEND_DROP_TYPE.SEND_GIFT);
|
||||
let { cnt: todayReceiveCnt = 0 } = frdPointRec||{};
|
||||
|
||||
let todayReceiveInc = 0;
|
||||
let list = new Array<FriendValueListParam>();
|
||||
for(let relation of canReceiveList) {
|
||||
if(relation.beSentHeart > 0) {
|
||||
if(todayReceiveCnt + 1 > max) break;
|
||||
|
||||
// 收取爱心
|
||||
let fs = <FriendShipType>relation.friendShip;
|
||||
let ref = shouldRefresh(fs.refTime, new Date(), 0);
|
||||
let receiveHeartRec = await FriendShipModel.receiveHeart(roleId, relation.roleId, 1, relation.beSentHeart, ref);
|
||||
if(!receiveHeartRec) continue;
|
||||
|
||||
let param = new FriendValueListParam(relation.roleId, roleId, receiveHeartRec);
|
||||
list.push(param);
|
||||
|
||||
todayReceiveCnt += 1;
|
||||
todayReceiveInc += 1;
|
||||
}
|
||||
}
|
||||
if(todayReceiveInc <= 0) return resResult(STATUS.FRIEND_HAS_RECEIVE);
|
||||
let fp = getFriendPointObject(todayReceiveCnt);
|
||||
let goods = await addItems(roleId, roleName, sid, [fp])
|
||||
|
||||
frdPointRec = await FriendPointModel.updatePointToday(roleId, roleName, todayReceiveInc, max, FRIEND_DROP_TYPE.SEND_GIFT);
|
||||
|
||||
return resResult(STATUS.SUCCESS, {
|
||||
goods,
|
||||
todayReceiveCnt, list
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user