SHARE
TWEET

Untitled

a guest Jul 17th, 2019 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. pragma solidity 0.5.1;
  2.  
  3. import "./HitchensOrderTree.sol";
  4. import "./HitchensUnorderedAddressSet.sol";
  5.  
  6. contract SoccerPlayers {
  7.  
  8.     // this is a strict list of team members
  9.     using HitchensUnorderedAddressSetLib for HitchensUnorderedAddressSetLib.Set;
  10.     HitchensUnorderedAddressSetLib.Set playerSet;
  11.  
  12.  
  13.     // this is the ordered list of scores
  14.     using HitchensOrderStatisticsTreeLib for HitchensOrderStatisticsTreeLib.Tree;
  15.     HitchensOrderStatisticsTreeLib.Tree leaderTree;
  16.  
  17.     // we will need this to stay organized
  18.     mapping(address => uint) public playerGoals;
  19.  
  20.     // using address for player is probably what you want.
  21.     // "as is" the tree works with bytes32 keys, so some conversions will be bandy.
  22.  
  23.     function addressToBytes32(address a) public pure returns(bytes32) {
  24.         return bytes32(uint(uint160(a)));
  25.     }
  26.     function bytes32ToAddress(bytes32 b) public pure returns(address) {
  27.         return address(uint160(uint(b)));
  28.     }
  29.  
  30.  
  31.     function addPlayer(address player) public {
  32.         playerSet.insert(player);
  33.         leaderTree.insert(addressToBytes32(player),0);
  34.     }
  35.     function updatePlayerGoals(address player, uint goals) public {
  36.         require(playerSet.exists(player), "Not a player");
  37.         uint playerOldGoals = playerGoals[player];
  38.         playerGoals[player] = goals;
  39.         leaderTree.remove(addressToBytes32(player), playerOldGoals);
  40.         leaderTree.insert(addressToBytes32(player), goals);
  41.     }
  42.     function removePlayer(address player) public {
  43.         require(playerSet.exists(player), "Not a player");
  44.         uint playerOldGoals = playerGoals[player];
  45.         leaderTree.remove(addressToBytes32(player), playerOldGoals);
  46.         playerSet.remove(player);
  47.     }
  48.  
  49.     // inspect sorted list (last number is highest)
  50.  
  51.     function highestGoals() public view returns(uint goals, uint playersTied) { // tie count starts at zero
  52.         goals = leaderTree.last();
  53.         uint p; uint l; uint r; bool _r; uint c; // unused internal stuff from tree organization
  54.         (p, l, r, _r, playersTied, c) = leaderTree.getNode(goals);
  55.     }
  56.  
  57.     // in case players are tied (playersTied > 0), iterate the players with the same goal count.
  58.  
  59.     function playerAtGoals(uint goals, uint index) public view returns(address) {
  60.         return bytes32ToAddress(leaderTree.valueKeyAtIndex(goals, index));
  61.     }
  62.     function nextHighest(uint belowGoals) public view returns(uint goals) {
  63.         return leaderTree.prev(belowGoals);
  64.     }
  65.  
  66. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top