Skip to content

Commit

Permalink
Merge pull request #14 from prototyp3-dev/feature/scoreboard-replay-n…
Browse files Browse the repository at this point in the history
…ew-riv-version

Feature/scoreboard replay new riv version
  • Loading branch information
felipefg authored Feb 1, 2024
2 parents ccdd465 + 2ad5f44 commit 35022de
Show file tree
Hide file tree
Showing 36 changed files with 923 additions and 455 deletions.
8 changes: 5 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ ARG MACHINE_EMULATOR_TOOLS_VERSION

LABEL io.sunodo.sdk_version=${SUNODORIV_SDK_VERSION}
LABEL io.cartesi.rollups.ram_size=128Mi
#LABEL io.cartesi.rollups.data_size=32Mb
LABEL io.cartesi.rollups.data_size=128Mb

WORKDIR /opt/tools

# Install tools
RUN <<EOF
apt-get update && \
apt-get install -y --no-install-recommends wget=1.21.2-2ubuntu1 ca-certificates=20230311ubuntu0.22.04.1 \
build-essential=12.9ubuntu3 sqlite3=3.37.2-2ubuntu0.3 git=1:2.34.1-1ubuntu1.10 && \
build-essential=12.9ubuntu3 sqlite3=3.37.2-2ubuntu0.3 git=1:2.34.1-1ubuntu1.10 squashfs-tools=1:4.5-3build1&& \
wget -O machine-emulator-tools.deb https://github.com/cartesi/machine-emulator-tools/releases/download/v${MACHINE_EMULATOR_TOOLS_VERSION}/machine-emulator-tools-v${MACHINE_EMULATOR_TOOLS_VERSION}.deb && \
rm -rf /var/lib/apt/lists/*
EOF
Expand Down Expand Up @@ -77,7 +77,9 @@ COPY main.py .
COPY cartesapp cartesapp
COPY app app
COPY misc/snake.sqfs misc/snake.sqfs
COPY misc/doom.sqfs misc/doom.sqfs
COPY misc/2048.sqfs misc/2048.sqfs
COPY misc/freedoom.sqfs misc/freedoom.sqfs
COPY misc/antcopter.sqfs misc/antcopter.sqfs

FROM base as dapp

Expand Down
24 changes: 20 additions & 4 deletions app/cartridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from cartesapp.storage import Entity, helpers, seed
from cartesapp.manager import query, mutation, get_metadata, event, output, add_output, emit_event, contract_call

from .riv import riv_get_cartridge_info, riv_get_cartridge_screenshot, riv_get_cartridges_path
from .riv import riv_get_cartridge_info, riv_get_cartridge_screenshot, riv_get_cartridges_path, riv_get_cover, riv_get_cartridge_outcard
from .setup import AppSettings

LOGGER = logging.getLogger(__name__)
Expand All @@ -37,12 +37,25 @@ def initialize_data():
cartridge_example_data = cartridge_example_file.read()
cartridge_example_file.close()
create_cartridge(cartridge_example_data,msg_sender="0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266")
if AppSettings.rivemu_path is None: os.remove('misc/snake.sqfs')

cartridge_example_file = open('misc/doom.sqfs','rb')
cartridge_example_file = open('misc/freedoom.sqfs','rb')
cartridge_example_data = cartridge_example_file.read()
cartridge_example_file.close()
create_cartridge(cartridge_example_data,msg_sender="0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266")
if AppSettings.rivemu_path is None: os.remove('misc/freedoom.sqfs')

cartridge_example_file = open('misc/antcopter.sqfs','rb')
cartridge_example_data = cartridge_example_file.read()
cartridge_example_file.close()
create_cartridge(cartridge_example_data,msg_sender="0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266")
if AppSettings.rivemu_path is None: os.remove('misc/antcopter.sqfs')

cartridge_example_file = open('misc/2048.sqfs','rb')
cartridge_example_data = cartridge_example_file.read()
cartridge_example_file.close()
create_cartridge(cartridge_example_data,msg_sender="0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266")
if AppSettings.rivemu_path is None: os.remove('misc/2048.sqfs')


# Inputs
Expand Down Expand Up @@ -158,7 +171,7 @@ def remove_cartridge(payload: RemoveCartridgePayload) -> bool:
###
# Queries

@query()
@query(splittable_output=True)
def cartridge(payload: CartridgePayload) -> bool:
query = helpers.select(c for c in Cartridge if c.id == payload.id)

Expand Down Expand Up @@ -252,7 +265,10 @@ def create_cartridge(cartridge_data,**metadata):
cartridge_info_json = json.loads(cartridge_info)
Info(**cartridge_info_json)

cartridge_cover = cartridge_info_json.get("cover")
# check if cartridge runs
riv_get_cartridge_outcard(data_hash,0,None,None)

cartridge_cover = riv_get_cover(data_hash)
if cartridge_cover is None or len(cartridge_cover) == 0:
cartridge_cover = riv_get_cartridge_screenshot(data_hash,0)

Expand Down
22 changes: 16 additions & 6 deletions app/replay.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from cartesapp.manager import mutation, get_metadata, add_output, event, emit_event, contract_call # TODO: create repo to avoid this relative import hassle
from cartesapp.utils import bytes2str

from .setup import AppSettings, ScoreType
from .setup import AppSettings, ScoreType, GameplayHash
from .riv import replay_log

LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -55,19 +55,26 @@ def replay(replay: Replay) -> bool:

metadata = get_metadata()

if not GameplayHash.check(replay.cartridge_id.hex(),sha256(replay.log).hexdigest()):
msg = f"Gameplay already submitted"
LOGGER.error(msg)
add_output(msg,tags=['error'])
return False

# process replay
LOGGER.info("Replaying cartridge...")
try:
outcard_raw = replay_log(replay.cartridge_id.hex(),replay.log,replay.args,replay.in_card)
outcard_raw, outhash = replay_log(replay.cartridge_id.hex(),replay.log,replay.args,replay.in_card)
except Exception as e:
msg = f"Couldn't replay log: {e}"
LOGGER.error(msg)
add_output(msg,tags=['error'])
return False

# process outcard
outcard_hash = sha256(outcard_raw.replace(b'\r',b"").replace(b'\t',b"").replace(b'\n',b"").replace(b' ',b"")).digest()
outcard_valid = outcard_hash == replay.outcard_hash
# outcard_hash = sha256(outcard_raw).digest()
# outcard_valid = outcard_hash == replay.outcard_hash
outcard_valid = outhash == replay.outcard_hash

outcard_format = outcard_raw[:4]
LOGGER.info(f"==== BEGIN OUTCARD ({outcard_format}) ====")
Expand All @@ -80,7 +87,8 @@ def replay(replay: Replay) -> bool:

LOGGER.info("==== END OUTCARD ====")
LOGGER.info(f"Expected Outcard Hash: {replay.outcard_hash.hex()}")
LOGGER.info(f"Computed Outcard Hash: {outcard_hash.hex()}")
# LOGGER.info(f"Computed Outcard Hash: {outcard_hash.hex()}")
LOGGER.info(f"Computed Outcard Hash: {outhash.hex()}")
LOGGER.info(f"Valid Outcard Hash : {outcard_valid}")

if not outcard_valid:
Expand All @@ -92,7 +100,7 @@ def replay(replay: Replay) -> bool:
score = 0
if outcard_format == b"JSON":
try:
score = int(json.loads(re.sub(r'\,(?!\s*?[\{\[\"\'\w])', '', outcard_str)).get('score')) or 0
score = int(json.loads(outcard_str).get('score')) or 0 # re.sub(r'\,(?!\s*?[\{\[\"\'\w])', '', outcard_str)
except Exception as e:
LOGGER.info(f"Couldn't load score from json: {e}")

Expand All @@ -106,4 +114,6 @@ def replay(replay: Replay) -> bool:
add_output(replay.log,tags=['replay',replay.cartridge_id.hex()])
emit_event(replay_score,tags=['score','general',replay.cartridge_id.hex()])

GameplayHash.add(replay.cartridge_id.hex(),sha256(replay.log).hexdigest())

return True
Loading

0 comments on commit 35022de

Please sign in to comment.