| SC Arguments: [Name:SC_ACTION Type:uint64 Value:'1' Name:SC_CODE Type:string Value:'// ==============================================================================
// CIPHER SNAKE DELUXE - On-chain Top 5 Leaderboard
// Smart Contract for DERO DVM-BASIC
// Target: Hologram Studio (Upload SC mode) or dero-wallet-cli install_sc
// ==============================================================================
//
// Purpose
// -------
// Stores only the TOP 5 highest scores ever published to the contract.
// Each slot is { pseudo, score, publisher_address, block_height }.
// If a new score is higher than the lowest of the current top 5,
// it replaces that lowest entry. Otherwise the publication is a no-op.
//
// Storage layout
// --------------
// Because the DVM stores strings / uints in a flat key-value map,
// we store the 5 slots as discrete keys (index 0..4).
//
// name_0 .. name_4 -> STRING (pseudo, max 16 chars)
// score_0 .. score_4 -> UINT64 (score)
// addr_0 .. addr_4 -> STRING (publisher signer address)
// time_0 .. time_4 -> UINT64 (block height at publish time)
// total_publishes -> UINT64 (global counter, lifetime)
// contract_version -> UINT64 (for future migrations)
//
// Entry points
// ------------
// Initialize() -> sets defaults, call once after deploy
// Publish(pseudo STRING, score UINT) -> publish a score, pays only tx fee
//
// Read side (off-chain, via daemon's DERO.GetSC)
// ----------------------------------------------
// The front-end reads the 20 keys (name_i, score_i, addr_i, time_i for i=0..4)
// with telaHost.getSmartContract(scid) then sorts by score desc for display.
//
// ==============================================================================
Function Initialize() Uint64
10 IF EXISTS("contract_version") == 1 THEN GOTO 90
20 STORE("contract_version", 1)
30 STORE("total_publishes", 0)
40 STORE("name_0", "") STORE("score_0", 0) STORE("addr_0", "") STORE("time_0", 0)
41 STORE("name_1", "") STORE("score_1", 0) STORE("addr_1", "") STORE("time_1", 0)
42 STORE("name_2", "") STORE("score_2", 0) STORE("addr_2", "") STORE("time_2", 0)
43 STORE("name_3", "") STORE("score_3", 0) STORE("addr_3", "") STORE("time_3", 0)
44 STORE("name_4", "") STORE("score_4", 0) STORE("addr_4", "") STORE("time_4", 0)
90 RETURN 0
End Function
// Publish(pseudo, score)
// ----------------------
// Validates inputs, finds the slot with the lowest score, replaces it IF the
// incoming score is strictly greater. Returns 0 on success, 1 if inputs are
// invalid, 2 if the score did not qualify for the top 5.
Function Publish(pseudo String, score Uint64) Uint64
10 IF STRLEN(pseudo) < 1 THEN GOTO 900
20 IF STRLEN(pseudo) > 16 THEN GOTO 900
30 IF score == 0 THEN GOTO 900
100 DIM min_slot as Uint64
101 DIM min_score as Uint64
110 LET min_slot = 0
111 LET min_score = LOAD("score_0")
120 IF LOAD("score_1") >= min_score THEN GOTO 130
121 LET min_slot = 1
122 LET min_score = LOAD("score_1")
130 IF LOAD("score_2") >= min_score THEN GOTO 140
131 LET min_slot = 2
132 LET min_score = LOAD("score_2")
140 IF LOAD("score_3") >= min_score THEN GOTO 150
141 LET min_slot = 3
142 LET min_score = LOAD("score_3")
150 IF LOAD("score_4") >= min_score THEN GOTO 200
151 LET min_slot = 4
152 LET min_score = LOAD("score_4")
200 IF score <= min_score THEN GOTO 910
300 IF min_slot == 0 THEN GOTO 400
301 IF min_slot == 1 THEN GOTO 410
302 IF min_slot == 2 THEN GOTO 420
303 IF min_slot == 3 THEN GOTO 430
304 IF min_slot == 4 THEN GOTO 440
400 STORE("name_0", pseudo) STORE("score_0", score) STORE("addr_0", SIGNER()) STORE("time_0", BLOCK_HEIGHT())
401 GOTO 800
410 STORE("name_1", pseudo) STORE("score_1", score) STORE("addr_1", SIGNER()) STORE("time_1", BLOCK_HEIGHT())
411 GOTO 800
420 STORE("name_2", pseudo) STORE("score_2", score) STORE("addr_2", SIGNER()) STORE("time_2", BLOCK_HEIGHT())
421 GOTO 800
430 STORE("name_3", pseudo) STORE("score_3", score) STORE("addr_3", SIGNER()) STORE("time_3", BLOCK_HEIGHT())
431 GOTO 800
440 STORE("name_4", pseudo) STORE("score_4", score) STORE("addr_4", SIGNER()) STORE("time_4", BLOCK_HEIGHT())
441 GOTO 800
800 STORE("total_publishes", LOAD("total_publishes") + 1)
801 RETURN 0
900 RETURN 1
910 RETURN 2
End Function
'] |