Files
ZYZ/game-server/app/servers/role/handler/friendHandler.ts
2021-02-02 19:48:06 +08:00

224 lines
8.6 KiB
TypeScript

import { Application, BackendSession } from "pinus";
import { resResult, getRandEelm, getResStr } from "../../../pubUtils/util";
import { STATUS, ROLE, FRIEND_DROP_TYPE, FRIEND_RELATION_TYPE } from "../../../consts";
import { RoleModel, RoleType } from "../../../db/Role";
import { getBeforeDaySeconds } from "../../../pubUtils/timeUtil";
import { FriendApplyModel } from "../../../db/FriendApply";
import { FriendApplyParams, FriendListParam, FriendRecommendParams } from "../../../domain/roleField/friend";
import { FriendShipModel, FriendShipType } from "../../../db/FriendShip";
import { FriendRelationModel } from "../../../db/FriendRelation";
import { isRoleOnline } from "../../../services/redisService";
import { increaseFrdCnt, getRecommendType } from "../../../services/friendService";
import { FriendPointModel } from "../../../db/FriendPoint";
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, false);
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, false);
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, false);
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, false);
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, 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);
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 param = new FriendListParam(role, 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;
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
});
}
}