Created
October 31, 2018 05:45
-
-
Save tlatkdgus1/831fcaaa91ec60e1abcdc348c24f60ea to your computer and use it in GitHub Desktop.
sample_callback
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 public privateNet; | |
address public privateAddr; | |
event _CallbackSetPlayer(uint userId, address msgSender); | |
event SetPlayer(bytes32 _userName, bytes32 _comment, address msgSender); | |
event Winner(bytes32 winnerName); | |
mapping(address => uint) public userIds; | |
mapping(uint => address) public idUsers; | |
mapping(uint => uint) public betAmount; | |
function getUser(uint _id) returns(address){ | |
return(idUsers[_id]); | |
} | |
function getOwner() public view returns(address){ | |
return privateNet.owner(); | |
} | |
function setPrivateNet(address _address){ | |
privateNet = privateGame(_address); | |
} | |
function setPlayer(bytes32 _userName, bytes32 _comment) public payable{ | |
privateNet.setPlayer(_userName, _comment, msg.sender); | |
// privateAddr.call(bytes4(keccak256("setPlayer(byte32, byte32, address)")), _userName, _comment, msg.sender); | |
} | |
function _callbackSetPlayer(uint256 _userId, address _msgSender) public { | |
userIds[_msgSender] = _userId; | |
idUsers[_userId] = _msgSender; | |
emit _CallbackSetPlayer(_userId, _msgSender); | |
} | |
function setComment(bytes32 _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; | |
privateNet.bettingGame(userId, _gameId, _betNumber); | |
} | |
function _callbackBettingGame(bool _result, uint _winnerId, bytes32 _winnerName) payable{ | |
if(_result){ | |
emit Winner(_winnerName); | |
idUsers[_winnerId].transfer(betAmount[_winnerId]*2); | |
} | |
} | |
} | |
// ------------------ Private Network Part ----------------------- | |
contract privateGame { | |
publicGame internal publicNet; | |
address public owner; | |
constructor(address _address) { | |
owner = msg.sender; | |
publicNet = publicGame(_address); | |
} | |
modifier onlyOwner() { | |
require(msg.sender == owner); | |
_; | |
} | |
function transferOwnership(address newOwner) onlyOwner { | |
if (newOwner != address(0)) { | |
owner = newOwner; | |
} | |
} | |
event SetPlayer(bytes32 userName, bytes32 comment); | |
event SetComment(bytes32 userName, bytes32 comment); | |
event CreateGame(uint gameId); | |
event BettingGame(uint gameId, bytes32 userName, uint betNumber); | |
event Winner(uint gameId, bytes32 userName); | |
struct Player{ | |
uint playerId; | |
bytes32 userName; | |
bytes32 comment; | |
mapping(uint => uint) betGame; | |
} | |
struct Game{ | |
uint256 gameId; | |
uint[] playerIds; | |
mapping(uint => uint) betNumber; | |
uint256 randomNumber; | |
} | |
struct GameHistory{ | |
uint date; | |
bytes32 winner; | |
} | |
Player[] public players; | |
Game[] private games; | |
GameHistory[] public gameHistorys; | |
uint public playerId = 0; | |
function setPlayer(bytes32 _userName, bytes32 _comment, address _msgSender) payable{ | |
Player storage player; | |
player.userName = _userName; | |
player.comment = _comment; | |
players[playerId] = player; | |
emit SetPlayer(_userName, _comment); | |
publicNet._callbackSetPlayer(playerId, _msgSender); | |
playerId= playerId+1; | |
} | |
function setComment(uint _playerId, bytes32 _comment) public returns (uint) { | |
Player storage player = players[_playerId]; | |
if (player.playerId > 0) { revert(); } | |
player.comment = _comment; | |
return(_playerId); | |
} | |
function createGame() onlyOwner private returns(uint){ | |
uint256 gameId = games.length; | |
Game storage game = games[gameId]; | |
game.gameId = gameId; | |
uint256 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; | |
if(game.playerIds.length == 4){ | |
uint winnerId = finishGame(game.gameId); | |
return (true, winnerId); | |
}else{ | |
publicNet._callbackBettingGame(false, 0, 0x00); | |
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; | |
publicNet._callbackBettingGame(true, winnerId, winner.userName); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment