From 25adccb622e39d65274e827f2ee4b9f8ecf4c05e Mon Sep 17 00:00:00 2001 From: Starknet Dev Date: Sat, 22 Jun 2024 07:06:06 +0100 Subject: [PATCH 1/3] - add item discoveries event parsing - remove xp event logging - fix gold discovery type --- indexer/graphql/src/indexer/config.py | 2 +- indexer/src/adventurers.ts | 10 +- indexer/src/discoveries.ts | 16 +-- indexer/src/items.ts | 57 +++++++++++ indexer/src/utils/events.ts | 14 ++- scripts/katana/graphql/indexer/config.py | 2 +- scripts/katana/indexer/adventurers.ts | 10 +- scripts/katana/indexer/discoveries.ts | 16 +-- scripts/katana/indexer/items.ts | 57 +++++++++++ scripts/katana/indexer/utils/events.ts | 14 ++- ui/src/app/abi/Game.json | 118 +++++++++++++++++++++-- ui/src/app/lib/data/GameData.tsx | 2 +- ui/src/app/lib/utils/parseEvents.ts | 60 ++++++++++++ ui/src/app/lib/utils/processData.ts | 89 +++++++++++++++-- ui/src/app/lib/utils/syscalls.ts | 34 +++++-- ui/src/app/types/events.ts | 12 +++ 16 files changed, 453 insertions(+), 60 deletions(-) diff --git a/indexer/graphql/src/indexer/config.py b/indexer/graphql/src/indexer/config.py index c06f8ed63..96cc3070d 100644 --- a/indexer/graphql/src/indexer/config.py +++ b/indexer/graphql/src/indexer/config.py @@ -395,7 +395,7 @@ def __init__(self): 3: "Item", } - self.SUB_DISCOVERY_TYPES = {1: "Health", 2: "Gold", 3: "XP"} + self.SUB_DISCOVERY_TYPES = {1: "Health", 2: "Gold", 3: "Loot"} self.MATERIALS = { 0: "Generic", diff --git a/indexer/src/adventurers.ts b/indexer/src/adventurers.ts index 87c103073..56e090fbc 100644 --- a/indexer/src/adventurers.ts +++ b/indexer/src/adventurers.ts @@ -6,11 +6,9 @@ import { ADVENTURER_UPGRADED, DISCOVERED_GOLD, DISCOVERED_HEALTH, - DISCOVERED_XP, parseAdventurerUpgraded, parseDiscoveredGold, parseDiscoveredHealth, - parseDiscoveredXp, parseStartGame, START_GAME, PURCHASED_POTIONS, @@ -43,6 +41,8 @@ import { parseUpgradesAvailable, DISCOVERED_BEAST, parseDiscoveredBeast, + DISCOVERED_LOOT, + parseDiscoveredLoot, } from "./utils/events.ts"; import { insertAdventurer, updateAdventurer } from "./utils/helpers.ts"; import { MONGO_CONNECTION_STRING } from "./utils/constants.ts"; @@ -167,9 +167,9 @@ export default function transform({ header, events }: Block) { }), ]; } - case DISCOVERED_XP: { - console.log("DISCOVERED_XP", "->", "ADVENTURER UPDATES"); - const { value } = parseDiscoveredXp(event.data, 0); + case DISCOVERED_LOOT: { + console.log("DISCOVERED_LOOT", "->", "ADVENTURER UPDATES"); + const { value } = parseDiscoveredLoot(event.data, 0); return [ updateAdventurer({ timestamp: new Date().toISOString(), diff --git a/indexer/src/discoveries.ts b/indexer/src/discoveries.ts index 3b484a11f..abac0f1f2 100644 --- a/indexer/src/discoveries.ts +++ b/indexer/src/discoveries.ts @@ -7,6 +7,7 @@ import { DISCOVERED_BEAST, DISCOVERED_GOLD, DISCOVERED_HEALTH, + DISCOVERED_LOOT, DISCOVERED_XP, DODGED_OBSTACLE, HIT_BY_OBSTACLE, @@ -14,6 +15,7 @@ import { parseDiscoveredBeast, parseDiscoveredGold, parseDiscoveredHealth, + parseDiscoveredLoot, parseDiscoveredXp, parseHitByObstacle, } from "./utils/events.ts"; @@ -92,14 +94,14 @@ export default function transform({ header, events }: Block) { case DISCOVERED_GOLD: { const { value } = parseDiscoveredGold(event.data, 0); const as = value.adventurerState; - console.log("DISCOVERED_HEALTH", "->", "DISCOVERIES UPDATES"); + console.log("DISCOVERED_GOLD", "->", "DISCOVERIES UPDATES"); return [ insertDiscovery({ txHash: receipt.transactionHash, adventurerId: as.adventurerId, adventurerHealth: as.adventurer.health, discoveryType: 3, - subDiscoveryType: 1, + subDiscoveryType: 2, outputAmount: value.goldAmount, obstacle: 0, obstacleLevel: 0, @@ -121,18 +123,18 @@ export default function transform({ header, events }: Block) { }), ]; } - case DISCOVERED_XP: { - const { value } = parseDiscoveredXp(event.data, 0); + case DISCOVERED_LOOT: { + const { value } = parseDiscoveredLoot(event.data, 0); const as = value.adventurerState; - console.log("DISCOVERED_XP", "->", "DISCOVERIES UPDATES"); + console.log("DISCOVERED_LOOT", "->", "DISCOVERIES UPDATES"); return [ insertDiscovery({ txHash: receipt.transactionHash, adventurerId: as.adventurerId, adventurerHealth: as.adventurer.health, discoveryType: 3, - subDiscoveryType: 1, - outputAmount: value.xpAmount, + subDiscoveryType: 3, + outputAmount: value.itemId, obstacle: 0, obstacleLevel: 0, dodgedObstacle: false, diff --git a/indexer/src/items.ts b/indexer/src/items.ts index 9b58e5410..a5008021e 100644 --- a/indexer/src/items.ts +++ b/indexer/src/items.ts @@ -23,6 +23,10 @@ import { PURCHASED_ITEMS, SLAYED_BEAST, START_GAME, + DISCOVERED_LOOT, + parseDiscoveredLoot, + EQUIPMENT_CHANGED, + parseEquipmentChanged, } from "./utils/events.ts"; import { insertItem, updateItemsXP } from "./utils/helpers.ts"; import { checkExistsInt, encodeIntAsBytes } from "./utils/encode.ts"; @@ -119,6 +123,59 @@ export default function transform({ header, events }: Block) { })); return result; } + case DISCOVERED_LOOT: { + const { value } = parseDiscoveredLoot(event.data, 0); + const as = value.adventurerState; + console.log("DISCOVERED_LOOT", "->", "ITEMS UPDATES"); + return { + entity: { + item: checkExistsInt(BigInt(value.itemId)), + adventurerId: checkExistsInt(BigInt(as.adventurerId)), + }, + update: { + $set: { + item: checkExistsInt(BigInt(value.itemId)), + adventurerId: checkExistsInt(BigInt(as.adventurerId)), + equipped: false, + timestamp: new Date().toISOString(), + }, + }, + }; + } + case EQUIPMENT_CHANGED: { + const { value } = parseEquipmentChanged(event.data, 0); + const as = value.adventurerStateWithBag.adventurerState; + console.log("EQUIPMENT_CHANGED", "->", "ITEMS UPDATES"); + const equippedResult = value.equippedItems.map((item) => ({ + entity: { + item: checkExistsInt(BigInt(item)), + adventurerId: checkExistsInt(BigInt(as.adventurerId)), + }, + update: { + $set: { + item: checkExistsInt(BigInt(item)), + adventurerId: checkExistsInt(BigInt(as.adventurerId)), + equipped: true, + timestamp: new Date().toISOString(), + }, + }, + })); + const baggedResult = value.baggedItems.map((item) => ({ + entity: { + item: checkExistsInt(BigInt(item)), + adventurerId: checkExistsInt(BigInt(as.adventurerId)), + }, + update: { + $set: { + item: checkExistsInt(BigInt(item)), + adventurerId: checkExistsInt(BigInt(as.adventurerId)), + equipped: false, + timestamp: new Date().toISOString(), + }, + }, + })); + return [...equippedResult, ...baggedResult]; + } case EQUIPPED_ITEMS: { const { value } = parseEquippedItems(event.data, 0); const as = value.adventurerStateWithBag.adventurerState; diff --git a/indexer/src/utils/events.ts b/indexer/src/utils/events.ts index 6e61b6a25..0cefd9372 100644 --- a/indexer/src/utils/events.ts +++ b/indexer/src/utils/events.ts @@ -22,7 +22,7 @@ export const START_GAME = eventKey("StartGame"); export const ADVENTURER_UPGRADED = eventKey("AdventurerUpgraded"); export const DISCOVERED_HEALTH = eventKey("DiscoveredHealth"); export const DISCOVERED_GOLD = eventKey("DiscoveredGold"); -export const DISCOVERED_XP = eventKey("DiscoveredXP"); +export const DISCOVERED_LOOT = eventKey("DiscoveredLoot"); export const HIT_BY_OBSTACLE = eventKey("HitByObstacle"); export const DODGED_OBSTACLE = eventKey("DodgedObstacle"); @@ -43,6 +43,7 @@ export const UPGRADES_AVAILABLE = eventKey("UpgradesAvailable"); export const EQUIPPED_ITEMS = eventKey("EquippedItems"); export const DROPPED_ITEMS = eventKey("DroppedItems"); export const ITEMS_LEVELED_UP = eventKey("ItemsLeveledUp"); +export const EQUIPMENT_CHANGED = eventKey("EquipmentChanged"); export const NEW_HIGH_SCORE = eventKey("NewHighScore"); export const REWARD_DISTRIBUTION = eventKey("RewardDistribution"); @@ -230,9 +231,9 @@ export const parseDiscoveredGold = combineParsers({ goldAmount: { index: 1, parser: parseU16 }, }); -export const parseDiscoveredXp = combineParsers({ +export const parseDiscoveredLoot = combineParsers({ adventurerState: { index: 0, parser: parseAdventurerState }, - xpAmount: { index: 1, parser: parseU16 }, + itemId: { index: 1, parser: parseU8 }, }); export const parseHitByObstacle = combineParsers({ @@ -295,6 +296,13 @@ export const parseDroppedItems = combineParsers({ itemIds: { index: 1, parser: parseArray(parseU8) }, }); +export const parseEquipmentChanged = combineParsers({ + adventurerStateWithBag: { index: 0, parser: parseAdventurerStateWithBag }, + equippedItems: { index: 1, parser: parseArray(parseU8) }, + baggedItems: { index: 2, parser: parseArray(parseU8) }, + droppedItems: { index: 3, parser: parseArray(parseU8) }, +}); + export const parseUpgradesAvailable = combineParsers({ adventurerState: { index: 0, parser: parseAdventurerState }, items: { index: 1, parser: parseArray(parseU8) }, diff --git a/scripts/katana/graphql/indexer/config.py b/scripts/katana/graphql/indexer/config.py index c06f8ed63..96cc3070d 100644 --- a/scripts/katana/graphql/indexer/config.py +++ b/scripts/katana/graphql/indexer/config.py @@ -395,7 +395,7 @@ def __init__(self): 3: "Item", } - self.SUB_DISCOVERY_TYPES = {1: "Health", 2: "Gold", 3: "XP"} + self.SUB_DISCOVERY_TYPES = {1: "Health", 2: "Gold", 3: "Loot"} self.MATERIALS = { 0: "Generic", diff --git a/scripts/katana/indexer/adventurers.ts b/scripts/katana/indexer/adventurers.ts index 87c103073..56e090fbc 100644 --- a/scripts/katana/indexer/adventurers.ts +++ b/scripts/katana/indexer/adventurers.ts @@ -6,11 +6,9 @@ import { ADVENTURER_UPGRADED, DISCOVERED_GOLD, DISCOVERED_HEALTH, - DISCOVERED_XP, parseAdventurerUpgraded, parseDiscoveredGold, parseDiscoveredHealth, - parseDiscoveredXp, parseStartGame, START_GAME, PURCHASED_POTIONS, @@ -43,6 +41,8 @@ import { parseUpgradesAvailable, DISCOVERED_BEAST, parseDiscoveredBeast, + DISCOVERED_LOOT, + parseDiscoveredLoot, } from "./utils/events.ts"; import { insertAdventurer, updateAdventurer } from "./utils/helpers.ts"; import { MONGO_CONNECTION_STRING } from "./utils/constants.ts"; @@ -167,9 +167,9 @@ export default function transform({ header, events }: Block) { }), ]; } - case DISCOVERED_XP: { - console.log("DISCOVERED_XP", "->", "ADVENTURER UPDATES"); - const { value } = parseDiscoveredXp(event.data, 0); + case DISCOVERED_LOOT: { + console.log("DISCOVERED_LOOT", "->", "ADVENTURER UPDATES"); + const { value } = parseDiscoveredLoot(event.data, 0); return [ updateAdventurer({ timestamp: new Date().toISOString(), diff --git a/scripts/katana/indexer/discoveries.ts b/scripts/katana/indexer/discoveries.ts index 3b484a11f..abac0f1f2 100644 --- a/scripts/katana/indexer/discoveries.ts +++ b/scripts/katana/indexer/discoveries.ts @@ -7,6 +7,7 @@ import { DISCOVERED_BEAST, DISCOVERED_GOLD, DISCOVERED_HEALTH, + DISCOVERED_LOOT, DISCOVERED_XP, DODGED_OBSTACLE, HIT_BY_OBSTACLE, @@ -14,6 +15,7 @@ import { parseDiscoveredBeast, parseDiscoveredGold, parseDiscoveredHealth, + parseDiscoveredLoot, parseDiscoveredXp, parseHitByObstacle, } from "./utils/events.ts"; @@ -92,14 +94,14 @@ export default function transform({ header, events }: Block) { case DISCOVERED_GOLD: { const { value } = parseDiscoveredGold(event.data, 0); const as = value.adventurerState; - console.log("DISCOVERED_HEALTH", "->", "DISCOVERIES UPDATES"); + console.log("DISCOVERED_GOLD", "->", "DISCOVERIES UPDATES"); return [ insertDiscovery({ txHash: receipt.transactionHash, adventurerId: as.adventurerId, adventurerHealth: as.adventurer.health, discoveryType: 3, - subDiscoveryType: 1, + subDiscoveryType: 2, outputAmount: value.goldAmount, obstacle: 0, obstacleLevel: 0, @@ -121,18 +123,18 @@ export default function transform({ header, events }: Block) { }), ]; } - case DISCOVERED_XP: { - const { value } = parseDiscoveredXp(event.data, 0); + case DISCOVERED_LOOT: { + const { value } = parseDiscoveredLoot(event.data, 0); const as = value.adventurerState; - console.log("DISCOVERED_XP", "->", "DISCOVERIES UPDATES"); + console.log("DISCOVERED_LOOT", "->", "DISCOVERIES UPDATES"); return [ insertDiscovery({ txHash: receipt.transactionHash, adventurerId: as.adventurerId, adventurerHealth: as.adventurer.health, discoveryType: 3, - subDiscoveryType: 1, - outputAmount: value.xpAmount, + subDiscoveryType: 3, + outputAmount: value.itemId, obstacle: 0, obstacleLevel: 0, dodgedObstacle: false, diff --git a/scripts/katana/indexer/items.ts b/scripts/katana/indexer/items.ts index 9b58e5410..a5008021e 100644 --- a/scripts/katana/indexer/items.ts +++ b/scripts/katana/indexer/items.ts @@ -23,6 +23,10 @@ import { PURCHASED_ITEMS, SLAYED_BEAST, START_GAME, + DISCOVERED_LOOT, + parseDiscoveredLoot, + EQUIPMENT_CHANGED, + parseEquipmentChanged, } from "./utils/events.ts"; import { insertItem, updateItemsXP } from "./utils/helpers.ts"; import { checkExistsInt, encodeIntAsBytes } from "./utils/encode.ts"; @@ -119,6 +123,59 @@ export default function transform({ header, events }: Block) { })); return result; } + case DISCOVERED_LOOT: { + const { value } = parseDiscoveredLoot(event.data, 0); + const as = value.adventurerState; + console.log("DISCOVERED_LOOT", "->", "ITEMS UPDATES"); + return { + entity: { + item: checkExistsInt(BigInt(value.itemId)), + adventurerId: checkExistsInt(BigInt(as.adventurerId)), + }, + update: { + $set: { + item: checkExistsInt(BigInt(value.itemId)), + adventurerId: checkExistsInt(BigInt(as.adventurerId)), + equipped: false, + timestamp: new Date().toISOString(), + }, + }, + }; + } + case EQUIPMENT_CHANGED: { + const { value } = parseEquipmentChanged(event.data, 0); + const as = value.adventurerStateWithBag.adventurerState; + console.log("EQUIPMENT_CHANGED", "->", "ITEMS UPDATES"); + const equippedResult = value.equippedItems.map((item) => ({ + entity: { + item: checkExistsInt(BigInt(item)), + adventurerId: checkExistsInt(BigInt(as.adventurerId)), + }, + update: { + $set: { + item: checkExistsInt(BigInt(item)), + adventurerId: checkExistsInt(BigInt(as.adventurerId)), + equipped: true, + timestamp: new Date().toISOString(), + }, + }, + })); + const baggedResult = value.baggedItems.map((item) => ({ + entity: { + item: checkExistsInt(BigInt(item)), + adventurerId: checkExistsInt(BigInt(as.adventurerId)), + }, + update: { + $set: { + item: checkExistsInt(BigInt(item)), + adventurerId: checkExistsInt(BigInt(as.adventurerId)), + equipped: false, + timestamp: new Date().toISOString(), + }, + }, + })); + return [...equippedResult, ...baggedResult]; + } case EQUIPPED_ITEMS: { const { value } = parseEquippedItems(event.data, 0); const as = value.adventurerStateWithBag.adventurerState; diff --git a/scripts/katana/indexer/utils/events.ts b/scripts/katana/indexer/utils/events.ts index 6e61b6a25..0cefd9372 100644 --- a/scripts/katana/indexer/utils/events.ts +++ b/scripts/katana/indexer/utils/events.ts @@ -22,7 +22,7 @@ export const START_GAME = eventKey("StartGame"); export const ADVENTURER_UPGRADED = eventKey("AdventurerUpgraded"); export const DISCOVERED_HEALTH = eventKey("DiscoveredHealth"); export const DISCOVERED_GOLD = eventKey("DiscoveredGold"); -export const DISCOVERED_XP = eventKey("DiscoveredXP"); +export const DISCOVERED_LOOT = eventKey("DiscoveredLoot"); export const HIT_BY_OBSTACLE = eventKey("HitByObstacle"); export const DODGED_OBSTACLE = eventKey("DodgedObstacle"); @@ -43,6 +43,7 @@ export const UPGRADES_AVAILABLE = eventKey("UpgradesAvailable"); export const EQUIPPED_ITEMS = eventKey("EquippedItems"); export const DROPPED_ITEMS = eventKey("DroppedItems"); export const ITEMS_LEVELED_UP = eventKey("ItemsLeveledUp"); +export const EQUIPMENT_CHANGED = eventKey("EquipmentChanged"); export const NEW_HIGH_SCORE = eventKey("NewHighScore"); export const REWARD_DISTRIBUTION = eventKey("RewardDistribution"); @@ -230,9 +231,9 @@ export const parseDiscoveredGold = combineParsers({ goldAmount: { index: 1, parser: parseU16 }, }); -export const parseDiscoveredXp = combineParsers({ +export const parseDiscoveredLoot = combineParsers({ adventurerState: { index: 0, parser: parseAdventurerState }, - xpAmount: { index: 1, parser: parseU16 }, + itemId: { index: 1, parser: parseU8 }, }); export const parseHitByObstacle = combineParsers({ @@ -295,6 +296,13 @@ export const parseDroppedItems = combineParsers({ itemIds: { index: 1, parser: parseArray(parseU8) }, }); +export const parseEquipmentChanged = combineParsers({ + adventurerStateWithBag: { index: 0, parser: parseAdventurerStateWithBag }, + equippedItems: { index: 1, parser: parseArray(parseU8) }, + baggedItems: { index: 2, parser: parseArray(parseU8) }, + droppedItems: { index: 3, parser: parseArray(parseU8) }, +}); + export const parseUpgradesAvailable = combineParsers({ adventurerState: { index: 0, parser: parseAdventurerState }, items: { index: 1, parser: parseArray(parseU8) }, diff --git a/ui/src/app/abi/Game.json b/ui/src/app/abi/Game.json index 24bee1bee..b3f4a67e0 100644 --- a/ui/src/app/abi/Game.json +++ b/ui/src/app/abi/Game.json @@ -198,7 +198,7 @@ }, { "name": "name", - "type": "core::integer::u128" + "type": "core::felt252" } ] }, @@ -479,7 +479,7 @@ }, { "name": "name", - "type": "core::integer::u128" + "type": "core::felt252" }, { "name": "golden_token_id", @@ -784,6 +784,54 @@ ], "state_mutability": "view" }, + { + "name": "get_starting_stats", + "type": "function", + "inputs": [ + { + "name": "adventurer_id", + "type": "core::felt252" + } + ], + "outputs": [ + { + "type": "adventurer::stats::Stats" + } + ], + "state_mutability": "view" + }, + { + "name": "equipment_specials_unlocked", + "type": "function", + "inputs": [ + { + "name": "adventurer_id", + "type": "core::felt252" + } + ], + "outputs": [ + { + "type": "core::bool" + } + ], + "state_mutability": "view" + }, + { + "name": "equipment_stat_boosts", + "type": "function", + "inputs": [ + { + "name": "adventurer_id", + "type": "core::felt252" + } + ], + "outputs": [ + { + "type": "adventurer::stats::Stats" + } + ], + "state_mutability": "view" + }, { "name": "get_equipped_items", "type": "function", @@ -1278,14 +1326,17 @@ ] }, { - "name": "game::Game::Discovery", - "type": "struct", + "kind": "struct", + "name": "game::Game::DiscoveredHealth", + "type": "event", "members": [ { + "kind": "data", "name": "adventurer_state", "type": "game::Game::AdventurerState" }, { + "kind": "data", "name": "amount", "type": "core::integer::u16" } @@ -1293,25 +1344,35 @@ }, { "kind": "struct", - "name": "game::Game::DiscoveredHealth", + "name": "game::Game::DiscoveredGold", "type": "event", "members": [ { "kind": "data", - "name": "discovery", - "type": "game::Game::Discovery" + "name": "adventurer_state", + "type": "game::Game::AdventurerState" + }, + { + "kind": "data", + "name": "amount", + "type": "core::integer::u16" } ] }, { "kind": "struct", - "name": "game::Game::DiscoveredGold", + "name": "game::Game::DiscoveredLoot", "type": "event", "members": [ { "kind": "data", - "name": "discovery", - "type": "game::Game::Discovery" + "name": "adventurer_state", + "type": "game::Game::AdventurerState" + }, + { + "kind": "data", + "name": "item_id", + "type": "core::integer::u8" } ] }, @@ -1793,6 +1854,33 @@ } ] }, + { + "kind": "struct", + "name": "game::Game::EquipmentChanged", + "type": "event", + "members": [ + { + "kind": "data", + "name": "adventurer_state_with_bag", + "type": "game::Game::AdventurerStateWithBag" + }, + { + "kind": "data", + "name": "equipped_items", + "type": "core::array::Array::" + }, + { + "kind": "data", + "name": "bagged_items", + "type": "core::array::Array::" + }, + { + "kind": "data", + "name": "dropped_items", + "type": "core::array::Array::" + } + ] + }, { "kind": "struct", "name": "game::Game::EquippedItems", @@ -2095,6 +2183,11 @@ "name": "DiscoveredGold", "type": "game::Game::DiscoveredGold" }, + { + "kind": "nested", + "name": "DiscoveredLoot", + "type": "game::Game::DiscoveredLoot" + }, { "kind": "nested", "name": "DodgedObstacle", @@ -2160,6 +2253,11 @@ "name": "AdventurerUpgraded", "type": "game::Game::AdventurerUpgraded" }, + { + "kind": "nested", + "name": "EquipmentChanged", + "type": "game::Game::EquipmentChanged" + }, { "kind": "nested", "name": "EquippedItems", diff --git a/ui/src/app/lib/data/GameData.tsx b/ui/src/app/lib/data/GameData.tsx index 16fdab114..06ef3f808 100644 --- a/ui/src/app/lib/data/GameData.tsx +++ b/ui/src/app/lib/data/GameData.tsx @@ -855,7 +855,7 @@ export class GameData { this.ITEM_DISCOVERY_TYPES = { 1: "Health", 2: "Gold", - 3: "XP", + 3: "Loot", }; this.ITEM_NAME_PREFIXES = { diff --git a/ui/src/app/lib/utils/parseEvents.ts b/ui/src/app/lib/utils/parseEvents.ts index b8805ca15..938636751 100644 --- a/ui/src/app/lib/utils/parseEvents.ts +++ b/ui/src/app/lib/utils/parseEvents.ts @@ -6,6 +6,8 @@ import { DiscoveredGoldEvent, StartGameEvent, DiscoveredXPEvent, + DiscoveredLootEvent, + EquipmentChangedEvent, DodgedObstacleEvent, HitByObstacleEvent, DiscoveredBeastEvent, @@ -207,6 +209,25 @@ function parseEquippedItems(data: string[]) { return { equippedItems, unequippedItems }; } +function parseEquipmentChanged(data: string[]) { + const equippedLength = parseInt(data[0]); + const equippedItems = []; + const baggedItems = []; + const droppedItems = []; + for (let i = 1; i <= equippedLength; i++) { + equippedItems.push(parseInt(data[i])); + } + const baggedLength = parseInt(data[equippedLength + 1]); + for (let i = 2; i <= baggedLength + 1; i++) { + baggedItems.push(parseInt(data[i + equippedLength])); + } + const droppedLength = parseInt(data[baggedLength + 1]); + for (let i = 2; i <= droppedLength + 1; i++) { + droppedItems.push(parseInt(data[i + baggedLength])); + } + return { equippedItems, baggedItems, droppedItems }; +} + export async function parseEvents( receipt: InvokeTransactionReceiptResponse, currentAdventurer?: AdventurerClass, @@ -332,6 +353,45 @@ export async function parseEvents( ); events.push({ name: eventName, data: discoveredXPEvent }); break; + case "DiscoveredLoot": + const discoveredLootData: DiscoveredLootEvent = { + adventurerState: parseAdventurerState(raw.data.slice(0, 31)), + itemId: parseInt(raw.data[32]), + }; + const discoveredLootEvent = processData( + discoveredLootData, + eventName, + receipt.transaction_hash, + currentAdventurer + ); + events.push({ name: eventName, data: discoveredLootEvent }); + break; + case "EquipmentChanged": + const { + equippedItems: equipmentChangedEquippedItems, + baggedItems: equipmentChangedBaggedItems, + droppedItems: equipmentChangedDroppedItems, + } = parseEquipmentChanged( + // Include equipped array length + raw.data.slice(63) + ); + const equipmentChangedData: EquipmentChangedEvent = { + adventurerStateWithBag: { + adventurerState: parseAdventurerState(raw.data.slice(0, 31)), + bag: parseBag(raw.data.slice(32, 63)), + }, + equippedItems: equipmentChangedEquippedItems, + baggedItems: equipmentChangedBaggedItems, + droppedItems: equipmentChangedDroppedItems, + }; + const equipmentChangedEvent = processData( + equipmentChangedData, + eventName, + receipt.transaction_hash, + currentAdventurer + ); + events.push({ name: eventName, data: equipmentChangedEvent }); + break; case "DodgedObstacle": const dodgedObstacleData: DodgedObstacleEvent = { adventurerState: parseAdventurerState(raw.data.slice(0, 31)), diff --git a/ui/src/app/lib/utils/processData.ts b/ui/src/app/lib/utils/processData.ts index f1ae37802..5d56a0723 100644 --- a/ui/src/app/lib/utils/processData.ts +++ b/ui/src/app/lib/utils/processData.ts @@ -24,6 +24,8 @@ import { AdventurerLeveledUpEvent, UpgradesAvailableEvent, AdventurerUpgradedEvent, + DiscoveredLootEvent, + EquipmentChangedEvent, } from "@/app/types/events"; import { Adventurer, @@ -42,6 +44,8 @@ type EventData = | DiscoveredGoldEvent | StartGameEvent | DiscoveredXPEvent + | DiscoveredLootEvent + | EquipmentChangedEvent | DodgedObstacleEvent | HitByObstacleEvent | DiscoveredBeastEvent @@ -174,6 +178,32 @@ export function processItemLevels(data: any) { return itemLevels; } +export function processLootDiscovery( + data: any, + equipped: boolean, + adventurerState: any +) { + const gameData = new GameData(); + const lootDiscoveries: Item[] = []; + for (let item of data) { + lootDiscoveries.push({ + item: gameData.ITEMS[item], + adventurerId: adventurerState["adventurerId"], + owner: true, + equipped: equipped, + ownerAddress: adventurerState["owner"], + xp: 0, + special1: undefined, + special2: undefined, + special3: undefined, + isAvailable: false, + purchasedTime: new Date(), + timestamp: new Date(), + }); + } + return lootDiscoveries; +} + export function processData( event: EventData, eventName: string, @@ -331,20 +361,20 @@ export function processData( }; return [discoveredGoldAdventurerData, discoverGoldData]; - case "DiscoveredXP": - const discoveredXPEvent = event as DiscoveredXPEvent; - const discoveredXPAdventurerData = processAdventurerState( - discoveredXPEvent, + case "DiscoveredLoot": + const discoveredLootEvent = event as DiscoveredLootEvent; + const discoveredLootAdventurerData = processAdventurerState( + discoveredLootEvent, currentAdventurer ); - const discoverXPData: Discovery = { + const discoverLootData: Discovery = { txHash: txHash, - adventurerId: discoveredXPEvent.adventurerState["adventurerId"], + adventurerId: discoveredLootEvent.adventurerState["adventurerId"], adventurerHealth: - discoveredXPEvent.adventurerState["adventurer"]["health"], + discoveredLootEvent.adventurerState["adventurer"]["health"], discoveryType: gameData.DISCOVERY_TYPES[3], subDiscoveryType: gameData.ITEM_DISCOVERY_TYPES[3], - outputAmount: discoveredXPEvent.xpAmount, + outputAmount: discoveredLootEvent.itemId, obstacle: undefined, obstacleLevel: undefined, dodgedObstacle: false, @@ -364,7 +394,48 @@ export function processData( timestamp: new Date(), }; - return [discoveredXPAdventurerData, discoverXPData]; + return [discoveredLootAdventurerData, discoverLootData]; + case "EquipmentChanged": + const equipmentChangedEvent = event as EquipmentChangedEvent; + const equipmentChangedAdventurerData = processAdventurerState( + equipmentChangedEvent.adventurerStateWithBag, + currentAdventurer + ); + const formattedEquipmentChangedEquippedItems: string[] = []; + for (let i = 0; i < equipmentChangedEvent.equippedItems.length; i++) { + formattedEquipmentChangedEquippedItems.push( + gameData.ITEMS[equipmentChangedEvent.equippedItems[i]] + ); + } + const formattedEquipmentChangedBaggedItems: string[] = []; + for (let i = 0; i < equipmentChangedEvent.baggedItems.length; i++) { + formattedEquipmentChangedBaggedItems.push( + gameData.ITEMS[equipmentChangedEvent.baggedItems[i]] + ); + } + const formattedEquipmentChangedDroppedItems: string[] = []; + for (let i = 0; i < equipmentChangedEvent.droppedItems.length; i++) { + formattedEquipmentChangedDroppedItems.push( + gameData.ITEMS[equipmentChangedEvent.droppedItems[i]] + ); + } + + const processedEquippedItems = processLootDiscovery( + formattedEquipmentChangedEquippedItems, + true, + equipmentChangedEvent.adventurerStateWithBag.adventurerState + ); + const processedBaggedItems = processLootDiscovery( + formattedEquipmentChangedBaggedItems, + false, + equipmentChangedEvent.adventurerStateWithBag.adventurerState + ); + return [ + equipmentChangedAdventurerData, + processedEquippedItems, + processedBaggedItems, + formattedEquipmentChangedDroppedItems, + ]; case "DodgedObstacle": const dodgedObstacleEvent = event as DodgedObstacleEvent; const dodgedObstacleAdventurerData = processAdventurerState( diff --git a/ui/src/app/lib/utils/syscalls.ts b/ui/src/app/lib/utils/syscalls.ts index 1ae57c0bf..c594c8ea3 100644 --- a/ui/src/app/lib/utils/syscalls.ts +++ b/ui/src/app/lib/utils/syscalls.ts @@ -493,22 +493,18 @@ export function syscalls({ (item: Item) => !unequippedItems.some((droppedItem) => droppedItem.item == item.item) ); - const filteredDrops = [ - ...(filteredUnequips ?? []), - ...equippedItems, - ...unequippedItems, - ]?.filter((item: Item) => !droppedItems.includes(item.item ?? "")); - setData("itemsByAdventurerQuery", { - items: [...filteredDrops], - }); const discoveries: Discovery[] = []; + let discoveredLootEquipped = []; + let discoveredLootBagged = []; + const filteredDiscoveries = events.filter( (event) => event.name === "DiscoveredHealth" || event.name === "DiscoveredGold" || event.name === "DiscoveredXP" || + event.name === "DiscoveredLoot" || event.name === "DodgedObstacle" || event.name === "HitByObstacle" ); @@ -558,9 +554,31 @@ export function syscalls({ } } } + if (discovery.name === "DiscoveredLoot") { + const filteredEquipmentChangedEvents = events.filter( + (event) => event.name === "EquipmentChanged" + ); + for (let filteredEquipmentChangedEvent of filteredEquipmentChangedEvents) { + discoveredLootEquipped = filteredEquipmentChangedEvent.data[1]; + discoveredLootBagged = filteredEquipmentChangedEvent.data[2]; + } + } } } + const filteredDrops = [ + ...(filteredUnequips ?? []), + ...equippedItems, + ...unequippedItems, + ]?.filter((item: Item) => !droppedItems.includes(item.item ?? "")); + setData("itemsByAdventurerQuery", { + items: [ + ...filteredDrops, + ...discoveredLootEquipped, + ...discoveredLootBagged, + ], + }); + const filteredBeastDiscoveries = events.filter( (event) => event.name === "DiscoveredBeast" ); diff --git a/ui/src/app/types/events.ts b/ui/src/app/types/events.ts index 0c0134339..0ddee31cf 100644 --- a/ui/src/app/types/events.ts +++ b/ui/src/app/types/events.ts @@ -162,6 +162,18 @@ export type DiscoveredXPEvent = { xpAmount: u16; }; +export type DiscoveredLootEvent = { + adventurerState: AdventurerState; + itemId: u16; +}; + +export type EquipmentChangedEvent = { + adventurerStateWithBag: AdventurerStateWithBag; + equippedItems: u8[]; + baggedItems: u8[]; + droppedItems: u8[]; +}; + export type DodgedObstacleEvent = { adventurerState: AdventurerState; id: u8; From 8bee025827eb12a212fee32eea44c5fc5c9b9bd4 Mon Sep 17 00:00:00 2001 From: Starknet Dev Date: Sat, 22 Jun 2024 10:13:35 +0100 Subject: [PATCH 2/3] - add discovery display for loot - add env variable for network --- indexer/env-sepolia | 2 +- ui/.env.preview | 2 +- ui/.env.production | 2 +- .../components/actions/DiscoveryDisplay.tsx | 21 +++++++++++++++++++ ui/src/app/components/onboarding/Intro.tsx | 18 +++++++++------- ui/src/app/lib/networkConfig.ts | 6 +++--- 6 files changed, 38 insertions(+), 13 deletions(-) diff --git a/indexer/env-sepolia b/indexer/env-sepolia index 9c99ffa64..215c2ae84 100644 --- a/indexer/env-sepolia +++ b/indexer/env-sepolia @@ -1,4 +1,4 @@ -GAME="0x0305f26ad19e0a10715d9f3137573d3a543de7b707967cd85d11234d6ec0fb7e" +GAME="0x05545d79b497a9b924ed3321361520566c90dc1abb0aa1643cc4839b12c73136" START=68300 MONGO_CONNECTION_STRING="mongodb://mongo:mongo@mongo:27017" MONGO_DB="mongo" diff --git a/ui/.env.preview b/ui/.env.preview index e9351d77f..600689064 100644 --- a/ui/.env.preview +++ b/ui/.env.preview @@ -1 +1 @@ -NEXT_PUBLIC_NETWORK=preview \ No newline at end of file +NEXT_PUBLIC_NETWORK=sepolia \ No newline at end of file diff --git a/ui/.env.production b/ui/.env.production index bc2125a48..47cc682cb 100644 --- a/ui/.env.production +++ b/ui/.env.production @@ -1 +1 @@ -NEXT_PUBLIC_NETWORK=prod \ No newline at end of file +NEXT_PUBLIC_NETWORK=mainnet \ No newline at end of file diff --git a/ui/src/app/components/actions/DiscoveryDisplay.tsx b/ui/src/app/components/actions/DiscoveryDisplay.tsx index 40294b0d1..ee3f11866 100644 --- a/ui/src/app/components/actions/DiscoveryDisplay.tsx +++ b/ui/src/app/components/actions/DiscoveryDisplay.tsx @@ -7,7 +7,10 @@ import { TwoCoinIcon, HealthPotionIcon, } from "@/app/components/icons/Icons"; +import LootIcon from "@/app/components/icons/LootIcon"; import { Discovery } from "@/app/types"; +import { GameData } from "@/app/lib/data/GameData"; +import { getItemData } from "@/app/lib/utils"; interface DiscoveryProps { discoveryData: Discovery; @@ -22,6 +25,8 @@ export const DiscoveryDisplay = ({ discoveryData }: DiscoveryProps) => { const AdventurerHealthExists = (discoveryData?.adventurerHealth ?? 0) > 0; + const gameData = new GameData(); + const renderDiscoveryMessage = () => { if (discoveryData?.discoveryType === "Beast") { if (discoveryData?.ambushed) { @@ -130,6 +135,22 @@ export const DiscoveryDisplay = ({ discoveryData }: DiscoveryProps) => { ); } + + if (discoveryData?.subDiscoveryType === "Loot") { + const itemName = gameData.ITEMS[discoveryData?.outputAmount!]; + const { slot } = getItemData(itemName ?? ""); + return ( + +
+

+ WOW! Discovered the Loot item {itemName}!{" "} + +

+
+ +
+ ); + } } return null; diff --git a/ui/src/app/components/onboarding/Intro.tsx b/ui/src/app/components/onboarding/Intro.tsx index 0679bce41..e5a323f70 100644 --- a/ui/src/app/components/onboarding/Intro.tsx +++ b/ui/src/app/components/onboarding/Intro.tsx @@ -1,7 +1,7 @@ import { Button } from "@/app/components/buttons/Button"; import Profile from "public/icons/profile.svg"; import QuestionMark from "public/icons/question-mark.svg"; -import useUIStore from "@/app/hooks/useUIStore"; +import useUIStore, { Network } from "@/app/hooks/useUIStore"; import { SoundOffIcon, SoundOnIcon } from "@/app/components/icons/Icons"; import { useUiSounds, soundSelector } from "@/app/hooks/useUiSound"; @@ -16,6 +16,8 @@ const Intro = () => { } = useUIStore(); const { play: clickPlay } = useUiSounds(soundSelector.click); + + const network = process.env.NEXT_PUBLIC_NETWORK; return (
-

Play On Testnet

+

+ Play On {network == "sepolia" ? "Testnet" : "Mainnet"} +

Looking for a hassle-free gaming experience? Play on Testnet, diff --git a/ui/src/app/lib/networkConfig.ts b/ui/src/app/lib/networkConfig.ts index b7857a4e8..d9b6fa1ec 100644 --- a/ui/src/app/lib/networkConfig.ts +++ b/ui/src/app/lib/networkConfig.ts @@ -8,16 +8,16 @@ export const networkConfig = { ethAddress: "0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7", gameAddress: - "0x05a502f60dac021a4d15a7a2df02b8a486aa672d747cca614589d10e8c50ba9d", + "0x05545d79b497a9b924ed3321361520566c90dc1abb0aa1643cc4839b12c73136", lordsAddress: - "0x0619077dc1fcc970a03a3239a5dc9c55990afcdb7f02a48a32dd107593cc3f71", + "0x0213c1cd576885fac65226505f8e01b908ad569c138e34f17ca220d67dd3c106", beastsAddress: "0x020c7c02c973ffa3a48fb78e9472b679c72c11b59512f524154ade0a39f54136", goldenTokenAddress: "0x07626660faba349aad9ad2aaa0ff8645c079fa8e043a168d640d92472806eeac", revenueAddress: "0x0314924118945405ac0bcd6181457712795c0effc29d8dd3be86d3f3ec62adc1", - appUrl: "https://sepolia-survivor.provable.games", + appUrl: "https://sepolia-lootsurvivor.io", beastsViewer: "https://testnet.realms.world/collection/beasts", goldenTokenMintUrl: "https://testnet.realms.world/collection/goldenToken", blockExplorerUrl: "https://sepolia.voyager.online/", From 4e8c3afda8fddca09f06144e16d82fff658f9314 Mon Sep 17 00:00:00 2001 From: Starknet Dev Date: Sat, 22 Jun 2024 10:34:05 +0100 Subject: [PATCH 3/3] fix indexer files --- indexer/src/adventurers.ts | 14 +------------- indexer/src/discoveries.ts | 4 +--- indexer/src/items.ts | 2 ++ indexer/src/utils/events.ts | 1 - 4 files changed, 4 insertions(+), 17 deletions(-) diff --git a/indexer/src/adventurers.ts b/indexer/src/adventurers.ts index 56e090fbc..cb7a434fa 100644 --- a/indexer/src/adventurers.ts +++ b/indexer/src/adventurers.ts @@ -11,7 +11,6 @@ import { parseDiscoveredHealth, parseStartGame, START_GAME, - PURCHASED_POTIONS, PURCHASED_ITEMS, ATTACKED_BY_BEAST, ADVENTURER_DIED, @@ -59,11 +58,10 @@ const filter = { { fromAddress: GAME, keys: [ADVENTURER_UPGRADED] }, { fromAddress: GAME, keys: [DISCOVERED_HEALTH] }, { fromAddress: GAME, keys: [DISCOVERED_GOLD] }, - { fromAddress: GAME, keys: [DISCOVERED_XP] }, + { fromAddress: GAME, keys: [DISCOVERED_LOOT] }, { fromAddress: GAME, keys: [DODGED_OBSTACLE] }, { fromAddress: GAME, keys: [HIT_BY_OBSTACLE] }, { fromAddress: GAME, keys: [DISCOVERED_BEAST] }, - { fromAddress: GAME, keys: [PURCHASED_POTIONS] }, { fromAddress: GAME, keys: [PURCHASED_ITEMS] }, { fromAddress: GAME, keys: [EQUIPPED_ITEMS] }, { fromAddress: GAME, keys: [DROPPED_ITEMS] }, @@ -207,16 +205,6 @@ export default function transform({ header, events }: Block) { }), ]; } - case PURCHASED_POTIONS: { - console.log("PURCHASED_POTIONS", "->", "ADVENTURER UPDATES"); - const { value } = parseDiscoveredXp(event.data, 0); - return [ - updateAdventurer({ - timestamp: new Date().toISOString(), - adventurerState: value.adventurerState, - }), - ]; - } case PURCHASED_ITEMS: { console.log("PURCHASED_ITEMS", "->", "ADVENTURER UPDATES"); const { value } = parsePurchasedItems(event.data, 0); diff --git a/indexer/src/discoveries.ts b/indexer/src/discoveries.ts index abac0f1f2..f4228f401 100644 --- a/indexer/src/discoveries.ts +++ b/indexer/src/discoveries.ts @@ -8,7 +8,6 @@ import { DISCOVERED_GOLD, DISCOVERED_HEALTH, DISCOVERED_LOOT, - DISCOVERED_XP, DODGED_OBSTACLE, HIT_BY_OBSTACLE, parseAmbushedByBeast, @@ -16,7 +15,6 @@ import { parseDiscoveredGold, parseDiscoveredHealth, parseDiscoveredLoot, - parseDiscoveredXp, parseHitByObstacle, } from "./utils/events.ts"; import { insertDiscovery } from "./utils/helpers.ts"; @@ -32,7 +30,7 @@ const filter = { events: [ { fromAddress: GAME, keys: [DISCOVERED_HEALTH] }, { fromAddress: GAME, keys: [DISCOVERED_GOLD] }, - { fromAddress: GAME, keys: [DISCOVERED_XP] }, + { fromAddress: GAME, keys: [DISCOVERED_LOOT] }, { fromAddress: GAME, keys: [HIT_BY_OBSTACLE] }, { fromAddress: GAME, keys: [DODGED_OBSTACLE] }, { fromAddress: GAME, keys: [AMBUSHED_BY_BEAST] }, diff --git a/indexer/src/items.ts b/indexer/src/items.ts index a5008021e..0408759cd 100644 --- a/indexer/src/items.ts +++ b/indexer/src/items.ts @@ -42,6 +42,8 @@ const filter = { events: [ { fromAddress: GAME, keys: [START_GAME] }, { fromAddress: GAME, keys: [PURCHASED_ITEMS] }, + { fromAddress: GAME, keys: [DISCOVERED_LOOT] }, + { fromAddress: GAME, keys: [EQUIPMENT_CHANGED] }, { fromAddress: GAME, keys: [EQUIPPED_ITEMS] }, { fromAddress: GAME, keys: [DROPPED_ITEMS] }, { fromAddress: GAME, keys: [HIT_BY_OBSTACLE] }, diff --git a/indexer/src/utils/events.ts b/indexer/src/utils/events.ts index 0cefd9372..eafed7516 100644 --- a/indexer/src/utils/events.ts +++ b/indexer/src/utils/events.ts @@ -37,7 +37,6 @@ export const ADVENTURER_DIED = eventKey("AdventurerDied"); export const FLEE_FAILED = eventKey("FleeFailed"); export const FLEE_SUCCEEDED = eventKey("FleeSucceeded"); -export const PURCHASED_POTIONS = eventKey("PurchasedPotions"); export const PURCHASED_ITEMS = eventKey("PurchasedItems"); export const UPGRADES_AVAILABLE = eventKey("UpgradesAvailable"); export const EQUIPPED_ITEMS = eventKey("EquippedItems");