-
Notifications
You must be signed in to change notification settings - Fork 2
/
fetch.php
90 lines (77 loc) · 3.76 KB
/
fetch.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
<?php
$data_url = $_GET['data_url']; // Target URL
$server = $_GET['server']; // Server ID
$data = file_get_contents($data_url);
/*
The dgl-status output that we receive looks like this:
blackcustard#dcss-svn#L12 SpAs, Lair:1#80x24#2230#0#
Disco#dcss-svn#L1 TeAE, D:1#80x24#31#0#
elliptic#dcss-svn#L24 MuEn, Elf:5#80x24#1#3#
ZChris13#spr-svn##80x24#1730#0#
We want to transform it in the following ways:
1. Insert the server acronym at the end of each record.
2. Split out the third field into four subfields.
3. Transform each server's idiosyncratic game/version identifiers (found in
field 2) into a more uniform representation.
4. Split out the game/version identifiers into two fields.
5. Get rid of the termsize field.
6. Remove the L from the XL field.
This will yield a string that is still easily parseable. If we're careful, and
if we know the characteristics of the dgl-status output, we can do this with
string replacement and regexes. This is hilariously fragile! If only the
servers could respond in JSON, using standardized formats... but such is life.
*/
/* Lasciate ogne speranza, voi ch'intrate. */
// We'll use | as our record delimiter as it won't appear in the dgl output.
$data = str_replace("\n", ($server . '|'), $data);
// Field three represents four data points: xl, race, role, and place.
$data = str_replace("#Lost in Time#", "#?#?#?#?#", $data); // Ancient crawl
$data = str_replace(", ", "#", $data); // ", " separates char and place
$data = str_replace(" ", " ", $data); // If two spaces delimit xl and char
$data = str_replace(" ", "#", $data); // The space between xl and char
$pattern = "/#(L\d{1,2})#([a-zA-Z]{2})([a-zA-Z]{2})#/";
$replacement = "#$1#$2#$3#";
$data = preg_replace($pattern, $replacement, $data); // char -> race#role
$data = str_replace("##", "#####", $data); // If field three was totally blank
// If only servers could agree on compact, uniform game/version names...
$data = str_replace("#dc-anc#", "#dcss-old#", $data); // Ancient crawl
$data = str_replace("-git", "-trunk", $data);
$data = str_replace("-svn", "-trunk", $data); // CDO
// CDO sends us "spr-0.8" but we want "0.8-spr"
$pattern = "/#(dcss|spr|zd|tut)-([\d\.a-zA-Z]{3,5})#/";
$replacement = "#$2#$1#";
$data = preg_replace($pattern, $replacement, $data);
// We hate termsize!
$pattern = "/#\d{1,3}x\d{1,3}#/";
$replacement = "#";
$data = preg_replace($pattern, $replacement, $data);
// And we hate that L in front of the XL field! We're mad with regex power.
$pattern = "/#L(\d{1,2})#/";
$replacement = "#$1#";
$data = preg_replace($pattern, $replacement, $data);
// Bonus round
$data = str_replace("#Shoals:", "#Shoal:", $data);
$data = str_replace("#Spider:", "#Spidr:", $data);
$data = str_replace("#Vaults:", "#Vault:", $data);
$data = substr($data, 0, strlen($data)-1); // Chop off the last pipe character
/*
So now we have something that looks like this:
Chapayev#0.9#dcss#12#Na#VM#Lair:6#51#0#CAO/Term|crate#git#dcss#3#OgBe#D:2#0#0#CAO/Term|demonblade#git##dcss###1915#0#CAO/Term
Let's transform it into JSON.
*/
$data = str_replace('#', '","', $data);
$data = str_replace('|', '"],["', $data);
$data = '[["' . $data . '"]]';
/*
In the event that we can't connect to the remote data source, $data will end up
as '[[""]]' and PHP may or may not decide to echo an error message (there is no
way to prevent this behavior, as far as I know, short of reconfiguring PHP). If
it echoes an error message, the client will catch the error when it attempts to
parse invalid JSON, so no harm. If it doesn't echo an error message, we'd
rather return an empty string (and thereby also trigger a failure in the
parser) rather than return an empty JSON array. We'd love it if PHP could be
guaranteed to not insert garbage into its responses, but such is life.
*/
if ($data == '[[""]]') { $data = ''; }
echo $data;
?>