118 lines
4.2 KiB
TypeScript
118 lines
4.2 KiB
TypeScript
import { gameData, getDicFriendByLv } from "../pubUtils/data";
|
|
import { RoleType, RoleModel } from "../db/Role";
|
|
import { FriendRelationType, Relation } from "../db/FriendRelation";
|
|
import { getResStr } from "../pubUtils/util";
|
|
import { STATUS, FRIEND_RELATION_TYPE } from "../consts";
|
|
import { FriendShipType, FriendShipModel } from "../db/FriendShip";
|
|
import { outputCnt } from '../pubUtils/friendUtil';
|
|
import { isRoleOnline } from "./redisService";
|
|
|
|
/**
|
|
* 增加双方好友数
|
|
* @param role1 我方
|
|
* @param role2 对方
|
|
* @returns 是否添加成功
|
|
*/
|
|
export async function increaseFrdCnt(role1: RoleType, role2: RoleType, originalFriendCnt: number) {
|
|
|
|
let { roleId, lv, friendCnt } = role1;
|
|
let dicFriend = getDicFriendByLv(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;
|
|
role1 = incMyFrdCnt; role2 = incHisFrdCnt;
|
|
|
|
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;
|
|
}
|
|
|
|
/**
|
|
* 根据收到的新及时间排序
|
|
*
|
|
* @param roleId 自己的玩家id
|
|
* @param list 需要排序列表
|
|
*/
|
|
export async function sortByBeSentHeart(roleId: string, list: Relation[]) {
|
|
|
|
let result = new Array<Relation & {beSentHeart: number, beSentHeartTime: number} & { quitTime: number, friendValue: number, isOnline: boolean}>();
|
|
for(let cur of list) {
|
|
let json = getSentHeart(roleId, cur);
|
|
let friendRole = <RoleType>cur.role;
|
|
let friendShip = <FriendShipType>cur.friendShip;
|
|
let isOnline = await isRoleOnline(friendRole.roleId);
|
|
result.push({...cur, ...json, isOnline, quitTime: friendRole.quitTime, friendValue: friendShip.friendValue});
|
|
}
|
|
|
|
result.sort((a, b) => {
|
|
if(a.beSentHeart && b.beSentHeart) { // 只要有一方送过
|
|
if(a.beSentHeart == b.beSentHeart) {
|
|
return a.beSentHeartTime - b.beSentHeartTime;
|
|
} else {
|
|
return b.beSentHeart - a.beSentHeart;
|
|
}
|
|
} else { // 都没送过的情况
|
|
if(a.isOnline != b.isOnline) {
|
|
return a.isOnline?1:-1
|
|
}
|
|
if(a.quitTime != b.quitTime) {
|
|
return b.quitTime - a.quitTime
|
|
}
|
|
return a.friendValue - b.friendValue;
|
|
}
|
|
});
|
|
return result
|
|
}
|
|
|
|
function getSentHeart(roleId: string, relation: Relation) {
|
|
let friendShip = <FriendShipType>relation.friendShip;
|
|
let json = outputCnt(roleId, friendShip);
|
|
if(!json) {
|
|
return {
|
|
beSentHeart: 0,
|
|
beSentHeartTime: 0
|
|
}
|
|
}
|
|
return {
|
|
beSentHeart: json.beSentHeart,
|
|
beSentHeartTime: json.beSentHeartTime
|
|
}
|
|
}
|
|
|
|
export async function getFriendLvAdd(roleId: string, hisRoleId: string) {
|
|
let friendShip = await FriendShipModel.getFriendLv(roleId, hisRoleId);
|
|
if(!friendShip) {
|
|
return 0
|
|
}
|
|
let dic = gameData.roleFriendLv.get(friendShip.friendLv);
|
|
if(!dic) {
|
|
return 0
|
|
}
|
|
return dic.comBattleAdd
|
|
} |