Skip to content

Commit

Permalink
feat(facets): implement functions in PlayerFacet.sol
Browse files Browse the repository at this point in the history
- player
- addPlayer
- levelUp
  • Loading branch information
florianleger committed Jun 18, 2022
1 parent 465e98b commit 283ba44
Show file tree
Hide file tree
Showing 13 changed files with 524 additions and 233 deletions.
15 changes: 9 additions & 6 deletions contracts/facets/LootboxFacet.sol
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import {AppStorage} from "../libraries/LibAppStorage.sol";
import {AppStorage, Modifiers} from "../libraries/LibAppStorage.sol";

contract LootboxFacet is Modifiers {

contract LootboxFacet {
AppStorage internal s;
event OpenLootboxEvent(address player, uint32 lootboxId);

function openLootbox(address player, uint32 lootboxId) external {
require(player != address(0), "Player address is not valid");
require(s.playersExists[player], "Player does not exist");
/// @notice Open a lootbox
/// @dev This function throws for queries about the zero address and non-existing players.
/// @param playerAddress The player to query
function openLootbox(address playerAddress, uint32 lootboxId) external {
require(playerAddress != address(0), "Player address is not valid");
require(s.players[playerAddress].createdAt > 0, "Player does not exist");
}

}
5 changes: 5 additions & 0 deletions contracts/facets/OwnershipFacet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import { LibDiamond } from "../libraries/LibDiamond.sol";
import { IERC173 } from "../interfaces/IERC173.sol";

contract OwnershipFacet is IERC173 {
function transferOwnership(address _newOwner) external override {
LibDiamond.enforceIsContractOwner();
LibDiamond.setContractOwner(_newOwner);
}

function owner() external override view returns (address owner_) {
owner_ = LibDiamond.contractOwner();
}
Expand Down
46 changes: 35 additions & 11 deletions contracts/facets/PlayerFacet.sol
Original file line number Diff line number Diff line change
@@ -1,20 +1,44 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
pragma solidity 0.8.13;

import {AppStorage} from "../libraries/LibAppStorage.sol";
import {AppStorage, Modifiers, Player} from "../libraries/LibAppStorage.sol";

contract PlayerFacet {
AppStorage internal s;
contract PlayerFacet is Modifiers {
event LevelUpEvent(address player, uint16 level);

function levelUp(address player) external {
require(player != address(0), "Player address is not valid");
//require(s.playersExists[player], "Player does not exist");
//s.players[player].level++;
/// @notice Query all details relating to a player
/// @dev This function throws for queries about the zero address and non-existing players.
/// @param playerAddress The player to query
/// @return _player The player's details
function player(address playerAddress) public view returns (Player memory _player) {
require(playerAddress != address(0), "Player address is not valid");
require(s.players[playerAddress].createdAt > 0, "Player does not exist");
_player = s.players[playerAddress];
}

// for testing purposes
function supportsInterface(bytes4 _interfaceID) external view returns (bool) {
return false;
/// @notice Add a player
/// @dev This function throws for queries about the zero address and already existing players.
/// @param playerAddress Address of the player to add
function addPlayer(address playerAddress) external onlyGuildAdmin {
require(playerAddress != address(0), "PlayerFacet: Player address is not valid");
require(!(s.players[playerAddress].createdAt > 0), "PlayerFacet: Player already exists");
s.players[playerAddress] = Player(block.timestamp, 0);
}

/// @notice Level-up a player
/// @dev This function throws for queries about the zero address and non-existing players.
/// @param playerAddress Address of the player to level-up
function levelUp(address playerAddress) external onlyGallion playerExists(playerAddress) {
require(playerAddress != address(0), "PlayerFacet: Player address is not valid");
s.players[playerAddress].level++;
}

/// @notice Remove a player
/// @dev This function throws for queries about the zero address and non-existing players.
/// @param playerAddress Address of the player to remove
function removePlayer(address playerAddress) external onlyGuildAdmin {
require(playerAddress != address(0), "PlayerFacet: Player address is not valid");
require(s.players[playerAddress].createdAt > 0, "PlayerFacet: Player does not exist");
delete s.players[playerAddress];
}
}
50 changes: 50 additions & 0 deletions contracts/facets/Test1Facet.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

// For test purposes only.
contract Test1Facet {

event TestEvent(address something);

function test1Func1() external {}

function test1Func2() external {}

function test1Func3() external {}

function test1Func4() external {}

function test1Func5() external {}

function test1Func6() external {}

function test1Func7() external {}

function test1Func8() external {}

function test1Func9() external {}

function test1Func10() external {}

function test1Func11() external {}

function test1Func12() external {}

function test1Func13() external {}

function test1Func14() external {}

function test1Func15() external {}

function test1Func16() external {}

function test1Func17() external {}

function test1Func18() external {}

function test1Func19() external {}

function test1Func20() external {}

function supportsInterface(bytes4 _interfaceID) external view returns (bool) {}
}
46 changes: 46 additions & 0 deletions contracts/facets/Test2Facet.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

// For test purposes only.
contract Test2Facet {

function test2Func1() external {}

function test2Func2() external {}

function test2Func3() external {}

function test2Func4() external {}

function test2Func5() external {}

function test2Func6() external {}

function test2Func7() external {}

function test2Func8() external {}

function test2Func9() external {}

function test2Func10() external {}

function test2Func11() external {}

function test2Func12() external {}

function test2Func13() external {}

function test2Func14() external {}

function test2Func15() external {}

function test2Func16() external {}

function test2Func17() external {}

function test2Func18() external {}

function test2Func19() external {}

function test2Func20() external {}
}
69 changes: 51 additions & 18 deletions contracts/libraries/LibAppStorage.sol
Original file line number Diff line number Diff line change
@@ -1,28 +1,61 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.13;

struct AppStorage {
address gallionLabs;
address guildTokenContract;
address guildContract;
mapping(address => bool) playersExists;
mapping(address => Player) players;
mapping(uint32 => Lootbox) lootboxes;
import {LibDiamond} from "./LibDiamond.sol";
import {LibMeta} from "./LibMeta.sol";

struct AppStorage {
bytes32 domainSeparator;
address owner;
address gallionLabs;
address guildTokenContract;
address guildContract;
mapping(address => Admin) guildAdmins;
mapping(address => Player) players;
mapping(uint32 => Lootbox) lootboxes;
}

struct Admin {
uint createdAt;
}

struct Player {
uint createdAt;
uint16 level;
}

struct Lootbox {
address owner;
Rarity rarity;
}

enum Rarity {
common,
rare,
epic,
legendary
}

contract Modifiers {
AppStorage internal s;

modifier onlyGuildAdmin() {
require(s.guildAdmins[LibMeta.msgSender()].createdAt > 0, "LibAppStorage: Only guild admins can call this function");
_;
}

struct Player {
address owner;
uint16 level;
modifier playerExists(address player) {
require(s.players[player].createdAt > 0, "LibAppStorage: Player does not exist");
_;
}

struct Lootbox {
address owner;
Rarity rarity;
modifier onlyOwner() {
LibDiamond.enforceIsContractOwner();
_;
}

enum Rarity {
common,
rare,
epic,
legendary
modifier onlyGallion() {
require(LibMeta.msgSender() == s.gallionLabs, "LibAppStorage: Only Gallion can call this function");
_;
}
}
32 changes: 32 additions & 0 deletions contracts/libraries/LibMeta.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.13;

library LibMeta {
bytes32 internal constant EIP712_DOMAIN_TYPEHASH =
keccak256(bytes("EIP712Domain(string name,string version,uint256 salt,address verifyingContract)"));

function domainSeparator(string memory name, string memory version) internal view returns (bytes32 domainSeparator_) {
domainSeparator_ = keccak256(
abi.encode(EIP712_DOMAIN_TYPEHASH, keccak256(bytes(name)), keccak256(bytes(version)), getChainID(), address(this))
);
}

function getChainID() internal view returns (uint256 id) {
assembly {
id := chainid()
}
}

function msgSender() internal view returns (address sender_) {
if (msg.sender == address(this)) {
bytes memory array = msg.data;
uint256 index = msg.data.length;
assembly {
// Load the 32 bytes word from memory with the address on the lower 20 bytes, and mask those.
sender_ := and(mload(add(array, index)), 0xffffffffffffffffffffffffffffffffffffffff)
}
} else {
sender_ = msg.sender;
}
}
}
11 changes: 10 additions & 1 deletion contracts/upgradeInitializers/DiamondInit.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,26 @@ import {IDiamondLoupe} from "../interfaces/IDiamondLoupe.sol";
import {IDiamondCut} from "../interfaces/IDiamondCut.sol";
import {IERC173} from "../interfaces/IERC173.sol";
import {IERC165} from "../interfaces/IERC165.sol";
import {AppStorage} from "../libraries/LibAppStorage.sol";
import {AppStorage, Admin} from "../libraries/LibAppStorage.sol";
import {LibMeta} from "../libraries/LibMeta.sol";

contract DiamondInit {
AppStorage internal s;

struct Args {
address gallionLabs;
address[] guildAdmins;
}

function init(Args memory _args) external {
s.gallionLabs = _args.gallionLabs;

for(uint i = 0; i < _args.guildAdmins.length; i++) {
s.guildAdmins[_args.guildAdmins[i]] = Admin(block.timestamp);
}

s.domainSeparator = LibMeta.domainSeparator("GallionDiamond", "V1");

// adding ERC165 data
LibDiamond.DiamondStorage storage ds = LibDiamond.diamondStorage();
ds.supportedInterfaces[type(IERC165).interfaceId] = true;
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
"version": "0.0.1",
"description": "",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"test": "npx hardhat test",
"prettier": "prettier --write contracts/**/*.sol",
"solhint": "solhint 'contracts/**/*.sol'"
"solhint": "solhint 'contracts/**/*.sol'",
"compile": "npx hardhat compile --force"
},
"repository": {
"type": "git",
Expand Down
Loading

0 comments on commit 283ba44

Please sign in to comment.