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
- // confirm min/max amount
- contract GameStation is Owned {
- ////////////////////////////////////////////////////////////
- // library
- ////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////
- // varialbe
- // basic
- string[] public leagues; // NFL MLB NBA NHL WorldCup Premier League
- // game
- Game[] public games;
- // notify next address
- string public notice;
- ////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////
- // struct
- // when PLAN, change : title, league, match*, max/min, reporters
- // reporter : condition, result
- struct Game {
- string title; // slogan and key
- string condition;
- string league;
- Team leftTeam;
- Team rightTeam;
- uint256 matchDate;
- string matchLocation;
- uint256 maxAmount;
- uint256 minAmount;
- address[] reporters;
- uint8[] result; // 0(even), 1(left), 2(right)
- }
- ////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////
- // modifier
- ////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////
- // event
- event DoPlan(string _title, string _cmd);
- event DoOpen(string _title, string _cmd);
- event DoStart(string _title, string _cmd, address _addr);
- event DoEnd(string _title, string _cmd, uint8 _result, address _addr);
- event DoConfirm(string _title, string _cmd);
- event DoSettle(string _title, string _cmd, uint8 _self_result);
- event DoClose(string _title, string _cmd);
- ////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////
- constructor() public {
- emit DoPlan("constructor start", "test1");
- owner = msg.sender;
- emit DoPlan("constructor start2", "test12");
- }
- ////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////
- // game control
- // PLAN, OPEN, START, END, (CONFIRM, SETTLE, FAILCOUNT*), CLOSE
- function doPlan(string _title) public onlyMember returns(bool) {
- require(bytes(_title).length > 0);
- int16 i_find = findGame(_title);
- require(i_find != -1);
- // condition
- string memory tmp_str = games[uint256(i_find)].condition;
- if (tmp_str.equal("")) {
- games[uint256(i_find)].condition = "PLAN";
- emit DoPlan(_title, "PLAN");
- } else {
- return false;
- }
- return true;
- }
- function doOpen(string _title) public onlyOwner returns(bool) {
- require(bytes(_title).length > 0);
- int16 i_find = findGame(_title);
- require(i_find != -1);
- // condition
- string memory tmp_str = games[uint256(i_find)].condition;
- if (tmp_str.equal("") || tmp_str.equal("PLAN")) {
- games[uint256(i_find)].condition = "OPEN";
- emit DoOpen(_title, "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(isMember(msg.sender) || isReporter(_title, msg.sender));
- // condition
- string memory tmp_str = games[uint256(i_find)].condition;
- if (tmp_str.equal("OPEN")) {
- games[uint256(i_find)].condition = "START";
- emit DoStart(_title, "START", msg.sender);
- } 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(isMember(msg.sender) || isReporter(_title, msg.sender));
- // condition
- string memory tmp_str = games[uint256(i_find)].condition;
- if (tmp_str.equal("START")) {
- games[uint256(i_find)].condition = "END";
- }
- // regist
- if (isReporter(_title, msg.sender)) {
- registResult(_title, msg.sender, _result);
- }
- emit DoEnd(_title, "END", _result, msg.sender);
- 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) {
- require(bytes(_title).length > 0);
- int16 i_find = findGame(_title);
- require(i_find != -1);
- // condition
- string memory tmp_str = games[uint256(i_find)].condition;
- require(tmp_str.equal("END"));
- // no reporter
- if (games[uint256(i_find)].reporters.length == 0) {
- return 3;
- }
- // check result
- uint8 i_result = games[uint256(i_find)].result[0];
- for (uint8 i = 1; i < games[uint256(i_find)].result.length; i++) {
- if (games[uint256(i_find)].result[i] == i_result) {
- } else {
- return 4;
- }
- }
- //emit DoConfirm(_title, "CONFIRM");
- 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
- string memory tmp_str = games[uint256(i_find)].condition;
- require(tmp_str.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) {
- games[uint256(i_find)].reporters.push(owner);
- games[uint256(i_find)].result.push(_self_result);
- i_confirm = _self_result;
- }
- // one more check
- if (i_confirm != _self_result) {
- // TODO : log
- 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;
- }
- emit DoSettle(_title, "SETTLE", _self_result);
- 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
- string memory tmp_str = games[uint256(i_find)].condition;
- require(tmp_str.equal("END"));
- games[uint256(i_find)].condition = "CLOSE";
- games[uint256(i_find)].leftTeam.setIsClosed(true);
- games[uint256(i_find)].rightTeam.setIsClosed(true);
- emit DoClose(_title, "CLOSE");
- return true;
- }
- ////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////
- // Team call
- function getGameCondition(string _title) view public returns(string) {
- require(bytes(_title).length > 0);
- int16 i_find = findLeague(_title);
- require(i_find == -1);
- return games[uint256(i_find)].condition;
- }
- function getGameInfo(string _title) view public returns(string _condition, string _league,
- Team _left_team, Team _right_team, uint256 _match_date, string _match_location,
- uint256 _max_amount, uint256 _min_amount, address[] _reporters) {
- require(bytes(_title).length > 0);
- int16 i_find = findLeague(_title);
- require(i_find == -1);
- _condition = games[uint256(i_find)].condition;
- _league = games[uint256(i_find)].league;
- _left_team = games[uint256(i_find)].leftTeam;
- _right_team = games[uint256(i_find)].rightTeam;
- _match_date = games[uint256(i_find)].matchDate;
- _match_location = games[uint256(i_find)].matchLocation;
- _max_amount = games[uint256(i_find)].maxAmount;
- _min_amount = games[uint256(i_find)].minAmount;
- _reporters = games[uint256(i_find)].reporters;
- }
- ////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////
- // league
- function addLeague(string _title, uint8 _idx) public onlyOwner returns(bool) {
- require(bytes(_title).length > 0);
- require(_idx >= 0);
- require(_idx <= leagues.length);
- int16 i_find = findLeague(_title);
- require(i_find == -1);
- // idx
- if (leagues.length == 0 || _idx == leagues.length) {
- leagues.push(_title);
- } else {
- string memory tmp_new = _title;
- string memory tmp_old = leagues[_idx];
- for (uint256 i = uint256(_idx); i < leagues.length - 1; i++) {
- leagues[i] = tmp_new;
- tmp_new = tmp_old;
- tmp_old = leagues[i + 1];
- }
- leagues[leagues.length] = tmp_new;
- leagues.push(tmp_old);
- }
- return true;
- }
- function moveLeague(string _title, uint16 _to_idx) public onlyMember returns(bool) {
- require(bytes(_title).length > 0);
- require(_to_idx >= 0);
- require(_to_idx < leagues.length);
- int16 i_find = findLeague(_title);
- require(i_find != -1);
- require(uint16(i_find) != _to_idx);
- string memory tmp_move = leagues[_to_idx];
- leagues[_to_idx] = leagues[uint256(i_find)];
- leagues[uint256(i_find)] = tmp_move;
- return true;
- }
- // WARNING : consider games' league
- function removeLeague(string _title) public onlyOwner returns(bool) {
- require(bytes(_title).length > 0);
- int16 i_find = findLeague(_title);
- require(i_find != -1);
- for (uint256 i = uint256(i_find); i < leagues.length - 1; i++) {
- if (i == uint256(i_find)) {
- i_find++;
- }
- leagues[i] = leagues[uint256(i_find)];
- i_find++;
- }
- leagues.length--;
- return true;
- }
- // WARNING : consider games' league
- function renameLeague(string _title_old, string _title_new) public onlyOwner returns(bool) {
- require(bytes(_title_old).length > 0);
- require(bytes(_title_new).length > 0);
- int16 i_find = findLeague(_title_old);
- require(i_find != -1);
- for (uint256 i = 0; i < leagues.length; i++) {
- if (leagues[i].equal(_title_old)) {
- leagues[i] = _title_new;
- break;
- }
- }
- return true;
- }
- ////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////
- // game
- function addGame(string _title, string _condition, string _league,
- address _left_team, address _right_team, uint256 _match_date, string _match_location, address[] _reporters, uint8 _idx)
- public onlyMember returns(bool) {
- require(bytes(_title).length > 0 && bytes(_condition).length > 0 && bytes(_league).length > 0
- && _left_team != 0 && _right_team != 0);
- //int16 i_find = findGame(_title);
- require(findGame(_title) == -1);
- //int16 i_find_league = findLeague(_league);
- require(findLeague(_league) != -1);
- // create
- Game memory game;
- game.title = _title;
- game.condition = _condition;
- game.league = _league;
- game.leftTeam = Team(_left_team);
- game.rightTeam = Team(_right_team);
- game.matchDate = _match_date;
- game.matchLocation = _match_location;
- game.minAmount = 1000 * 1000 * 1000 * 1000 * 1000;
- game.maxAmount = game.minAmount * 1000;
- game.reporters = _reporters;
- game.result = new uint8[](_reporters.length);
- // 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] = tmp_new;
- games.push(tmp_old);
- }
- // Team
- game.leftTeam.setTitle(_title);
- game.rightTeam.setTitle(_title);
- return true;
- }
- function moveGame(string _title, uint16 _to_idx) public onlyMember returns(bool) {
- require(bytes(_title).length > 0);
- require(_to_idx >= 0);
- require(_to_idx < games.length);
- int16 i_find = findGame(_title);
- require(i_find != -1);
- require(uint16(i_find) != _to_idx);
- Game memory tmp_move = games[_to_idx];
- games[_to_idx] = games[uint256(i_find)];
- games[uint256(i_find)] = tmp_move;
- 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 updateGameExt(string _title, uint256 _match_date, string _match_location,
- uint256 _max_amount, uint256 _min_amount) public onlyMember returns(bool) {
- require(bytes(_title).length > 0);
- int16 i_find = findGame(_title);
- require(i_find != -1);
- require(games[uint256(i_find)].condition.equal("PLAN"));
- games[uint256(i_find)].matchDate = _match_date;
- games[uint256(i_find)].matchLocation = _match_location;
- games[uint256(i_find)].maxAmount = _max_amount;
- games[uint256(i_find)].minAmount = _min_amount;
- return true;
- }
- function updateGameTitle(string _title_old, string _title_new) public onlyMember returns(bool) {
- require(bytes(_title_old).length > 0 && bytes(_title_new).length > 0);
- int16 i_find = findGame(_title_old);
- require(i_find != -1);
- require(games[uint256(i_find)].condition.equal("PLAN"));
- games[uint256(i_find)].title = _title_new;
- // Team
- games[uint256(i_find)].leftTeam.setTitle(_title_new);
- games[uint256(i_find)].rightTeam.setTitle(_title_new);
- return true;
- }
- function updateGameLeague(string _title, string _league) public onlyMember returns(bool) {
- require(bytes(_title).length > 0 && bytes(_league).length > 0);
- int16 i_find = findGame(_title);
- require(i_find != -1);
- require(games[uint256(i_find)].condition.equal("PLAN"));
- int16 i_find_league = findLeague(_league);
- require(i_find_league != -1);
- games[uint256(i_find)].league = _league;
- return true;
- }
- function updateGameReporters(string _title, address[] _reporters) public onlyMember returns(bool) {
- require(bytes(_title).length > 0);
- int16 i_find = findGame(_title);
- require(i_find != -1);
- require(games[uint256(i_find)].condition.equal("PLAN"));
- games[uint256(i_find)].reporters = _reporters;
- games[uint256(i_find)].result = new uint8[](_reporters.length);
- return true;
- }
- function updateGameTeam(string _title, address _left_team, address _right_team)
- public onlyOwner returns(bool) {
- require(bytes(_title).length > 0);
- require(_left_team != 0 && _right_team != 0);
- int16 i_find = findGame(_title);
- require(i_find != -1);
- require(games[uint256(i_find)].condition.equal("PLAN"));
- games[uint256(i_find)].leftTeam = Team(_left_team);
- games[uint256(i_find)].rightTeam = Team(_right_team);
- return true;
- }
- function updateNotice(string _notice) public onlyOwner returns(bool) {
- //require(bytes(_notice).length > 0);
- notice = _notice;
- return true;
- }
- ////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////
- // private
- function findLeague(string _name) view private onlyMember returns(int16) {
- if (leagues.length == 0) return -1;
- for (uint256 i = 0; i < leagues.length; i++) {
- if (leagues[i].equal(_name)) {
- return int16(i);
- }
- }
- return -1;
- }
- function findGame(string _title) view private onlyMember 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(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)].result[uint256(i_find_reporter)] = _result;
- }
- return true;
- }
- ////////////////////////////////////////////////////////////
- }
Add Comment
Please, Sign In to add comment