Guest User

Untitled

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