Skip to content

Commit

Permalink
Merge pull request #306 from hymccord/story-seeds
Browse files Browse the repository at this point in the history
Add Story Seeds snack pack to sb factory
  • Loading branch information
AardWolf authored Feb 28, 2023
2 parents 3dbc539 + c30dc6e commit 865de01
Show file tree
Hide file tree
Showing 6 changed files with 371 additions and 161 deletions.
161 changes: 0 additions & 161 deletions src/scripts/modules/ajax-handlers/sbFactory.js

This file was deleted.

99 changes: 99 additions & 0 deletions src/scripts/modules/ajax-handlers/sbFactory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import type { HgItem } from "@scripts/types/mhct";
import type { LoggerService } from "@scripts/util/logger";
import { AjaxSuccessHandler } from "./ajaxSuccessHandler";
import type { HgResponseWithVendingMachine, VendingMachinePurchaseType } from "./sbFactory.types";

export class SBFactoryAjaxHandler extends AjaxSuccessHandler {
/**
* Create a new instance of SuperBrieFactoryHandler
* @param logger logger to log events
* @param submitConvertibleCallback delegate to submit convertibles to mhct
*/
constructor(
private logger: LoggerService,
private submitConvertibleCallback: (convertible: HgItem, items: HgItem[]) => void) {
super();
this.logger = logger;
this.submitConvertibleCallback = submitConvertibleCallback;
}

/**
* Determine if given url applies to this handler
* @param url The url where the ajax reponse originated
* @returns True if this handler applies, otherwise false
*/
match(url: string): boolean {
return url.includes("mousehuntgame.com/managers/ajax/events/birthday_factory.php");
}

async execute(responseJSON: any): Promise<void> {
this.recordSnackPack(responseJSON as HgResponseWithVendingMachine);
}

/**
* Record Birthday snack pack submissions as convertibles in MHCT
* @param {import("@scripts/types/hg").HgResponse} responseJSON HitGrab ajax response from vending machine.
*/
recordSnackPack(responseJSON: HgResponseWithVendingMachine) {
const purchase = responseJSON.vending_machine_purchase;
if (purchase?.type == null) {
this.logger.debug('Skipped Bday snack pack submission due to unhandled XHR structure');
this.logger.warn('Unable to parse bday response', {responseJSON});
return;
}

// Convert pack code names to made-up internal identifiers
const packs: Record<VendingMachinePurchaseType, number> = {
larry_starter_mix_snack_pack: 130001,
tribal_crunch_snack_pack: 130002,
wild_west_ranch_rings_snack_pack: 130003,
sandy_bert_bites_snack_pack: 130004,
hollow_heights_party_pack_snack_pack: 130005,
riftios_snack_pack: 130006,
story_seeds_snack_pack: 130007,
};

if (!(purchase.type in packs)) {
this.logger.warn('Unsupported snack pack type', { vending_machine_purchase: purchase });
return;
}

const convertible: HgItem = {
id: packs[purchase.type],
name: purchase.type,
quantity: purchase.quantity
};

if (responseJSON.inventory == null || Array.isArray(responseJSON.inventory)) {
// inventory can be emtpy array [], which is unsupported
this.logger.warn('Vending machine inventory response was undefined or an array', {responseJSON});
return;
}

const inventory = responseJSON.inventory;
const items: HgItem[] = [];

try {
purchase.items.forEach(item => {
var inventoryItem = Object.values(inventory).find(i => i.name == item.name);
if (inventoryItem == null) {
this.logger.debug('Snack pack item missing from inventory', {inventory, item});
throw new Error(`Snack pack item ${item.name} wasn\'t found in inventory response.`);
}

items.push({
id: inventoryItem.item_id,
name: item.name,
quantity: item.quantity,
});
});

} catch (error) {
this.logger.warn((error as Error) .toString());
return;
}

this.logger.debug('SBFactoryAjaxHandler submitting snack pack', {convertible, items});
this.submitConvertibleCallback(convertible, items);
}
}
26 changes: 26 additions & 0 deletions src/scripts/modules/ajax-handlers/sbFactory.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import type { HgResponse } from "@scripts/types/hg";

export interface HgResponseWithVendingMachine extends HgResponse {
vending_machine_purchase?: VendingMachinePurchase
}

export interface VendingMachinePurchase {
quantity: number;
type: VendingMachinePurchaseType;
items: VendingMachineItem[];
}

export type VendingMachinePurchaseType = "larry_starter_mix_snack_pack" |
"tribal_crunch_snack_pack" |
"wild_west_ranch_rings_snack_pack" |
"sandy_bert_bites_snack_pack" |
"hollow_heights_party_pack_snack_pack" |
"riftios_snack_pack" |
"story_seeds_snack_pack";

export interface VendingMachineItem {
name: string;
quantity: number;
is_epic: boolean | null;
// thumb: string;
}
21 changes: 21 additions & 0 deletions src/scripts/types/hg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export interface HgResponse {
page?: unknown;
success: 0 | 1;
active_turn?: boolean;
inventory?: Record<string, InventoryItem> | []
}

export interface User {
Expand Down Expand Up @@ -70,3 +71,23 @@ export interface EnvironmentAttributes {
export interface ViewingAttributes {

}

/**
*
*/
export interface InventoryItem {
/** HitGrab's internal number id of item */
item_id: number;
/** Friendly display name of item */
name: string;
/** Unique snake_case identifying name of item */
type: string;
/** Item category: bait, crafting, stat, etc... */
// classification: Classification;
/** Total amount of item in user inventory */
quantity: number;
}

// export type Classification = "weapon" | "base" | "bait" | "trinket" |
// "skin" | "crafting_item" | "stat" | "potion" | "quest" |
// "convertible" | "collectible" | "message_item" | "torn_page"
12 changes: 12 additions & 0 deletions src/scripts/types/mhct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,15 @@ interface Loot {
lucky: boolean;
plural_name: string;
}

/**
* An object opened (convertible) or recieved (convertible contents)
*/
export interface HgItem {
/** HitGrab's ID for the id */
id: number
/** HitGrab's display name for the item */
name: string;
/** The number of items opened or recieved */
quantity: number;
}
Loading

0 comments on commit 865de01

Please sign in to comment.