-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcronjob.php
190 lines (182 loc) · 11 KB
/
cronjob.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php
echo "<pre>";
set_time_limit(0); // Ekkert time limit svo það er hægt að uppfæra alla summoners
require_once 'config.php';
require_once 'vendor/autoload.php';
use LeagueWrap\Api;
$api = new Api($key);
$championsStaticData = $api->staticData()->getChampions();
foreach ($supported_league_servers as $server) {
$api->setRegion($server);
$sth = $pdo->prepare("SELECT id FROM {$server}_summoners");
$sth->execute();
$summoners_ids = $sth->fetchAll(PDO::FETCH_ASSOC);
foreach ($summoners_ids as $the_summoner_id) {
try {
#Fá summoner info til þess að setja í databaseið
$summoner = $api->summoner()->info($the_summoner_id);
$sth = $pdo->prepare("REPLACE INTO {$server}_summoners (id, summonerName, profileIconId, summonerLevel)
VALUES(:id, :summonerName, :profileIconId, :summonerLevel)");
$sth->execute(array(
':id' => $summoner->id,
':summonerName' => $summoner->name,
':profileIconId' => $summoner->profileIconId,
':summonerLevel' => $summoner->summonerLevel,
));
#telja hversu oft spilari spilar X role - uppfæra databaseið
$numberOfMatchesLoopedOver = 0;
$topGamesPlayed = 0;
$jungleGamesPlayed = 0;
$midGamesPlayed = 0;
$adcGamesPlayed = 0;
$supportGamesPlayed = 0;
$matchHistory = $api->matchlist()->matchlist($summoner);
foreach ($matchHistory as $match) {
$numberOfMatchesLoopedOver++;
if($numberOfMatchesLoopedOver == 20) break;
switch ($match->lane) {
case "TOP":
$topGamesPlayed++;
break;
case "JUNGLE":
$jungleGamesPlayed++;
break;
case "MID":
$midGamesPlayed++;
break;
case "BOTTOM":
$match->role == "DUO_CARRY" ? $adcGamesPlayed++ : $supportGamesPlayed++;
break;
}
}
$roles = array("top" => $topGamesPlayed, "jungle" => $jungleGamesPlayed, "mid" => $midGamesPlayed, "adc" => $adcGamesPlayed, "support" => $supportGamesPlayed);
$mainRole = array_search(max($roles),$roles);
$sth = $pdo->prepare("REPLACE INTO {$server}_roles (id, summonerName, mainRole, totalGamesPlayed, topGamesPlayed, jungleGamesPlayed, midGamesPlayed, adcGamesPlayed, supportGamesPlayed)
VALUES (:id, :summonerName, :mainRole,:totalGamesPlayed, :topGamesPlayed, :jungleGamesPlayed, :midGamesPlayed, :adcGamesPlayed, :supportGamesPlayed)");
$sth->execute(array(
':id' => $summoner->id,
':summonerName' => $summoner->name,
':mainRole' => $mainRole,
':totalGamesPlayed' => $matchHistory->totalGames,
':topGamesPlayed' => $topGamesPlayed,
':jungleGamesPlayed' => $jungleGamesPlayed,
':midGamesPlayed' => $midGamesPlayed,
':adcGamesPlayed' => $adcGamesPlayed,
':supportGamesPlayed' => $supportGamesPlayed,
));
#Fá League info hjá summoner - uppfæra databaseið
$leagues = $api->league()->league($summoner, true);
foreach ($leagues as $league) {
$queue = strtolower($league->queue); #lágstafa queue til þess að setja í table með lágstöfum
$sth = $pdo->prepare("REPLACE INTO {$server}_$queue (name, tier, queue, playerOrTeamid, playerOrTeamName, division, leaguePoints, wins, losses)
VALUES(:name, :tier, :queue, :playerOrTeamId, :playerOrTeamName, :division, :leaguePoints, :wins, :losses)");
$sth->execute(array(
':name' => $league->name,
':tier' => $league->tier,
':queue' => $league->queue,
':playerOrTeamId' => $league->entries[0]->playerOrTeamId,
':playerOrTeamName' => $league->entries[0]->playerOrTeamName,
':division' => $league->entries[0]->division,
':leaguePoints' => $league->entries[0]->leaguePoints,
':wins' => $league->entries[0]->wins,
':losses' => $league->entries[0]->losses,
));
}
#Fá champion mastery info - uppfæra databaseið
$masteryList = $api->championMastery()->topChampions($summoner, 3);
$champion1 = $championsStaticData->getChampion($masteryList[0]->championId);
$champion2 = $championsStaticData->getChampion($masteryList[1]->championId);
$champion3 = $championsStaticData->getChampion($masteryList[2]->championId);
$sth = $pdo->prepare("REPLACE INTO {$server}_champion_mastery (id, summonerName, championId1, championName1, championPoints1, championId2, championName2, championPoints2, championId3, championName3, championPoints3)
VALUES(:id, :summonerName, :championId1, :championName1, :championPoints1, :championId2, :championName2, :championPoints2, :championId3, :championName3, :championPoints3)");
$sth->execute(array(
':id' => $summoner->id,
':summonerName' => $summoner->name,
':championId1' => $masteryList[0]->championId,
':championName1' => $champion1->key,
':championPoints1' => $masteryList[0]->championPoints,
':championId2' => $masteryList[1]->championId,
':championName2' => $champion2->key,
':championPoints2' => $masteryList[1]->championPoints,
':championId3' => $masteryList[2]->championId,
':championName3' => $champion3->key,
':championPoints3' => $masteryList[2]->championPoints,
));
#Fá ranked stats info, raða stats eftir hæsta totalSessionsPlayed - uppfæra databaseið
$stats = $api->stats()->ranked($summoner)->raw();
usort($stats['champions'], function($a, $b) {
return $b['stats']['totalSessionsPlayed'] - $a['stats']['totalSessionsPlayed'];
});
#Fá info um fyrstu þrjá champana til þess að setja nafnið þeirra (key) í databaseið
$champion1 = $championsStaticData->getChampion($stats['champions'][1]['id']);
$champion2 = $championsStaticData->getChampion($stats['champions'][2]['id']);
$champion3 = $championsStaticData->getChampion($stats['champions'][3]['id']);
$sth = $pdo->prepare("REPLACE INTO {$server}_most_played_champions (id, summonerName, championId1, championName1, championId2, championName2, championId3, championName3)
VALUES(:id, :summonerName, :championId1, :championName1, :championId2, :championName2, :championId3, :championName3)");
$sth->execute(array(
':id' => $summoner->id,
':summonerName' => $summoner->name,
':championId1' => $stats['champions'][1]['id'],
':championName1' => $champion1->key,
':championId2' => $stats['champions'][2]['id'],
':championName2' => $champion2->key,
':championId3' => $stats['champions'][3]['id'],
':championName3' => $champion3->key,
));
$sth = $pdo->prepare("REPLACE INTO {$server}_ranked_stats (id, summonerName, championId, totalSessionsPlayed, totalSessionsLost, totalSessionsWon, totalChampionKills, totalDamageDealt, totalDamageTaken, mostChampionKillsPerSession, totalMinionKills, totalDoubleKills, totalTripleKills, totalQuadraKills, totalPentaKills, totalUnrealKills, totalDeathsPerSession, totalGoldEarned, mostSpellsCast, totalTurretsKilled, totalPhysicalDamageDealt, totalMagicDamageDealt, totalFirstBlood, totalAssists, maxChampionsKilled, maxNumDeaths)
VALUES(:id, :summonerName, :championId, :totalSessionsPlayed, :totalSessionsLost, :totalSessionsWon, :totalChampionKills, :totalDamageDealt, :totalDamageTaken, :mostChampionKillsPerSession, :totalMinionKills, :totalDoubleKills, :totalTripleKills, :totalQuadraKills, :totalPentaKills, :totalUnrealKills, :totalDeathsPerSession, :totalGoldEarned, :mostSpellsCast, :totalTurretsKilled, :totalPhysicalDamageDealt, :totalMagicDamageDealt, :totalFirstBlood, :totalAssists, :maxChampionsKilled, :maxNumDeaths)");
$sth->execute(array(
':id' => $summoner->id,
':summonerName' => $summoner->name,
':championId' => $stats['champions'][0]['id'],
':totalSessionsPlayed' =>$stats['champions'][0]['stats']['totalSessionsPlayed'],
':totalSessionsLost' => $stats['champions'][0]['stats']['totalSessionsLost'],
':totalSessionsWon' => $stats['champions'][0]['stats']['totalSessionsWon'],
':totalChampionKills' => $stats['champions'][0]['stats']['totalChampionKills'],
':totalDamageDealt' => $stats['champions'][0]['stats']['totalDamageDealt'],
':totalDamageTaken' => $stats['champions'][0]['stats']['totalDamageTaken'],
':mostChampionKillsPerSession' => $stats['champions'][0]['stats']['mostChampionKillsPerSession'],
':totalMinionKills' => $stats['champions'][0]['stats']['totalMinionKills'],
':totalDoubleKills' => $stats['champions'][0]['stats']['totalDoubleKills'],
':totalTripleKills' => $stats['champions'][0]['stats']['totalTripleKills'],
':totalQuadraKills' => $stats['champions'][0]['stats']['totalQuadraKills'],
':totalPentaKills' => $stats['champions'][0]['stats']['totalPentaKills'],
':totalUnrealKills' => $stats['champions'][0]['stats']['totalUnrealKills'],
':totalDeathsPerSession' => $stats['champions'][0]['stats']['totalDeathsPerSession'],
':totalGoldEarned' => $stats['champions'][0]['stats']['totalGoldEarned'],
':mostSpellsCast' => $stats['champions'][0]['stats']['mostSpellsCast'],
':totalTurretsKilled' => $stats['champions'][0]['stats']['totalTurretsKilled'],
':totalPhysicalDamageDealt' => $stats['champions'][0]['stats']['totalPhysicalDamageDealt'],
':totalMagicDamageDealt' => $stats['champions'][0]['stats']['totalMagicDamageDealt'],
':totalFirstBlood' => $stats['champions'][0]['stats']['totalFirstBlood'],
':totalAssists' => $stats['champions'][0]['stats']['totalAssists'],
':maxChampionsKilled' => $stats['champions'][0]['stats']['maxChampionsKilled'],
':maxNumDeaths' => $stats['champions'][0]['stats']['maxNumDeaths'],
));
echo "<br>Uppfært: $server - $summoner->name - $summoner->id";
usleep(100000); #Sofa í 0.1 sekundu til þess að fara ekki yfir API limitið
} catch (LeagueWrap\Response\Http404 $e) {
#Eyða þessum notanda úr frontend gagnagrunninum þar sem það var ekki fundið upplýsingar um hann (Líklegast unranked)
echo "<br>404 error: $server - $summoner->name - $summoner->id";
$sth = $pdo->prepare("DELETE FROM {$server}_ranked_stats WHERE id=$summoner->id");
$sth->execute();
$sth = $pdo->prepare("DELETE FROM {$server}_champion_mastery WHERE id=$summoner->id");
$sth->execute();
$sth = $pdo->prepare("DELETE FROM {$server}_ranked_solo_5x5 WHERE playerOrTeamId=$summoner->id");
$sth->execute();
$sth = $pdo->prepare("DELETE FROM {$server}_ranked_flex_sr WHERE playerOrTeamId=$summoner->id");
$sth->execute();
$sth = $pdo->prepare("DELETE FROM {$server}_ranked_flex_tt WHERE playerOrTeamId=$summoner->id");
$sth->execute();
} catch (LeagueWrap\Response\ResponseException $e) {
#Þetta er catch all (bæði 4xx og 5xx http errors)
echo "<br>ResponseException error: $server - $summoner->name - $summoner->id";
$e->hasResponse(); #Checks if response was attached
$response = $e->getResponse(); #Instance of LeagueWrap\Response
echo $response; #Logga responsið
} catch (LeagueWrap\Response\UnderlyingServiceRateLimitReached $e) {
#API Rate limitið var reached - sofa þangað til við megum reyna aftur
sleep($e->getRetryAfter());
}
}
}