/
update.php
144 lines (113 loc) · 4.19 KB
/
update.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
<?php
# Copyright (c) 2015 Jordan Turley, CSGO Win Big. All Rights Reserved.
session_start();
include 'default.php';
$db = getDB();
# Get all users in chat messages
$stmt = $db->query('SELECT * FROM `chat` ORDER BY `id` DESC LIMIT 50');
$chatMessages = $stmt->fetchAll();
$allUserIDsChat = array();
foreach ($chatMessages as $message) {
$steamUserID = $message['steamUserID'];
array_push($allUserIDsChat, $steamUserID);
}
# Get all users in pot
$stmt = $db->query('SELECT * FROM `currentPot`');
$currentPotArr = $stmt->fetchAll();
$allUserIDsPot = array();
foreach ($currentPotArr as $item) {
$steamUserID = $item['ownerSteamId64'];
array_push($allUserIDsPot, $steamUserID);
}
# Get previous winner's steam ID
$stmt = $db->query('SELECT * FROM history ORDER BY id DESC');
$allRounds = $stmt->fetchAll();
$mostRecentRound = $allRounds[0];
# If the most recent round in history has items in it, then the current round doesn't have any items in it,
# and there isn't a current round in the history table yet
if (strlen($mostRecentRound['allItemsJson']) > 0) {
$prevPot = $mostRecentRound;
$currentRound = null;
} else {
$prevPot = $allRounds[1];
$currentRound = $mostRecentRound;
}
$prevWinner = $prevPot['winnerSteamId64'];
$prevWinnerArr = array($prevWinner);
# Create array of all users, without repeats
$allUsersArr = array_unique(array_merge($allUserIDsChat, $allUserIDsPot, $prevWinnerArr));
$allUserIDsStr = join(',', $allUsersArr);
# Get all user info for the steam user IDs
$chatAPIKey = getSteamAPIKey();
$usersInfoStr = file_get_contents("http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=$chatAPIKey&steamids=$allUserIDsStr");
$chatMessagesArr = array();
for ($i1 = count($chatMessages) - 1; $i1 >= 0; $i1--) {
$message = $chatMessages{$i1};
$id = $message['id'];
$text = htmlspecialchars(stripcslashes($message['text']));
$date = $message['date'];
$time = $message['time'];
$steamUserID = $message['steamUserID'];
$steamUserInfo = getSteamProfileInfoForSteamID($usersInfoStr, $steamUserID);
$arr = array('id' => $id, 'text' => $text, 'date' => $date, 'time' => $time, 'steamUserInfo' => $steamUserInfo);
array_push($chatMessagesArr, $arr);
}
# Get the current pot
$stmt = $db->query('SELECT * FROM currentPot ORDER BY id DESC');
$currentPotArr = $stmt->fetchAll();
$currentPot = array();
$potPrice = 0;
foreach ($currentPotArr as $itemInPot) {
$itemID = $itemInPot['id'];
$itemName = $itemInPot['itemName'];
if ($itemName[0] === '?') {
$itemName = substr($itemName, 2);
}
$itemPrice = $itemInPot['itemPrice'];
$itemIcon = $itemInPot['itemIcon'];
$itemRarityColor = $itemInPot['itemRarityColor'];
$itemOwnerSteamID = $itemInPot['ownerSteamId64'];
$steamUserInfo = getSteamProfileInfoForSteamID($usersInfoStr, $itemOwnerSteamID);
$arr = array(
'itemID' => $itemID,
'itemSteamOwnerInfo' => $steamUserInfo,
'itemName' => $itemName,
'itemPrice' => $itemPrice,
'itemIcon' => $itemIcon,
'itemRarityColor' => $itemRarityColor
);
array_push($currentPot, $arr);
$potPrice += $itemPrice;
}
# Get the time left in the current round
$roundEndTime = is_null($currentRound) ? null : $currentRound['endTime'];
$stmt = $db->query('SELECT * FROM history ORDER BY id DESC');
$mostRecentInHistory = $stmt->fetch();
$mostRecentAllItems = $mostRecentInHistory['allItemsJson'];
# Get the past pot and check if someone just now won
$prevGameID = $prevPot['id'];
$winnerSteamId64 = $prevPot['winnerSteamId64'];
$userPutInPrice = $prevPot['userPutInPrice'];
$prevPotPrice = $prevPot['potPrice'];
$allItems = $prevPot['allItemsJson'];
$winnerSteamInfo = getSteamProfileInfoForSteamID($usersInfoStr, $winnerSteamId64);
$winnerSteamInfo['personaname'] = html_entity_decode($winnerSteamInfo['personaname']);
# The information for the previous round
$mostRecentGame = array(
'prevGameID' => $prevGameID,
'winnerSteamInfo' => $winnerSteamInfo,
'userPutInPrice' => $userPutInPrice,
'potPrice' => $prevPotPrice,
'allItems' => $allItems
);
# The information for the current round
$data = array(
'chat' => $chatMessagesArr,
'pot' => $currentPot,
'potPrice' => $potPrice,
'roundEndTime' => $roundEndTime,
'mostRecentAllItems' => $mostRecentAllItems,
'mostRecentGame' => $mostRecentGame
);
echo jsonSuccess($data);
?>