Guest User

Untitled

a guest
Jun 20th, 2018
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 20.56 KB | None | 0 0
  1. pragma solidity ^0.4.24;
  2.  
  3. import './Owned.sol';
  4. import './Team.sol';
  5.  
  6. // TODO
  7. // confirm min/max amount
  8.  
  9. contract GameStation is Owned {
  10.  
  11. ////////////////////////////////////////////////////////////
  12. // library
  13. ////////////////////////////////////////////////////////////
  14.  
  15. ////////////////////////////////////////////////////////////
  16. // varialbe
  17. // basic
  18. string[] public leagues; // NFL MLB NBA NHL WorldCup Premier League
  19.  
  20. // game
  21. Game[] public games;
  22.  
  23. // notify next address
  24. string public notice;
  25. ////////////////////////////////////////////////////////////
  26.  
  27. ////////////////////////////////////////////////////////////
  28. // struct
  29. // when PLAN, change : title, league, match*, max/min, reporters
  30. // reporter : condition, result
  31. struct Game {
  32. string title; // slogan and key
  33. string condition;
  34. string league;
  35. Team leftTeam;
  36. Team rightTeam;
  37.  
  38. uint256 matchDate;
  39. string matchLocation;
  40. uint256 maxAmount;
  41. uint256 minAmount;
  42.  
  43. address[] reporters;
  44. uint8[] result; // 0(even), 1(left), 2(right)
  45. }
  46. ////////////////////////////////////////////////////////////
  47.  
  48. ////////////////////////////////////////////////////////////
  49. // modifier
  50. ////////////////////////////////////////////////////////////
  51.  
  52. ////////////////////////////////////////////////////////////
  53. // event
  54. event DoPlan(string _title, string _cmd);
  55. event DoOpen(string _title, string _cmd);
  56. event DoStart(string _title, string _cmd, address _addr);
  57. event DoEnd(string _title, string _cmd, uint8 _result, address _addr);
  58. event DoConfirm(string _title, string _cmd);
  59. event DoSettle(string _title, string _cmd, uint8 _self_result);
  60. event DoClose(string _title, string _cmd);
  61. ////////////////////////////////////////////////////////////
  62.  
  63. ////////////////////////////////////////////////////////////
  64. constructor() public {
  65. emit DoPlan("constructor start", "test1");
  66. owner = msg.sender;
  67.  
  68. emit DoPlan("constructor start2", "test12");
  69. }
  70. ////////////////////////////////////////////////////////////
  71.  
  72. ////////////////////////////////////////////////////////////
  73. ////////////////////////////////////////////////////////////
  74. ////////////////////////////////////////////////////////////
  75. // game control
  76. // PLAN, OPEN, START, END, (CONFIRM, SETTLE, FAILCOUNT*), CLOSE
  77. function doPlan(string _title) public onlyMember returns(bool) {
  78. require(bytes(_title).length > 0);
  79.  
  80. int16 i_find = findGame(_title);
  81. require(i_find != -1);
  82.  
  83. // condition
  84. string memory tmp_str = games[uint256(i_find)].condition;
  85. if (tmp_str.equal("")) {
  86. games[uint256(i_find)].condition = "PLAN";
  87.  
  88. emit DoPlan(_title, "PLAN");
  89. } else {
  90. return false;
  91. }
  92.  
  93.  
  94. return true;
  95. }
  96. function doOpen(string _title) public onlyOwner returns(bool) {
  97. require(bytes(_title).length > 0);
  98.  
  99. int16 i_find = findGame(_title);
  100. require(i_find != -1);
  101.  
  102. // condition
  103. string memory tmp_str = games[uint256(i_find)].condition;
  104. if (tmp_str.equal("") || tmp_str.equal("PLAN")) {
  105. games[uint256(i_find)].condition = "OPEN";
  106.  
  107. emit DoOpen(_title, "OPEN");
  108. } else {
  109. return false;
  110. }
  111.  
  112. return true;
  113. }
  114. // by member and reporter
  115. function doStart(string _title) public returns(bool) {
  116. require(bytes(_title).length > 0);
  117.  
  118. int16 i_find = findGame(_title);
  119. require(i_find != -1);
  120.  
  121. // check reporter
  122. require(isMember(msg.sender) || isReporter(_title, msg.sender));
  123.  
  124. // condition
  125. string memory tmp_str = games[uint256(i_find)].condition;
  126. if (tmp_str.equal("OPEN")) {
  127. games[uint256(i_find)].condition = "START";
  128.  
  129. emit DoStart(_title, "START", msg.sender);
  130. } else {
  131. return false;
  132. }
  133.  
  134. return true;
  135. }
  136. // by member and reporter
  137. // report result
  138. function doEnd(string _title, uint8 _result) public returns(bool) {
  139. require(bytes(_title).length > 0);
  140.  
  141. int16 i_find = findGame(_title);
  142. require(i_find != -1);
  143.  
  144. // check reporter
  145. require(isMember(msg.sender) || isReporter(_title, msg.sender));
  146.  
  147. // condition
  148. string memory tmp_str = games[uint256(i_find)].condition;
  149. if (tmp_str.equal("START")) {
  150. games[uint256(i_find)].condition = "END";
  151. }
  152.  
  153. // regist
  154. if (isReporter(_title, msg.sender)) {
  155. registResult(_title, msg.sender, _result);
  156. }
  157.  
  158. emit DoEnd(_title, "END", _result, msg.sender);
  159. return true;
  160. }
  161. // check result
  162. // 0 : even, 1 : left, 2 : right, 3 : unknown (no reporter), 4 : not same
  163. function doConfirm(string _title) view public onlyMember returns(uint8) {
  164. require(bytes(_title).length > 0);
  165.  
  166. int16 i_find = findGame(_title);
  167. require(i_find != -1);
  168.  
  169. // condition
  170. string memory tmp_str = games[uint256(i_find)].condition;
  171. require(tmp_str.equal("END"));
  172.  
  173. // no reporter
  174. if (games[uint256(i_find)].reporters.length == 0) {
  175. return 3;
  176. }
  177.  
  178. // check result
  179. uint8 i_result = games[uint256(i_find)].result[0];
  180. for (uint8 i = 1; i < games[uint256(i_find)].result.length; i++) {
  181. if (games[uint256(i_find)].result[i] == i_result) {
  182. } else {
  183. return 4;
  184. }
  185. }
  186.  
  187. //emit DoConfirm(_title, "CONFIRM");
  188. return i_result;
  189. }
  190. // only one time
  191. function doSettle(string _title, uint8 _self_result) public onlyOwner returns(bool) {
  192. require(bytes(_title).length > 0);
  193.  
  194. int16 i_find = findGame(_title);
  195. require(i_find != -1);
  196.  
  197. // condition
  198. string memory tmp_str = games[uint256(i_find)].condition;
  199. require(tmp_str.equal("END"));
  200.  
  201. // check confirm
  202. uint8 i_confirm = doConfirm(_title);
  203. if (i_confirm == 4) {
  204. // if not same, wait 24 hours
  205. if (block.timestamp < (games[uint256(i_find)].matchDate + 24 hours)) {
  206. // TODO : log
  207. return false;
  208. }
  209. i_confirm = _self_result;
  210. } else if (i_confirm == 3) {
  211. games[uint256(i_find)].reporters.push(owner);
  212. games[uint256(i_find)].result.push(_self_result);
  213. i_confirm = _self_result;
  214. }
  215.  
  216. // one more check
  217. if (i_confirm != _self_result) {
  218. // TODO : log
  219. return false;
  220. }
  221.  
  222. address[] memory win_addr;
  223. uint256[] memory win_amount;
  224. uint256 win_balance;
  225. if (i_confirm == 0) {
  226. games[uint256(i_find)].leftTeam.doSettle(1);
  227. games[uint256(i_find)].rightTeam.doSettle(1);
  228.  
  229. } else if (i_confirm == 1) {
  230. win_balance = address(games[uint256(i_find)].leftTeam).balance;
  231. (win_addr, win_amount) = games[uint256(i_find)].leftTeam.getClientAccounts();
  232.  
  233. games[uint256(i_find)].leftTeam.doSettle(1);
  234.  
  235. games[uint256(i_find)].rightTeam.doYield(1, win_addr, win_amount, win_balance);
  236.  
  237. } else if (i_confirm == 2) {
  238. win_balance = address(games[uint256(i_find)].rightTeam).balance;
  239. (win_addr, win_amount) = games[uint256(i_find)].rightTeam.getClientAccounts();
  240.  
  241. games[uint256(i_find)].rightTeam.doSettle(1);
  242.  
  243. games[uint256(i_find)].leftTeam.doYield(1, win_addr, win_amount, win_balance);
  244.  
  245. } else {
  246. return false;
  247. }
  248.  
  249. emit DoSettle(_title, "SETTLE", _self_result);
  250. return true;
  251. }
  252. // done by Team
  253. //function doFailAccount(string _title) public onlyOwner returns(bool) {}
  254. function doClose(string _title) public onlyOwner returns(bool) {
  255. require(bytes(_title).length > 0);
  256.  
  257. int16 i_find = findGame(_title);
  258. require(i_find != -1);
  259.  
  260. // condition
  261. string memory tmp_str = games[uint256(i_find)].condition;
  262. require(tmp_str.equal("END"));
  263.  
  264. games[uint256(i_find)].condition = "CLOSE";
  265. games[uint256(i_find)].leftTeam.setIsClosed(true);
  266. games[uint256(i_find)].rightTeam.setIsClosed(true);
  267.  
  268. emit DoClose(_title, "CLOSE");
  269. return true;
  270. }
  271. ////////////////////////////////////////////////////////////
  272. ////////////////////////////////////////////////////////////
  273. ////////////////////////////////////////////////////////////
  274.  
  275. ////////////////////////////////////////////////////////////
  276. ////////////////////////////////////////////////////////////
  277. ////////////////////////////////////////////////////////////
  278. // Team call
  279. function getGameCondition(string _title) view public returns(string) {
  280. require(bytes(_title).length > 0);
  281.  
  282. int16 i_find = findLeague(_title);
  283. require(i_find == -1);
  284.  
  285. return games[uint256(i_find)].condition;
  286. }
  287. function getGameInfo(string _title) view public returns(string _condition, string _league,
  288. Team _left_team, Team _right_team, uint256 _match_date, string _match_location,
  289. uint256 _max_amount, uint256 _min_amount, address[] _reporters) {
  290. require(bytes(_title).length > 0);
  291.  
  292. int16 i_find = findLeague(_title);
  293. require(i_find == -1);
  294.  
  295. _condition = games[uint256(i_find)].condition;
  296. _league = games[uint256(i_find)].league;
  297. _left_team = games[uint256(i_find)].leftTeam;
  298. _right_team = games[uint256(i_find)].rightTeam;
  299. _match_date = games[uint256(i_find)].matchDate;
  300. _match_location = games[uint256(i_find)].matchLocation;
  301. _max_amount = games[uint256(i_find)].maxAmount;
  302. _min_amount = games[uint256(i_find)].minAmount;
  303. _reporters = games[uint256(i_find)].reporters;
  304. }
  305. ////////////////////////////////////////////////////////////
  306. ////////////////////////////////////////////////////////////
  307. ////////////////////////////////////////////////////////////
  308.  
  309. ////////////////////////////////////////////////////////////
  310. // league
  311. function addLeague(string _title, uint8 _idx) public onlyOwner returns(bool) {
  312. require(bytes(_title).length > 0);
  313. require(_idx >= 0);
  314. require(_idx <= leagues.length);
  315.  
  316. int16 i_find = findLeague(_title);
  317. require(i_find == -1);
  318.  
  319. // idx
  320. if (leagues.length == 0 || _idx == leagues.length) {
  321. leagues.push(_title);
  322. } else {
  323. string memory tmp_new = _title;
  324. string memory tmp_old = leagues[_idx];
  325. for (uint256 i = uint256(_idx); i < leagues.length - 1; i++) {
  326. leagues[i] = tmp_new;
  327. tmp_new = tmp_old;
  328. tmp_old = leagues[i + 1];
  329. }
  330. leagues[leagues.length] = tmp_new;
  331. leagues.push(tmp_old);
  332. }
  333.  
  334. return true;
  335. }
  336. function moveLeague(string _title, uint16 _to_idx) public onlyMember returns(bool) {
  337. require(bytes(_title).length > 0);
  338. require(_to_idx >= 0);
  339.  
  340. require(_to_idx < leagues.length);
  341. int16 i_find = findLeague(_title);
  342. require(i_find != -1);
  343. require(uint16(i_find) != _to_idx);
  344.  
  345. string memory tmp_move = leagues[_to_idx];
  346. leagues[_to_idx] = leagues[uint256(i_find)];
  347. leagues[uint256(i_find)] = tmp_move;
  348.  
  349. return true;
  350. }
  351. // WARNING : consider games' league
  352. function removeLeague(string _title) public onlyOwner returns(bool) {
  353. require(bytes(_title).length > 0);
  354.  
  355. int16 i_find = findLeague(_title);
  356. require(i_find != -1);
  357.  
  358. for (uint256 i = uint256(i_find); i < leagues.length - 1; i++) {
  359. if (i == uint256(i_find)) {
  360. i_find++;
  361. }
  362. leagues[i] = leagues[uint256(i_find)];
  363. i_find++;
  364. }
  365. leagues.length--;
  366.  
  367. return true;
  368. }
  369. // WARNING : consider games' league
  370. function renameLeague(string _title_old, string _title_new) public onlyOwner returns(bool) {
  371. require(bytes(_title_old).length > 0);
  372. require(bytes(_title_new).length > 0);
  373.  
  374. int16 i_find = findLeague(_title_old);
  375. require(i_find != -1);
  376.  
  377. for (uint256 i = 0; i < leagues.length; i++) {
  378. if (leagues[i].equal(_title_old)) {
  379. leagues[i] = _title_new;
  380. break;
  381. }
  382. }
  383.  
  384. return true;
  385. }
  386. ////////////////////////////////////////////////////////////
  387.  
  388. ////////////////////////////////////////////////////////////
  389. // game
  390. function addGame(string _title, string _condition, string _league,
  391. address _left_team, address _right_team, uint256 _match_date, string _match_location, address[] _reporters, uint8 _idx)
  392. public onlyMember returns(bool) {
  393. require(bytes(_title).length > 0 && bytes(_condition).length > 0 && bytes(_league).length > 0
  394. && _left_team != 0 && _right_team != 0);
  395.  
  396. //int16 i_find = findGame(_title);
  397. require(findGame(_title) == -1);
  398. //int16 i_find_league = findLeague(_league);
  399. require(findLeague(_league) != -1);
  400.  
  401. // create
  402. Game memory game;
  403. game.title = _title;
  404. game.condition = _condition;
  405. game.league = _league;
  406. game.leftTeam = Team(_left_team);
  407. game.rightTeam = Team(_right_team);
  408. game.matchDate = _match_date;
  409. game.matchLocation = _match_location;
  410. game.minAmount = 1000 * 1000 * 1000 * 1000 * 1000;
  411. game.maxAmount = game.minAmount * 1000;
  412.  
  413. game.reporters = _reporters;
  414. game.result = new uint8[](_reporters.length);
  415.  
  416. // idx
  417. if (games.length == 0 || _idx == games.length) {
  418. games.push(game);
  419. } else {
  420. Game memory tmp_new = game;
  421. Game memory tmp_old = games[_idx];
  422. for (uint256 i = uint256(_idx); i < games.length - 1; i++) {
  423. games[i] = tmp_new;
  424. tmp_new = tmp_old;
  425. tmp_old = games[i + 1];
  426. }
  427. games[games.length] = tmp_new;
  428. games.push(tmp_old);
  429. }
  430.  
  431. // Team
  432. game.leftTeam.setTitle(_title);
  433. game.rightTeam.setTitle(_title);
  434.  
  435. return true;
  436. }
  437. function moveGame(string _title, uint16 _to_idx) public onlyMember returns(bool) {
  438. require(bytes(_title).length > 0);
  439. require(_to_idx >= 0);
  440.  
  441. require(_to_idx < games.length);
  442. int16 i_find = findGame(_title);
  443. require(i_find != -1);
  444. require(uint16(i_find) != _to_idx);
  445.  
  446. Game memory tmp_move = games[_to_idx];
  447. games[_to_idx] = games[uint256(i_find)];
  448. games[uint256(i_find)] = tmp_move;
  449.  
  450. return true;
  451. }
  452. function removeGame(string _title) public onlyOwner returns(bool) {
  453. require(bytes(_title).length > 0);
  454.  
  455. int16 i_find = findGame(_title);
  456. require(i_find != -1);
  457.  
  458. for (uint256 i = uint256(i_find); i < games.length - 1; i++) {
  459. if (i == uint256(i_find)) {
  460. i_find++;
  461. }
  462. games[i] = games[uint256(i_find)];
  463. i_find++;
  464. }
  465. games.length--;
  466.  
  467. return true;
  468. }
  469. ////////////////////////////////////////////////////////////
  470.  
  471. ////////////////////////////////////////////////////////////
  472. // game update
  473. function updateGameExt(string _title, uint256 _match_date, string _match_location,
  474. uint256 _max_amount, uint256 _min_amount) public onlyMember returns(bool) {
  475. require(bytes(_title).length > 0);
  476.  
  477. int16 i_find = findGame(_title);
  478. require(i_find != -1);
  479. require(games[uint256(i_find)].condition.equal("PLAN"));
  480.  
  481. games[uint256(i_find)].matchDate = _match_date;
  482. games[uint256(i_find)].matchLocation = _match_location;
  483. games[uint256(i_find)].maxAmount = _max_amount;
  484. games[uint256(i_find)].minAmount = _min_amount;
  485.  
  486. return true;
  487. }
  488. function updateGameTitle(string _title_old, string _title_new) public onlyMember returns(bool) {
  489. require(bytes(_title_old).length > 0 && bytes(_title_new).length > 0);
  490.  
  491. int16 i_find = findGame(_title_old);
  492. require(i_find != -1);
  493. require(games[uint256(i_find)].condition.equal("PLAN"));
  494.  
  495. games[uint256(i_find)].title = _title_new;
  496.  
  497. // Team
  498. games[uint256(i_find)].leftTeam.setTitle(_title_new);
  499. games[uint256(i_find)].rightTeam.setTitle(_title_new);
  500.  
  501. return true;
  502. }
  503. function updateGameLeague(string _title, string _league) public onlyMember returns(bool) {
  504. require(bytes(_title).length > 0 && bytes(_league).length > 0);
  505.  
  506. int16 i_find = findGame(_title);
  507. require(i_find != -1);
  508. require(games[uint256(i_find)].condition.equal("PLAN"));
  509.  
  510. int16 i_find_league = findLeague(_league);
  511. require(i_find_league != -1);
  512.  
  513. games[uint256(i_find)].league = _league;
  514.  
  515. return true;
  516. }
  517. function updateGameReporters(string _title, address[] _reporters) public onlyMember returns(bool) {
  518. require(bytes(_title).length > 0);
  519.  
  520. int16 i_find = findGame(_title);
  521. require(i_find != -1);
  522. require(games[uint256(i_find)].condition.equal("PLAN"));
  523.  
  524. games[uint256(i_find)].reporters = _reporters;
  525. games[uint256(i_find)].result = new uint8[](_reporters.length);
  526.  
  527. return true;
  528. }
  529. function updateGameTeam(string _title, address _left_team, address _right_team)
  530. public onlyOwner returns(bool) {
  531. require(bytes(_title).length > 0);
  532. require(_left_team != 0 && _right_team != 0);
  533.  
  534. int16 i_find = findGame(_title);
  535. require(i_find != -1);
  536. require(games[uint256(i_find)].condition.equal("PLAN"));
  537.  
  538. games[uint256(i_find)].leftTeam = Team(_left_team);
  539. games[uint256(i_find)].rightTeam = Team(_right_team);
  540.  
  541. return true;
  542. }
  543. function updateNotice(string _notice) public onlyOwner returns(bool) {
  544. //require(bytes(_notice).length > 0);
  545.  
  546. notice = _notice;
  547.  
  548. return true;
  549. }
  550. ////////////////////////////////////////////////////////////
  551.  
  552. ////////////////////////////////////////////////////////////
  553. // private
  554. function findLeague(string _name) view private onlyMember returns(int16) {
  555. if (leagues.length == 0) return -1;
  556.  
  557. for (uint256 i = 0; i < leagues.length; i++) {
  558. if (leagues[i].equal(_name)) {
  559. return int16(i);
  560. }
  561. }
  562.  
  563. return -1;
  564. }
  565. function findGame(string _title) view private onlyMember returns(int16) {
  566. if (games.length == 0) return -1;
  567.  
  568. for (uint256 i = 0; i < games.length; i++) {
  569. if (games[i].title.equal(_title)) {
  570. return int16(i);
  571. }
  572. }
  573.  
  574. return -1;
  575. }
  576. function isReporter(string _title, address _addr) view private returns(bool) {
  577. int16 i_find = findGame(_title);
  578.  
  579. for (uint256 i = 0; i < games[uint256(i_find)].reporters.length; i++) {
  580. if (games[uint256(i_find)].reporters[i] == _addr) {
  581. return true;
  582. }
  583. }
  584.  
  585. return false;
  586. }
  587. function registResult(string _title, address _addr, uint8 _result) private returns(bool) {
  588. int16 i_find = findGame(_title);
  589.  
  590. int16 i_find_reporter = -1;
  591. for (uint256 i = 0; i < games[uint256(i_find)].reporters.length; i++) {
  592. if (games[uint256(i_find)].reporters[i] == _addr) {
  593. i_find_reporter = int16(i);
  594. break;
  595. }
  596. }
  597.  
  598. if (i_find_reporter != -1) {
  599. games[uint256(i_find)].result[uint256(i_find_reporter)] = _result;
  600. }
  601.  
  602. return true;
  603. }
  604. ////////////////////////////////////////////////////////////
  605.  
  606. }
Add Comment
Please, Sign In to add comment