From 71403a41c7c06ed4e1695ebaedc5dd7e3e7042e7 Mon Sep 17 00:00:00 2001 From: lza_menace Date: Mon, 2 Jan 2023 01:16:12 -0800 Subject: [PATCH] setup leaderboard --- src/Main.sol | 24 +++++++++++++++++++++--- src/Unaboomer.sol | 4 ++-- test/Unaboomer.t.sol | 4 ++-- 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/src/Main.sol b/src/Main.sol index dd3418c..f270431 100644 --- a/src/Main.sol +++ b/src/Main.sol @@ -58,6 +58,12 @@ import {Mailbomb} from "./Mailbomb.sol"; contract Main is Owned { + /// Track the number of kills for each address + mapping(address => uint256) public killCount; + /// Index the number of kills to form a basic leaderboard + mapping(uint256 => address) public leaderboard; + /// Point to the latest leaderboard update + uint256 public leaderboardPointer; /// Price of the Unaboomer ERC-721 token uint256 public unaboomerPrice = 0.01 ether; /// Price of the Mailbomb ERC-1155 token @@ -67,7 +73,7 @@ contract Main is Owned { /// Mailbomb contract Mailbomb public mailbomb; - /// SendBomb event is for recording the results of sendBombs + /// SentBomb event is for recording the results of sendBombs /// @param from Sender of the bombs /// @param tokenId Unaboomer token which was targeted /// @param hit Whether or not the bomb killed the token or not (was a dud / already killed) @@ -118,7 +124,7 @@ contract Main is Owned { /// The game stops. modifier missionNotCompleted { require( - unaboomer.killCount() <= (unaboomer.MAX_SUPPLY() - unaboomer.SURVIVOR_COUNT()), + unaboomer.totalKillCount() <= (unaboomer.MAX_SUPPLY() - unaboomer.SURVIVOR_COUNT()), "mission already completed" ); _; @@ -141,7 +147,7 @@ contract Main is Owned { /// Get BOOMR kill count (unaboomers killed) function unaboomersKilled() public view returns (uint256) { - return unaboomer.killCount(); + return unaboomer.totalKillCount(); } /// Get BOOMR token max supply @@ -191,6 +197,7 @@ contract Main is Owned { /// Send N bombs to pseudo-random Unaboomer tokenIds to kill them. /// If the Unaboomer is already dead, the bomb is considered a dud. + /// Update a leaderboard with updated kill counts. /// @dev Pick a pseudo-random tokenID from Unaboomer contract and toggle a mapping value /// @dev The likelihood of killing a boomer decreases as time goes on - i.e. more duds /// @param _amount Amount of bombs to send to kill Unaboomers @@ -204,6 +211,17 @@ contract Main is Owned { unaboomer.die(randomBoomer); bool senderOwned = msg.sender == unaboomer.ownerOf(randomBoomer); emit SentBomb(msg.sender, randomBoomer, !dud, senderOwned); + if(!dud && !senderOwned) { + killCount[msg.sender]++; + } + } + uint256 kills = killCount[msg.sender]; + address leader = leaderboard[leaderboardPointer]; + if (kills > killCount[leader]) { + if (leader != msg.sender) { + leaderboardPointer++; + leaderboard[leaderboardPointer] = msg.sender; + } } mailbomb.explode(msg.sender, _amount); } diff --git a/src/Unaboomer.sol b/src/Unaboomer.sol index 7fd11f7..77e41fb 100644 --- a/src/Unaboomer.sol +++ b/src/Unaboomer.sol @@ -13,7 +13,7 @@ contract Unaboomer is ERC721A, Owned { uint256 public constant MAX_SUPPLY = 10000; uint256 public constant SURVIVOR_COUNT = 1000; - uint256 public killCount; + uint256 public totalKillCount; uint256 public minted; string public aliveURI; string public deadURI; @@ -70,7 +70,7 @@ contract Unaboomer is ERC721A, Owned { function die(uint256 tokenId) external onlyMain { require(tokenId < totalSupply(), "invalid token id"); if (tokenDead[tokenId] == false) { - killCount++; + totalKillCount++; tokenDead[tokenId] = true; } } diff --git a/test/Unaboomer.t.sol b/test/Unaboomer.t.sol index 902968c..dbcf804 100644 --- a/test/Unaboomer.t.sol +++ b/test/Unaboomer.t.sol @@ -47,8 +47,8 @@ contract UnaboomerTest is Test { hoax(t1); main.sendBombs(40); assertEq(bomb.balanceOf(t1, 1), 20); - assertEq(boomr.killCount() > 0, true); - console.log(boomr.killCount()); + assertEq(boomr.totalKillCount() > 0, true); + console.log(boomr.totalKillCount()); } function testKilling() public {