162 lines
3.3 KiB
Python
162 lines
3.3 KiB
Python
import flask
|
|
import pbc_client
|
|
import base64
|
|
import dataclasses
|
|
import requests_cache
|
|
from collections.abc import Iterator
|
|
|
|
|
|
HTML_INDEX = """
|
|
|
|
|
|
<!doctype html>
|
|
<html>
|
|
<head>
|
|
<style>
|
|
|
|
html {
|
|
background-color: #222;
|
|
color: white;
|
|
font-family: sans;
|
|
}
|
|
|
|
body {
|
|
width: 80%;
|
|
margin: auto;
|
|
}
|
|
|
|
.notamon-grid {
|
|
display: grid;
|
|
grid-template-columns: repeat(10, 1fr);
|
|
justify-items: center;
|
|
text-align: center;
|
|
gap: 4em 1em;
|
|
}
|
|
|
|
.notamon h2, h3 {
|
|
margin: 0;
|
|
}
|
|
|
|
</style>
|
|
<title>Hello from Flask</title>
|
|
</head>
|
|
<body>
|
|
<div class="notamon-grid">
|
|
|
|
{% for notamon in notamons %}
|
|
<div class="notamon">
|
|
<img src="{{ notamon.image_src }}" style="{{ notamon.effect_css }}">
|
|
<h2>{{ notamon.nickname }}</h2>
|
|
<h3>{{ notamon.species_name }}</h3>
|
|
</div>
|
|
{% endfor %}
|
|
|
|
|
|
</div>
|
|
|
|
</body>
|
|
</html>
|
|
"""
|
|
|
|
@dataclasses.dataclass(frozen=True)
|
|
class Notamon:
|
|
image_src: str
|
|
effect_css: str
|
|
nickname: str
|
|
species_name: str
|
|
|
|
@dataclasses.dataclass(frozen=True)
|
|
class NotamonAttributes:
|
|
species_id: int
|
|
skin_id: int
|
|
effect_id: int
|
|
stat_hp: int
|
|
stat_attack: int
|
|
stat_defense: int
|
|
|
|
@dataclasses.dataclass(frozen=True)
|
|
class AssetVariable:
|
|
variable_id: int
|
|
asset_id: int
|
|
opened: bytes | None
|
|
|
|
|
|
|
|
app = flask.Flask(__name__)
|
|
|
|
TEST_NOTAMON = Notamon(
|
|
image_src = 'https://img.pokemondb.net/sprites/ruby-sapphire/normal/mudkip.png',
|
|
effect_css = '',
|
|
nickname = 'Dude',
|
|
species_name = 'Mudkip',
|
|
)
|
|
|
|
ADDRESS_NFTS = '02b82d36784146bd29415dbaa623a5f1cf6e6e1108'
|
|
ADDRESS_ASSETS = '0338d039e3960426d0cfc590efbdbef70b049696a3'
|
|
|
|
SESSION = requests_cache.CachedSession()
|
|
|
|
|
|
def compress(b: bytes) -> bytes:
|
|
l = []
|
|
for i in range(0, len(b)//8):
|
|
derp = 0
|
|
for j in range(0, 8):
|
|
derp |= b[i*8+j] << j
|
|
del j
|
|
l.append(derp)
|
|
del i, derp
|
|
return bytes(l)
|
|
|
|
|
|
def decode_opened_variable(value: str) -> bytes:
|
|
return compress(base64.b64decode(value))[4:]
|
|
|
|
|
|
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
|
|
|
|
yield AssetVariable(
|
|
variable_id = variable['key'],
|
|
asset_id = asset_id,
|
|
opened = opened,
|
|
)
|
|
|
|
|
|
def to_base64_png(b: bytes) -> str:
|
|
return f'data:image/png;base64,{base64.b64encode(b)}'
|
|
|
|
|
|
def get_notamon_nfts(client: pbc_client.PbcClient):
|
|
asset_contract_state, _ = client.get_contract_state(ADDRESS_NFTS)
|
|
print(asset_contract_state)
|
|
|
|
yield from []
|
|
|
|
|
|
|
|
|
|
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 [TEST_NOTAMON for i in range(100)]
|
|
|
|
@app.route("/")
|
|
def hello_world():
|
|
return flask.render_template_string(HTML_INDEX, notamons = select_notamons())
|
|
|