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;
- ////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////
- // variable
- // game
- Game[] private games;
- // notify next address
- string private notice;
- // when not reported or not same
- uint8 private retryCnt = 0;
- ////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////
- // struct
- // reporter change : condition, result
- struct Game {
- Team leftTeam;
- Team rightTeam;
- string title; // slogan and key
- string condition;
- string league;
- string matchLocation;
- // TODO : until date util
- uint256 matchDate;
- uint256 maxAmount;
- uint256 minAmount;
- address[] reporters;
- int8[] results; // -1(default), 0(even), 1(left), 2(right)
- }
- ////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////
- // modifier
- ////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////
- // event
- event TestTime(uint256 _time);
- ////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////
- constructor() public {
- owner = msg.sender;
- }
- ////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////
- // game control
- // (PLAN), OPEN, START, END, (CONFIRM, SETTLE, FAILCOUNT*), CLOSE
- function doOpenAndStart(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 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(uint256(i_find), msg.sender) || msg.sender == owner);
- // condition
- if (games[uint256(i_find)].condition.equal("START")) {
- games[uint256(i_find)].condition = "END";
- }
- // if else (CLOSE), skip
- // regist
- if (isReporter(uint256(i_find), msg.sender)) {
- registResult(uint256(i_find), msg.sender, _result);
- }
- return true;
- }
- // check result
- // -1 : not yet, 0 : even, 1 : left, 2 : right, 3 : no reporter, 4 : not same
- function doConfirm(string _title) view public returns(int8 _result) {
- require(bytes(_title).length > 0);
- int16 i_find = findGame(_title);
- require(i_find != -1);
- // check reporter
- require(isReporter(uint256(i_find), msg.sender) || msg.sender == owner);
- // no reporter
- if (games[uint256(i_find)].reporters.length == 0) {
- return 3;
- }
- // check result
- int8 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 int8(i_result);
- }
- // only one time
- function doSettle(string _title, int8 _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
- int8 i_confirm = doConfirm(_title);
- if (i_confirm == -1 || i_confirm == 4) {
- // if not same, wait 24 hours
- if (block.timestamp < (games[uint256(i_find)].matchDate + 24 hours)) {
- return false;
- }
- // last safety
- if (retryCnt < 3) return false;
- retryCnt++;
- i_confirm = _self_result;
- } else if (i_confirm == 3) {
- // no reporter
- games[uint256(i_find)].reporters.push(owner);
- games[uint256(i_find)].results.push(int8(_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;
- }
- games[uint256(i_find)].condition = "CLOSE";
- return true;
- }
- ////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////
- // Team call
- function getGameCondition(string _title) view public returns(string) {
- require(bytes(_title).length > 0);
- int16 i_find = findGame(_title);
- require(i_find != -1);
- return games[uint256(i_find)].condition;
- }
- ////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////
- // client call
- 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;
- }
- function getNotice() view public returns(string _notice) {
- _notice = notice;
- }
- function getTeamInfo(address _addr) view public
- returns(string _title, string _name, string _abbreviation, string _region,
- string _slogan, string _country, string _url) {
- Team team = Team(_addr);
- (_title, _name, _abbreviation,
- _region, _slogan, _country, _url) = team.getTeamInfo();
- }
- ////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////
- // game
- function addGame(string _title, string _league,
- address _left_team, address _right_team, uint256 _match_date, string _match_location,
- uint256 _min_amount, uint256 _max_amount, int8 _idx)
- public onlyOwner returns(bool) {
- require(bytes(_title).length > 0 && bytes(_league).length > 0
- && _left_team != 0 && _right_team != 0
- && _match_date > block.timestamp && _match_date < (block.timestamp + 90 days));
- //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;
- game.matchLocation = _match_location;
- game.minAmount = _min_amount;
- game.maxAmount = _max_amount;
- // idx
- if (games.length == 0 || _idx == -1 || _idx == int8(games.length)) {
- games.push(game);
- } else {
- Game memory tmp_new = game;
- Game memory tmp_old = games[uint256(_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).setTeamData(_title, _max_amount, _min_amount);
- Team(_right_team).setTeamData(_title, _max_amount, _min_amount);
- 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;
- }
- */
- function removeGame(uint8[] _titles) public onlyOwner returns(bool) {
- require(_titles.length > 0);
- for (uint256 i_a = 0; i_a < _titles.length; i_a++) {
- if (_titles[i_a] < 0 || _titles[i_a] >= games.length) continue;
- int16 i_find = _titles[i_a];
- for (uint256 i = _titles[i_a]; 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 onlyOwner
- returns(address[] __reporters, int8[] __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 int8[](_reporters.length);
- for (uint8 i = 0; i < _reporters.length; i++) {
- games[uint256(i_find)].results[i] = -1;
- }
- }
- __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++) {
- if (games[i].title.equal(_title)) {
- return int16(i);
- }
- }
- return -1;
- }
- function isReporter(uint256 i_find, address _addr) view private returns(bool) {
- 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(uint256 i_find, address _addr, uint8 _result) private returns(bool) {
- 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)] = int8(_result);
- }
- return true;
- }
- ////////////////////////////////////////////////////////////
- }
Add Comment
Please, Sign In to add comment