/
populate.php
128 lines (107 loc) · 3.92 KB
/
populate.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<!--
Desc: Populate tables with user information
Author: Jonathan Robello
File: populate.php
-->
<?php
// Include Helper methods
include "helpers.php";
// Start session
session_start();
// For MySQL
$servername = 'localhost';
$username = 'jlarobello';
$password = 'Ics321808';
$dbname = 'leaguestats';
// Summoner id
$s_id = $_SESSION["s_id"];
// Establish connection
$conn = new mysqli($servername, $username, $password, $dbname);
if($conn->connect_error)
{
$conn->close();
die("Connection failed: " . $conn->connect_error);
}
if($conn->connect_error)
{
$conn->close();
die("Connection failed: " . $conn->connect_error);
}
$query = "select * from stats where s_id=$s_id";
$result = $conn->query($query);
if($result->num_rows == 0)
{
$json = file_get_contents("https://na.api.pvp.net/api/lol/na/v2.2/matchlist/by-summoner/$s_id?seasons=PRESEASON2016&api_key=9073dedb-d0e0-43db-8557-9ae31bf7967e");
$obj = json_decode($json, TRUE);
if($obj["totalGames"] == 0)
{
header("Location: stats.php");
$conn->close();
die();
}
for($i = 0; $i < 10; $i++) // max 5 because of API request cap
{
$matchid = $obj["matches"][$i]["matchId"];
$championid = $obj["matches"][$i]["champion"];
$timestamp = $obj["matches"][$i]["timestamp"];
populate($matchid, $timestamp, $championid, $s_id); // 1 API request per a call.
sleep(1.5);
}
$query = "select * from wins
where wins.s_id = $s_id
union
select * from losses
where losses.s_id = $s_id
order by timestamp desc";
$results = $conn->query($query);
$numrows = $results->num_rows;
$latest_timestamp = 0;
$kills = 0;
$deaths = 0;
$assists = 0;
$gold = 0;
$cs = 0;
$resultrow = $results->fetch_assoc();
$latest_timestamp = $resultrow["timestamp"];
$kills = $resultrow["kills"];
$deaths = $resultrow["deaths"];
$assists = $resultrow["assists"];
$gold = $resultrow["gold"];
$cs = $resultrow["cs"];
while(($resultrow = $results->fetch_assoc()))
{
$kills += $resultrow["kills"];
$deaths += $resultrow["deaths"];
$assists += $resultrow["assists"];
$gold += $resultrow["gold"];
$cs += $resultrow["cs"];
}
$kills /= $numrows;
$deaths /= $numrows;
$assists /= $numrows;
$gold /= $numrows;
$cs /= $numrows;
// Average stats
$query = "insert into stats(s_id, latest_timestamp, kills, deaths, assists, gold, cs)
values($s_id, $latest_timestamp, $kills, $deaths, $assists, $gold, $cs)";
$result = $conn->query($query);
$conn->close();
header("Location: stats.php");
}
else
{
$json = file_get_contents("https://na.api.pvp.net/api/lol/na/v2.2/matchlist/by-summoner/$s_id?seasons=PRESEASON2016&api_key=9073dedb-d0e0-43db-8557-9ae31bf7967e");
$obj = json_decode($json, TRUE);
$timestamp = $obj["matches"][0]["timestamp"];
$query = "select latest_timestamp from stats
where s_id = $s_id";
$result = $conn->query($query);
$resultrow = $result->fetch_assoc();
if($result->numw_rows > 0 && $timestamp > $resultrow["latest_timestamp"] && !empty($obj))
{
repopulate($obj, $s_id);
}
$conn->close();
header("Location: stats.php");
}
?>