Skip to content

Commit

Permalink
fix nta's work #2318
Browse files Browse the repository at this point in the history
  • Loading branch information
romain22222 committed Dec 18, 2024
1 parent 70e1aff commit 264f358
Show file tree
Hide file tree
Showing 11 changed files with 146 additions and 90 deletions.
49 changes: 32 additions & 17 deletions Core/src/core/smallEvents/epicItemShop.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
import {Shop} from "./interfaces/Shop";
import {
SmallEventShopAcceptPacket,
SmallEventShopCannotBuyPacket,
SmallEventShopRefusePacket
} from "../../../../Lib/src/packets/smallEvents/SmallEventShopPacket";
import {GenericItem} from "../../data/GenericItem";
import {RandomUtils} from "../../../../Lib/src/utils/RandomUtils";
import {SmallEventConstants} from "../../../../Lib/src/constants/SmallEventConstants";
import {generateRandomItem} from "../utils/ItemUtils";
import {ItemCategory} from "../../../../Lib/src/constants/ItemConstants";
import {ItemCategory, ItemConstants} from "../../../../Lib/src/constants/ItemConstants";
import {makePacket} from "../../../../Lib/src/packets/DraftBotPacket";
import {SmallEventFuncs} from "../../data/SmallEvent";
import {MapConstants} from "../../../../Lib/src/constants/MapConstants";
import Player from "../database/game/models/Player";
import {
SmallEventEpicItemShopAcceptPacket,
SmallEventEpicItemShopCannotBuyPacket,
SmallEventEpicItemShopRefusePacket
} from "../../../../Lib/src/packets/smallEvents/SmallEventEpicItemShopPacket";
import {
ReactionCollectorEpicShopSmallEvent,
ReactionCollectorEpicShopSmallEventData
} from "../../../../Lib/src/packets/interaction/ReactionCollectorEpicShopSmallEvent";

class ShopSmallEvent extends Shop<SmallEventShopAcceptPacket, SmallEventShopRefusePacket, SmallEventShopCannotBuyPacket> {
class ShopSmallEvent extends Shop<
SmallEventEpicItemShopAcceptPacket,
SmallEventEpicItemShopRefusePacket,
SmallEventEpicItemShopCannotBuyPacket,
ReactionCollectorEpicShopSmallEvent
> {
getPriceMultiplier(player: Player): number {
const destination = player.getDestination();
const origin = player.getPreviousMap();
Expand All @@ -26,6 +35,8 @@ class ShopSmallEvent extends Shop<SmallEventShopAcceptPacket, SmallEventShopRefu
}

getRandomItem(): GenericItem {

Check warning on line 38 in Core/src/core/smallEvents/epicItemShop.ts

View workflow job for this annotation

GitHub Actions / eslint-core-module

Trailing spaces not allowed
// We exclude potions from the list of possible items
const categories = Object.values(ItemCategory).filter(
(value): value is ItemCategory => value !== ItemCategory.POTION
);
Expand All @@ -34,25 +45,29 @@ class ShopSmallEvent extends Shop<SmallEventShopAcceptPacket, SmallEventShopRefu

return generateRandomItem(
randomCategory,
SmallEventConstants.EPIC_ITEM_SHOP.MIN_RARITY,
SmallEventConstants.EPIC_ITEM_SHOP.MAX_RARITY
ItemConstants.RARITY.EPIC,
ItemConstants.RARITY.LEGENDARY
);
}

getTip(): boolean {
return RandomUtils.draftbotRandom.bool(SmallEventConstants.EPIC_ITEM_SHOP.REDUCTION_TIP_PROBABILITY) && this.itemMultiplier > SmallEventConstants.EPIC_ITEM_SHOP.ROAD_OF_WONDERS_MULTIPLIER;
getAcceptPacket(): SmallEventEpicItemShopAcceptPacket {
return makePacket(SmallEventEpicItemShopAcceptPacket, {});
}

getAcceptPacket(): SmallEventShopAcceptPacket {
return makePacket(SmallEventShopAcceptPacket, {});
getRefusePacket(): SmallEventEpicItemShopRefusePacket {
return makePacket(SmallEventEpicItemShopRefusePacket, {});
}

getRefusePacket(): SmallEventShopRefusePacket {
return makePacket(SmallEventShopRefusePacket, {});
getCannotBuyPacket(): SmallEventEpicItemShopCannotBuyPacket {
return makePacket(SmallEventEpicItemShopCannotBuyPacket, {});
}

getCannotBuyPacket(): SmallEventShopCannotBuyPacket {
return makePacket(SmallEventShopCannotBuyPacket, {});
getPopulatedReactionCollector(basePacket: ReactionCollectorEpicShopSmallEventData): ReactionCollectorEpicShopSmallEvent {
return new ReactionCollectorEpicShopSmallEvent({
...basePacket,
tip: RandomUtils.draftbotRandom.bool(SmallEventConstants.EPIC_ITEM_SHOP.REDUCTION_TIP_PROBABILITY)
&& this.itemMultiplier > SmallEventConstants.EPIC_ITEM_SHOP.ROAD_OF_WONDERS_MULTIPLIER
});
}
}

Expand Down
27 changes: 17 additions & 10 deletions Core/src/core/smallEvents/interfaces/Shop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,18 @@ import {InventorySlots} from "../../database/game/models/InventorySlot";
import {SmallEventConstants} from "../../../../../Lib/src/constants/SmallEventConstants";
import {NumberChangeReason} from "../../../../../Lib/src/constants/LogsConstants";
import {DraftBotPacket} from "../../../../../Lib/src/packets/DraftBotPacket";
import {ReactionCollectorMerchant} from "../../../../../Lib/src/packets/interaction/ReactionCollectorMerchant";
import {ReactionCollectorAcceptReaction} from "../../../../../Lib/src/packets/interaction/ReactionCollectorPacket";
import {
ReactionCollector,
ReactionCollectorAcceptReaction
} from "../../../../../Lib/src/packets/interaction/ReactionCollectorPacket";
import {ReactionCollectorAnyShopSmallEventData} from "../../../../../Lib/src/packets/interaction/ReactionCollectorAnyShopSmallEvent";

export abstract class Shop<AcceptPacket extends SmallEventAnyShopAcceptedPacket, RefusePacket extends SmallEventAnyShopRefusedPacket, CannotBuyPacket extends SmallEventAnyShopCannotBuyPacket> {
export abstract class Shop<
Accept extends SmallEventAnyShopAcceptedPacket,
Refuse extends SmallEventAnyShopRefusedPacket,
CannotBuy extends SmallEventAnyShopCannotBuyPacket,
Collector extends ReactionCollector
> {
canBeExecuted = Maps.isOnContinent;

protected itemMultiplier: number;
Expand All @@ -31,23 +39,22 @@ export abstract class Shop<AcceptPacket extends SmallEventAnyShopAcceptedPacket,

abstract getPriceMultiplier(player: Player): number | Promise<number>;

abstract getAcceptPacket(): AcceptPacket;
abstract getAcceptPacket(): Accept;

abstract getRefusePacket(): RefusePacket;
abstract getRefusePacket(): Refuse;

abstract getCannotBuyPacket(): CannotBuyPacket;
abstract getCannotBuyPacket(): CannotBuy;

abstract getTip(): boolean;
abstract getPopulatedReactionCollector(basePacket: ReactionCollectorAnyShopSmallEventData): Collector;

public executeSmallEvent: ExecuteSmallEventLike = async (context, response, player) => {
this.itemMultiplier = await this.getPriceMultiplier(player);
this.randomItem = await this.getRandomItem();
this.itemPrice = Math.round(getItemValue(this.randomItem) * this.itemMultiplier);

const collector = new ReactionCollectorMerchant({
const collector = this.getPopulatedReactionCollector({
item: toItemWithDetails(this.randomItem),
price: this.itemPrice,
tip: this.getTip()
price: this.itemPrice
});

const packet = new ReactionCollectorInstance(
Expand Down
10 changes: 7 additions & 3 deletions Core/src/core/smallEvents/shop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@ import {generateRandomItem} from "../utils/ItemUtils";
import {ItemRarity} from "../../../../Lib/src/constants/ItemConstants";
import {makePacket} from "../../../../Lib/src/packets/DraftBotPacket";
import {SmallEventFuncs} from "../../data/SmallEvent";
import {
ReactionCollectorShopSmallEvent,
ReactionCollectorShopSmallEventData
} from "../../../../Lib/src/packets/interaction/ReactionCollectorShopSmallEvent";

class ShopSmallEvent extends Shop<SmallEventShopAcceptPacket, SmallEventShopRefusePacket, SmallEventShopCannotBuyPacket> {
class ShopSmallEvent extends Shop<SmallEventShopAcceptPacket, SmallEventShopRefusePacket, SmallEventShopCannotBuyPacket, ReactionCollectorShopSmallEvent> {
getPriceMultiplier(): number | Promise<number> {
return RandomUtils.draftbotRandom.bool(SmallEventConstants.SHOP.SCAM_PROBABILITY) ? SmallEventConstants.SHOP.SCAM_MULTIPLIER : SmallEventConstants.SHOP.BASE_MULTIPLIER;
}
Expand All @@ -33,8 +37,8 @@ class ShopSmallEvent extends Shop<SmallEventShopAcceptPacket, SmallEventShopRefu
return makePacket(SmallEventShopCannotBuyPacket, {});
}

getTip(): boolean {
return false;
getPopulatedReactionCollector(basePacket: ReactionCollectorShopSmallEventData): ReactionCollectorShopSmallEvent {
return new ReactionCollectorShopSmallEvent(basePacket);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ import {ReactionCollectorGobletsGameData} from "../../../../Lib/src/packets/inte
import {gobletsGameCollector} from "../../smallEvents/gobletsGame";
import {createUnlockCollector} from "../../commands/player/UnlockCommand";
import {ReactionCollectorUnlockData} from "../../../../Lib/src/packets/interaction/ReactionCollectorUnlock";
import {ReactionCollectorMerchantData} from "../../../../Lib/src/packets/interaction/ReactionCollectorMerchant";
import {smallShopCollector} from "../../smallEvents/shop";
import {epicItemShopCollector} from "../../smallEvents/epicItemShop";
import {ReactionCollectorEpicShopSmallEventData} from "../../../../Lib/src/packets/interaction/ReactionCollectorEpicShopSmallEvent";
import {ReactionCollectorShopSmallEventData} from "../../../../Lib/src/packets/interaction/ReactionCollectorShopSmallEvent";

export default class ReactionCollectorHandler {

Expand All @@ -63,8 +64,8 @@ export default class ReactionCollectorHandler {
ReactionCollectorHandler.collectorMap.set(ReactionCollectorGuildInviteData.name, createGuildInviteCollector);
ReactionCollectorHandler.collectorMap.set(ReactionCollectorGobletsGameData.name, gobletsGameCollector);
ReactionCollectorHandler.collectorMap.set(ReactionCollectorUnlockData.name, createUnlockCollector);
ReactionCollectorHandler.collectorMap.set(ReactionCollectorMerchantData.name, smallShopCollector);
ReactionCollectorHandler.collectorMap.set(ReactionCollectorMerchantData.name, epicItemShopCollector);
ReactionCollectorHandler.collectorMap.set(ReactionCollectorShopSmallEventData.name, smallShopCollector);
ReactionCollectorHandler.collectorMap.set(ReactionCollectorEpicShopSmallEventData.name, epicItemShopCollector);
}

@packetHandler(ReactionCollectorCreationPacket)
Expand Down
20 changes: 12 additions & 8 deletions Discord/src/smallEvents/epicItemShop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import {DiscordCache} from "../bot/DiscordCache";
import {DraftbotSmallEventEmbed} from "../messages/DraftbotSmallEventEmbed";
import {StringUtils} from "../utils/StringUtils";
import {DiscordCollectorUtils} from "../utils/DiscordCollectorUtils";
import {ReactionCollectorMerchantData} from "../../../Lib/src/packets/interaction/ReactionCollectorMerchant";
import {DisplayUtils} from "../utils/DisplayUtils";
import {Constants} from "../../../Lib/src/constants/Constants";
import i18n from "../translations/i18n";
import {ReactionCollectorEpicShopSmallEventData} from "../../../Lib/src/packets/interaction/ReactionCollectorEpicShopSmallEvent";

/**
* Send the initial embed for this small event
Expand All @@ -16,24 +16,28 @@ import i18n from "../translations/i18n";
*/
export async function epicItemShopCollector(packet: ReactionCollectorCreationPacket, context: PacketContext): Promise<void> {
const interaction = DiscordCache.getInteraction(context.discord!.interaction)!;
const data = packet.data.data as ReactionCollectorMerchantData;
const tip = data.tip ? StringUtils.getRandomTranslation("smallEvents:epicItemShop.reductionTip",interaction.userLanguage) : "";
if (!interaction) {
return;
}
const lng = interaction.userLanguage;
const data = packet.data.data as ReactionCollectorEpicShopSmallEventData;
const tip = data.tip ? i18n.t("smallEvents:epicItemShop.reductionTip", {lng}) : "";

const embed = new DraftbotSmallEventEmbed(
"epicItemShop",
StringUtils.getRandomTranslation("smallEvents:epicItemShop.intro", interaction.userLanguage)
StringUtils.getRandomTranslation("smallEvents:epicItemShop.intro", lng)
+ tip
+ StringUtils.getRandomTranslation("smallEvents:shop.end", interaction.userLanguage, {
item: DisplayUtils.getItemDisplayWithStats(data.item, interaction.userLanguage),
+ StringUtils.getRandomTranslation("smallEvents:shop.end", lng, {
item: DisplayUtils.getItemDisplayWithStats(data.item, lng),
price: data.price,
type: `${Constants.REACTIONS.ITEM_CATEGORIES[data.item.category]}${i18n.t("smallEvents:shop.types", {
returnObjects: true,
lng: interaction.userLanguage
lng
})[data.item.category]}`,
interpolation: {escapeValue: false}
}),
interaction.user,
interaction.userLanguage
lng
);

await DiscordCollectorUtils.createAcceptRefuseCollector(interaction, embed, packet, context);
Expand Down
4 changes: 2 additions & 2 deletions Discord/src/smallEvents/shop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import {DiscordCache} from "../bot/DiscordCache";
import {DraftbotSmallEventEmbed} from "../messages/DraftbotSmallEventEmbed";
import {StringUtils} from "../utils/StringUtils";
import {DiscordCollectorUtils} from "../utils/DiscordCollectorUtils";
import {ReactionCollectorMerchantData} from "../../../Lib/src/packets/interaction/ReactionCollectorMerchant";
import {RandomUtils} from "../../../Lib/src/utils/RandomUtils";
import {DisplayUtils} from "../utils/DisplayUtils";
import {Constants} from "../../../Lib/src/constants/Constants";
import i18n from "../translations/i18n";
import {StringConstants} from "../../../Lib/src/constants/StringConstants";
import {ReactionCollectorShopSmallEventData} from "../../../Lib/src/packets/interaction/ReactionCollectorShopSmallEvent";

/**
* Send the initial embed for this small event
Expand All @@ -18,7 +18,7 @@ import {StringConstants} from "../../../Lib/src/constants/StringConstants";
*/
export async function smallShopCollector(packet: ReactionCollectorCreationPacket, context: PacketContext): Promise<void> {
const interaction = DiscordCache.getInteraction(context.discord!.interaction)!;
const data = packet.data.data as ReactionCollectorMerchantData;
const data = packet.data.data as ReactionCollectorShopSmallEventData;
const gender = RandomUtils.draftbotRandom.bool() ? StringConstants.SEX.MALE : StringConstants.SEX.FEMALE;
const name = StringUtils.getRandomTranslation("smallEvents:shop.names", interaction.userLanguage, {context: gender.short});

Expand Down
4 changes: 1 addition & 3 deletions Lib/src/constants/SmallEventConstants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {ItemConstants, ItemRarity} from "./ItemConstants";
import {ItemRarity} from "./ItemConstants";
import {PetConstants} from "./PetConstants";

export abstract class SmallEventConstants {
Expand Down Expand Up @@ -263,8 +263,6 @@ export abstract class SmallEventConstants {
};

static readonly EPIC_ITEM_SHOP = {
MIN_RARITY: ItemConstants.RARITY.EPIC,
MAX_RARITY: ItemConstants.RARITY.LEGENDARY,
GREAT_DEAL_PROBABILITY: 0.1,
GREAT_DEAL_MULTIPLAYER: 3.5,
BASE_MULTIPLIER: 5.5,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {ReactionCollectorData} from "./ReactionCollectorPacket";
import {ItemWithDetails} from "../../interfaces/ItemWithDetails";

export class ReactionCollectorAnyShopSmallEventData extends ReactionCollectorData {
item!: ItemWithDetails;

price!: number;
}
32 changes: 32 additions & 0 deletions Lib/src/packets/interaction/ReactionCollectorEpicShopSmallEvent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import {
ReactionCollector,
ReactionCollectorAcceptReaction,
ReactionCollectorCreationPacket,
ReactionCollectorRefuseReaction
} from "./ReactionCollectorPacket";
import {ReactionCollectorAnyShopSmallEventData} from "./ReactionCollectorAnyShopSmallEvent";

export class ReactionCollectorEpicShopSmallEventData extends ReactionCollectorAnyShopSmallEventData {
tip!: boolean;
}

export class ReactionCollectorEpicShopSmallEvent extends ReactionCollector {
private readonly data: ReactionCollectorEpicShopSmallEventData;

constructor(data: ReactionCollectorEpicShopSmallEventData) {
super();
this.data = data;
}

creationPacket(id: string, endTime: number): ReactionCollectorCreationPacket {
return {
id,
endTime,
reactions: [
this.buildReaction(ReactionCollectorAcceptReaction, {}),
this.buildReaction(ReactionCollectorRefuseReaction, {})
],
data: this.buildData(ReactionCollectorEpicShopSmallEventData, this.data)
};
}
}
44 changes: 0 additions & 44 deletions Lib/src/packets/interaction/ReactionCollectorMerchant.ts

This file was deleted.

31 changes: 31 additions & 0 deletions Lib/src/packets/interaction/ReactionCollectorShopSmallEvent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import {
ReactionCollector,
ReactionCollectorAcceptReaction,
ReactionCollectorCreationPacket,
ReactionCollectorRefuseReaction
} from "./ReactionCollectorPacket";
import {ReactionCollectorAnyShopSmallEventData} from "./ReactionCollectorAnyShopSmallEvent";

export class ReactionCollectorShopSmallEventData extends ReactionCollectorAnyShopSmallEventData {
}

export class ReactionCollectorShopSmallEvent extends ReactionCollector {
private readonly data: ReactionCollectorShopSmallEventData;

constructor(data: ReactionCollectorShopSmallEventData) {
super();
this.data = data;
}

creationPacket(id: string, endTime: number): ReactionCollectorCreationPacket {
return {
id,
endTime,
reactions: [
this.buildReaction(ReactionCollectorAcceptReaction, {}),
this.buildReaction(ReactionCollectorRefuseReaction, {})
],
data: this.buildData(ReactionCollectorShopSmallEventData, this.data)
};
}
}

0 comments on commit 264f358

Please sign in to comment.