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

Fix indexer purchase data #277

Merged
merged 1 commit into from
Oct 7, 2024
Merged
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
30 changes: 18 additions & 12 deletions indexer/src/items.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@
import type { Config } from "https://esm.sh/@apibara/indexer";
import type { Block, Starknet } from "https://esm.sh/@apibara/indexer/starknet";
import type { Mongo } from "https://esm.sh/@apibara/indexer/sink/mongo";
import type { Console } from "https://esm.sh/@apibara/indexer/sink/console";
import type { Mongo } from "https://esm.sh/@apibara/indexer/sink/mongo";
import type { Block, Starknet } from "https://esm.sh/@apibara/indexer/starknet";
import { MONGO_CONNECTION_STRING } from "./utils/constants.ts";
import { GameData } from "./utils/data.ts";
import { checkExistsInt } from "./utils/encode.ts";
import {
ADVENTURER_UPGRADED,
DISCOVERED_LOOT,
DODGED_OBSTACLE,
DROPPED_ITEMS,
EQUIPMENT_CHANGED,
EQUIPPED_ITEMS,
HIT_BY_OBSTACLE,
ITEMS_LEVELED_UP,
UPGRADES_AVAILABLE,
parseAdventurerUpgraded,
parseDiscoveredLoot,
parseDodgedObstacle,
parseDroppedItems,
parseEquipmentChanged,
parseEquippedItems,
parseHitByObstacle,
parseUpgradesAvailable,
parseItemsLeveledUp,
parsePurchasedItems,
parseSlayedBeast,
parseStartGame,
parseTransfer,
parseUpgradesAvailable,
PURCHASED_ITEMS,
SLAYED_BEAST,
START_GAME,
DISCOVERED_LOOT,
parseDiscoveredLoot,
EQUIPMENT_CHANGED,
parseEquipmentChanged,
TRANSFER,
parseTransfer,
UPGRADES_AVAILABLE,
} from "./utils/events.ts";
import {
insertItem,
updateItemsOwner,
updateItemsXP,
} from "./utils/helpers.ts";
import { checkExistsInt } from "./utils/encode.ts";
import { GameData } from "./utils/data.ts";
import { MONGO_CONNECTION_STRING } from "./utils/constants.ts";

const gameData = new GameData();

Expand Down Expand Up @@ -134,6 +134,12 @@ export default function transform({ header, events }: Block) {
slot: gameData.ITEM_SLOTS[item.item.id],
type: gameData.ITEM_TYPES[item.item.id],
ownerAddress: checkExistsInt(BigInt(as.owner).toString(16)),
xp: 0,
greatness: 1,
special1: 0,
special2: 0,
special3: 0,
isAvailable: false,
Comment on lines +137 to +142
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Consider refactoring repeated item initialization into a helper function

The initialization of item properties like xp, greatness, special1, special2, special3, and isAvailable is repeated across multiple event handlers (PURCHASED_ITEMS, EQUIPMENT_CHANGED, DISCOVERED_LOOT). Refactoring this repeated code into a helper function will improve maintainability and reduce potential errors from inconsistent values.

Apply this diff to create a helper function for item initialization:

+function initializeItemProperties(itemId, adventurerId, ownerAddress, equipped, isAvailable) {
+  return {
+    item: checkExistsInt(itemId),
+    adventurerId: checkExistsInt(parseInt(adventurerId)),
+    owner: true,
+    equipped: equipped,
+    tier: gameData.ITEM_TIERS[itemId],
+    slot: gameData.ITEM_SLOTS[itemId],
+    type: gameData.ITEM_TYPES[itemId],
+    ownerAddress: checkExistsInt(BigInt(ownerAddress).toString(16)),
+    xp: 0,
+    greatness: 1,
+    special1: 0,
+    special2: 0,
+    special3: 0,
+    isAvailable: isAvailable,
+    purchasedTime: isAvailable ? null : new Date().toISOString(),
+    timestamp: new Date().toISOString(),
+  };
+}

Then replace the repeated initialization code in event handlers with calls to this function:

-// Existing initialization code in PURCHASED_ITEMS
-{
-  item: checkExistsInt(item.item.id),
-  adventurerId: checkExistsInt(parseInt(as.adventurerId)),
-  owner: true,
-  equipped: false,
-  tier: gameData.ITEM_TIERS[item.item.id],
-  slot: gameData.ITEM_SLOTS[item.item.id],
-  type: gameData.ITEM_TYPES[item.item.id],
-  ownerAddress: checkExistsInt(BigInt(as.owner).toString(16)),
-  xp: 0,
-  greatness: 1,
-  special1: 0,
-  special2: 0,
-  special3: 0,
-  isAvailable: false,
-  purchasedTime: new Date().toISOString(),
-  timestamp: new Date().toISOString(),
-},
+// Refactored code using the helper function
+initializeItemProperties(
+  item.item.id,
+  as.adventurerId,
+  as.owner,
+  false,
+  false
+),

Committable suggestion was skipped due to low confidence.

purchasedTime: new Date().toISOString(),
timestamp: new Date().toISOString(),
},
Expand Down
Loading