From 5265d1a234eb4c490ac442978419bd5d01513566 Mon Sep 17 00:00:00 2001 From: Jon Michael Aanes Date: Wed, 9 Apr 2025 22:32:36 +0200 Subject: [PATCH] Look for the correct assets --- python/notamon_viewer/app.py | 49 +++++++++++++++++++++++------------- 1 file changed, 31 insertions(+), 18 deletions(-) diff --git a/python/notamon_viewer/app.py b/python/notamon_viewer/app.py index 480c835..96a1051 100644 --- a/python/notamon_viewer/app.py +++ b/python/notamon_viewer/app.py @@ -4,7 +4,7 @@ import base64 import dataclasses import requests_cache from collections.abc import Iterator - +import logging HTML_INDEX = """ @@ -94,7 +94,12 @@ TEST_NOTAMON = Notamon( ) ADDRESS_NFTS = '02b82d36784146bd29415dbaa623a5f1cf6e6e1108' -ADDRESS_ASSETS = '0338d039e3960426d0cfc590efbdbef70b049696a3' +ADDRESS_ASSETS = '036080dc8a15e496e5b970f65112b4fe4ac022e62c' + + +ASSET_ID_OFFSET_SPECIES = 0x0100_0000 +ASSET_ID_OFFSET_SKIN = 0x0200_0000 +ASSET_ID_OFFSET_EFFECT = 0x0300_0000 SESSION = requests_cache.CachedSession() @@ -119,10 +124,8 @@ def get_asset_variables(client, address) -> Iterator[AssetVariable]: asset_contract_state, _ = client.get_contract_state(ADDRESS_ASSETS) for variable in asset_contract_state['variables']: - print(variable) - asset_id = int.from_bytes(base64.b64decode(variable['value']['information']['data'])) - opened = decode_opened_variable(variable['value']['openValue']['data']) if variable['value']['openValue'] else None + opened = decode_opened_variable(variable['value']['openValue']['data']) if 'openValue' in variable['value'] else None yield AssetVariable( variable_id = variable['key'], @@ -155,28 +158,38 @@ def get_notamon_nfts(client: pbc_client.PbcClient) -> Iterator[NotamonNFTData]: ) -def nft_to_notamon_view(nft: NotamonNFTData, assets) -> Notamon: +def get_asset(assets, asset_type_id, asset_type_offset) -> bytes | None: + asset_id = asset_type_id | asset_type_offset + asset = assets.get(asset_id) + if asset is None: + app.logger.error('Unknown asset with id %s = %s | %s', asset_id, asset_type_id, asset_type_offset) + return None + asset = asset.opened + if asset is None: + app.logger.warning('Secret asset with id %s = %s | %s', asset_id, asset_type_id, asset_type_offset) + return asset + +def nft_to_notamon_view(nft: NotamonNFTData, assets) -> Notamon | None: + species_image_bytes = get_asset(assets, nft.species_id, ASSET_ID_OFFSET_SPECIES) + effect_str = get_asset(assets, nft.effect_id, ASSET_ID_OFFSET_EFFECT) + + if species_image_bytes is None or effect_str is None: + return None + return Notamon( - image_src = to_base64_png(assets[nft.species_id]), - effect_css = to_base64_png(assets[nft.effect_id]), + image_src = to_base64_png(species_image_bytes), + effect_css = effect_str, nickname = nft.nickname, species_name = 'Mudkip', ) def select_notamons(): client = pbc_client.PbcClient(SESSION, pbc_client.HOSTNAME_TESTNET) - nfts = list(get_notamon_nfts(client)) - print(nfts) - - - print(client) - assets = list(get_asset_variables(client, ADDRESS_ASSETS)) - print(assets ) - - return [nft_to_notamon_view(nft, assets) for nft in nfts] + assets = {v.asset_id: v for v in get_asset_variables(client, ADDRESS_ASSETS)} + notamon_views = [nft_to_notamon_view(nft, assets) for nft in nfts] + return [n for n in notamon_views if n is not None] @app.route("/") def hello_world(): return flask.render_template_string(HTML_INDEX, notamons = select_notamons()) -