好友:初步申请到查看链

This commit is contained in:
luying
2021-02-02 14:16:56 +08:00
parent a9e2b79c79
commit 5df5f6ed93
15 changed files with 582 additions and 16 deletions

70
shared/db/FriendApply.ts Normal file
View File

@@ -0,0 +1,70 @@
import BaseModel from './BaseModel';
import { index, getModelForClass, prop, DocumentType, mongoose, Ref } from '@typegoose/typegoose';
import Role, { RoleType } from './Role';
import { genCode } from '../pubUtils/util';
import { ROLE } from '../consts';
/**
* 好友系统关系表
*/
@index({ roleId: 1, createdAt: -1 })
export default class FriendApply extends BaseModel {
@prop({ required: true, default: '' })
applyCode: string; // 唯一code
@prop({ required: true, default: '' })
roleId: string; // 操作玩家本人id
@prop({ required: true, default: '' })
frdRoleId: string; // 好友id
@prop({ required: true, ref: 'Role', type: mongoose.Schema.Types.ObjectId })
friend: Ref<Role>;
// 创建申请
public static async createApply(roleId: string, friend: RoleType) {
const applyCode = genCode(10);
const result: FriendApplyType = await FriendApplyModel.findOneAndUpdate({ roleId, frdRoleId: friend.roleId },
{
$set: { roleId, frdRoleId: friend.roleId, friend: friend._id },
$setOnInsert: { applyCode }
}, { upsert: true, new: true }).lean();
return result;
}
// 获取列表
public static async getApplyList(roleId: string) {
const list: FriendApplyType[] = await FriendApplyModel.find({ roleId }, { _id: 0 })
// .select(select)
.populate('friend', ROLE.SHOW_SIMPLE, 'Role')
.lean({ getters: true });
return list;
}
// 根据applyCode获得申请
public static async getApplyListByCode(applyCodeList: string[]) {
const list: FriendApplyType[] = await FriendApplyModel.find({ applyCode: { $in: applyCodeList } }, { _id: 0 })
// .select(select)
.populate('friend', ROLE.SHOW_SIMPLE, 'Role')
.lean({ getters: true });
return list;
}
// 删除申请
public static async deleteApply(applyCodeList: string[]) {
const result = await FriendApplyModel.deleteMany({ applyCode: { $in: applyCodeList } });
return result;
}
public static async deleteAccount(roleId: string) {
let result = await FriendApplyModel.deleteMany({ $or: [{ roleId: roleId }, { frdRoleId: roleId }] });
return result;
}
}
export const FriendApplyModel = getModelForClass(FriendApply);
export interface FriendApplyType extends Pick<DocumentType<FriendApply>, keyof FriendApply> { };

View File

@@ -1,7 +1,8 @@
import BaseModel from './BaseModel';
import { index, getModelForClass, prop, DocumentType, mongoose, Ref } from '@typegoose/typegoose';
import Role from './Role';
import FriendShip from './FriendShip';
import Role, { RoleType } from './Role';
import FriendShip, { FriendShipType } from './FriendShip';
import { ROLE } from '../consts';
class Relation {
@prop({ required: true, default: '' })
@@ -26,9 +27,36 @@ export default class FriendRelation extends BaseModel {
@prop({ required: true, default: [], type: Relation, _id: false })
blacklist: Relation[]; // 黑名单列表
// 创建
public static async addFriend(roleId: string, friend: RoleType, friendShip: FriendShipType) {
const result: FriendRelationType = await FriendRelationModel.findOneAndUpdate(
{ roleId }, {
$setOnInsert: { blacklist: [] },
$push: { friends: { roleId: friend.roleId, role: friend._id, friendShip: friendShip._id } }
}, { upsert: true, new: true }).lean();
return result;
}
// 获取列表
public static async findFriendByRole(roleId: string) {
const result: FriendRelationType = await FriendRelationModel.findOne({ roleId })
.populate('friends.role', ROLE.SHOW_SIMPLE, 'Role')
.populate('friends.friendShip', null, 'FriendShip')
.lean({ getters: true, virtuals: true });
return result;
}
public static async deleteAccount(roleId: string) {
let result = await FriendRelationModel.deleteMany({ roleId });
await FriendRelationModel.updateMany({}, { $pull: { friends: { roleId }, blacklist: { roleId } }});
return result;
}
}
export const FriendRelationModel = getModelForClass(FriendRelation);
export interface FriendRelationType extends Pick<DocumentType<FriendRelation>, keyof FriendRelation>{};
export interface FriendRelationType extends Pick<DocumentType<FriendRelation>, keyof FriendRelation> { };

View File

@@ -1,5 +1,6 @@
import BaseModel from './BaseModel';
import { index, getModelForClass, prop, DocumentType } from '@typegoose/typegoose';
import { getFriendLvByExp } from '../pubUtils/data';
/**
* 好友直接的亲密值
@@ -28,15 +29,63 @@ export default class FriendShip extends BaseModel {
refTime: Date;
@prop({ required: true, default: 0 })
frdValue: number; // 两人之间的亲密值
friendValue: number; // 两人之间的亲密值
public get frdLv() { // 亲密等级
return this.frdValue;
public get friendLv() { // 亲密等级
return getFriendLvByExp(this.friendValue);
}
private static getRoleIds(roleIds: string[]) {
roleIds.sort();
return {
roleIdA: roleIds[0],
roleIdB: roleIds[1],
roleIds: roleIds.join('|')
}
}
public static getRoleSendAndReceive(myRoleId: string, roleIds: string[], friendShip: FriendShipType) {
let { roleIdA, roleIdB } = this.getRoleIds(roleIds);
if (roleIdA == myRoleId) {
return {
sendHeart: friendShip.sendHeartCntA,
beSentHeart: friendShip.sendHeartCntB,
recieveHeart: friendShip.receiveHeartCntA,
beReceivedHeart: friendShip.receiveHeartCntB
}
} else if (roleIdB == myRoleId) {
return {
sendHeart: friendShip.sendHeartCntB,
beSentHeart: friendShip.sendHeartCntA,
receiveHeart: friendShip.receiveHeartCntB,
beReceivedHeart: friendShip.receiveHeartCntA
}
} else {
return false
}
}
// 创建
public static async createFriendShip(roleIdList: string[]) {
if (roleIdList.length != 2) return false;
let { roleIdA, roleIdB, roleIds } = this.getRoleIds(roleIdList);
console.log(roleIdA, roleIdB, roleIds)
const doc = new FriendShipModel();
const update = Object.assign(doc.toJSON(), { roleIdA, roleIdB, roleIds });
const result: FriendShipType = await FriendShipModel.findOneAndUpdate({ roleIds }, update, { upsert: true, new: true }).lean();
return result;
}
public static async deleteAccount(roleId: string) {
let result = await FriendShipModel.deleteMany({ $or: [{ roleIdA: roleId }, { roleIdB: roleId }] });
return result;
}
}
export const FriendShipModel = getModelForClass(FriendShip);
export interface FriendShipType extends Pick<DocumentType<FriendShip>, keyof FriendShip>{};
export interface FriendShipType extends Pick<DocumentType<FriendShip>, keyof FriendShip> { };

View File

@@ -437,7 +437,7 @@ export default class Role extends BaseModel {
// 获取未加入公会且登录时间在三天内的人
public static async getInviteList(time: number, serverId: number) {
const result = await RoleModel.find({ quitTime: { $gt: time }, hasGuild: false, serverId })
const result = await RoleModel.find({ loginTime: { $gt: time }, hasGuild: false, serverId })
.select('roleId roleName ce headHid sHid lv title job quitTime')
.sort({quitTime: -1, lv: -1, ce: -1})
.limit(100).lean({getters: true});
@@ -445,8 +445,8 @@ export default class Role extends BaseModel {
}
// 获取好友推荐列表
public static async getRecommedList(minLv: number, maxLv: number, time: number) {
const result = await RoleModel.find({ quitTime: { $gt: time }, lv: { $gte: minLv, $lte: maxLv } }, { _id: 0 })
public static async getRecommedList(roleId: string, minLv: number, maxLv: number, time: number) {
const result = await RoleModel.find({ loginTime: { $gt: time }, lv: { $gte: minLv, $lte: maxLv }, roleId: { $ne: roleId } }, { _id: 0 })
.select(ROLE.SHOW_SIMPLE)
.sort({quitTime: -1, lv: -1, ce: -1})
.limit(100).lean({ getters: true });