Created
October 28, 2018 18:07
-
-
Save tlatkdgus1/7bd999973e6c19d6b736eb10c678e6d9 to your computer and use it in GitHub Desktop.
This file contains 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.4.24; | |
// ------------------ Public Network Part ----------------------- | |
contract publicGame{ | |
privateGame internal privateNet; | |
mapping(address => uint) userIds; | |
mapping(uint => address) idUsers; | |
mapping(uint => uint) betAmount; | |
function setPlayer(string _userName, string _comment){ | |
uint userId = privateNet.setPlayer(_userName, _comment); | |
userIds[msg.sender] = userId; | |
idUsers[userId] = msg.sender; | |
} | |
function setComment(string _comment){ | |
uint userId = userIds[msg.sender]; | |
privateNet.setComment(userId, _comment); | |
} | |
function bettingGame(uint _gameId, uint _betNumber){ | |
uint userId = userIds[msg.sender]; | |
betAmount[userId] = msg.value; | |
(bool result, uint winnerId) = privateNet.bettingGame(userId, _gameId, _betNumber); | |
if(result){ | |
idUsers[winnerId].transfer(betAmount[winnerId]*2); | |
} | |
} | |
} | |
// ------------------ Private Network Part ----------------------- | |
contract privateGame { | |
address public owner; | |
constructor() { | |
owner = msg.sender; | |
} | |
modifier onlyOwner() { | |
require(msg.sender == owner); | |
_; | |
} | |
function transferOwnership(address newOwner) onlyOwner { | |
if (newOwner != address(0)) { | |
owner = newOwner; | |
} | |
} | |
event SetPlayer(string userName, string comment); | |
event SetComment(string userName, string comment); | |
event CreateGame(uint gameId); | |
event BettingGame(uint gameId, string userName, uint betNumber); | |
event Winner(uint gameId, string userName); | |
struct Player{ | |
uint playerId; | |
string userName; | |
string comment; | |
mapping(uint => uint) betGame; | |
} | |
struct Game{ | |
uint gameId; | |
uint[] playerIds; | |
mapping(uint => uint) betNumber; | |
uint randomNumber; | |
} | |
struct GameHistory{ | |
uint date; | |
string winner; | |
} | |
Player[] public players; | |
Game[] private games; | |
GameHistory[] public gameHistorys; | |
function setPlayer(string _userName, string _comment) public returns (uint){ | |
uint playerId = players.length; | |
Player storage player = players[playerId]; | |
player.playerId = playerId; | |
player.userName = _userName; | |
player.comment = _comment; | |
emit SetPlayer(player.userName, player.comment); | |
return(playerId); | |
} | |
function setComment(uint _playerId, string _comment) public returns (uint) { | |
Player storage player = players[_playerId]; | |
if (player.playerId > 0) { revert(); } | |
player.comment = _comment; | |
emit SetComment(player.userName, player.comment); | |
return(_playerId); | |
} | |
function createGame() onlyOwner private returns(uint){ | |
uint gameId = games.length; | |
Game storage game = games[gameId]; | |
game.gameId = gameId; | |
uint random_number = uint(block.blockhash(block.number-1))%4 + 1; | |
game.randomNumber = random_number; | |
emit CreateGame(game.gameId); | |
return (game.gameId); | |
} | |
function bettingGame(uint _playerId, uint _gameId, uint _betNumber) returns (bool, uint){ | |
Player storage player = players[_playerId]; | |
Game storage game = games[_gameId]; | |
if (game.gameId > 0){ revert(); } | |
game.playerIds.push(player.playerId); | |
game.betNumber[_betNumber] = player.playerId; | |
emit BettingGame(game.gameId, player.userName, _betNumber); | |
if(game.playerIds.length == 4){ | |
uint winnerId = finishGame(game.gameId); | |
return (true, winnerId); | |
}else{ | |
return (false, 0); | |
} | |
} | |
function finishGame(uint _gameId) internal returns(uint){ | |
Game storage game = games[_gameId]; | |
uint winNumber = game.randomNumber; | |
uint winnerId = game.betNumber[winNumber]; | |
Player storage winner = players[winnerId]; | |
uint gameHistoryId = gameHistorys.length; | |
gameHistorys[gameHistoryId].date = now; | |
gameHistorys[gameHistoryId].winner = winner.userName; | |
emit Winner(game.gameId, winner.userName); | |
return (winnerId); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment