Guest User

Untitled

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