Guest User

Untitled

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