Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- pragma solidity 0.5.1;
- import "./HitchensOrderTree.sol";
- import "./HitchensUnorderedAddressSet.sol";
- contract SoccerPlayers {
- // this is a strict list of team members
- using HitchensUnorderedAddressSetLib for HitchensUnorderedAddressSetLib.Set;
- HitchensUnorderedAddressSetLib.Set playerSet;
- // this is the ordered list of scores
- using HitchensOrderStatisticsTreeLib for HitchensOrderStatisticsTreeLib.Tree;
- HitchensOrderStatisticsTreeLib.Tree leaderTree;
- // we will need this to stay organized
- mapping(address => uint) public playerGoals;
- // using address for player is probably what you want.
- // "as is" the tree works with bytes32 keys, so some conversions will be bandy.
- function addressToBytes32(address a) public pure returns(bytes32) {
- return bytes32(uint(uint160(a)));
- }
- function bytes32ToAddress(bytes32 b) public pure returns(address) {
- return address(uint160(uint(b)));
- }
- function addPlayer(address player) public {
- playerSet.insert(player);
- leaderTree.insert(addressToBytes32(player),0);
- }
- function updatePlayerGoals(address player, uint goals) public {
- require(playerSet.exists(player), "Not a player");
- uint playerOldGoals = playerGoals[player];
- playerGoals[player] = goals;
- leaderTree.remove(addressToBytes32(player), playerOldGoals);
- leaderTree.insert(addressToBytes32(player), goals);
- }
- function removePlayer(address player) public {
- require(playerSet.exists(player), "Not a player");
- uint playerOldGoals = playerGoals[player];
- leaderTree.remove(addressToBytes32(player), playerOldGoals);
- playerSet.remove(player);
- }
- // inspect sorted list (last number is highest)
- function highestGoals() public view returns(uint goals, uint playersTied) { // tie count starts at zero
- goals = leaderTree.last();
- uint p; uint l; uint r; bool _r; uint c; // unused internal stuff from tree organization
- (p, l, r, _r, playersTied, c) = leaderTree.getNode(goals);
- }
- // in case players are tied (playersTied > 0), iterate the players with the same goal count.
- function playerAtGoals(uint goals, uint index) public view returns(address) {
- return bytes32ToAddress(leaderTree.valueKeyAtIndex(goals, index));
- }
- function nextHighest(uint belowGoals) public view returns(uint goals) {
- return leaderTree.prev(belowGoals);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement