Created
May 25, 2022 11:36
-
-
Save ngmachado/59e23f1909fb247f7883af8642878008 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.8.0; | |
/* | |
We can pack all players permissions in one variable. Not using different maps for each one. | |
*/ | |
contract ExgrasiaPackPermissions { | |
//masks | |
uint256 constant internal IS_PLAYER_INIT = 1; | |
uint256 constant internal IS_PLAYER_ADMIN = 1 << 1; | |
uint256 constant internal CAN_MOVE_WATER = 1 << 2; | |
uint256 constant internal CAN_MOVE_SNOW = 1 << 3; | |
uint256 constant internal CAN_PUT_ANYTHING = 1 << 4; | |
mapping(address => uint256) public userPerm; | |
//someone needs to be admin | |
constructor() { | |
userPerm[msg.sender] = IS_PLAYER_INIT | IS_PLAYER_ADMIN; | |
} | |
//bit getters | |
function isPlayerInit(address player) external view returns(bool) { | |
return (userPerm[player] & IS_PLAYER_INIT) == 1; | |
} | |
function isPlayerAdmin(address player) external view returns(bool) { | |
return (userPerm[player] & IS_PLAYER_ADMIN) > 0; | |
} | |
function canMoveWater(address player) external view returns(bool) { | |
return (userPerm[player] & CAN_MOVE_WATER) > 0; | |
} | |
function canMoveSnow(address player) external view returns(bool) { | |
return (userPerm[player] & CAN_MOVE_SNOW) > 0; | |
} | |
function canMovePutAnything(address player) external view returns(bool) { | |
return (userPerm[player] & CAN_PUT_ANYTHING) > 0; | |
} | |
//bit setters | |
function setPlayerInit(address player) external onlyAdmin { | |
userPerm[player] = (userPerm[player] | IS_PLAYER_INIT); | |
} | |
//admin is always a player | |
function setPlayerAdmin(address player) external onlyAdmin { | |
userPerm[player] = (userPerm[player] | IS_PLAYER_INIT | IS_PLAYER_ADMIN); | |
} | |
function setMoveWater(address player) external onlyAdmin onlyToInitPlayers(player) { | |
userPerm[player] = (userPerm[player] | CAN_MOVE_WATER); | |
} | |
function setMoveSnow(address player) external onlyAdmin onlyToInitPlayers(player) { | |
userPerm[player] = (userPerm[player] | CAN_MOVE_SNOW); | |
} | |
function setPutAnything(address player) external onlyAdmin onlyToInitPlayers(player) { | |
userPerm[player] = (userPerm[player] | CAN_PUT_ANYTHING); | |
} | |
modifier onlyAdmin { | |
require((userPerm[msg.sender] & IS_PLAYER_ADMIN) > 0, "not admin"); | |
_; | |
} | |
modifier onlyToInitPlayers(address player) { | |
require((userPerm[player] & IS_PLAYER_INIT) > 0, "player not init"); | |
_; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment