137 lines
5.1 KiB
TypeScript
137 lines
5.1 KiB
TypeScript
import { Application, BackendSession } from "pinus";
|
|
import { resResult, getRandEelm } from "../../../pubUtils/util";
|
|
import { STATUS, ROLE } from "../../../consts";
|
|
import Role, { RoleModel, RoleType } from "../../../db/Role";
|
|
import { getBeforeDaySeconds } from "../../../pubUtils/timeUtil";
|
|
import { FriendApplyModel } from "../../../db/FriendApply";
|
|
import { FriendApplyParams, FriendListParam } from "../../../domain/roleField/friend";
|
|
import { FriendShipModel, FriendShipType } from "../../../db/FriendShip";
|
|
import { FriendRelationModel } from "../../../db/FriendRelation";
|
|
import { isRoleOnline } from "../../../services/redisService";
|
|
|
|
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);
|
|
const allList = await RoleModel.getRecommedList(roleId, lv - 5, lv + 5, day);
|
|
let list = getRandEelm(allList, 10);
|
|
if(!list.length) list = allList;
|
|
|
|
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);
|
|
for(let hisRoleId of roleIds) {
|
|
await FriendApplyModel.createApply(hisRoleId, role);
|
|
}
|
|
|
|
return resResult(STATUS.SUCCESS, {
|
|
isSuccess: true
|
|
});
|
|
}
|
|
|
|
// 获取申请列表
|
|
public async getApplyList(msg: { lastApplyCode: string }, session: BackendSession) {
|
|
// TODO 分页
|
|
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 applyList = await FriendApplyModel.getApplyListByCode(applyCodeList);
|
|
let result = new Array<FriendApplyParams>();
|
|
if(isPass) {
|
|
for(let apply of applyList) {
|
|
let friend = <RoleType>apply.friend;
|
|
// TODO检查我的好友人数上限,检查加过好友没有
|
|
// TODO检查对方的好友人数上限
|
|
// 创建friendShip
|
|
let friendShip = await FriendShipModel.createFriendShip([roleId, friend.roleId]);
|
|
if(!friendShip) continue;
|
|
// 创建或push我的好友relation
|
|
let result1 = await FriendRelationModel.addFriend(roleId, friend, friendShip);
|
|
// 创建或push他的好友relation
|
|
let result2 = await FriendRelationModel.addFriend(friend.roleId, role, friendShip);
|
|
}
|
|
}
|
|
|
|
// await FriendApplyModel.deleteApply(applyCodeList);
|
|
|
|
return resResult(STATUS.SUCCESS, {
|
|
list: result
|
|
});
|
|
}
|
|
|
|
// 获取好友列表
|
|
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;
|
|
return resResult(STATUS.SUCCESS, {
|
|
todayReceiveCnt: 0,
|
|
todaySendCnt: 0,
|
|
list,
|
|
friendCnt, blockCnt
|
|
});
|
|
}
|
|
} |