196 lines
5.0 KiB
JavaScript
196 lines
5.0 KiB
JavaScript
const GuildService = require("../guildService");
|
|
const Code = require("../../../../../shared/code");
|
|
const { errors } = require("../../../lib/db");
|
|
|
|
module.exports = function (app) {
|
|
return new Handler(app);
|
|
};
|
|
|
|
class Handler {
|
|
constructor(app) {
|
|
this.guildService = new GuildService(app);
|
|
}
|
|
|
|
async listGuild(msg, session, next) {
|
|
const guilds = (await this.guildService.listGuild()) || [];
|
|
next(null, { code: Code.OK, guilds });
|
|
}
|
|
|
|
async createGuild(msg, session, next) {
|
|
const uid = session.uid;
|
|
const userInfo = session.get("userInfo");
|
|
const { guildName } = msg;
|
|
try {
|
|
const result = await this.guildService.createGuild(
|
|
guildName,
|
|
userInfo.account_id
|
|
);
|
|
if (result) {
|
|
next(null, { code: Code.OK, guildId: result });
|
|
} else {
|
|
next(null, {
|
|
code: Code.GUILD.FA_GUILD_CREATE_FAILED,
|
|
msg: "Create guild failed",
|
|
});
|
|
}
|
|
} catch (err) {
|
|
switch (err.errno) {
|
|
case errors.ER_DUP_ENTRY:
|
|
next(null, {
|
|
code: Code.GUILD.FA_GUILD_CREATE_FAILED,
|
|
msg: "Guild already created",
|
|
});
|
|
break;
|
|
default:
|
|
next(null, err);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
async joinGuild(msg, session, next) {
|
|
try {
|
|
const { uid, guildId } = msg;
|
|
const result = await this.guildService.joinGuild(uid, guildId);
|
|
const response = result
|
|
? { code: Code.OK, msg: "Join guild done" }
|
|
: { code: Code.GUILD.FA_GUILD_JOIN_FAILED, msg: "Join guild failed" };
|
|
next(null, response);
|
|
} catch (error) {
|
|
next(null, error);
|
|
}
|
|
}
|
|
|
|
async approveGuild(msg, session, next) {
|
|
const { guildId, newuid } = msg;
|
|
const { uid } = session;
|
|
try {
|
|
const result = await this.guildService.approveGuild(guildId, uid, newuid);
|
|
if (result) {
|
|
next(null, { code: Code.OK, msg: "Approve guild done" });
|
|
} else {
|
|
next(null, {
|
|
code: Code.GUILD.FA_GUILD_APPROVE_FAILED,
|
|
msg: "Approve guild failed",
|
|
});
|
|
}
|
|
} catch (err) {
|
|
next(null, err);
|
|
}
|
|
}
|
|
|
|
async inviteNewMember(msg, session, next) {
|
|
const { guildId, newuid } = msg;
|
|
const { uid } = session;
|
|
try {
|
|
const result = await this.guildService.inviteNewMember(
|
|
uid,
|
|
guildId,
|
|
newuid
|
|
);
|
|
if (result) {
|
|
next(null, { code: Code.OK, msg: "Invite new member done" });
|
|
} else {
|
|
next(null, {
|
|
code: Code.GUILD.FA_GUILD_JOIN_FAILED,
|
|
msg: "Invite new member failed",
|
|
});
|
|
}
|
|
} catch (err) {
|
|
next(null, err);
|
|
}
|
|
}
|
|
|
|
async getInviteList(msg, session, next) {
|
|
const { uid } = session;
|
|
try {
|
|
const result = await this.guildService.getInviteList(uid);
|
|
if (result) {
|
|
next(null, { code: Code.OK, result: result });
|
|
} else {
|
|
next(null, { code: Code.FAIL, msg: "Get invite list failed" });
|
|
}
|
|
} catch (err) {
|
|
next(null, err);
|
|
}
|
|
}
|
|
|
|
async confirmInvite(msg, session, next) {
|
|
const { guildId } = msg;
|
|
const { uid } = session;
|
|
try {
|
|
const result = await this.guildService.confirmInvite(uid, guildId);
|
|
if (result) {
|
|
next(null, { code: Code.OK, msg: "Confirm invite done" });
|
|
} else {
|
|
next(null, { code: Code.FAIL, msg: "Confirm invite failed" });
|
|
}
|
|
} catch (err) {
|
|
next(null, err);
|
|
}
|
|
}
|
|
|
|
async leaveGuild(msg, session, next) {
|
|
const { uid } = session;
|
|
const result = await this.guildService.leaveGuild(uid);
|
|
next(null, result);
|
|
}
|
|
|
|
async kick(msg, session, next) {
|
|
const { muid } = msg;
|
|
const { uid } = session;
|
|
try {
|
|
const result = await this.guildService.kick(uid, muid);
|
|
next(null, result);
|
|
} catch (err) {
|
|
next(null, err);
|
|
}
|
|
}
|
|
async getGuildMembers(msg, session, next) {
|
|
const { guildId, apply } = msg;
|
|
try {
|
|
const result = await this.guildService.getMembers(guildId, apply);
|
|
next(null, { code: Code.OK, members: result, apply: apply });
|
|
} catch (err) {
|
|
next(null, err);
|
|
}
|
|
}
|
|
|
|
async updateMemberRank(msg, session, next) {
|
|
const { memberUid, guildId, g_rank } = msg;
|
|
const { uid } = session;
|
|
try {
|
|
const result = await this.guildService.updateMemberRank(
|
|
uid,
|
|
guildId,
|
|
memberUid,
|
|
g_rank
|
|
);
|
|
next(null, { code: Code.OK, result: result });
|
|
} catch (err) {
|
|
next(null, err);
|
|
}
|
|
}
|
|
|
|
async setAnnounce(msg, session, next) {
|
|
const {gid, announce} = msg;
|
|
const {uid} = session;
|
|
try {
|
|
const result = await this.guildService.setAnnounce(uid, gid, announce);
|
|
next(null, { code: Code.OK, result: result });
|
|
} catch(err) {
|
|
next(null, err);
|
|
}
|
|
}
|
|
|
|
async disbandGuild(msg, session, next) {
|
|
const { uid } = session;
|
|
try {
|
|
const result = await this.guildService.disbandGuild(uid);
|
|
next(null, { code: Code.OK, msg: "Disband guild done" });
|
|
} catch (err) {
|
|
next(null, err);
|
|
}
|
|
}
|
|
}
|