pragma solidity ^0.4.24; contract Betting{ mapping(address => bool) owners; address[] private admins; constructor () public payable { owners[msg.sender] = true; admins.push(msg.sender); } struct Gamer{ uint id; uint idMatch; address player; string descriptions; bool isWin; uint256 money; uint256 rate; } mapping (uint=> Gamer) public gamers; uint gamerCounter; //Gamer place a bet function PlaceBet(uint _idMatch,string _descriptions,bool _isWin, uint256 _money, uint256 _rate) public payable returns(bool){ require(msg.sender!= 0x0 && msg.sender.balance >=_money); gamerCounter++; gamers[gamerCounter] = Gamer( gamerCounter, _idMatch, msg.sender, _descriptions, _isWin, _money, _rate ); } //Get amount gamer function getAllPlayers() external view returns (uint[]){ uint[] memory gamerIds = new uint[](gamerCounter); uint count = 0; for(uint i = 1; i <= gamerCounter; i++){ if (gamers[i].player!=0x0){ gamerIds[count] = gamers[i].id; count++; } uint[] memory fordishPlay = new uint[](count); for(uint j=0; j < count; j++){ fordishPlay[j] = gamerIds[j]; } } return fordishPlay; } //withdraw from smart cotract function withdraw() public onlyOwner{ msg.sender.transfer(address(this).balance); } //Pay winer function payWiners(uint _idMatch, bool _isWin) public onlyOwner returns(bool){ uint256[] memory idWiners = getWiners(_idMatch,_isWin); for(uint i = 0; i< idWiners.length; i++){ Gamer winer = gamers[idWiners[i]]; payWiner(winer); } return true; } //Get list gamer place bet function getNumberOfGamers() public view returns (uint){ return gamerCounter; } //////////////////////////////Function Addmin//////////////////////////////////// //Add Onwer to Contract function addOwners(address _newAddress) public returns (bool){ require(_newAddress != 0x0 && !owners[_newAddress] && owners[msg.sender]); owners[_newAddress] = true; admins.push(_newAddress); return true; } //View Owners function viewOwners() public onlyOwner view returns (address[]){ require(owners[msg.sender]); return admins; } //tranfer Eth from contract to Address function transferETHFromContract(address _to, uint256 _value) public onlyOwner { // allowance has big enough require(address(this).balance >= _value); _to.transfer(_value); } ////////////////////Private Function///////////////////////////////// //Return array id winer function getWiners(uint _idMatch, bool _isWin) public onlyOwner returns(uint256[]){ uint256[] memory gamerIds = new uint[](gamerCounter); uint numberWiner = 0; for(uint i = 0; i < gamerCounter; i++){ if (gamers[i].idMatch == _idMatch && gamers[i].isWin == _isWin){ gamerIds[numberWiner] = gamers[i].id; numberWiner++; } } uint256[] memory winerIds = new uint[](numberWiner); for(uint j = 0; j < numberWiner; j++){ winerIds[j] = gamerIds[j]; } return winerIds; } //Pay winer _money function payWiner(Gamer _gamer)private onlyOwner returns(bool){ uint256 value = _gamer.money * (100 + _gamer.rate)*(10**13); transferETHFromContract(_gamer.player,value); return true; } //require Owner modifier onlyOwner(){ require(owners[msg.sender]); _; } }
0.4.23