struct room { address player1; address player2; bytes32 bit1; bytes32 bit2; uint8 res1; uint8 res2; uint256 bet; uint8 counter; uint8 c1; uint8 c2; uint roomTime; uint startTime; bool open; bool close; } mapping (uint => room) rooms;
function revertRoom(uint id) internal { if( rooms[id].bet > 0 ) { rooms[id].player1.transfer(rooms[id].bet); if( rooms[id].player2 != 0x0 ) rooms[id].player2.transfer(rooms[id].bet); } RevertRoom(id); closeRoom(id); } function closeRoom(uint id) internal { rooms[id].close = true; RoomClosed( id ); delete rooms[id]; }
event RoomOpened( uint indexed room, address indexed player1, uint256 bet, uint8 counter, uint openedTime, bool indexed privat ); event RoomClosed( uint indexed room ); event JoinPlayer1( uint indexed room, address indexed player1 ); event JoinPlayer2( uint indexed room, address indexed player2, uint countdownTime ); event BetsFinished(uint indexed room ); event BetsAdd(address indexed from, uint indexed room ); event OneMoreGame(uint indexed room ); event SeedOpened( uint indexed room ); event RoundFinished( uint indexed room, uint8 res1, uint8 res2 ); event Revard(address win, uint256 amount, uint indexed room ); event Winner(address win, uint indexed room ); event Result(address indexed player, uint8 r, uint indexed room ); event RevertRoom(uint indexed room); event ScoreChanged(uint indexed room, uint8 score1, uint8 score2);
# this.autorun => filter5 = contractInstance.Winner {room: Number(FlowRouter.getParam('id')), }, {fromBlock:0, toBlock: 'latest', address: contrAdress} filter5.watch (error, result) -> console.log result if result instance.winner.set result.args.win console.log result.args.win UIkit.modal("#modal-winner").show()
if (typeof web3 !== 'undefined') { web3 = new Web3(web3.currentProvider); var contrAdress = '0x80dd7334a28579a9e96601573555db15b7fe523a'; var contrInterface = [ { "anonymous": false, "inputs": [ { "indexed": true, "name": "room", "type": "uint256" }, { "indexed": false, "name": "score1", "type": "uint8" }, { "indexed": false, "name": "score2", "type": "uint8" } ], "name": "ScoreChanged", "type": "event" }, { "constant": false, "inputs": [], "name": "deleteContract", "outputs": [], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": false, "inputs": [ { "name": "id", "type": "uint256" } ], "name": "exitRoom", "outputs": [], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": false, "inputs": [ { "name": "id", "type": "uint256" } ], "name": "fixResults", "outputs": [], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": false, "inputs": [ { "name": "id", "type": "uint256" } ], "name": "fixTimerResults", "outputs": [], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": false, "inputs": [ { "name": "id", "type": "uint256" } ], "name": "joinRoom", "outputs": [ { "name": "", "type": "uint256" } ], "payable": true, "stateMutability": "payable", "type": "function" }, { "constant": false, "inputs": [ { "name": "id", "type": "uint256" }, { "name": "count", "type": "uint8" }, { "name": "privat", "type": "bool" } ], "name": "newRoom", "outputs": [ { "name": "", "type": "uint256" } ], "payable": true, "stateMutability": "payable", "type": "function" }, { "constant": false, "inputs": [ { "name": "id", "type": "uint256" }, { "name": "bet", "type": "bytes32" } ], "name": "setBet", "outputs": [], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "anonymous": false, "inputs": [ { "indexed": true, "name": "room", "type": "uint256" } ], "name": "OneMoreGame", "type": "event" }, { "anonymous": false, "inputs": [ { "indexed": true, "name": "player", "type": "address" }, { "indexed": false, "name": "r", "type": "uint8" }, { "indexed": true, "name": "room", "type": "uint256" } ], "name": "Result", "type": "event" }, { "anonymous": false, "inputs": [ { "indexed": true, "name": "room", "type": "uint256" } ], "name": "SeedOpened", "type": "event" }, { "anonymous": false, "inputs": [ { "indexed": false, "name": "win", "type": "address" }, { "indexed": true, "name": "room", "type": "uint256" } ], "name": "Winner", "type": "event" }, { "anonymous": false, "inputs": [ { "indexed": true, "name": "room", "type": "uint256" }, { "indexed": false, "name": "res1", "type": "uint8" }, { "indexed": false, "name": "res2", "type": "uint8" } ], "name": "RoundFinished", "type": "event" }, { "anonymous": false, "inputs": [ { "indexed": false, "name": "win", "type": "address" }, { "indexed": false, "name": "amount", "type": "uint256" }, { "indexed": true, "name": "room", "type": "uint256" } ], "name": "Revard", "type": "event" }, { "constant": false, "inputs": [ { "name": "mreic", "type": "uint256" } ], "name": "setMaxReic", "outputs": [], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "anonymous": false, "inputs": [ { "indexed": true, "name": "room", "type": "uint256" } ], "name": "BetsFinished", "type": "event" }, { "anonymous": false, "inputs": [ { "indexed": true, "name": "room", "type": "uint256" }, { "indexed": true, "name": "player2", "type": "address" }, { "indexed": false, "name": "countdownTime", "type": "uint256" } ], "name": "JoinPlayer2", "type": "event" }, { "anonymous": false, "inputs": [ { "indexed": true, "name": "room", "type": "uint256" } ], "name": "RevertRoom", "type": "event" }, { "anonymous": false, "inputs": [ { "indexed": true, "name": "room", "type": "uint256" }, { "indexed": true, "name": "player1", "type": "address" } ], "name": "JoinPlayer1", "type": "event" }, { "anonymous": false, "inputs": [ { "indexed": true, "name": "room", "type": "uint256" } ], "name": "RoomClosed", "type": "event" }, { "anonymous": false, "inputs": [ { "indexed": true, "name": "room", "type": "uint256" }, { "indexed": true, "name": "player1", "type": "address" }, { "indexed": false, "name": "bet", "type": "uint256" }, { "indexed": false, "name": "counter", "type": "uint8" }, { "indexed": false, "name": "openedTime", "type": "uint256" }, { "indexed": true, "name": "privat", "type": "bool" } ], "name": "RoomOpened", "type": "event" }, { "anonymous": false, "inputs": [ { "indexed": true, "name": "from", "type": "address" }, { "indexed": true, "name": "room", "type": "uint256" } ], "name": "BetsAdd", "type": "event" }, { "constant": false, "inputs": [ { "name": "id", "type": "uint256" }, { "name": "seed", "type": "uint256" } ], "name": "setSeed", "outputs": [], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": false, "inputs": [], "name": "transferOutAll", "outputs": [], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": false, "inputs": [ { "name": "newOwner", "type": "address" } ], "name": "transferOwnership", "outputs": [], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "inputs": [], "payable": false, "stateMutability": "nonpayable", "type": "constructor" }, { "constant": true, "inputs": [ { "name": "id", "type": "uint256" } ], "name": "checkRoomBet", "outputs": [ { "name": "", "type": "uint256" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": true, "inputs": [ { "name": "id", "type": "uint256" } ], "name": "checkRoomBet1", "outputs": [ { "name": "", "type": "bytes32" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": true, "inputs": [ { "name": "id", "type": "uint256" } ], "name": "checkRoomBet2", "outputs": [ { "name": "", "type": "bytes32" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": true, "inputs": [ { "name": "id", "type": "uint256" } ], "name": "checkRoomCounter", "outputs": [ { "name": "", "type": "uint8" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": true, "inputs": [ { "name": "id", "type": "uint256" }, { "name": "player", "type": "address" } ], "name": "checkRoomIsBet", "outputs": [ { "name": "", "type": "bytes32" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": true, "inputs": [ { "name": "id", "type": "uint256" } ], "name": "checkRoomNotClosed", "outputs": [ { "name": "", "type": "bool" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": true, "inputs": [ { "name": "id", "type": "uint256" } ], "name": "checkRoomOpened", "outputs": [ { "name": "", "type": "bool" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": true, "inputs": [ { "name": "id", "type": "uint256" } ], "name": "checkRoomPlayer1", "outputs": [ { "name": "", "type": "address" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": true, "inputs": [ { "name": "id", "type": "uint256" } ], "name": "checkRoomPlayer2", "outputs": [ { "name": "", "type": "address" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": true, "inputs": [ { "name": "id", "type": "uint256" } ], "name": "checkRoomRes1", "outputs": [ { "name": "", "type": "uint8" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": true, "inputs": [ { "name": "id", "type": "uint256" } ], "name": "checkRoomRes2", "outputs": [ { "name": "", "type": "uint8" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": true, "inputs": [ { "name": "id", "type": "uint256" } ], "name": "checkRoomScore1", "outputs": [ { "name": "", "type": "uint8" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": true, "inputs": [ { "name": "id", "type": "uint256" } ], "name": "checkRoomScore2", "outputs": [ { "name": "", "type": "uint8" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": true, "inputs": [ { "name": "id", "type": "uint256" } ], "name": "checkRoomStartTime", "outputs": [ { "name": "", "type": "uint256" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": true, "inputs": [ { "name": "id", "type": "uint256" } ], "name": "checkSenderBet", "outputs": [ { "name": "", "type": "bytes32" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": true, "inputs": [], "name": "owner", "outputs": [ { "name": "", "type": "address" } ], "payable": false, "stateMutability": "view", "type": "function" } ]; var contr = web3.eth.contract(contrInterface); var contractInstance = contr.at(contrAdress); var address = web3.eth.defaultAccount; var block = 0; web3.eth.getBlockNumber( function(er, res){ if(res) block = res }); }
function setBet(uint id, bytes32 bet) public { require(msg.sender == rooms[id].player1 || msg.sender == rooms[id].player2 ); // if(rooms[id].startTime + 5 minutes > now) { if(msg.sender == rooms[id].player1) { rooms[id].res2 = 5; rooms[id].bit1 = bet; } else if(msg.sender == rooms[id].player2) { rooms[id].res1 = 5; rooms[id].bit2 = bet; } if(rooms[id].bit1 != 0x0 && rooms[id].bit2 != 0x0) { SeedOpened(id); BetsFinished(id); } BetsAdd(msg.sender , id); } else { Result(rooms[id].player1, rooms[id].res1, id); Result(rooms[id].player2, rooms[id].res2, id); } }
function setSeed(uint256 id, uint256 seed) public { require( rooms[id].bit2 != 0x0 && rooms[id].bit1 != 0x0 ); require(msg.sender == rooms[id].player1 || msg.sender == rooms[id].player2 ); // if(rooms[id].startTime + 5 minutes > now) { if(msg.sender == rooms[id].player1) decodeHash1(id, seed); else if(msg.sender == rooms[id].player2) decodeHash2(id, seed); } else { Result(rooms[id].player1, rooms[id].res1, id); Result(rooms[id].player2, rooms[id].res2, id); } }
function decodeHash1(uint id, uint seed) internal { uint e1 = seed + 1; bytes32 bitHash1a = keccak256(uintToString(e1)); uint e2 = seed + 2; bytes32 bitHash1b = keccak256(uintToString(e2)); uint e3 = seed + 3; bytes32 bitHash1c = keccak256(uintToString(e3)); if(rooms[id].bit1 == bitHash1a) rooms[id].res1 = 1; if(rooms[id].bit1 == bitHash1b) rooms[id].res1 = 2; if(rooms[id].bit1 == bitHash1c) rooms[id].res1 = 3; Result(rooms[id].player1, rooms[id].res1, id); // return res1; }
function uintToString(uint i) internal pure returns (string){ // bytes memory bstr = new bytes; if (i == 0) return "0"; uint j = i; uint length; while (j != 0){ length++; j /= 10; } bytes memory bstr = new bytes(length); uint k = length - 1; while (i != 0){ bstr[k--] = byte(48 + i % 10); i /= 10; } return string(bstr); }
function winRes(uint id) internal { require(rooms[id].res1 > 0 || rooms[id].res2 > 0); address win = 0x0; if(rooms[id].res1 == 1 && rooms[id].res2 == 2) win = rooms[id].player1; if(rooms[id].res1 == 1 && rooms[id].res2 == 3) win = rooms[id].player2; if(rooms[id].res1 == 2 && rooms[id].res2 == 1) win = rooms[id].player2; if(rooms[id].res1 == 2 && rooms[id].res2 == 3) win = rooms[id].player1; if(rooms[id].res1 == 3 && rooms[id].res2 == 1) win = rooms[id].player1; if(rooms[id].res1 == 3 && rooms[id].res2 == 2) win = rooms[id].player2; if(rooms[id].res1 == 4 && rooms[id].res2 != 4 ) win = rooms[id].player2; if(rooms[id].res2 == 4 && rooms[id].res1 != 4 ) win = rooms[id].player1; if(rooms[id].res1 == 5 && rooms[id].res2 != 5 ) win = rooms[id].player2; if(rooms[id].res2 == 5 && rooms[id].res1 != 5 ) win = rooms[id].player1; if((rooms[id].res2 == 4 && rooms[id].res1 == 4 ) || (rooms[id].res2 == 5 && rooms[id].res1 == 5 )) revertRoom(id); else { // - if(win == 0x0) { replay(id); OneMoreGame(id); } else { // if( win == rooms[id].player1 ) rooms[id].c1 += 1; if( win == rooms[id].player2 ) rooms[id].c2 += 1; // n- if( rooms[id].counter > 1 && rooms[id].c1 < rooms[id].counter && rooms[id].c2 < rooms[id].counter ) { ScoreChanged(id, rooms[id].c1, rooms[id].c2); replay(id); OneMoreGame(id); } else { // ScoreChanged(id, rooms[id].c1, rooms[id].c2); if( rooms[id].bet > 0 ) { rewardWin(win, id); } Winner(win, id); closeRoom(id); } } } }
Source: https://habr.com/ru/post/358772/
All Articles