Skip to content

Commit

Permalink
Implement changing adventurer name (#276)
Browse files Browse the repository at this point in the history
- add component in adventurer list card to change the name
- add support in the indexer for tracking changed name events
- add check for gas
- add setting of new adventurer name in list
  • Loading branch information
starknetdev authored Oct 8, 2024
1 parent b908030 commit 0370e3b
Show file tree
Hide file tree
Showing 10 changed files with 481 additions and 145 deletions.
81 changes: 48 additions & 33 deletions indexer/src/adventurers.ts
Original file line number Diff line number Diff line change
@@ -1,57 +1,60 @@
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 { getLevelFromXp } from "./utils/encode.ts";
import {
ADVENTURER_DIED,
ADVENTURER_UPGRADED,
AMBUSHED_BY_BEAST,
ATTACKED_BEAST,
ATTACKED_BY_BEAST,
DISCOVERED_BEAST,
DISCOVERED_GOLD,
DISCOVERED_HEALTH,
DISCOVERED_LOOT,
DODGED_OBSTACLE,
DROPPED_ITEMS,
EQUIPPED_ITEMS,
FLEE_FAILED,
FLEE_SUCCEEDED,
HIT_BY_OBSTACLE,
ITEMS_LEVELED_UP,
parseAdventurerDied,
parseAdventurerUpgraded,
parseAmbushedByBeast,
parseAttackedByBeast,
parseDiscoveredBeast,
parseDiscoveredGold,
parseDiscoveredHealth,
parseStartGame,
START_GAME,
PURCHASED_ITEMS,
ATTACKED_BY_BEAST,
ADVENTURER_DIED,
parseAdventurerDied,
parseAttackedByBeast,
AMBUSHED_BY_BEAST,
parseAmbushedByBeast,
ATTACKED_BEAST,
SLAYED_BEAST,
parseSlayedBeast,
FLEE_FAILED,
parseDiscoveredLoot,
parseDodgedObstacle,
parseDroppedItems,
parseEquippedItems,
parseFleeFailed,
FLEE_SUCCEEDED,
parseFleeSucceeded,
ITEMS_LEVELED_UP,
parseHitByObstacle,
parseItemsLeveledUp,
EQUIPPED_ITEMS,
parsePurchasedItems,
parseEquippedItems,
DROPPED_ITEMS,
parseDroppedItems,
HIT_BY_OBSTACLE,
parseHitByObstacle,
DODGED_OBSTACLE,
parseDodgedObstacle,
UPGRADES_AVAILABLE,
parseSlayedBeast,
parseStartGame,
parseTransfer,
parseUpdateAdventurerName,
parseUpgradesAvailable,
DISCOVERED_BEAST,
parseDiscoveredBeast,
DISCOVERED_LOOT,
parseDiscoveredLoot,
PURCHASED_ITEMS,
SLAYED_BEAST,
START_GAME,
TRANSFER,
parseTransfer,
UPDATE_ADVENTURER_NAME,
UPGRADES_AVAILABLE,
} from "./utils/events.ts";
import {
insertAdventurer,
updateAdventurer,
updateAdventurerName,
updateAdventurerOwner,
} from "./utils/helpers.ts";
import { MONGO_CONNECTION_STRING } from "./utils/constants.ts";
import { getLevelFromXp } from "./utils/encode.ts";

const GAME = Deno.env.get("GAME");
const START = +(Deno.env.get("START") || 0);
Expand Down Expand Up @@ -83,6 +86,7 @@ const filter = {
{ fromAddress: GAME, keys: [UPGRADES_AVAILABLE] },
{ fromAddress: GAME, keys: [ADVENTURER_UPGRADED] },
{ fromAddress: GAME, keys: [TRANSFER] },
{ fromAddress: GAME, keys: [UPDATE_ADVENTURER_NAME] },
],
};

Expand Down Expand Up @@ -351,6 +355,17 @@ export default function transform({ header, events }: Block) {
}),
];
}
return [];
}
case UPDATE_ADVENTURER_NAME: {
console.log("UPDATE_ADVENTURER_NAME", "->", "ADVENTURER UPDATES");
const { value } = parseUpdateAdventurerName(event.data, 0);
return [
updateAdventurerName({
adventurerId: value.adventurerId,
adventurerName: value.name,
}),
];
}
default: {
console.warn("Unknown event", event.keys[0]);
Expand Down
8 changes: 7 additions & 1 deletion indexer/src/utils/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import {
parseU128,
parseU16,
parseU256,
parseU64,
parseU32,
parseU64,
parseU8,
} from "./parser.ts";

Expand Down Expand Up @@ -53,6 +53,7 @@ export const NEW_COLLECTION_TOTAL = eventKey("NewCollectionTotal");
// Tokens
export const TRANSFER = eventKey("Transfer");
export const CLAIMED_FREE_GAME = eventKey("ClaimedFreeGame");
export const UPDATE_ADVENTURER_NAME = eventKey("UpdatedAdventurerName");

export const parseStats = combineParsers({
strength: { index: 0, parser: parseU8 },
Expand Down Expand Up @@ -384,3 +385,8 @@ export const parseNewCollectionTotal = combineParsers({
xp: { index: 1, parser: parseU32 },
gamesPlayed: { index: 2, parser: parseU32 },
});

export const parseUpdateAdventurerName = combineParsers({
adventurerId: { index: 0, parser: parseFelt252 },
name: { index: 1, parser: parseFelt252 },
});
18 changes: 17 additions & 1 deletion indexer/src/utils/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { checkExistsInt, computeHash, getLevelFromXp } from "./encode.ts";
import { parseAdventurerState } from "./events.ts";
import { computeHash, checkExistsInt, getLevelFromXp } from "./encode.ts";

export function insertAdventurer({
id,
Expand Down Expand Up @@ -235,6 +235,22 @@ export function updateAdventurerOwner({
};
}

export function updateAdventurerName({ adventurerId, adventurerName }: any) {
const entity = {
id: checkExistsInt(parseInt(adventurerId)),
};

return {
entity,
update: {
$set: {
...entity,
name: checkExistsInt(BigInt(adventurerName).toString(16)),
},
},
};
}

export function insertDiscovery({
txHash,
adventurerId,
Expand Down
10 changes: 5 additions & 5 deletions ui/src/app/components/navigation/TxActivity.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { useEffect, useState } from "react";
import { InvokeTransactionReceiptResponse } from "starknet";
import { useWaitForTransaction } from "@starknet-react/core";
import { displayAddress, padAddress } from "@/app/lib/utils";
import useLoadingStore from "@/app/hooks/useLoadingStore";
import LootIconLoader from "@/app/components/icons/Loader";
import useLoadingStore from "@/app/hooks/useLoadingStore";
import useTransactionCartStore from "@/app/hooks/useTransactionCartStore";
import useUIStore from "@/app/hooks/useUIStore";
import { networkConfig } from "@/app/lib/networkConfig";
import { displayAddress, padAddress } from "@/app/lib/utils";
import { useWaitForTransaction } from "@starknet-react/core";
import { useEffect, useState } from "react";
import { InvokeTransactionReceiptResponse } from "starknet";

export const TxActivity = () => {
const stopLoading = useLoadingStore((state) => state.stopLoading);
Expand Down
Loading

0 comments on commit 0370e3b

Please sign in to comment.