From 161e149f8e7ba1cbcb5da251440382b037e4dfd2 Mon Sep 17 00:00:00 2001 From: Ntalcme Date: Wed, 15 Jan 2025 17:56:35 +0100 Subject: [PATCH] first version core guildleave #2273 --- Core/src/commands/guild/GuildLeaveCommand.ts | 91 +++++++++++++++++++ .../src/commands/guild/GuildLeaveCommand.ts | 0 .../commands/CommandGuildLeavePacket.ts | 16 ++++ .../ReactionCollectorGuildLeave.ts | 40 ++++++++ 4 files changed, 147 insertions(+) create mode 100644 Core/src/commands/guild/GuildLeaveCommand.ts create mode 100644 Discord/src/commands/guild/GuildLeaveCommand.ts create mode 100644 Lib/src/packets/commands/CommandGuildLeavePacket.ts create mode 100644 Lib/src/packets/interaction/ReactionCollectorGuildLeave.ts diff --git a/Core/src/commands/guild/GuildLeaveCommand.ts b/Core/src/commands/guild/GuildLeaveCommand.ts new file mode 100644 index 000000000..0a525cfb8 --- /dev/null +++ b/Core/src/commands/guild/GuildLeaveCommand.ts @@ -0,0 +1,91 @@ +import {commandRequires, CommandUtils} from "../../core/utils/CommandUtils"; +import {GuildConstants} from "../../../../Lib/src/constants/GuildConstants"; +import {DraftBotPacket, makePacket, PacketContext} from "../../../../Lib/src/packets/DraftBotPacket"; +import Player, {Players} from "../../core/database/game/models/Player"; +import { + CommandGuildLeaveAcceptPacketRes, + CommandGuildLeavePacketReq, CommandGuildLeaveRefusePacketRes +} from "../../../../Lib/src/packets/commands/CommandGuildLeavePacket"; +import {Guilds} from "../../core/database/game/models/Guild"; +import {ReactionCollectorGuildLeave} from "../../../../Lib/src/packets/interaction/ReactionCollectorGuildLeave"; +import {EndCallback, ReactionCollectorInstance} from "../../core/utils/ReactionsCollector"; +import {ReactionCollectorAcceptReaction} from "../../../../Lib/src/packets/interaction/ReactionCollectorPacket"; +import {BlockingUtils} from "../../core/utils/BlockingUtils"; +import {BlockingConstants} from "../../../../Lib/src/constants/BlockingConstants"; + +async function acceptGuildleave(player: Player, response: DraftBotPacket[]): Promise { + await player.reload(); + if (player.guildId === null) { + return; + } + const guild = await Guilds.getById(player.guildId); + if (player.id === guild.chiefId) { + if (guild.elderId !== null) { + const elder = await Players.getById(guild.elderId); + guild.elderId = null; + guild.chiefId = elder.id; + response.push(makePacket(CommandGuildLeaveAcceptPacketRes, { + newChiefKeycloakId: elder.keycloakId, + guildName: guild.name + })); + return; + } + // TODO : détruire la guilde ici + response.push(makePacket(CommandGuildLeaveAcceptPacketRes, { + guildName: guild.name + })); + return; + } + if (guild.elderId === player.id) { + guild.elderId = null; + } + player.guildId = null; + response.push(makePacket(CommandGuildLeaveAcceptPacketRes, { + guildName: guild.name + })); +} + +export default class GuildElderCommand { + @commandRequires(CommandGuildLeavePacketReq, { + notBlocked: true, + disallowedEffects: CommandUtils.DISALLOWED_EFFECTS.NOT_STARTED_OR_DEAD, + level: GuildConstants.REQUIRED_LEVEL, + guildNeeded: true + }) + async execute(response: DraftBotPacket[], player: Player, packet: CommandGuildLeavePacketReq, context: PacketContext): Promise { + if (player.guildId === null) { + return; + } + const guild = await Guilds.getById(player.guildId); + const elder = await Players.getById(guild.elderId); + + const collector = new ReactionCollectorGuildLeave( + guild.name, + elder.keycloakId + ); + const endCallback: EndCallback = async (collector: ReactionCollectorInstance, response: DraftBotPacket[]): Promise => { + const reaction = collector.getFirstReaction(); + if (reaction && reaction.reaction.type === ReactionCollectorAcceptReaction.name) { + await acceptGuildleave(player, response); + } + else { + response.push(makePacket(CommandGuildLeaveRefusePacketRes, {})); + } + BlockingUtils.unblockPlayer(player.id, BlockingConstants.REASONS.GUILD_LEAVE); + }; + + const collectorPacket = new ReactionCollectorInstance( + collector, + context, + { + allowedPlayerKeycloakIds: [player.keycloakId], + reactionLimit: 1 + }, + endCallback + ) + .block(player.id, BlockingConstants.REASONS.GUILD_LEAVE) + .build(); + + response.push(collectorPacket); + } +} \ No newline at end of file diff --git a/Discord/src/commands/guild/GuildLeaveCommand.ts b/Discord/src/commands/guild/GuildLeaveCommand.ts new file mode 100644 index 000000000..e69de29bb diff --git a/Lib/src/packets/commands/CommandGuildLeavePacket.ts b/Lib/src/packets/commands/CommandGuildLeavePacket.ts new file mode 100644 index 000000000..0c33dbb40 --- /dev/null +++ b/Lib/src/packets/commands/CommandGuildLeavePacket.ts @@ -0,0 +1,16 @@ +import {DraftBotPacket, PacketDirection, sendablePacket} from "../DraftBotPacket"; + +@sendablePacket(PacketDirection.FRONT_TO_BACK) +export class CommandGuildLeavePacketReq extends DraftBotPacket { +} + +@sendablePacket(PacketDirection.BACK_TO_FRONT) +export class CommandGuildLeaveRefusePacketRes extends DraftBotPacket { +} + +@sendablePacket(PacketDirection.BACK_TO_FRONT) +export class CommandGuildLeaveAcceptPacketRes extends DraftBotPacket { + newChiefKeycloakId?: string; + + guildName!: string; +} \ No newline at end of file diff --git a/Lib/src/packets/interaction/ReactionCollectorGuildLeave.ts b/Lib/src/packets/interaction/ReactionCollectorGuildLeave.ts new file mode 100644 index 000000000..aeda15b14 --- /dev/null +++ b/Lib/src/packets/interaction/ReactionCollectorGuildLeave.ts @@ -0,0 +1,40 @@ +import { + ReactionCollector, + ReactionCollectorAcceptReaction, + ReactionCollectorCreationPacket, + ReactionCollectorData, + ReactionCollectorRefuseReaction +} from "./ReactionCollectorPacket"; + +export class ReactionCollectorGuildLeaveData extends ReactionCollectorData { + guildName!: string; + + newChiefKeycloakId!: string | null; +} + +export class ReactionCollectorGuildLeave extends ReactionCollector { + private readonly guildName: string; + + private readonly newChiefKeycloakId: string; + + constructor(guildName: string, newChiefKeycloakId: string) { + super(); + this.guildName = guildName; + this.newChiefKeycloakId = newChiefKeycloakId; + } + + creationPacket(id: string, endTime: number): ReactionCollectorCreationPacket { + return { + id, + endTime, + reactions: [ + this.buildReaction(ReactionCollectorAcceptReaction, {}), + this.buildReaction(ReactionCollectorRefuseReaction, {}) + ], + data: this.buildData(ReactionCollectorGuildLeaveData, { + guildName: this.guildName, + newChiefKeycloakId: this.newChiefKeycloakId + }) + }; + } +} \ No newline at end of file