好友:一键申请提示
This commit is contained in:
@@ -1,13 +1,15 @@
|
||||
import { Application, BackendSession } from "pinus";
|
||||
import { resResult, getRandEelm } from "../../../pubUtils/util";
|
||||
import { STATUS, ROLE } from "../../../consts";
|
||||
import Role, { RoleModel, RoleType } from "../../../db/Role";
|
||||
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 } from "../../../domain/roleField/friend";
|
||||
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);
|
||||
@@ -25,9 +27,41 @@ export class FriendHandler {
|
||||
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;
|
||||
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 });
|
||||
}
|
||||
@@ -39,18 +73,38 @@ export class FriendHandler {
|
||||
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: true
|
||||
isSuccess: str == '',
|
||||
msg: str,
|
||||
roleIds: resultRoleIds
|
||||
});
|
||||
}
|
||||
|
||||
// 获取申请列表
|
||||
public async getApplyList(msg: { lastApplyCode: string }, session: BackendSession) {
|
||||
// TODO 分页
|
||||
public async getApplyList(msg: { }, session: BackendSession) {
|
||||
|
||||
let roleId: string = session.get('roleId');
|
||||
|
||||
let list = await FriendApplyModel.getApplyList(roleId);
|
||||
@@ -72,28 +126,58 @@ export class FriendHandler {
|
||||
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 result = new Array<FriendApplyParams>();
|
||||
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;
|
||||
// TODO检查我的好友人数上限,检查加过好友没有
|
||||
// TODO检查对方的好友人数上限
|
||||
str = await increaseFrdCnt(role, friend, friendCnt);
|
||||
if(str != '') continue;
|
||||
|
||||
// 创建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);
|
||||
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(applyCodeList);
|
||||
await FriendApplyModel.deleteApply(resultApplyCodeList);
|
||||
|
||||
return resResult(STATUS.SUCCESS, {
|
||||
list: result
|
||||
isSuccess: str == '',
|
||||
msg: str,
|
||||
applyCodeList: resultApplyCodeList,
|
||||
list,
|
||||
friendCnt
|
||||
});
|
||||
}
|
||||
|
||||
@@ -127,9 +211,12 @@ export class FriendHandler {
|
||||
}
|
||||
|
||||
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: 0,
|
||||
todaySendCnt: 0,
|
||||
todayReceiveCnt,
|
||||
todaySendCnt,
|
||||
list,
|
||||
friendCnt, blockCnt
|
||||
});
|
||||
|
||||
51
game-server/app/services/friendService.ts
Normal file
51
game-server/app/services/friendService.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import { gameData } from "../pubUtils/data";
|
||||
import { RoleType, RoleModel } from "../db/Role";
|
||||
import { FriendRelationType } from "../db/FriendRelation";
|
||||
import { getResStr } from "../pubUtils/util";
|
||||
import { STATUS, FRIEND_RELATION_TYPE } from "../consts";
|
||||
|
||||
/**
|
||||
* 增加双方好友数
|
||||
* @param role1 我方
|
||||
* @param role2 对方
|
||||
* @returns 是否添加成功
|
||||
*/
|
||||
export async function increaseFrdCnt(role1: RoleType, role2: RoleType, originalFriendCnt: number) {
|
||||
|
||||
let { roleId, lv, friendCnt } = role1;
|
||||
let dicFriend = gameData.roleFriend.get(lv);
|
||||
|
||||
if(friendCnt >= dicFriend.frdCnt) return getResStr(STATUS.FRIEND_MY_CNT_MAX);
|
||||
|
||||
let { roleId: _roleId, lv: _lv, friendCnt: _friendCnt } = role2;
|
||||
let _dicFriend = gameData.roleFriend.get(_lv);
|
||||
if(_friendCnt >= _dicFriend.frdCnt) return getResStr(STATUS.FRIEND_THEY_CNT_MAX);
|
||||
|
||||
let incMyFrdCnt = await RoleModel.increaseFriendCnt(roleId, 1, dicFriend.frdCnt);
|
||||
if(!incMyFrdCnt) return getResStr(STATUS.FRIEND_MY_CNT_MAX);
|
||||
let incHisFrdCnt = await RoleModel.increaseFriendCnt(_roleId, 1, _dicFriend.frdCnt);
|
||||
if(!incHisFrdCnt) { // 回滚
|
||||
await RoleModel.increaseFriendCnt(roleId, -1, dicFriend.frdCnt);
|
||||
return getResStr(STATUS.FRIEND_THEY_CNT_MAX);
|
||||
}
|
||||
originalFriendCnt = incMyFrdCnt.friendCnt;
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
export function getRecommendType(myFriendRelation: FriendRelationType, myRoleId: string, roleId: string) {
|
||||
if(myRoleId == roleId) {
|
||||
return FRIEND_RELATION_TYPE.MYSELF;
|
||||
}
|
||||
let friendList = myFriendRelation? myFriendRelation.friends: [];
|
||||
let blackList = myFriendRelation? myFriendRelation.blacklist: [];
|
||||
let hasFriend = friendList.find(cur => cur.roleId == roleId);
|
||||
if(hasFriend) {
|
||||
return FRIEND_RELATION_TYPE.HAS_FRIEND;
|
||||
}
|
||||
let hasBlcklist = blackList.find(cur => cur.roleId == roleId);
|
||||
if(hasBlcklist) {
|
||||
return FRIEND_RELATION_TYPE.HAS_BLOCKED;
|
||||
}
|
||||
return FRIEND_RELATION_TYPE.NORMAL;
|
||||
}
|
||||
Reference in New Issue
Block a user