This repository has been archived by the owner on Jun 14, 2021. It is now read-only.
/
misc.php
69 lines (65 loc) · 2.25 KB
/
misc.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
<?php
/**
* Assorted random functions
*
* @license http://www.gnu.org/licenses/gpl-3.0.txt GNU General Public License 3
* @author Sylae Jiendra Corell <sylae@calref.net>
*/
/**
* Get a team name from an ID. Also optionally return a "pending" notice that tells
* the reader what match will determine which player goes in this slot.
*
* @todo I bet this could be coded better
* @param int $needle team ID to search for
* @param array $haystack raw challonge payload
* @param int $pending ID of match we are waiting on
* @param boolean $pending_loser if true, we want the pending match's loser
* @return string name of team, or HTML chunk describing what we're waiting on.
*/
function getName($needle, $haystack, $pending = null, $pending_loser = null) {
if (is_null($needle)) {
$d = getMatch($pending, $haystack);
$who = "<em>" . ($pending_loser ? "Winner of " : "Loser of ") . "</em>";
return $who . $d['identifier'] . ' <small>Match ID <a href="#match_' . $d['id'] . '">' . $d['id'] . "</a></small>";
}
foreach ($haystack['participants'] as $i => $data) {
if ($data['participant']['id'] == $needle) {
return $data['participant']['name'];
}
}
}
/**
* Turn a match ID into its data array
*
* @todo I'm sure this can be more efficient.
* @param int $needle match ID to return
* @param array $haystack raw challonge payload
* @return array challonge match information
*/
function getMatch($needle, $haystack) {
foreach ($haystack['matches'] as $i => $data) {
if ($data['match']['id'] == $needle) {
return $data['match'];
}
}
}
/**
* Simple function to HTTP PUT an array. Shamelessly stolen from stackoverflow.
* @link http://stackoverflow.com/a/6609181
* @param array $payload Data to upload
* @param string $destination URL to PUT at
* @param string $method Method to use, default PUT
* @return string the response from the server.
*/
function httpPut($payload, $destination, $method='PUT') {
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => $method,
'content' => http_build_query($payload),
),
);
$context = stream_context_create($options);
$result = file_get_contents($destination, false, $context);
return $result;
}