Skip to content

Instantly share code, notes, and snippets.

@p3c-bot
Last active August 10, 2020 00:00
Show Gist options
  • Save p3c-bot/64ba102a50473e65a086676c85571f4a to your computer and use it in GitHub Desktop.
Save p3c-bot/64ba102a50473e65a086676c85571f4a to your computer and use it in GitHub Desktop.
Node for Node Game
pragma solidity ^0.4.21;
/***
* _ _ _ ______ _ _ _
* | \ | | | | | ___| | \ | | | |
* | \| | ___ __| | ___| |_ ___ _ __| \| | ___ __| | ___
* | . ` |/ _ \ / _` |/ _ \ _/ _ \| '__| . ` |/ _ \ / _` |/ _ \
* | |\ | (_) | (_| | __/ || (_) | | | |\ | (_) | (_| | __/
* \_| \_/\___/ \__,_|\___\_| \___/|_| \_| \_/\___/ \__,_|\___|
*
* v 1.0.0
* "If you want to go fast, go alone, if you want to go far go with others."
* What?
* -> Create a NodeForNode Game of any amount of Players and Amounts in the lobby.
* -> Put money into the Game, and when it hits the threshold, all players buy into Commonwealth on each other's masternode links.
* -> NodeForNode contract self destructs after payout, but Lobby still lasts.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
contract Hourglass {
function myTokens() public pure returns(uint256) {}
function myDividends(bool) public pure returns(uint256) {}
function transfer(address, uint256) public returns(bool) {}
function buy(address) public payable returns(uint256) {}
}
contract Farm {
function myCrop() public pure returns(address) {}
function myCropTokens() public pure returns(uint256) {}
function myCropDividends() public pure returns(uint256) {}
}
contract Lobby {
event NewGame(address indexed _from, address _game, uint _id, uint _amountOfPlayers, uint _entryCost);
mapping (uint256 => address) public games;
uint256 public gameNumber = 0;
/**
* User specifies how many players they want, and what the entry cost in wei is for a new game.
* Creates a new contract for them, and buys them automatic entry.
*/
function createGame(uint256 amountOfPlayers, uint256 entryCost) public payable returns (address) {
address gameAddress = new NodeForNode(gameNumber, amountOfPlayers, entryCost);
games[gameNumber] = gameAddress;
NodeForNode game = NodeForNode(gameAddress);
game.BuyIn.value(entryCost)(msg.sender);
emit NewGame(msg.sender, gameAddress, gameNumber, amountOfPlayers, entryCost);
gameNumber += 1;
return gameAddress;
}
}
contract NodeForNode {
event GameJoined(address indexed _from);
event GameExecuted(uint256 indexed _id, address indexed _from, uint size);
Hourglass p3c;
Farm farm;
address internal p3cAddress = 0x8c01128ff13E8296c34b22b20Ffc2829D85A2A22;
// address internal p3cAddress = 0xDF9AaC76b722B08511A4C561607A9bf3AfA62E49;
address internal farmAddress = 0x93123bA3781bc066e076D249479eEF760970aa32;
mapping(address => bool) public waiting;
address[] public players;
uint256 public id;
uint256 public playerAmount;
uint256 public entryCost;
function NodeForNode(uint256 _id, uint256 _amountOfPlayers, uint256 _cost) public {
p3c = Hourglass(p3cAddress);
farm = Farm(farmAddress);
id = _id;
playerAmount = _amountOfPlayers;
entryCost = _cost;
}
function waitingPlayers() public view returns (uint256){
return players.length;
}
function BuyIn(address user) payable public {
require(msg.value == entryCost);
require(waiting[user] == false);
// address user = msg.sender;
// If the user has crop tokens, use that as the N4N destination
// if (farm.myCropTokens() > p3c.myTokens()){
// user = farm.myCrop();
// }
players.push(user);
waiting[user] = true;
emit GameJoined(user);
if (players.length >= playerAmount){
// uint tokensBought = p3c.buy.value(entryCost)(msg.sender);
// p3c.transfer(msg.sender,1);
// p3c.transfer(players[0],1);
// p3c.transfer(players[1],(tokensBought / 2));
// Iterate through players and distribute tokens
for (uint i=0; i<players.length;i++){
// Each player buys in using their own node. Game theory is a beautiful thing.
p3c.buy.value(entryCost)(players[i]);
// THIS TRANSFER FUNCTION ISN' WORKING - TRY with 1
uint myTokens = (p3c.myTokens());
p3c.transfer(players[i], myTokens);
}
emit GameExecuted(id, user, playerAmount);
// Send any extra dividends back to the first player
selfdestruct(msg.sender);
}
}
function Refund(address user) public {
require(user == msg.sender);
require(waiting[user] == true);
// address user = msg.sender;
// If the user has crop tokens, use that as the N4N destination
// if (farm.myCropTokens() > p3c.myTokens()){
// user = farm.myCrop();
// }
uint index = find(players, user);
removeByIndex(players, index);
waiting[user] = false;
user.transfer(entryCost);
if (players.length == 0){
selfdestruct(msg.sender);
}
}
function removeByIndex(address[] storage items, uint index) internal {
if (index >= items.length) {
return;
}
for (uint i = index; i < items.length-1; i++) {
items[i] = items[i + 1];
}
items.length--;
}
function find(address[] storage items, address value) internal view returns (uint) {
uint i = 0;
while (items[i] != value) {
i++;
}
return i;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment