Skip to content

Commit 7ca40f2

Browse files
committed
feat: add parameter parsing
The interface now has parameters!!! Woo 🎊🎊 All configurable variables are inside the `utils/config.php` file :)
1 parent e26f1d6 commit 7ca40f2

File tree

2 files changed

+96
-6
lines changed

2 files changed

+96
-6
lines changed

β€Žindex.php

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<!DOCTYPE html>
22

33
<?php
4-
54
require_once("utils/minifier.php");
65
require_once("utils/config.php");
6+
require_once("utils/params.php");
77
?>
88

99
<html>
@@ -52,11 +52,7 @@
5252
///////////////////////////////////////////////////////////////////////////
5353
// PLEASE CHNAGE THE VAUES INSIDE THE CONFIG FILE
5454
///////////////////////////////////////////////////////////////////////////
55-
56-
// Set relative tile directory
5755
var _MAP_tileURL = "<?php echo $mapTileUrl; ?>";
58-
59-
// Set relative icon directory
6056
var _MAP_iconURL = "<?php echo $mapIconUrl; ?>";
6157

6258
// Set if to show Atlas map (WARNING: REQUIRES "atlas" TILE DIRECTORY)
@@ -151,7 +147,7 @@
151147
<a id="toggleLive" href="#">Live update <span id="live_enabled" class="label label-danger pull-right">off</span></a>
152148
</li>
153149
-->
154-
<li>
150+
<li>
155151
<a id="reconnect" href="#">Connect <span id="connection" class="label label-danger pull-right">disconnected</span></a>
156152
</li>
157153

@@ -183,6 +179,7 @@
183179

184180
<?php
185181
printLastJs($debug);
182+
printJsForParams();
186183
?>
187184

188185
</body>

β€Žutils/params.php

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
3+
$getParams = array(
4+
//If this is present, the rest is parsed.
5+
/*
6+
The higher in the array, the higher priority the parameter is..
7+
This means you should put the stuff that requires multiple parameters at the top.
8+
*/
9+
10+
"track" => // If this is present as a GET parameter, the rest are parsed
11+
array(
12+
"track" => "string", // We want to get the value of "tracked" as a string,
13+
"_js" => '$("#playerSelect").val("%s"); $("#playerSelect").change();' // the parameters above will be parsed into this string (see "trackAndZoom")
14+
),
15+
16+
/*
17+
Example query
18+
19+
?trackAndZoom&id=ip:127.0.0.1&zoom=3
20+
?trackAndZoom&id=steam:11000010573D0E2&zoom=6
21+
*/
22+
"trackAndZoom" =>
23+
array(
24+
"id" => "string",
25+
"zoom" => "int",
26+
"_js" => '$("#playerSelect").val("%s"); $("#playerSelect").change(); map.setZoom(%d);'
27+
),
28+
29+
// Zoom won't be ran if "trackAndZoom" is ran because the "zoom" parameter is unset
30+
"zoom" =>
31+
array(
32+
"zoom" => "int",
33+
"_js" => "map.setZoom(%d);"
34+
),
35+
36+
);
37+
38+
$javascriptToPrint = array();
39+
40+
// Get any parameters and do stuff.. I guess
41+
42+
foreach ($getParams as $key => $parameters) {
43+
44+
if (isset($_GET[$key])){
45+
echo "$key is set<br/>";
46+
$gotParams = array();
47+
$javascript = "";
48+
49+
foreach ($parameters as $paramName => $type) {
50+
if ($paramName == "_js"){
51+
$javascript = $type;
52+
continue;
53+
}
54+
echo "Checking $paramName<br/>";
55+
if(isset($_GET[$paramName])){ // Been set, check it and add it
56+
57+
$val = $_GET[$paramName];
58+
59+
settype($val, $type);
60+
61+
array_push($gotParams, $val);
62+
unset($_GET[$paramName]);
63+
}
64+
}
65+
// Now that we have each parameter, format the JS code and add it to the array
66+
$formattedString = vsprintf($javascript, $gotParams);
67+
68+
array_push($javascriptToPrint, $formattedString);
69+
}
70+
71+
if(isset($_GET[$key])){
72+
unset($_GET[$key]);
73+
}
74+
}
75+
76+
// Echos the JS for the params to work
77+
function printJsForParams(){
78+
global $javascriptToPrint;
79+
80+
$js = "";
81+
foreach ($javascriptToPrint as $value) {
82+
$js .= "$value\n";
83+
}
84+
85+
echo "<script>$(document).ready(function(){
86+
setTimeout(function(){ // Give the page some time to load all other JS code.
87+
// Inject the code gotten from the parameters
88+
$js
89+
}, 500);
90+
});</script>";
91+
}
92+
93+
?>

0 commit comments

Comments
Β (0)