好友:拉黑

This commit is contained in:
luying
2021-02-03 11:49:46 +08:00
parent 585a741be5
commit 2d2c9907ab
7 changed files with 194 additions and 14 deletions
@@ -1,10 +1,10 @@
import { Application, BackendSession } from "pinus"; import { Application, BackendSession } from "pinus";
import { resResult, getRandEelm, getResStr } from "../../../pubUtils/util"; import { resResult, getRandEelm, getResStr } from "../../../pubUtils/util";
import { STATUS, ROLE, FRIEND_DROP_TYPE, FRIEND_RELATION_TYPE } from "../../../consts"; import { STATUS, ROLE, FRIEND_DROP_TYPE, FRIEND_RELATION_TYPE, POPULATE_TYPE, BLOCK_OPEATE } from "../../../consts";
import { RoleModel, RoleType } from "../../../db/Role"; import { RoleModel, RoleType } from "../../../db/Role";
import { getBeforeDaySeconds } from "../../../pubUtils/timeUtil"; import { getBeforeDaySeconds } from "../../../pubUtils/timeUtil";
import { FriendApplyModel } from "../../../db/FriendApply"; import { FriendApplyModel } from "../../../db/FriendApply";
import { FriendApplyParams, FriendListParam, FriendRecommendParams } from "../../../domain/roleField/friend"; import { FriendApplyParams, FriendListParam, FriendRecommendParams, BlackListParam } from "../../../domain/roleField/friend";
import { FriendShipModel, FriendShipType } from "../../../db/FriendShip"; import { FriendShipModel, FriendShipType } from "../../../db/FriendShip";
import { FriendRelationModel } from "../../../db/FriendRelation"; import { FriendRelationModel } from "../../../db/FriendRelation";
import { isRoleOnline } from "../../../services/redisService"; import { isRoleOnline } from "../../../services/redisService";
@@ -29,7 +29,7 @@ export class FriendHandler {
const { lv } = await RoleModel.findByRoleId(roleId, ROLE.GET_LV); const { lv } = await RoleModel.findByRoleId(roleId, ROLE.GET_LV);
let allList = await RoleModel.getRecommedList(lv - 5, lv + 5, day); let allList = await RoleModel.getRecommedList(lv - 5, lv + 5, day);
let myFriendRelation = await FriendRelationModel.findFriendByRole(roleId, false); let myFriendRelation = await FriendRelationModel.findFriendByRole(roleId, POPULATE_TYPE.NOT);
let paramAllList = new Array<FriendRecommendParams>(); let paramAllList = new Array<FriendRecommendParams>();
for(let role of allList) { for(let role of allList) {
@@ -53,7 +53,7 @@ export class FriendHandler {
let { value } = msg; let { value } = msg;
let allList = await RoleModel.searchByNameOrId(value); let allList = await RoleModel.searchByNameOrId(value);
let myFriendRelation = await FriendRelationModel.findFriendByRole(roleId, false); let myFriendRelation = await FriendRelationModel.findFriendByRole(roleId, POPULATE_TYPE.NOT);
let list = new Array<FriendRecommendParams>(); let list = new Array<FriendRecommendParams>();
for(let role of allList) { for(let role of allList) {
@@ -73,7 +73,7 @@ export class FriendHandler {
let roleIds = msg.roleIds; let roleIds = msg.roleIds;
const role = await RoleModel.findByRoleId(roleId, ROLE.GET_ROLE_ID); const role = await RoleModel.findByRoleId(roleId, ROLE.GET_ROLE_ID);
let myFriendRelation = await FriendRelationModel.findFriendByRole(roleId, false); let myFriendRelation = await FriendRelationModel.findFriendByRole(roleId, POPULATE_TYPE.NOT);
let str = '', resultRoleIds = new Array<string>(); let str = '', resultRoleIds = new Array<string>();
for(let hisRoleId of roleIds) { for(let hisRoleId of roleIds) {
@@ -128,7 +128,7 @@ export class FriendHandler {
let role = await RoleModel.findByRoleId(roleId); let role = await RoleModel.findByRoleId(roleId);
let friendCnt = role.friendCnt; let friendCnt = role.friendCnt;
let myFriendRelation = await FriendRelationModel.findFriendByRole(roleId, false); let myFriendRelation = await FriendRelationModel.findFriendByRole(roleId, POPULATE_TYPE.NOT);
let applyList = await FriendApplyModel.getApplyListByCode(applyCodeList); let applyList = await FriendApplyModel.getApplyListByCode(applyCodeList);
let list = new Array<FriendListParam>(); let list = new Array<FriendListParam>();
@@ -187,7 +187,7 @@ export class FriendHandler {
let role = await RoleModel.findByRoleId(roleId); let role = await RoleModel.findByRoleId(roleId);
let list = new Array<FriendListParam>(); let list = new Array<FriendListParam>();
let myRelation = await FriendRelationModel.findFriendByRole(roleId); let myRelation = await FriendRelationModel.findFriendByRole(roleId, POPULATE_TYPE.FRIEND);
let friendList = myRelation?myRelation.friends: []; let friendList = myRelation?myRelation.friends: [];
for(let friend of friendList) { for(let friend of friendList) {
let role = <RoleType>friend.role; let role = <RoleType>friend.role;
@@ -221,4 +221,112 @@ export class FriendHandler {
friendCnt, blockCnt 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);
}
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
});
}
} }
+12
View File
@@ -369,4 +369,16 @@ export enum FRIEND_RELATION_TYPE {
HAS_FRIEND = 2, HAS_FRIEND = 2,
HAS_BLOCKED = 3, HAS_BLOCKED = 3,
MYSELF = 4 MYSELF = 4
}
export enum POPULATE_TYPE {
NOT = 0,
FRIEND = 1,
BLACKLIST = 2
}
export enum BLOCK_OPEATE {
ADD = 1,
REMOVE = 2,
REMOVE_AND_APPLY = 3
} }
+3 -1
View File
@@ -251,7 +251,9 @@ export const STATUS = {
FRIEND_YOURSELF: { code: 30704, simStr: '不能添加自己为好友' }, FRIEND_YOURSELF: { code: 30704, simStr: '不能添加自己为好友' },
FRIEND_HIS_APPLY_MAX: { code: 30705, simStr: '该玩家申请达上限' }, FRIEND_HIS_APPLY_MAX: { code: 30705, simStr: '该玩家申请达上限' },
FRIEND_SHIP_CREATE_ERROR: { code: 30706, simStr: '创建失败' }, FRIEND_SHIP_CREATE_ERROR: { code: 30706, simStr: '创建失败' },
FRIEND_BLACK_MAX: { code: 30707, simStr: '黑名单数量已达上限' },
FRIEND_BLACK_NOT_FOUND: { code: 30709, simStr: '该玩家不在黑名单' },
FRIEND_BLACK_HAS_ADD: { code: 30710, simStr: '该玩家已在黑名单' },
// 社交相关状态 40000 - 49999 // 社交相关状态 40000 - 49999
// 运营模块相关状态 50000 - 59999 // 运营模块相关状态 50000 - 59999
+24 -2
View File
@@ -41,20 +41,42 @@ export default class FriendRelation extends BaseModel {
// 获取列表 // 获取列表
public static async findFriendByRole(roleId: string, populate = true) { public static async findFriendByRole(roleId: string, populateType: number) {
let document = FriendRelationModel.findOne({ roleId }) let document = FriendRelationModel.findOne({ roleId })
.lean({ getters: true, virtuals: true }); .lean({ getters: true, virtuals: true });
if (populate) { if (populateType == 1) {
document document
.populate('friends.role', ROLE.SHOW_SIMPLE, 'Role') .populate('friends.role', ROLE.SHOW_SIMPLE, 'Role')
.populate('friends.friendShip', null, 'FriendShip'); .populate('friends.friendShip', null, 'FriendShip');
} else if (populateType == 2) {
document
.populate('blacklist.role', ROLE.SHOW_SIMPLE, 'Role')
} }
let result: FriendRelationType = await document; let result: FriendRelationType = await document;
return result; return result;
} }
public static async moveFromFriend(roleId: string, friend: RoleType, friendShip: FriendShipType, toBlack: boolean ) {
let update = {
$pull: { friends: { roleId: friend.roleId } }
}
if(toBlack) {
update['$push'] = { blacklist: { roleId: friend.roleId, role: friend._id, friendShip: friendShip? friendShip._id: null } };
}
let result: FriendRelationType = await FriendRelationModel.findOneAndUpdate({ roleId }, update, { new: true });
return result
}
public static async removeFromBlack(roleId: string, frdRoleId: string ) {
let result: FriendRelationType = await FriendRelationModel.findOneAndUpdate({ roleId }, {
$pull: { blacklist: { roleId: frdRoleId } }
}, { new: true });
return result
}
public static async deleteAccount(roleId: string) { public static async deleteAccount(roleId: string) {
let result = await FriendRelationModel.deleteMany({ roleId }); let result = await FriendRelationModel.deleteMany({ roleId });
await FriendRelationModel.updateMany({}, { $pull: { friends: { roleId }, blacklist: { roleId } }}); await FriendRelationModel.updateMany({}, { $pull: { friends: { roleId }, blacklist: { roleId } }});
+10 -2
View File
@@ -69,16 +69,24 @@ export default class FriendShip extends BaseModel {
public static async createFriendShip(roleIdList: string[]) { public static async createFriendShip(roleIdList: string[]) {
if (roleIdList.length != 2) return false; if (roleIdList.length != 2) return false;
let { roleIdA, roleIdB, roleIds } = this.getRoleIds(roleIdList); let { roleIdA, roleIdB, roleIds } = this.getRoleIds(roleIdList);
console.log(roleIdA, roleIdB, roleIds)
const doc = new FriendShipModel(); const doc = new FriendShipModel();
const update = Object.assign(doc.toJSON(), { roleIdA, roleIdB, roleIds }); const update = Object.assign(doc.toJSON(), { roleIdA, roleIdB, roleIds });
const result: FriendShipType = await FriendShipModel.findOneAndUpdate({ roleIds }, update, { upsert: true, new: true }).lean(); const result: FriendShipType = await FriendShipModel.findOneAndUpdate({ roleIds }, { $setOnInsert: update }, { upsert: true, new: true }).lean();
return result; return result;
} }
// 清空亲密度
public static async clearValue(roleIdList: string[]) {
if (roleIdList.length != 2) return false;
let { roleIds } = this.getRoleIds(roleIdList);
const result: FriendShipType = await FriendShipModel.findOneAndUpdate({ roleIds }, { $set: { friendValue: 0 } }, { new: true }).lean();
return result;
}
// 查询关系 // 查询关系
public static async deleteAccount(roleId: string) { public static async deleteAccount(roleId: string) {
let result = await FriendShipModel.deleteMany({ $or: [{ roleIdA: roleId }, { roleIdB: roleId }] }); let result = await FriendShipModel.deleteMany({ $or: [{ roleIdA: roleId }, { roleIdB: roleId }] });
+13 -2
View File
@@ -353,6 +353,7 @@ export default class Role extends BaseModel {
const role: RoleType = await RoleModel.findOneAndUpdate({roleId}, {$inc: {towerTaskCnt: num}}, {new: true}).lean(lean); const role: RoleType = await RoleModel.findOneAndUpdate({roleId}, {$inc: {towerTaskCnt: num}}, {new: true}).lean(lean);
return role; return role;
} }
// 派遣任务增加刷新次数 // 派遣任务增加刷新次数
public static async increaseTowerRefCnt(roleId: string, num: number, lean = true) { public static async increaseTowerRefCnt(roleId: string, num: number, lean = true) {
const role: RoleType = await RoleModel.findOneAndUpdate({roleId}, {$inc: {towerTaskReCnt: num}}, {new: true}).lean(lean); const role: RoleType = await RoleModel.findOneAndUpdate({roleId}, {$inc: {towerTaskReCnt: num}}, {new: true}).lean(lean);
@@ -482,8 +483,12 @@ export default class Role extends BaseModel {
} }
// 增加好友数量 // 增加好友数量
public static async increaseFriendCnt(roleId: string, inc: number, max: number) { public static async increaseFriendCnt(roleId: string, inc: number, max?: number) {
const result = await RoleModel.findOneAndUpdate({ roleId, friendCnt: {$lte: max - inc}}, {$inc: { friendCnt: inc }}, {new: true}).lean(); let condition = { roleId };
if(max) {
condition['friendCnt'] = {$lte: max - inc};
}
const result = await RoleModel.findOneAndUpdate(condition, {$inc: { friendCnt: inc }}, {new: true}).lean();
return result; return result;
} }
@@ -492,6 +497,12 @@ export default class Role extends BaseModel {
const result = await RoleModel.findOneAndUpdate({ roleId, recFrdApplyCnt: {$lte: max - inc}}, {$inc: { recFrdApplyCnt: inc }}, {new: true}).lean(); const result = await RoleModel.findOneAndUpdate({ roleId, recFrdApplyCnt: {$lte: max - inc}}, {$inc: { recFrdApplyCnt: inc }}, {new: true}).lean();
return result; return result;
} }
// 增加黑名单数量
public static async increaseBlockCnt(roleId: string, blockCnt: number, friendCnt: number, maxBlockCnt: number) {
const result = await RoleModel.findOneAndUpdate({ roleId, blockCnt: {$lte: maxBlockCnt - blockCnt}}, {$inc: { blockCnt, friendCnt }}, {new: true}).lean();
return result;
}
} }
export const RoleModel = getModelForClass(Role); export const RoleModel = getModelForClass(Role);
+17
View File
@@ -77,4 +77,21 @@ export class FriendListParam extends FriendParams {
setCanSend(canSend: boolean) { setCanSend(canSend: boolean) {
this.canSend = canSend; this.canSend = canSend;
} }
}
export class BlackListParam extends FriendParams {
guildName: string = "";
isOnline: boolean = false;
quitTime: number = 0;
constructor(role: RoleType) {
super(role);
if(role.guildName) this.guildName = role.guildName;
this.quitTime = role.quitTime;
}
setOnline(isOnline: boolean) {
this.isOnline = isOnline;
}
} }