Guest User

Untitled

a guest
Jun 21st, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.67 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. // game
  18. Game[] public games;
  19.  
  20. // notify next address
  21. string public notice;
  22. ////////////////////////////////////////////////////////////
  23.  
  24. ////////////////////////////////////////////////////////////
  25. // struct
  26. // when PLAN, change : title, league, match*, max/min, reporters
  27. // reporter : condition, result
  28. struct Game {
  29. string title; // slogan and key
  30. string condition;
  31. string league;
  32. Team leftTeam;
  33. Team rightTeam;
  34.  
  35. uint256 matchDate;
  36. string matchLocation;
  37. uint256 maxAmount;
  38. uint256 minAmount;
  39.  
  40. address[] reporters;
  41. uint8[] result; // 0(even), 1(left), 2(right)
  42. }
  43. ////////////////////////////////////////////////////////////
  44.  
  45. ////////////////////////////////////////////////////////////
  46. // modifier
  47. ////////////////////////////////////////////////////////////
  48.  
  49. ////////////////////////////////////////////////////////////
  50. // event
  51. ////////////////////////////////////////////////////////////
  52.  
  53. ////////////////////////////////////////////////////////////
  54. constructor() public {
  55. owner = msg.sender;
  56. }
  57. ////////////////////////////////////////////////////////////
  58.  
  59. ////////////////////////////////////////////////////////////
  60. ////////////////////////////////////////////////////////////
  61. ////////////////////////////////////////////////////////////
  62. // game control
  63. // (PLAN), OPEN, START, END, (CONFIRM, SETTLE, FAILCOUNT*), CLOSE
  64. function doOpen(string _title) public onlyOwner returns(bool) {
  65. require(bytes(_title).length > 0);
  66.  
  67. int16 i_find = findGame(_title);
  68. require(i_find != -1);
  69.  
  70. // condition
  71. if (games[uint256(i_find)].condition.equal("PLAN")) {
  72. games[uint256(i_find)].condition = "OPEN";
  73. } else {
  74. return false;
  75. }
  76.  
  77. return true;
  78. }
  79. // by member and reporter
  80. function doStart(string _title) public returns(bool) {
  81. require(bytes(_title).length > 0);
  82.  
  83. int16 i_find = findGame(_title);
  84. require(i_find != -1);
  85.  
  86. // check reporter
  87. require(isMember(msg.sender) || isReporter(_title, msg.sender));
  88.  
  89. // condition
  90. if (games[uint256(i_find)].condition.equal("OPEN")) {
  91. games[uint256(i_find)].condition = "START";
  92. } else {
  93. return false;
  94. }
  95.  
  96. return true;
  97. }
  98. // by member and reporter
  99. // report result
  100. function doEnd(string _title, uint8 _result) public returns(bool) {
  101. require(bytes(_title).length > 0);
  102.  
  103. int16 i_find = findGame(_title);
  104. require(i_find != -1);
  105.  
  106. // check reporter
  107. require(isMember(msg.sender) || isReporter(_title, msg.sender));
  108.  
  109. // condition
  110. if (games[uint256(i_find)].condition.equal("START")) {
  111. games[uint256(i_find)].condition = "END";
  112. }
  113.  
  114. // regist
  115. if (isReporter(_title, msg.sender)) {
  116. registResult(_title, msg.sender, _result);
  117. }
  118.  
  119. return true;
  120. }
  121. // check result
  122. // 0 : even, 1 : left, 2 : right, 3 : unknown (no reporter), 4 : not same
  123. function doConfirm(string _title) view public onlyMember returns(uint8) {
  124. require(bytes(_title).length > 0);
  125.  
  126. int16 i_find = findGame(_title);
  127. require(i_find != -1);
  128.  
  129. // no reporter
  130. if (games[uint256(i_find)].reporters.length == 0) {
  131. return 3;
  132. }
  133.  
  134. // check result
  135. uint8 i_result = games[uint256(i_find)].result[0];
  136. for (uint8 i = 1; i < games[uint256(i_find)].result.length; i++) {
  137. if (games[uint256(i_find)].result[i] == i_result) {
  138. } else {
  139. return 4;
  140. }
  141. }
  142.  
  143. return i_result;
  144. }
  145. // only one time
  146. function doSettle(string _title, uint8 _self_result) public onlyOwner returns(bool) {
  147. require(bytes(_title).length > 0);
  148.  
  149. int16 i_find = findGame(_title);
  150. require(i_find != -1);
  151.  
  152. // condition
  153. require(games[uint256(i_find)].condition.equal("END"));
  154.  
  155. // check confirm
  156. uint8 i_confirm = doConfirm(_title);
  157. if (i_confirm == 4) {
  158. // if not same, wait 24 hours
  159. if (block.timestamp < (games[uint256(i_find)].matchDate + 24 hours)) {
  160. // TODO : log
  161. return false;
  162. }
  163. i_confirm = _self_result;
  164. } else if (i_confirm == 3) {
  165. games[uint256(i_find)].reporters.push(owner);
  166. games[uint256(i_find)].result.push(_self_result);
  167. i_confirm = _self_result;
  168. }
  169.  
  170. // one more check
  171. if (i_confirm != _self_result) {
  172. return false;
  173. }
  174.  
  175. address[] memory win_addr;
  176. uint256[] memory win_amount;
  177. uint256 win_balance;
  178. if (i_confirm == 0) {
  179. games[uint256(i_find)].leftTeam.doSettle(1);
  180. games[uint256(i_find)].rightTeam.doSettle(1);
  181.  
  182. } else if (i_confirm == 1) {
  183. win_balance = address(games[uint256(i_find)].leftTeam).balance;
  184. (win_addr, win_amount) = games[uint256(i_find)].leftTeam.getClientAccounts();
  185.  
  186. games[uint256(i_find)].leftTeam.doSettle(1);
  187.  
  188. games[uint256(i_find)].rightTeam.doYield(1, win_addr, win_amount, win_balance);
  189.  
  190. } else if (i_confirm == 2) {
  191. win_balance = address(games[uint256(i_find)].rightTeam).balance;
  192. (win_addr, win_amount) = games[uint256(i_find)].rightTeam.getClientAccounts();
  193.  
  194. games[uint256(i_find)].rightTeam.doSettle(1);
  195.  
  196. games[uint256(i_find)].leftTeam.doYield(1, win_addr, win_amount, win_balance);
  197.  
  198. } else {
  199. return false;
  200. }
  201.  
  202. return true;
  203. }
  204. // done by Team
  205. //function doFailAccount(string _title) public onlyOwner returns(bool) {}
  206. function doClose(string _title) public onlyOwner returns(bool) {
  207. require(bytes(_title).length > 0);
  208.  
  209. int16 i_find = findGame(_title);
  210. require(i_find != -1);
  211.  
  212. // condition
  213. require(games[uint256(i_find)].condition.equal("END"));
  214.  
  215. games[uint256(i_find)].condition = "CLOSE";
  216. games[uint256(i_find)].leftTeam.setIsClosed(true);
  217. games[uint256(i_find)].rightTeam.setIsClosed(true);
  218.  
  219. return true;
  220. }
  221. ////////////////////////////////////////////////////////////
  222. ////////////////////////////////////////////////////////////
  223. ////////////////////////////////////////////////////////////
  224.  
  225. ////////////////////////////////////////////////////////////
  226. ////////////////////////////////////////////////////////////
  227. ////////////////////////////////////////////////////////////
  228. // Team call
  229. function getGameConditionAndLimit(string _title) view public returns(string, uint256, uint256) {
  230. require(bytes(_title).length > 0);
  231.  
  232. int16 i_find = findGame(_title);
  233. require(i_find == -1);
  234.  
  235. return (games[uint256(i_find)].condition, games[uint256(i_find)].maxAmount, games[uint256(i_find)].minAmount);
  236. }
  237. ////////////////////////////////////////////////////////////
  238. ////////////////////////////////////////////////////////////
  239. ////////////////////////////////////////////////////////////
  240.  
  241. ////////////////////////////////////////////////////////////
  242. // game
  243. function addGame(string _title, string _league,
  244. address _left_team, address _right_team, uint256 _match_date, string _match_location,
  245. uint256 _min_amount, uint256 _max_amount, uint8 _idx)
  246. public onlyMember returns(bool) {
  247. require(bytes(_title).length > 0 && bytes(_league).length > 0 && _left_team != 0 && _right_team != 0);
  248.  
  249. //int16 i_find = findGame(_title);
  250. require(findGame(_title) == -1);
  251.  
  252. // create
  253. Game memory game;
  254. game.title = _title;
  255. game.condition = "PLAN";
  256. game.league = _league;
  257. game.leftTeam = Team(_left_team);
  258. game.rightTeam = Team(_right_team);
  259. game.matchDate = _match_date;
  260. game.matchLocation = _match_location;
  261. game.minAmount = _min_amount;
  262. game.maxAmount = _max_amount;
  263.  
  264. // idx
  265. if (games.length == 0 || _idx == games.length) {
  266. games.push(game);
  267. } else {
  268. Game memory tmp_new = game;
  269. Game memory tmp_old = games[_idx];
  270. for (uint256 i = uint256(_idx); i < games.length - 1; i++) {
  271. games[i] = tmp_new;
  272. tmp_new = tmp_old;
  273. tmp_old = games[i + 1];
  274. }
  275. games[games.length] = tmp_new;
  276. games.push(tmp_old);
  277. }
  278.  
  279. // Team
  280. game.leftTeam.setTitle(_title);
  281. game.rightTeam.setTitle(_title);
  282.  
  283. return true;
  284. }
  285. /*
  286. function moveGame(string _title, uint16 _to_idx) public onlyMember returns(bool) {
  287. require(bytes(_title).length > 0);
  288. require(_to_idx >= 0);
  289.  
  290. require(_to_idx < games.length);
  291. int16 i_find = findGame(_title);
  292. require(i_find != -1);
  293. require(uint16(i_find) != _to_idx);
  294.  
  295. Game memory tmp_move = games[_to_idx];
  296. games[_to_idx] = games[uint256(i_find)];
  297. games[uint256(i_find)] = tmp_move;
  298.  
  299. return true;
  300. }
  301. */
  302. function removeGame(string _title) public onlyOwner returns(bool) {
  303. require(bytes(_title).length > 0);
  304.  
  305. int16 i_find = findGame(_title);
  306. require(i_find != -1);
  307.  
  308. for (uint256 i = uint256(i_find); i < games.length - 1; i++) {
  309. if (i == uint256(i_find)) {
  310. i_find++;
  311. }
  312. games[i] = games[uint256(i_find)];
  313. i_find++;
  314. }
  315. games.length--;
  316.  
  317. return true;
  318. }
  319. ////////////////////////////////////////////////////////////
  320.  
  321. ////////////////////////////////////////////////////////////
  322. // game update
  323. function updateGameReporters(string _title, address[] _reporters) public onlyMember returns(bool) {
  324. require(bytes(_title).length > 0);
  325.  
  326. int16 i_find = findGame(_title);
  327. require(i_find != -1);
  328. require(games[uint256(i_find)].condition.equal("PLAN"));
  329.  
  330. games[uint256(i_find)].reporters = _reporters;
  331. games[uint256(i_find)].result = new uint8[](_reporters.length);
  332.  
  333. return true;
  334. }
  335. function updateNotice(string _notice) public onlyOwner returns(bool) {
  336. //require(bytes(_notice).length > 0);
  337.  
  338. notice = _notice;
  339.  
  340. return true;
  341. }
  342. ////////////////////////////////////////////////////////////
  343.  
  344. ////////////////////////////////////////////////////////////
  345. // private
  346. function findGame(string _title) view private returns(int16) {
  347. if (games.length == 0) return -1;
  348.  
  349. for (uint256 i = 0; i < games.length; i++) {
  350. if (games[i].title.equal(_title)) {
  351. return int16(i);
  352. }
  353. }
  354.  
  355. return -1;
  356. }
  357. function isReporter(string _title, address _addr) view private returns(bool) {
  358. int16 i_find = findGame(_title);
  359.  
  360. for (uint256 i = 0; i < games[uint256(i_find)].reporters.length; i++) {
  361. if (games[uint256(i_find)].reporters[i] == _addr) {
  362. return true;
  363. }
  364. }
  365.  
  366. return false;
  367. }
  368. function registResult(string _title, address _addr, uint8 _result) private returns(bool) {
  369. int16 i_find = findGame(_title);
  370.  
  371. int16 i_find_reporter = -1;
  372. for (uint256 i = 0; i < games[uint256(i_find)].reporters.length; i++) {
  373. if (games[uint256(i_find)].reporters[i] == _addr) {
  374. i_find_reporter = int16(i);
  375. break;
  376. }
  377. }
  378.  
  379. if (i_find_reporter != -1) {
  380. games[uint256(i_find)].result[uint256(i_find_reporter)] = _result;
  381. }
  382.  
  383. return true;
  384. }
  385. ////////////////////////////////////////////////////////////
  386.  
  387. }
Add Comment
Please, Sign In to add comment