Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

shop SE v5 #2673

Merged
merged 8 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Core/src/core/database/game/models/PetEntity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {Pet, PetDataController} from "../../../../data/Pet";
import {draftBotInstance} from "../../../../index";
import {DraftBotPacket} from "../../../../../../Lib/src/packets/DraftBotPacket";
import {PlayerReceivePetPacket} from "../../../../../../Lib/src/packets/events/PlayerReceivePetPacket";
import {StringConstants} from "../../../../../../Lib/src/constants/StringConstants";
import moment = require("moment");

export class PetEntity extends Model {
Expand Down Expand Up @@ -122,11 +123,11 @@ export class PetEntity extends Model {
}

public isFemale(): boolean {
return this.sex === PetConstants.SEX.FEMALE;
return this.sex === StringConstants.SEX.FEMALE.short;
}

public isMale(): boolean {
return this.sex === PetConstants.SEX.MALE;
return this.sex === StringConstants.SEX.MALE.short;
}
}

Expand Down
35 changes: 20 additions & 15 deletions Core/src/core/smallEvents/interfaces/Shop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,23 @@ import {GenericItem} from "../../../data/GenericItem";
import Player from "../../database/game/models/Player";
import {Maps} from "../../maps/Maps";
import {ExecuteSmallEventLike} from "../../../data/SmallEvent";
import {getItemValue, giveItemToPlayer} from "../../utils/ItemUtils";
import {getItemValue, giveItemToPlayer, toItemWithDetails} from "../../utils/ItemUtils";
import {EndCallback, ReactionCollectorInstance} from "../../utils/ReactionsCollector";
import {BlockingConstants} from "../../../../../Lib/src/constants/BlockingConstants";
import {BlockingUtils} from "../../utils/BlockingUtils";
import {SmallEventAnyShopPacket} from "../../../../../Lib/src/packets/smallEvents/SmallEventAnyShopPacket";
import {
SmallEventAnyShopAcceptedPacket,
SmallEventAnyShopCannotBuyPacket,
SmallEventAnyShopRefusedPacket
} from "../../../../../Lib/src/packets/smallEvents/SmallEventAnyShopPacket";
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, ReactionCollectorMerchantAcceptReaction} from "../../../../../Lib/src/packets/interaction/ReactionCollectorMerchant";
import {ReactionCollectorMerchant} from "../../../../../Lib/src/packets/interaction/ReactionCollectorMerchant";
import {ReactionCollectorAcceptReaction} from "../../../../../Lib/src/packets/interaction/ReactionCollectorPacket";

export abstract class Shop<T extends SmallEventAnyShopPacket> {
export abstract class Shop<AcceptPacket extends SmallEventAnyShopAcceptedPacket, RefusePacket extends SmallEventAnyShopRefusedPacket, CannotBuyPacket extends SmallEventAnyShopCannotBuyPacket> {
canBeExecuted = Maps.isOnContinent;

protected itemMultiplier: number;
Expand All @@ -26,18 +31,19 @@ export abstract class Shop<T extends SmallEventAnyShopPacket> {

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

abstract getSmallEventPacket(): T;
abstract getAcceptPacket(): AcceptPacket;

abstract getRefusePacket(): RefusePacket;

abstract getCannotBuyPacket(): CannotBuyPacket;

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({
item: {
category: this.randomItem.getCategory(),
id: this.randomItem.id
},
item: toItemWithDetails(this.randomItem),
price: this.itemPrice
});

Expand All @@ -58,12 +64,11 @@ export abstract class Shop<T extends SmallEventAnyShopPacket> {
private callbackShopSmallEvent(player: Player): EndCallback {
return async (collector: ReactionCollectorInstance, response: DraftBotPacket[]) => {
BlockingUtils.unblockPlayer(player.id, BlockingConstants.REASONS.MERCHANT);
const packet = this.getSmallEventPacket();
const reaction = collector.getFirstReaction().reaction;
packet.isValidated = reaction && reaction instanceof ReactionCollectorMerchantAcceptReaction;
packet.canBuy = player.money >= this.itemPrice;
response.push(packet);
if (!packet.isValidated || !packet.canBuy) {
const reaction = collector.getFirstReaction();
const isValidated = reaction && reaction.reaction.type === ReactionCollectorAcceptReaction.name;
const canBuy = player.money >= this.itemPrice;
response.push(!isValidated ? this.getRefusePacket() : !canBuy ? this.getCannotBuyPacket() : this.getAcceptPacket());
if (!isValidated || !canBuy) {
return;
}
await giveItemToPlayer(
Expand Down
20 changes: 16 additions & 4 deletions Core/src/core/smallEvents/shop.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import {Shop} from "./interfaces/Shop";
import {SmallEventShopPacket} from "../../../../Lib/src/packets/smallEvents/SmallEventShopPacket";
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";
Expand All @@ -8,7 +12,7 @@ import {ItemRarity} from "../../../../Lib/src/constants/ItemConstants";
import {makePacket} from "../../../../Lib/src/packets/DraftBotPacket";
import {SmallEventFuncs} from "../../data/SmallEvent";

class ShopSmallEvent extends Shop<SmallEventShopPacket> {
class ShopSmallEvent extends Shop<SmallEventShopAcceptPacket, SmallEventShopRefusePacket, SmallEventShopCannotBuyPacket> {
getPriceMultiplier(): number | Promise<number> {
return RandomUtils.draftbotRandom.bool(SmallEventConstants.SHOP.SCAM_PROBABILITY) ? SmallEventConstants.SHOP.SCAM_MULTIPLIER : SmallEventConstants.SHOP.BASE_MULTIPLIER;
}
Expand All @@ -17,8 +21,16 @@ class ShopSmallEvent extends Shop<SmallEventShopPacket> {
return generateRandomItem(null, ItemRarity.COMMON, SmallEventConstants.SHOP.MAX_RARITY);
}

getSmallEventPacket(): SmallEventShopPacket {
return makePacket(SmallEventShopPacket,{});
getAcceptPacket(): SmallEventShopAcceptPacket {
return makePacket(SmallEventShopAcceptPacket, {});
}

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

getCannotBuyPacket(): SmallEventShopCannotBuyPacket {
return makePacket(SmallEventShopCannotBuyPacket, {});
}
}

Expand Down
4 changes: 2 additions & 2 deletions Discord/src/commands/guild/GuildDailyCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {DiscordCache} from "../../bot/DiscordCache";
import {DraftBotEmbed} from "../../messages/DraftBotEmbed";
import {DraftBotErrorEmbed} from "../../messages/DraftBotErrorEmbed";
import {finishInTimeDisplay} from "../../../../Lib/src/utils/TimeUtils";
import {PetConstants} from "../../../../Lib/src/constants/PetConstants";
import {StringConstants} from "../../../../Lib/src/constants/StringConstants";

function getPacket(): CommandGuildDailyPacketReq {
return makePacket(CommandGuildDailyPacketReq, {});
Expand Down Expand Up @@ -50,7 +50,7 @@ export function getCommandGuildDailyRewardPacketString(packet: CommandGuildDaily
if (packet.pet) {
desc += `${i18n.t("commands:guildDaily.rewards.pet", {
lng,
context: packet.pet.isFemale ? PetConstants.SEX.FEMALE_FULL : PetConstants.SEX.MALE_FULL,
context: packet.pet.isFemale ? StringConstants.SEX.FEMALE.long : StringConstants.SEX.MALE.long,
pet: DisplayUtils.getPetDisplay(packet.pet.typeId, packet.pet.isFemale, lng),
petId: packet.pet.typeId,
interpolation: {escapeValue: false}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ 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";

export default class ReactionCollectorHandler {

Expand All @@ -60,6 +62,7 @@ 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);
}

@packetHandler(ReactionCollectorCreationPacket)
Expand Down
27 changes: 21 additions & 6 deletions Discord/src/packetHandlers/handlers/SmallEventsHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ import {SmallEventBotFactsPacket} from "../../../../Lib/src/packets/smallEvents/
import {SmallEventDoNothingPacket} from "../../../../Lib/src/packets/smallEvents/SmallEventDoNothingPacket";
import {SmallEventFightPetPacket} from "../../../../Lib/src/packets/smallEvents/SmallEventFightPetPacket";
import {SmallEventGobletsGamePacket} from "../../../../Lib/src/packets/smallEvents/SmallEventGobletsGamePacket";
import {SmallEventShopPacket} from "../../../../Lib/src/packets/smallEvents/SmallEventShopPacket";
import {
SmallEventShopAcceptPacket,
SmallEventShopCannotBuyPacket,
SmallEventShopRefusePacket
} from "../../../../Lib/src/packets/smallEvents/SmallEventShopPacket";
import {SmallEventStaffMemberPacket} from "../../../../Lib/src/packets/smallEvents/SmallEventStaffMemberPacket";
import {SmallEventWinEnergyPacket} from "../../../../Lib/src/packets/smallEvents/SmallEventWinEnergyPacket";
import {SmallEventWinFightPointsPacket} from "../../../../Lib/src/packets/smallEvents/SmallEventWinFightPointsPacket";
Expand All @@ -55,7 +59,6 @@ import {
} from "../../../../Lib/src/packets/smallEvents/SmallEventSpacePacket";
import {SmallEventFindPetPacket} from "../../../../Lib/src/packets/smallEvents/SmallEventFindPetPacket";
import {PetUtils} from "../../utils/PetUtils";
import {PetConstants} from "../../../../Lib/src/constants/PetConstants";
import {ClassUtils} from "../../utils/ClassUtils";
import {SmallEventFindPotionPacket} from "../../../../Lib/src/packets/smallEvents/SmallEventFindPotionPacket";
import {SmallEventFindItemPacket} from "../../../../Lib/src/packets/smallEvents/SmallEventFindItemPacket";
Expand All @@ -69,6 +72,8 @@ import {cartResult} from "../../smallEvents/cart";
import {SmallEventFindMissionPacket} from "../../../../Lib/src/packets/smallEvents/SmallEventFindMissionPacket";
import {MissionUtils} from "../../utils/MissionUtils";
import {DraftBotEmbed} from "../../messages/DraftBotEmbed";
import {baseFunctionHandler} from "../../smallEvents/shop";
import {StringConstants} from "../../../../Lib/src/constants/StringConstants";


export function getRandomSmallEventIntro(language: Language): string {
Expand Down Expand Up @@ -288,7 +293,7 @@ export default class SmallEventsHandler {
level: packet.data.level,
class: `${DraftBotIcons.classes[packet.data.classId]} ${i18n.t(`models:classes.${packet.data.classId}`, {lng: interaction.userLanguage})}`,
advice: StringUtils.getRandomTranslation("advices:advices", interaction.userLanguage),
petEmote: packet.data.petId ? PetUtils.getPetIcon(packet.data.petId, packet.data.petSex ?? PetConstants.SEX.MALE) : "",
petEmote: packet.data.petId ? PetUtils.getPetIcon(packet.data.petId, packet.data.petSex ?? StringConstants.SEX.MALE.short) : "",
petName: packet.data.petName,
guildName: packet.data.guildName,
weapon: DisplayUtils.getWeaponDisplay(packet.data.weaponId, interaction.userLanguage),
Expand Down Expand Up @@ -807,9 +812,19 @@ export default class SmallEventsHandler {
}


@packetHandler(SmallEventShopPacket)
async smallEventShop(packet: SmallEventShopPacket, context: PacketContext): Promise<void> {
// Todo
@packetHandler(SmallEventShopRefusePacket)
async smallEventShopRefuse(packet: SmallEventShopRefusePacket, context: PacketContext): Promise<void> {
await baseFunctionHandler(context, "smallEvents:shop.refused");
}

@packetHandler(SmallEventShopAcceptPacket)
async smallEventShopAccept(packet: SmallEventShopAcceptPacket, context: PacketContext): Promise<void> {
await baseFunctionHandler(context, "smallEvents:shop.purchased");
}

@packetHandler(SmallEventShopCannotBuyPacket)
async smallEventShopCannotBuy(packet: SmallEventShopCannotBuyPacket, context: PacketContext): Promise<void> {
await baseFunctionHandler(context, "smallEvents:shop.notEnoughMoney");
}

@packetHandler(SmallEventFindMissionPacket)
Expand Down
4 changes: 2 additions & 2 deletions Discord/src/smallEvents/fightPet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import {
ReactionCollectorFightPetReaction
} from "../../../Lib/src/packets/interaction/ReactionCollectorFightPet";
import {DraftbotInteraction} from "../messages/DraftbotInteraction";
import {PetConstants} from "../../../Lib/src/constants/PetConstants";
import {DraftbotButtonReaction, DraftbotButtonReactionMessage} from "../messages/DraftbotButtonReactionMessage";
import {StringConstants} from "../../../Lib/src/constants/StringConstants";

function getFightPetReactions(interaction: DraftbotInteraction, baseReactions: ReactionCollectorFightPetReaction[]): DraftbotButtonReaction[] {
const reactions: DraftbotButtonReaction[] = [];
Expand All @@ -32,7 +32,7 @@ export async function fightPetCollector(packet: ReactionCollectorCreationPacket,

const formatBaseOptions = {
lng: interaction.userLanguage,
context: data.isFemale ? PetConstants.SEX.MALE_FULL : PetConstants.SEX.FEMALE_FULL
context: data.isFemale ? StringConstants.SEX.MALE.long : StringConstants.SEX.FEMALE.long
};

const reactions = getFightPetReactions(interaction, packet.reactions.map(reaction => reaction.data as ReactionCollectorFightPetReaction));
Expand Down
63 changes: 63 additions & 0 deletions Discord/src/smallEvents/shop.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import {ReactionCollectorCreationPacket} from "../../../Lib/src/packets/interaction/ReactionCollectorPacket";
import {PacketContext} from "../../../Lib/src/packets/DraftBotPacket";
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";

/**
* Send the initial embed for this small event
* @param packet
* @param context
*/
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 gender = RandomUtils.draftbotRandom.bool() ? StringConstants.SEX.MALE : StringConstants.SEX.FEMALE;
const name = StringUtils.getRandomTranslation("smallEvents:shop.names", interaction.userLanguage, {context: gender.short});

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

await DiscordCollectorUtils.createAcceptRefuseCollector(interaction, embed, packet, context);
}

export async function baseFunctionHandler(context: PacketContext, translationKey: string): Promise<void> {
const originalInteraction = DiscordCache.getInteraction(context.discord!.interaction!);
if (!originalInteraction) {
return;
}
const buttonInteraction = DiscordCache.getButtonInteraction(context.discord!.buttonInteraction!);
await buttonInteraction?.editReply({
embeds: [
new DraftbotSmallEventEmbed(
"shop",
StringUtils.getRandomTranslation(translationKey, originalInteraction.userLanguage),
buttonInteraction.user,
originalInteraction.userLanguage
)
]
});
}
4 changes: 2 additions & 2 deletions Discord/src/utils/DisplayUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {ItemWithDetails} from "../../../Lib/src/interfaces/ItemWithDetails";
import {minutesDisplay} from "../../../Lib/src/utils/TimeUtils";
import {Item} from "../../../Lib/src/interfaces/Item";
import {EmoteUtils} from "./EmoteUtils";
import {PetConstants} from "../../../Lib/src/constants/PetConstants";
import {StringConstants} from "../../../Lib/src/constants/StringConstants";

export class DisplayUtils {

Expand Down Expand Up @@ -81,7 +81,7 @@ export class DisplayUtils {
}

static getPetDisplay(petId: number, isFemale: boolean, lng: Language): string {
const context = isFemale ? PetConstants.SEX.FEMALE_FULL : PetConstants.SEX.MALE_FULL;
const context = isFemale ? StringConstants.SEX.FEMALE.long : StringConstants.SEX.MALE.long;
return i18n.t(`{emote:pets.{{petId}}.emote${context[0].toUpperCase() + context.slice(1)}} $t(models:pets.{{petId}})`, {
lng,
context,
Expand Down
15 changes: 5 additions & 10 deletions Discord/src/utils/PetUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {Language} from "../../../Lib/src/Language";
import {PetConstants} from "../../../Lib/src/constants/PetConstants";
import i18n from "../translations/i18n";
import {EmoteUtils} from "./EmoteUtils";
import {StringConstants} from "../../../Lib/src/constants/StringConstants";

export type PetData = {
petTypeId: number,
Expand All @@ -20,19 +21,13 @@ export class PetUtils {
* @param petData
*/
static petToString(lng: Language, petData: PetData): string {
const sexDisplay = `${
i18n.t(`commands:pet.sexDisplay.${
petData.sex === PetConstants.SEX.MALE ? PetConstants.SEX.MALE_FULL : PetConstants.SEX.FEMALE_FULL
}`, {lng})} ${
petData.sex === PetConstants.SEX.MALE ? PetConstants.ICONS.MALE : PetConstants.ICONS.FEMALE
}`;
return i18n.t("commands:pet.petField", {
lng,
emote: PetUtils.getPetIcon(petData.petTypeId, petData.sex),
typeName: PetUtils.getPetTypeName(lng, petData.petTypeId, petData.sex),
nickname: PetUtils.displayNickname(lng, petData.nickname),
rarity: PetUtils.getRarityDisplay(petData.rarity),
sex: sexDisplay,
sex: i18n.t(`commands:pet.sexDisplay.${petData.sex}`, {lng}),
loveLevel: PetUtils.getLoveLevelDisplay(petData.loveLevel, petData.sex, lng)
});
}
Expand All @@ -58,7 +53,7 @@ export class PetUtils {
* @param sex
*/
static getPetIcon(typeId: number, sex: string): string {
return EmoteUtils.translateEmojiToDiscord(sex === PetConstants.SEX.MALE ? DraftBotIcons.pets[typeId].emoteMale : DraftBotIcons.pets[typeId].emoteFemale);
return EmoteUtils.translateEmojiToDiscord(sex === StringConstants.SEX.MALE.short ? DraftBotIcons.pets[typeId].emoteMale : DraftBotIcons.pets[typeId].emoteFemale);
}

/**
Expand All @@ -85,7 +80,7 @@ export class PetUtils {
* @param sex
*/
static getPetTypeName(lng: Language, typeId: number, sex: string): string {
const sexStringContext: string = sex === PetConstants.SEX.MALE ? PetConstants.SEX.MALE_FULL : PetConstants.SEX.FEMALE_FULL;
const sexStringContext: string = sex === StringConstants.SEX.MALE.short ? StringConstants.SEX.MALE.long : StringConstants.SEX.FEMALE.long;
return i18n.t(
`models:pets:${typeId}`,
{lng, context: sexStringContext}
Expand All @@ -99,7 +94,7 @@ export class PetUtils {
* @param lng
*/
static getLoveLevelDisplay(loveLevel: number, sex: string, lng: Language): string {
const sexStringContext: string = sex === PetConstants.SEX.MALE ? PetConstants.SEX.MALE_FULL : PetConstants.SEX.FEMALE_FULL;
const sexStringContext: string = sex === StringConstants.SEX.MALE.short ? StringConstants.SEX.MALE.long : StringConstants.SEX.FEMALE.long;
return i18n.t(`commands:pet.loveLevels.${loveLevel}`, {
context: sexStringContext as unknown,
lng
Expand Down
Loading
Loading