Advertisement
Guest User

Untitled

a guest
Aug 6th, 2017
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.73 KB | None | 0 0
  1. <?php
  2.  
  3. $servername = "localhost";
  4. $username = "root";
  5. $password = "root";
  6. $dbname = "testing";
  7.  
  8.  
  9. $infoArray = new stdClass();
  10. $infoArray->defs = [98, 34, 101, 97, 138];
  11. $infoArray->fwds = [212, 25, 92];
  12. $infoArray->gk = [260, 74];
  13. $infoArray->mid = [16, 228, 409, 454, 453];
  14. $totalMoney = 100;
  15.  
  16.  
  17.  
  18. // Create connection
  19. $conn = new mysqli($servername, $username, $password, $dbname);
  20.  
  21. function getDataFromExternalApi ($url) {
  22. $ch = curl_init();
  23. curl_setopt($ch, CURLOPT_URL, $url);
  24. curl_setopt($ch, CURLOPT_POST, 0);
  25. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  26. $server_output = curl_exec($ch);
  27. curl_close($ch);
  28. $data = json_decode($server_output);
  29. return $data;
  30. }
  31.  
  32. function multiCurlReq($urlArray) {
  33. $nodes = $urlArray;
  34. $node_count = count($nodes);
  35.  
  36. $curl_arr = array();
  37. $master = curl_multi_init();
  38.  
  39. for($i = 0; $i < $node_count; $i++)
  40. {
  41. $url =$nodes[$i];
  42. $curl_arr[$i] = curl_init($url);
  43. curl_setopt($curl_arr[$i], CURLOPT_RETURNTRANSFER, true);
  44. curl_multi_add_handle($master, $curl_arr[$i]);
  45. }
  46.  
  47. do {
  48. curl_multi_exec($master,$running);
  49. } while($running > 0);
  50.  
  51.  
  52. for($i = 0; $i < $node_count; $i++)
  53. {
  54. $results[] = curl_multi_getcontent ($curl_arr[$i]);
  55. }
  56. echo '<pre>';
  57. print_r($results);
  58. }
  59.  
  60. $playerUrl = "https://fantasy.premierleague.com/drf/bootstrap";
  61. $allPlayerMetaData = getDataFromExternalApi($playerUrl);
  62.  
  63. function seedPlayer($allPlayerMetaData, $con) {
  64. $detailUrlArray = array();
  65. $sql = "";
  66. if($allPlayerMetaData && $allPlayerMetaData->elements) {
  67. foreach ($allPlayerMetaData->elements as $key => $value) {
  68. $playerId = $value->id;
  69. $singlePlayerInfo = new stdClass();
  70. $singlePlayerInfo->player_cost = $value->now_cost ? ($value->now_cost/10) : 0;
  71. $singlePlayerInfo->player_points = $value->total_points ? $value->total_points: 0;
  72. $singlePlayerInfo->player_id = $playerId;
  73. $singlePlayerInfo = json_encode($singlePlayerInfo);
  74. $sqlSel = "select * from players where player_id = $playerId";
  75. $results = $con->query($sqlSel);
  76. if ($results->num_rows > 0) {
  77. $sql .= "UPDATE players set data = '$singlePlayerInfo' where player_id=$playerId;";
  78. }
  79. else {
  80. $sql .= "INSERT INTO players (data, player_id) VALUES ('$singlePlayerInfo', '$playerId');";
  81. }
  82. }
  83. if (mysqli_multi_query($con,$sql))
  84. {
  85. do
  86. {
  87. // Store first result set
  88. if ($result=mysqli_store_result($con)) {
  89. // Fetch one and one row
  90. while ($row=mysqli_fetch_row($result))
  91. {
  92. printf("%s\n",$row[0]);
  93. }
  94. // Free result set
  95. mysqli_free_result($result);
  96. }
  97. }
  98. while (mysqli_next_result($con));
  99. }
  100. mysqli_close($con);
  101. }
  102.  
  103. }
  104.  
  105. function calculateAvailableBalance($teamInfoObj, $con, $totalMoney){
  106. $sqlSel = "select * from players";
  107. $results = $con->query($sqlSel);
  108. $totalPlayerCost = 0;
  109. $availableBalance = 0;
  110. if ($results->num_rows > 0) {
  111. while($row = $results->fetch_assoc()) {
  112. $playerInfo = json_decode($row["data"]);
  113. $playerId = $playerInfo->player_id;
  114. foreach($teamInfoObj as $key => $value) {
  115. if(in_array($playerId, $value)){
  116. $totalPlayerCost += (float) $playerInfo->player_cost;
  117. }
  118. }
  119. }
  120. }
  121. $availableBalance = $totalMoney - $totalPlayerCost;
  122. return $availableBalance;
  123.  
  124. }
  125.  
  126. $availabLeBalance = calculateAvailableBalance($infoArray, $conn, $totalMoney);
  127.  
  128.  
  129. // seedPlayer($allPlayerMetaData, $conn);
  130.  
  131.  
  132.  
  133. // $a = new stdClass();
  134. // $a->test = 'name';
  135. // $a->b = new stdClass();
  136. // $a->b->name = 'what';
  137. // $a->b->place = 'what';
  138. // $c = json_encode($a);
  139. // var_dump(json_decode($c));die();
  140.  
  141. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement