Skip to content

Commit

Permalink
first version core guildleave
Browse files Browse the repository at this point in the history
  • Loading branch information
Ntalcme committed Jan 15, 2025
1 parent a4efc70 commit 161e149
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 0 deletions.
91 changes: 91 additions & 0 deletions Core/src/commands/guild/GuildLeaveCommand.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
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<void> {
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<void> => {
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);
}
}
Empty file.
16 changes: 16 additions & 0 deletions Lib/src/packets/commands/CommandGuildLeavePacket.ts
Original file line number Diff line number Diff line change
@@ -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;
}
40 changes: 40 additions & 0 deletions Lib/src/packets/interaction/ReactionCollectorGuildLeave.ts
Original file line number Diff line number Diff line change
@@ -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
})
};
}
}

0 comments on commit 161e149

Please sign in to comment.