Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- pragma solidity ^0.4.24;
- import './Owned.sol';
- import './Team.sol';
- ////////////////////////////////////////////////////////////
- // TODO
- ////////////////////////////////////////////////////////////
- contract GameStation is Owned {
- ////////////////////////////////////////////////////////////
- // library
- using StringUtils for string;
- ////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////
- // varialbe
- // game
- Game[] private games;
- // notify next address
- string public notice;
- ////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////
- // struct
- // reporter change : condition, result
- struct Game {
- Team leftTeam;
- Team rightTeam;
- string title; // slogan and key
- string condition;
- string league;
- string matchLocation;
- uint256 matchDate;
- uint256 maxAmount;
- uint256 minAmount;
- address[] reporters;
- uint8[] results; // 0(even), 1(left), 2(right)
- }
- ////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////
- // modifier
- ////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////
- // event
- event DoWithdraw(address indexed _to, uint256 _value);
- ////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////
- constructor() public {
- owner = msg.sender;
- }
- function doWithdraw() public onlyOwner returns(uint256) {
- uint256 this_balance = address(this).balance;
- owner.transfer(this_balance);
- emit DoWithdraw(owner, this_balance);
- return this_balance;
- }
- ////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////
- // game control
- // (PLAN), OPEN, START, END, (CONFIRM, SETTLE, FAILCOUNT*), CLOSE
- function doOpen(string _title) public onlyOwner returns(bool) {
- require(bytes(_title).length > 0);
- int16 i_find = findGame(_title);
- require(i_find != -1);
- // condition
- if (games[uint256(i_find)].condition.equal("PLAN")) {
- games[uint256(i_find)].condition = "OPEN";
- } else {
- return false;
- }
- return true;
- }
- // by member and reporter
- function doStart(string _title) public returns(bool) {
- require(bytes(_title).length > 0);
- int16 i_find = findGame(_title);
- require(i_find != -1);
- // check reporter
- require(isReporter(_title, msg.sender) || isMember(msg.sender) || msg.sender == owner);
- // condition
- if (games[uint256(i_find)].condition.equal("OPEN")) {
- games[uint256(i_find)].condition = "START";
- } else {
- return false;
- }
- return true;
- }
- // by member and reporter
- // report result
- function doEnd(string _title, uint8 _result) public returns(bool) {
- require(bytes(_title).length > 0);
- int16 i_find = findGame(_title);
- require(i_find != -1);
- // check reporter
- require(isReporter(_title, msg.sender) || isMember(msg.sender) || msg.sender == owner);
- // condition
- if (games[uint256(i_find)].condition.equal("START")) {
- games[uint256(i_find)].condition = "END";
- }
- // regist
- if (isReporter(_title, msg.sender)) {
- registResult(_title, msg.sender, _result);
- }
- return true;
- }
- // check result
- // 0 : even, 1 : left, 2 : right, 3 : unknown (no reporter), 4 : not same
- function doConfirm(string _title) view public onlyMember returns(uint8 _result) {
- require(bytes(_title).length > 0);
- int16 i_find = findGame(_title);
- require(i_find != -1);
- // no reporter
- if (games[uint256(i_find)].reporters.length == 0) {
- return 3;
- }
- // check result
- uint8 i_result = games[uint256(i_find)].results[0];
- for (uint8 i = 1; i < games[uint256(i_find)].results.length; i++) {
- if (games[uint256(i_find)].results[i] == i_result) {
- } else {
- return 4;
- }
- }
- return i_result;
- }
- // only one time
- function doSettle(string _title, uint8 _self_result) public onlyOwner returns(bool) {
- require(bytes(_title).length > 0);
- int16 i_find = findGame(_title);
- require(i_find != -1);
- // condition
- require(games[uint256(i_find)].condition.equal("END"));
- // check confirm
- uint8 i_confirm = doConfirm(_title);
- if (i_confirm == 4) {
- // if not same, wait 24 hours
- if (block.timestamp < (games[uint256(i_find)].matchDate + 24 hours)) {
- // TODO : log
- return false;
- }
- i_confirm = _self_result;
- } else if (i_confirm == 3) {
- // no reporter
- games[uint256(i_find)].reporters.push(owner);
- games[uint256(i_find)].results.push(_self_result);
- i_confirm = _self_result;
- }
- // one more check
- if (i_confirm != _self_result) {
- return false;
- }
- address[] memory win_addr;
- uint256[] memory win_amount;
- uint256 win_balance;
- if (i_confirm == 0) {
- games[uint256(i_find)].leftTeam.doSettle(1);
- games[uint256(i_find)].rightTeam.doSettle(1);
- } else if (i_confirm == 1) {
- win_balance = address(games[uint256(i_find)].leftTeam).balance;
- (win_addr, win_amount) = games[uint256(i_find)].leftTeam.getClientAccounts();
- games[uint256(i_find)].leftTeam.doSettle(1);
- games[uint256(i_find)].rightTeam.doYield(1, win_addr, win_amount, win_balance);
- } else if (i_confirm == 2) {
- win_balance = address(games[uint256(i_find)].rightTeam).balance;
- (win_addr, win_amount) = games[uint256(i_find)].rightTeam.getClientAccounts();
- games[uint256(i_find)].rightTeam.doSettle(1);
- games[uint256(i_find)].leftTeam.doYield(1, win_addr, win_amount, win_balance);
- } else {
- return false;
- }
- return true;
- }
- // done by Team
- //function doFailAccount(string _title) public onlyOwner returns(bool) {}
- function doClose(string _title) public onlyOwner returns(bool) {
- require(bytes(_title).length > 0);
- int16 i_find = findGame(_title);
- require(i_find != -1);
- // condition
- require(games[uint256(i_find)].condition.equal("END"));
- games[uint256(i_find)].condition = "CLOSE";
- games[uint256(i_find)].leftTeam.setIsClosed(true);
- games[uint256(i_find)].rightTeam.setIsClosed(true);
- return true;
- }
- ////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////
- // Team call
- function getGameConditionAndLimit(string _title) view public returns(string, uint256, uint256) {
- require(bytes(_title).length > 0);
- int16 i_find = findGame(_title);
- require(i_find != -1);
- return (games[uint256(i_find)].condition, games[uint256(i_find)].maxAmount, games[uint256(i_find)].minAmount);
- }
- ////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////
- function getGameCount() view public returns(uint256) {
- return games.length;
- }
- //string _title, string _condition, string _league,
- //address _leftTeam, address _rightTeam,
- //uint256 _matchDate, string _matchLocation,
- //uint256 _maxAmount, uint256 _minAmount
- function getGameInfo(uint256 _idx) view public
- returns(string _title, string _condition, string _league,
- address _leftTeam, address _rightTeam, uint256 _matchDate, string _matchLocation,
- uint256 _maxAmount, uint256 _minAmount) {
- require(games.length > 0 && _idx < games.length);
- _title = games[_idx].title;
- _condition = games[_idx].condition;
- _league = games[_idx].league;
- _leftTeam = games[_idx].leftTeam;
- _rightTeam = games[_idx].rightTeam;
- _matchDate = games[_idx].matchDate;
- _matchLocation = games[_idx].matchLocation;
- _maxAmount = games[_idx].maxAmount;
- _minAmount = games[_idx].minAmount;
- }
- ////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////
- // game
- function addGame(string _title, string _league,
- address _left_team, address _right_team, uint256 _match_after_hours, string _match_location,
- uint256 _min_amount, uint256 _max_amount, uint8 _idx)
- public onlyMember returns(bool) {
- require(bytes(_title).length > 0 && bytes(_league).length > 0 && _left_team != 0 && _right_team != 0);
- //int16 i_find = findGame(_title);
- require(findGame(_title) == -1);
- // min : 1 / 1000, max : 10 * 1
- if (_min_amount == 0) _min_amount = 1000 * 1000 * 1000 * 1000 * 1000;
- if (_max_amount == 0) _max_amount = 10 * 1000 * 1000 * 1000 * 1000 * 1000 * 1000;
- // create
- Game memory game;
- game.title = _title;
- game.condition = "PLAN";
- game.league = _league;
- game.leftTeam = Team(_left_team);
- game.rightTeam = Team(_right_team);
- //game.matchDate = _match_date;
- if (_match_after_hours == 0) {
- game.matchDate = block.timestamp + 24 hours;
- } else {
- game.matchDate = block.timestamp + _match_after_hours * 1 hours;
- }
- game.matchLocation = _match_location;
- game.minAmount = _min_amount;
- game.maxAmount = _max_amount;
- // idx
- if (games.length == 0 || _idx == games.length) {
- games.push(game);
- } else {
- Game memory tmp_new = game;
- Game memory tmp_old = games[_idx];
- for (uint256 i = uint256(_idx); i < games.length - 1; i++) {
- games[i] = tmp_new;
- tmp_new = tmp_old;
- tmp_old = games[i + 1];
- }
- games[games.length - 1] = tmp_new;
- games.push(tmp_old);
- }
- // Team
- Team(_left_team).setTitle(_title);
- Team(_right_team).setTitle(_title);
- return true;
- }
- function removeGame(string _title) public onlyOwner returns(bool) {
- require(bytes(_title).length > 0);
- int16 i_find = findGame(_title);
- require(i_find != -1);
- for (uint256 i = uint256(i_find); i < games.length - 1; i++) {
- if (i == uint256(i_find)) {
- i_find++;
- }
- games[i] = games[uint256(i_find)];
- i_find++;
- }
- games.length--;
- return true;
- }
- ////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////
- // game update
- function updateReporters(string _title, address[] _reporters) public onlyMember
- returns(address[] __reporters, uint8[] __results) {
- require(bytes(_title).length > 0);
- int16 i_find = findGame(_title);
- require(i_find != -1);
- require(games[uint256(i_find)].condition.equal("PLAN"));
- if (_reporters.length == 0) {
- } else {
- games[uint256(i_find)].reporters = _reporters;
- games[uint256(i_find)].results = new uint8[](_reporters.length);
- }
- __reporters = games[uint256(i_find)].reporters;
- __results = games[uint256(i_find)].results;
- }
- function updateNotice(string _notice) public onlyOwner {
- //require(bytes(_notice).length > 0);
- notice = _notice;
- }
- ////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////
- // private
- function findGame(string _title) view private returns(int16) {
- if (games.length == 0) return -1;
- for (uint256 i = 0; i < games.length; i++) {
- //emit TestLog(games[i].title, _title);
- if (games[i].title.equal(_title)) {
- return int16(i);
- }
- }
- return -1;
- }
- function isReporter(string _title, address _addr) view private returns(bool) {
- int16 i_find = findGame(_title);
- for (uint256 i = 0; i < games[uint256(i_find)].reporters.length; i++) {
- if (games[uint256(i_find)].reporters[i] == _addr) {
- return true;
- }
- }
- return false;
- }
- function registResult(string _title, address _addr, uint8 _result) private returns(bool) {
- int16 i_find = findGame(_title);
- int16 i_find_reporter = -1;
- for (uint256 i = 0; i < games[uint256(i_find)].reporters.length; i++) {
- if (games[uint256(i_find)].reporters[i] == _addr) {
- i_find_reporter = int16(i);
- break;
- }
- }
- if (i_find_reporter != -1) {
- games[uint256(i_find)].results[uint256(i_find_reporter)] = _result;
- }
- return true;
- }
- ////////////////////////////////////////////////////////////
- }
Add Comment
Please, Sign In to add comment