获取列表,申请,同意
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
import { Application, BackendSession, pinus } from 'pinus';
|
||||
import { resResult } from '../../../pubUtils/util';
|
||||
import { STATUS, GUILD_OPERATE, GUILD_AUTH, GUILD_JOB } from '../../../consts';
|
||||
import { STATUS, GUILD_OPERATE, GUILD_AUTH, GUILD_JOB, GUILD_APPLY_TYPE } from '../../../consts';
|
||||
import { UserGuildModel } from '../../../db/UserGuild';
|
||||
import { checkAuth } from '../../../services/guildService';
|
||||
import { checkAuth, joinGuild } from '../../../services/guildService';
|
||||
import { GuildModel } from '../../../db/Guild';
|
||||
import { RoleModel, RoleType } from '../../../db/Role';
|
||||
import { GUILD } from '../../../pubUtils/dicParam';
|
||||
@@ -10,6 +10,7 @@ import { handleCost } from '../../../services/rewardService';
|
||||
import { getGoldObject } from '../../../pubUtils/itemUtils';
|
||||
import { getSeconds } from '../../../pubUtils/timeUtil';
|
||||
import { GuildListInfo } from '../../../pubUtils/interface';
|
||||
import { UserGuildApplyModel } from '../../../db/UserGuildApply';
|
||||
|
||||
export default function (app: Application) {
|
||||
return new GuildHandler(app);
|
||||
@@ -92,7 +93,9 @@ export class GuildHandler {
|
||||
if(!userGuild) {
|
||||
return resResult(STATUS.SUCCESS, { hasGuild: false })
|
||||
};
|
||||
const guild = await GuildModel.findByCode(userGuild.guildCode);
|
||||
let guild = await GuildModel.findByCode(userGuild.guildCode);
|
||||
|
||||
// TODO 刷新日活跃,周活跃
|
||||
|
||||
// TODO 获取排行榜
|
||||
const rank = 0;
|
||||
@@ -102,5 +105,80 @@ export class GuildHandler {
|
||||
return resResult(STATUS.SUCCESS, result);
|
||||
}
|
||||
|
||||
// 玩家申请公会
|
||||
async applyGuild(msg: { code: string }, session: BackendSession) {
|
||||
|
||||
const roleId = session.get('roleId');
|
||||
const { code } = msg;
|
||||
|
||||
//TODO 检查权限是否够 等策划表
|
||||
|
||||
const guild = await GuildModel.findByCode(code);
|
||||
const { isMemberMax, isAuto, ceLimit, lv } = guild;
|
||||
if(isMemberMax) {
|
||||
return resResult(STATUS.GUILD_MEMBER_MAX);
|
||||
}
|
||||
|
||||
const role = await RoleModel.findByRoleId(roleId);
|
||||
if(ceLimit > role.ce) {
|
||||
return resResult(STATUS.GUILD_NOT_REACH_CONDI);
|
||||
}
|
||||
|
||||
let hasGuild = false;
|
||||
if(isAuto) { // 自动加入
|
||||
const joinResult = await joinGuild(code, lv, roleId);
|
||||
if(joinResult.status == 0) {
|
||||
return joinResult.resResult;
|
||||
}
|
||||
|
||||
hasGuild = true;
|
||||
} else { // 不自动加入,插入申请表
|
||||
await UserGuildApplyModel.createUserGuildApply(role, guild, GUILD_APPLY_TYPE.APPLY);
|
||||
}
|
||||
|
||||
// 返回
|
||||
return resResult(STATUS.SUCCESS, { code, hasGuild });
|
||||
}
|
||||
|
||||
// 玩家申请公会
|
||||
async getApplyList(msg: { code: string, lastApplyCode: string }, session: BackendSession) {
|
||||
|
||||
const roleId = session.get('roleId');
|
||||
const { code, lastApplyCode } = msg;
|
||||
|
||||
// TODO 查询权限 等策划表
|
||||
|
||||
const result = await UserGuildApplyModel.getListByGuild(code, lastApplyCode);
|
||||
const list = result.map(cur => {
|
||||
let role = <RoleType>cur.role;
|
||||
return { applyCode: cur.applyCode, ...role };
|
||||
});
|
||||
|
||||
return resResult(STATUS.SUCCESS, { list });
|
||||
}
|
||||
|
||||
// 团长同意/拒绝公会申请
|
||||
async receiveApply(msg: { code: string, applyCodeList: [string], isReceived: boolean }, session: BackendSession) {
|
||||
|
||||
const roleId = session.get('roleId');
|
||||
const { code, applyCodeList, isReceived } = msg;
|
||||
|
||||
// TODO 查询权限 等策划表
|
||||
|
||||
let roleIds = new Array<string>();
|
||||
if(isReceived) { // 同意申请,加入
|
||||
const guild = await GuildModel.findByCode(code);
|
||||
const applyList = await UserGuildApplyModel.getListByApplyCode(applyCodeList);
|
||||
|
||||
for(let { roleId } of applyList) {
|
||||
const joinResult = await joinGuild(code, guild.lv, roleId);
|
||||
if(joinResult.status == 0) break;
|
||||
roleIds.push(roleId);
|
||||
}
|
||||
} else { // 拒绝申请,删除申请
|
||||
await UserGuildApplyModel.deleteApplyByApplyCode(applyCodeList);
|
||||
}
|
||||
|
||||
return resResult(STATUS.SUCCESS, { roleIds });
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,10 @@
|
||||
import { gameData } from "../pubUtils/data";
|
||||
import { GuildModel } from "../db/Guild";
|
||||
import { resResult } from "../pubUtils/util";
|
||||
import { STATUS } from "../consts";
|
||||
import { RoleModel } from "../db/Role";
|
||||
import { UserGuildModel } from "../db/UserGuild";
|
||||
import { UserGuildApplyModel } from "../db/UserGuildApply";
|
||||
|
||||
/**
|
||||
* @description 检查该玩家是否有权限做操作
|
||||
@@ -9,4 +15,35 @@ export function checkAuth(func: number, auth: number) {
|
||||
const dicGuildAuth = gameData.guildAuth.get(func);
|
||||
if(!dicGuildAuth) return false;
|
||||
return dicGuildAuth.includes(auth);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 加入公会
|
||||
* @param code 公会code
|
||||
* @param lv 公会当前等级,判断人数用
|
||||
* @param roleId 加入的玩家
|
||||
*/
|
||||
export async function joinGuild(code: string, lv: number, roleId: string) {
|
||||
|
||||
const hasGuild = await UserGuildModel.getMyGuild(roleId);
|
||||
if(!!hasGuild) {
|
||||
return { status: 0, resResult: resResult(STATUS.GUILD_HAS_JOIN) };
|
||||
}
|
||||
|
||||
// TODO 读策划表获取最大人数
|
||||
const maxMemberCnt = 10;
|
||||
const guild = await GuildModel.addGuild(code, roleId, maxMemberCnt);
|
||||
if(!guild) {
|
||||
return { status: 0, resResult: resResult(STATUS.GUILD_MEMBER_MAX) };
|
||||
}
|
||||
|
||||
const role = await RoleModel.findByRoleId(roleId);
|
||||
const userGuild = await UserGuildModel.createUserGuild(guild.code, role, false);
|
||||
if(!userGuild) {
|
||||
return { status: 0, resResult: resResult(STATUS.GUILD_CREATE_ERROR) };
|
||||
}
|
||||
|
||||
await UserGuildApplyModel.deleteApply(roleId); // 删除玩家所有对其他公会的申请
|
||||
|
||||
return { status: 1, guild, userGuild }
|
||||
}
|
||||
@@ -112,7 +112,7 @@ describe('军团测试', function() {
|
||||
expect(res.data.leader).to.have.deep.property('loginTime').that.is.an('number');
|
||||
|
||||
expect(res.data).to.have.deep.property('myInfo').that.is.an('object');
|
||||
expect(res.data.myInfo).to.have.deep.property('honour').that.is.an('number');
|
||||
expect(res.data.myInfo).to.have.deep.property('honourWeekly').that.is.an('number');
|
||||
expect(res.data.myInfo).to.have.deep.property('job').that.is.an('number');
|
||||
expect(res.data.myInfo).to.have.deep.property('auth').that.is.an('number');
|
||||
|
||||
@@ -126,4 +126,25 @@ describe('军团测试', function() {
|
||||
});
|
||||
});
|
||||
|
||||
it('获取申请列表', function (done) {
|
||||
pinusClient.request('battle.guildHandler.getApplyList', { }, (res) => {
|
||||
// 消息回调
|
||||
expect(res).to.be.an('object');
|
||||
expect(res.code).equal(0);
|
||||
expect(res.data).to.be.an('object');
|
||||
expect(res.data).to.have.deep.property('list').that.is.an('array');
|
||||
res.data.list.forEach(list => {
|
||||
expect(list).to.have.deep.property('applyCode').that.is.a('string');
|
||||
expect(list).to.have.deep.property('roleId').that.is.a('string');
|
||||
expect(list).to.have.deep.property('roleName').that.is.a('string');
|
||||
expect(list).to.have.deep.property('ce').that.is.a('number');
|
||||
expect(list).to.have.deep.property('headHid').that.is.a('number');
|
||||
expect(list).to.have.deep.property('sHid').that.is.a('number');
|
||||
expect(list).to.have.deep.property('lv').that.is.a('number');
|
||||
expect(list).to.have.deep.property('title').that.is.a('string');
|
||||
expect(list).to.have.deep.property('loginTime').that.is.a('number');
|
||||
});
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -48,4 +48,17 @@ export enum USER_GUILD_STATUS {
|
||||
}
|
||||
|
||||
// 查询每页显示
|
||||
export const GUILD_PER_PAGE = 30;
|
||||
export const GUILD_PER_PAGE = 30;
|
||||
|
||||
// 公会申请状态
|
||||
export enum GUILD_APPLY_STATUS {
|
||||
WAITING = 1, // 等待操作
|
||||
AGREE = 2, // 同意
|
||||
REFUSE = 3 // 拒绝
|
||||
}
|
||||
|
||||
// 公会申请类型
|
||||
export enum GUILD_APPLY_TYPE {
|
||||
APPLY = 1, // 申请
|
||||
INVITE = 2 // 邀请
|
||||
}
|
||||
@@ -124,8 +124,11 @@ export const STATUS = {
|
||||
|
||||
// 军团 20900-20999
|
||||
GUILD_AUTH_NOT_ENOUGH: { code: 20900, simStr: '权限不足' },
|
||||
GUILD_NAME_DUP: { code: 20901, simStr: '公会名重复' },
|
||||
GUILD_NAME_DUP: { code: 20901, simStr: '军团名重复' },
|
||||
GUILD_CREATE_ERROR: { code: 20902, simStr: '创建失败' },
|
||||
GUILD_MEMBER_MAX: { code: 20903, simStr: '军团人员已满' },
|
||||
GUILD_NOT_REACH_CONDI: { code: 20903, simStr: '您未达到申请军团条件' },
|
||||
GUILD_HAS_JOIN: { code: 20904, simStr: '已加入过其他军团' },
|
||||
|
||||
// 通用 30000 - 30099
|
||||
DIC_DATA_NOT_FOUND: { code: 30000, simStr: '数据表未找到' },
|
||||
|
||||
@@ -95,7 +95,7 @@ export default class Guild extends BaseModel {
|
||||
}
|
||||
|
||||
public static async findByCondition(page: number, showPeopleMax: boolean, name: string) {
|
||||
const condition = {};
|
||||
const condition = { status: GUILD_STATUS.RUNNING };
|
||||
if(!showPeopleMax) {
|
||||
condition['isMemberMax'] = false;
|
||||
}
|
||||
@@ -103,6 +103,7 @@ export default class Guild extends BaseModel {
|
||||
condition['name'] = { $regex: new RegExp(name, 'i') }
|
||||
}
|
||||
const guildList = await GuildModel.find(condition)
|
||||
.sort({ lv: -1, ce: -1 })
|
||||
.limit(GUILD_PER_PAGE).skip((page - 1) * GUILD_PER_PAGE)
|
||||
.select('code icon name lv memberCnt leader ceLimit isAuto')
|
||||
.populate('leader', 'roleName', 'Role')
|
||||
@@ -111,11 +112,19 @@ export default class Guild extends BaseModel {
|
||||
}
|
||||
|
||||
public static async findByCode(code: string) {
|
||||
const result = await GuildModel.findOne({ code, status: GUILD_STATUS.RUNNING }, { _id: 0 })
|
||||
const result = await GuildModel.findOne({ code, status: GUILD_STATUS.RUNNING })
|
||||
.populate('leader', 'roleName sHid headHid lv loginTime ce', 'Role')
|
||||
.lean();
|
||||
return result;
|
||||
}
|
||||
|
||||
public static async addGuild(code: string, roleId: string, maxMemberCnt: number) {
|
||||
let result = await GuildModel.findOneAndUpdate({ code, memberCnt: {$lt: maxMemberCnt } }, { $inc: { memberCnt: 1 }, $push: { members: roleId } }, { new: true }).lean();
|
||||
if(result && result.memberCnt >= maxMemberCnt) {
|
||||
result = await GuildModel.findOneAndUpdate({ code }, { $set: { isMemberMax: true } }).lean();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
export const GuildModel = getModelForClass(Guild);
|
||||
|
||||
@@ -30,10 +30,7 @@ export default class UserGuild extends BaseModel {
|
||||
job: number;
|
||||
|
||||
@prop({ required: true, default: 0 })
|
||||
honour: number;
|
||||
|
||||
@prop({ required: true, default: 0 })
|
||||
honourWeek: number;
|
||||
honourWeekly: number;
|
||||
|
||||
@prop({ required: true, default: USER_GUILD_STATUS.ON, enum: USER_GUILD_STATUS})
|
||||
status: number;
|
||||
@@ -66,10 +63,10 @@ export default class UserGuild extends BaseModel {
|
||||
const doc = new UserGuildModel();
|
||||
let job = isLeader? GUILD_JOB.JIANGJUN: GUILD_JOB.SHIZHANG;
|
||||
let auth = isLeader? GUILD_AUTH.LEADER: GUILD_AUTH.MEMBER;
|
||||
const update = Object.assign(doc.toJSON(), { guildCode, roleId: role.roleId, job, auth });
|
||||
const update = Object.assign(doc.toJSON(), { guildCode, roleId: role.roleId, role: role._id, job, auth });
|
||||
delete update._id;
|
||||
const result: UserGuildType = await UserGuildModel.findOneAndUpdate({ guildCode, status: USER_GUILD_STATUS.ON }, update, { upsert: true, new: true })
|
||||
.select('honour job auth')
|
||||
.select('honourWeekly job auth')
|
||||
.lean();
|
||||
|
||||
return result;
|
||||
|
||||
76
shared/db/UserGuildApply.ts
Normal file
76
shared/db/UserGuildApply.ts
Normal file
@@ -0,0 +1,76 @@
|
||||
import BaseModel from './BaseModel';
|
||||
import { index, getModelForClass, prop, DocumentType, Ref } from '@typegoose/typegoose';
|
||||
import Role, { RoleType } from './Role';
|
||||
import Guild, { GuildType } from './Guild';
|
||||
import { GUILD_AUTH, USER_GUILD_STATUS, GUILD_JOB, GUILD_APPLY_STATUS, GUILD_APPLY_TYPE, GUILD_PER_PAGE, GUILD_STATUS } from '../consts';
|
||||
import { genCode } from '../pubUtils/util';
|
||||
|
||||
@index({ roleId: 1 })
|
||||
export default class UserGuildApply extends BaseModel {
|
||||
@prop({ required: true })
|
||||
applyCode: string; // 唯一code
|
||||
|
||||
@prop({ required: true })
|
||||
roleId: string; // 玩家id
|
||||
|
||||
@prop({ required: true })
|
||||
role: Ref<Role>;
|
||||
|
||||
@prop({ required: true })
|
||||
guildCode: string; // 公会唯一code
|
||||
|
||||
@prop({ required: true })
|
||||
guild: Ref<Guild>;
|
||||
|
||||
@prop({ required: true, enum: GUILD_APPLY_TYPE, select: false})
|
||||
type: number;
|
||||
|
||||
public static async createUserGuildApply(role: RoleType, guild: GuildType, type: number) {
|
||||
const applyCode = genCode(10);
|
||||
const doc = new UserGuildApplyModel();
|
||||
const update = Object.assign(doc.toJSON(), { applyCode, roleId: role.roleId, role: role._id, guildCode: guild.code, guild: guild._id, type });
|
||||
delete update._id;
|
||||
const result: UserGuildApplyType = await UserGuildApplyModel.findOneAndUpdate({ applyCode }, update, { upsert: true, new: true }).lean();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public static async deleteApply(roleId: string) {
|
||||
const result = await UserGuildApplyModel.deleteMany({ roleId, type: GUILD_APPLY_TYPE.APPLY });
|
||||
return result;
|
||||
}
|
||||
|
||||
public static async deleteApplyByApplyCode(applyCodeList: string[]) {
|
||||
const result = await UserGuildApplyModel.deleteMany({ applyCode: { $in: applyCodeList}, type: GUILD_APPLY_TYPE.APPLY });
|
||||
return result;
|
||||
}
|
||||
|
||||
public static async findByCode(applyCode: string) {
|
||||
const userGuildApply: UserGuildApplyType = await UserGuildApplyModel.findOne({ applyCode }).lean();
|
||||
return userGuildApply;
|
||||
}
|
||||
|
||||
public static async getListByApplyCode(applyCodeList: string[]) {
|
||||
const userGuildApply: UserGuildApplyType[] = await UserGuildApplyModel.find({ applyCode: { $in: applyCodeList}, type: GUILD_APPLY_TYPE.APPLY }).lean();
|
||||
return userGuildApply;
|
||||
}
|
||||
|
||||
public static async getListByGuild(code: string, lastApplyCode: string) {
|
||||
let condition = { guildCode: code, type: GUILD_APPLY_TYPE.APPLY };
|
||||
if(lastApplyCode) {
|
||||
const lastApply = await this.findByCode(lastApplyCode);
|
||||
condition["createdAt"] = { $lt: lastApply.createdAt };
|
||||
}
|
||||
const list: UserGuildApplyType[] = await UserGuildApplyModel.find(condition)
|
||||
.sort({ createdAt: -1 })
|
||||
.select('applyCode role')
|
||||
.populate('role', 'roleId roleName ce headHid sHid lv title job loginTime', 'Role')
|
||||
.limit(GUILD_PER_PAGE).lean();
|
||||
return list
|
||||
}
|
||||
}
|
||||
|
||||
export const UserGuildApplyModel = getModelForClass(UserGuildApply);
|
||||
|
||||
export interface UserGuildApplyType extends Pick<DocumentType<UserGuildApply>, keyof UserGuildApply> { };
|
||||
export type UserGuildApplyUpdateParam = Partial<UserGuildApplyType>; // 将所有字段变成可选项
|
||||
Reference in New Issue
Block a user