Files
ZYZ/game-server/app/servers/role/handler/friendHandler.ts
T
2021-02-03 20:17:55 +08:00

494 lines
19 KiB
TypeScript

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, CONSUME_TYPE, ITID } from "../../../consts";
import { RoleModel, RoleType } from "../../../db/Role";
import { getBeforeDaySeconds } from "../../../pubUtils/timeUtil";
import { FriendApplyModel } from "../../../db/FriendApply";
import { FriendApplyParams, FriendListParam, FriendRecommendParams, BlackListParam, FriendValueListParam } from "../../../domain/roleField/friend";
import { FriendShipModel, FriendShipType } from "../../../db/FriendShip";
import { FriendRelationModel, Relation } from "../../../db/FriendRelation";
import { isRoleOnline } from "../../../services/redisService";
import { increaseFrdCnt, getRecommendType, sortByBeSentHeart } from "../../../services/friendService";
import { FriendPointModel } from "../../../db/FriendPoint";
import { gameData } from "../../../pubUtils/data";
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) {
return new FriendHandler(app);
}
export class FriendHandler {
constructor(private app: Application) {
}
// 获取推荐好友列表
public async getRecommend(msg: { }, session: BackendSession) {
let roleId: string = session.get('roleId');
const day = getBeforeDaySeconds(1);
const { lv } = await RoleModel.findByRoleId(roleId, ROLE.GET_LV);
let allList = await RoleModel.getRecommedList(lv - 5, lv + 5, day);
let myFriendRelation = await FriendRelationModel.findFriendByRole(roleId, POPULATE_TYPE.NOT);
let paramAllList = new Array<FriendRecommendParams>();
for(let role of allList) {
let type = getRecommendType(myFriendRelation, roleId, role.roleId);
if(type == FRIEND_RELATION_TYPE.NORMAL) {
let param = new FriendRecommendParams(role);
param.setType(type);
paramAllList.push(param);
}
}
let list = getRandEelm(paramAllList, 6);
if(!list.length) list = paramAllList;
return resResult(STATUS.SUCCESS, { list });
}
// 搜索好友
public async searchUser(msg: { value: string }, session: BackendSession) {
let roleId: string = session.get('roleId');
let { value } = msg;
let allList = await RoleModel.searchByNameOrId(value);
let myFriendRelation = await FriendRelationModel.findFriendByRole(roleId, POPULATE_TYPE.NOT);
let list = new Array<FriendRecommendParams>();
for(let role of allList) {
let type = getRecommendType(myFriendRelation, roleId, role.roleId);
let param = new FriendRecommendParams(role);
param.setType(type);
list.push(param);
}
return resResult(STATUS.SUCCESS, { list });
}
// 申请
public async applyFriend(msg: { roleIds: string[] }, session: BackendSession) {
let roleId: string = session.get('roleId');
let roleIds = msg.roleIds;
const role = await RoleModel.findByRoleId(roleId, ROLE.GET_ROLE_ID);
let myFriendRelation = await FriendRelationModel.findFriendByRole(roleId, POPULATE_TYPE.NOT);
let str = '', resultRoleIds = new Array<string>();
for(let hisRoleId of roleIds) {
let type = getRecommendType(myFriendRelation, roleId, hisRoleId);
if(type == FRIEND_RELATION_TYPE.HAS_FRIEND) {
str = getResStr(STATUS.FRIEND_HAS_ADD); continue;
} else if(type == FRIEND_RELATION_TYPE.HAS_BLOCKED) {
str = getResStr(STATUS.FRIEND_HAS_BLOCKED); continue;
} else if (type == FRIEND_RELATION_TYPE.MYSELF) {
str = getResStr(STATUS.FRIEND_YOURSELF); continue;
}
let incResult = await RoleModel.increaseFriendApplyCnt(hisRoleId, 1, 50);
if(!incResult) {
str = getResStr(STATUS.FRIEND_HIS_APPLY_MAX); continue;
}
await FriendApplyModel.createApply(hisRoleId, role);
resultRoleIds.push(hisRoleId);
}
return resResult(STATUS.SUCCESS, {
isSuccess: str == '',
msg: str,
roleIds: resultRoleIds
});
}
// 获取申请列表
public async getApplyList(msg: { }, session: BackendSession) {
let roleId: string = session.get('roleId');
let list = await FriendApplyModel.getApplyList(roleId);
let result = new Array<FriendApplyParams>();
for(let apply of list) {
let friend = <RoleType>apply.friend;
let param = new FriendApplyParams(apply.applyCode, friend);
result.push(param);
}
return resResult(STATUS.SUCCESS, {
list: result
});
}
// (一键)同意/拒绝申请
public async handleApply(msg: { applyCodeList: string[], isPass: boolean }, session: BackendSession) {
let roleId: string = session.get('roleId');
let { applyCodeList, isPass } = msg;
let role = await RoleModel.findByRoleId(roleId);
let friendCnt = role.friendCnt;
let myFriendRelation = await FriendRelationModel.findFriendByRole(roleId, POPULATE_TYPE.NOT);
let applyList = await FriendApplyModel.getApplyListByCode(applyCodeList);
let list = new Array<FriendListParam>();
let str = '', resultApplyCodeList = new Array<string>();
if(isPass) {
for(let apply of applyList) {
let type = getRecommendType(myFriendRelation, roleId, apply.frdRoleId);
if(type == FRIEND_RELATION_TYPE.HAS_FRIEND) {
str = getResStr(STATUS.FRIEND_HAS_ADD); continue;
} else if(type == FRIEND_RELATION_TYPE.HAS_BLOCKED) {
str = getResStr(STATUS.FRIEND_HAS_BLOCKED); continue;
} else if (type == FRIEND_RELATION_TYPE.MYSELF) {
str = getResStr(STATUS.FRIEND_YOURSELF); continue;
}
// 好友数量校验
let friend = <RoleType>apply.friend;
str = await increaseFrdCnt(role, friend, friendCnt);
if(str != '') continue;
// 创建friendShip
let friendShip = await FriendShipModel.createFriendShip([roleId, friend.roleId]);
if(!friendShip) {
str = getResStr(STATUS.FRIEND_SHIP_CREATE_ERROR); continue;
}
await FriendRelationModel.addFriend(roleId, friend, friendShip);
await FriendRelationModel.addFriend(friend.roleId, role, friendShip);
let param = new FriendListParam(friend, roleId, friendShip);
let isOnline = await isRoleOnline(friend.roleId);
param.setOnline(isOnline);
list.push(param);
resultApplyCodeList.push(apply.applyCode);
}
} else {
resultApplyCodeList = applyCodeList;
}
await FriendApplyModel.deleteApply(resultApplyCodeList);
return resResult(STATUS.SUCCESS, {
isSuccess: str == '',
msg: str,
applyCodeList: resultApplyCodeList,
list,
friendCnt
});
}
// 获取好友列表
public async getFriendList(msg: { }, session: BackendSession) {
let roleId: string = session.get('roleId');
let role = await RoleModel.findByRoleId(roleId);
let list = new Array<FriendListParam>();
let myRelation = await FriendRelationModel.findFriendByRole(roleId, POPULATE_TYPE.FRIEND);
let friendList = myRelation?myRelation.friends: [];
for(let friend of friendList) {
let role = <RoleType>friend.role;
let friendShip = <FriendShipType>friend.friendShip;
if(!role || !friendShip) continue;
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);
list.push(param);
}
let { friendCnt = 0, blockCnt = 0 } = role;
let frdPointRec = await FriendPointModel.getFrdPointRecToday(roleId, FRIEND_DROP_TYPE.SEND_GIFT);
let { cnt: todayReceiveCnt = 0, sendCnt: todaySendCnt = 0 } = frdPointRec||{};
return resResult(STATUS.SUCCESS, {
todayReceiveCnt,
todaySendCnt,
list,
friendCnt, blockCnt
});
}
// 获取黑名单
public async getBlackList(msg: { }, session: BackendSession) {
let roleId: string = session.get('roleId');
let role = await RoleModel.findByRoleId(roleId);
let list = new Array<BlackListParam>();
let myRelation = await FriendRelationModel.findFriendByRole(roleId, POPULATE_TYPE.BLACKLIST);
let blacklist = myRelation?myRelation.blacklist: [];
for(let bl of blacklist) {
let role = <RoleType>bl.role;
if(!role) continue;
let param = new BlackListParam(role);
let isOnline = await isRoleOnline(role.roleId);
param.setOnline(isOnline);
list.push(param);
}
let { friendCnt = 0, blockCnt = 0 } = role;
return resResult(STATUS.SUCCESS, {
list,
friendCnt, blockCnt
});
}
// 拉黑/移除
public async setBlackList(msg: { roleId: string, type: number }, session: BackendSession) {
let roleId: string = session.get('roleId');
let { roleId: hisRoleId, type } = msg;
let role = await RoleModel.findByRoleId(roleId);
let str = '', list = new Array<BlackListParam>();
if(type == BLOCK_OPEATE.ADD) {
let myRelation = await FriendRelationModel.findFriendByRole(roleId, POPULATE_TYPE.FRIEND);
let friends = myRelation?myRelation.friends: [];
let blacklist = myRelation?myRelation.blacklist: [];
let curFriend = friends.find(cur => cur.roleId == hisRoleId);
let curBlack = blacklist.find(cur => cur.roleId == hisRoleId);
if(curBlack) {
return resResult(STATUS.FRIEND_BLACK_HAS_ADD);
}
role = await RoleModel.increaseBlockCnt(roleId, 1, curFriend? -1: 0, 100); // 增加黑名单人数
if(!role) {
return resResult(STATUS.FRIEND_BLACK_MAX)
}
await RoleModel.increaseFriendCnt(hisRoleId, curFriend? -1: 0); // 对方好友减少
let friend: RoleType, friendShip: FriendShipType;
if(!!curFriend) {
await FriendShipModel.clearValue([roleId, hisRoleId]);
friend = <RoleType>curFriend.role;
friendShip = <FriendShipType>curFriend.friendShip;
} else {
friend = await RoleModel.findByRoleId(hisRoleId);
}
await FriendRelationModel.moveFromFriend(hisRoleId, role, friendShip, false); //从对方好友删除
await FriendRelationModel.moveFromFriend(roleId, friend, friendShip, true); // 删除好友关系并拉黑
let param = new BlackListParam(friend);
let isOnline = await isRoleOnline(role.roleId);
param.setOnline(isOnline);
list.push(param);
} else if(type == BLOCK_OPEATE.REMOVE || type == BLOCK_OPEATE.REMOVE_AND_APPLY){
let myRelation = await FriendRelationModel.findFriendByRole(roleId, POPULATE_TYPE.BLACKLIST);
let blacklist = myRelation?myRelation.blacklist: [];
let curFriend = blacklist.find(cur => cur.roleId == hisRoleId);
if(!curFriend) {
return resResult(STATUS.FRIEND_BLACK_NOT_FOUND);
}
role = await RoleModel.increaseBlockCnt(roleId, -1, 0, 100); // 黑名单人数减
if(!role) {
return resResult(STATUS.FRIEND_BLACK_MAX)
}
await FriendRelationModel.removeFromBlack(roleId, hisRoleId);
if(type == BLOCK_OPEATE.REMOVE_AND_APPLY) {
// 申请好友
let applyResult = await RoleModel.increaseFriendApplyCnt(hisRoleId, 1, 50);
if(!applyResult) {
str = getResStr(STATUS.FRIEND_HIS_APPLY_MAX);
} else {
await FriendApplyModel.createApply(hisRoleId, role);
}
}
} else {
return resResult(STATUS.WRONG_PARMS);
}
let { friendCnt = 0, blockCnt = 0 } = role;
return resResult(STATUS.SUCCESS, {
isSuccess: str == '',
msg: str,
list,
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
})
}
// 赠送礼物
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
})
}
}