[READ-ONLY] Mirror of https://github.com/andrioid/travian.scrolls.org. Retired project. I got a bit overly engaged in an online game called Travian.
6.4 kB
260 lines
1<?php
2
3function a2syntax ($a) {
4 global $_GET;
5 $syntax = "(";
6 if (!empty($a)) {
7 $a_split = split(",", $a);
8 $a_total = count($a_split)-1;
9 for ($i = 0; $i <= $a_total; $i++) {
10 if (strstr($a_split[$i], "id:")) {
11 $AllyId = substr(trim($a_split[$i]), 3, strlen($a_split[$i])-3);
12 if ($i == $a_total) {
13 $syntax .= sprintf("aid = '%d'", $AllyId);
14 } else {
15 $syntax .= sprintf("aid = '%d' OR ", $AllyId);
16 }
17 //printf("Ally id: %d <br>\n", $AllyId);
18 } else {
19 $a_split[$i] = addslashes(trim($a_split[$i]));
20 if ($i == $a_total) {
21 $syntax .= sprintf("alliance = '%s'", $a_split[$i]);
22 } else {
23 $syntax .= sprintf("alliance = '%s' OR ", $a_split[$i]);
24 }
25 //echo "a: $a_split[$i] ($i / $a_total)<br>\n";
26 }
27 }
28 $syntax .= ")";
29 return $syntax;
30 } else {
31 $syntax = "aid=0";
32 return $syntax;
33 }
34}
35function s2syntax($s,$Syntax) {
36 global $_GET;
37 if (empty($s)) { return; }
38 if (empty($Syntax)) {
39 $t = sprintf("population >= %d", $s);
40 } else {
41 $t = sprintf(" OR population >= %d", $s);
42 }
43 return $t;
44}
45function s3syntax($s,$Syntax) {
46 global $_GET;
47 if (empty($s)) { return; }
48 if (empty($Syntax)) {
49 $t = sprintf("population >= %d", $s);
50 } else {
51 $t = sprintf(" AND population >= %d", $s);
52 }
53 return $t;
54}
55
56function p2syntax ($a,$syn) {
57 global $_GET;
58 if (empty($syn)) { $syntax = "("; } else { $syntax = $oldSyntax." OR ("; }
59 if (!empty($a)) {
60 $a_split = split(",", $a);
61 $a_total = count($a_split)-1;
62 for ($i = 0; $i <= $a_total; $i++) {
63 if (strstr($a_split[$i], "id:")) {
64 $AllyId = substr(trim($a_split[$i]), 3, strlen($a_split[$i])-3);
65 if ($i == $a_total) {
66 $syntax .= sprintf("uid = '%d'", $AllyId);
67 } else {
68 $syntax .= sprintf("uid = '%d' OR ", $AllyId);
69 }
70 //printf("Ally id: %d <br>\n", $AllyId);
71 } else {
72 $a_split[$i] = addslashes(trim($a_split[$i]));
73 if ($i == $a_total) {
74 $syntax .= sprintf("player = '%s'", $a_split[$i]);
75 } else {
76 $syntax .= sprintf("player = '%s' OR ", $a_split[$i]);
77 }
78 //echo "a: $a_split[$i] ($i / $a_total)<br>\n";
79 }
80 }
81 $syntax .= ")";
82 return $syntax;
83 } else {
84 //$syntax = "1";
85 return NULL;
86 }
87}
88
89function xy_sec ($sqph=3, $distance, $t=0) {
90 //printf("tsq = %s <br>\n", $t);
91 if ($t==0 or $distance<=30) {
92 $tt = 1;
93 } else {
94 $tt = 1+$t/10;
95 }
96 $uspeed = $sqph * $tt;
97 $f30time = 30 * 3600/$sqph; // 30 squares in seconds (instead of hours) devided by speed of unit
98 $rtime = ($distance-30) * 3600/$uspeed;
99/*
100 printf("%f / (%f * %f) <br>\n", $distance, $sqph, $tt);
101 $th = $rest / ($sqph * $tt);
102 $ts = $th * 60 * 60;
103 return $ts;
104*/
105 return $f30time+$rtime;
106}
107
108function sec2counter ($ts) {
109 $periods = array(
110 'hours' => 3600,
111 'minutes' => 60,
112 'seconds' => 1
113 );
114
115 foreach ($periods as $period=>$period_sec) {
116 if ($ts >= $period_sec) {
117 $duration[$period] = sprintf("%d", floor($ts / $period_sec));
118 $ts -= $duration[$period] * $period_sec;
119 } else {
120 $duration[$period] = 0;
121 }
122 }
123 //echo "th: $th ts: $ts \n";
124 return sprintf("%02d:%02d:%02d", $duration['hours'], $duration['minutes'], $duration['seconds']);
125}
126
127function traveltime ($sqph, $distance) {
128 $periods = array(
129 'hours' => 3600,
130 'minutes' => 60,
131 'seconds' => 1
132 );
133 # Distance divided by unit speed
134 $th = $distance / $sqph;
135 # Into seconds
136 $ts = $th * 60 * 60;
137
138 foreach ($periods as $period=>$period_sec) {
139 if ($ts >= $period_sec) {
140 $duration[$period] = sprintf("%02.0f", floor($ts / $period_sec));
141 $ts -= $duration[$period] * $period_sec;
142 } else {
143 $duration[$period] = 0;
144 }
145 }
146 //echo "th: $th ts: $ts \n";
147 return $duration['hours'].":".$duration['minutes'].":".$duration['seconds']."\n";
148}
149function time_to_sec ($timestring) {
150 $t = explode(":", $timestring);
151 $sec = ($t[0]*3600+$t[1]*60+$t[2]);
152 return $sec;
153}
154function time_to_iso ($in, $st) { /* incoming counter, server time */
155 $in_s = time_to_sec($in);
156 $st_s = time_to_sec($st);
157 $dt_s = $in_s+$st_s;
158 $days_to_go = gmdate("z", $dt_s);
159 $time = gmdate("H:i:s", $dt_s);
160 if ($days_to_go == 0) {
161 $date = date("Y-m-d", strtotime("today"));
162 } else {
163 $date = date("Y-m-d", strtotime($days_to_go." day"));
164 }
165 $return = $date." ".$time;
166 #printf("Date debug - Timer(%s) - Servertime (%s) - Result(%s) <br>\n", $in, $st, $return);
167 return $return;
168}
169
170function allow_referer ($referer, $allowlist) {
171 //return 1;
172 if (empty($referer)) { return 1; }
173 preg_match('@^(?:http://)?([^/]+)@i', $referer, $TEMP);
174 $REFHOST = $TEMP[0];
175 preg_match('/[^.]+\.[^.]+$/', $REFHOST, $TEMP);
176 $REFHOST = $TEMP[0];
177 if (in_array($REFHOST,$allowlist)) { return 1; } else { return 0; }
178}
179
180function in_blocklist ($host, $blocklist) {
181 if (in_array($host,$blocklist)) { return 1; } else { return 0; }
182}
183
184function xytoz ($x, $y) {
185 $return = ($x + 401) + ((400 - $y) * 801);
186 return $return;
187}
188
189function ztoxy ($z) {
190 $y=ceil(400-$z/801);
191 $x=z-401-((400-$y)*801);
192 return(array(
193 'x' => $x,
194 'y' => $z
195 ));
196}
197
198function serverNameFix ($dumpname) {
199 if (preg_match('/^http:\/\/(?P<server>[a-z.0-9]+)/', $dumpname, $match)) {
200 return $match['server'];
201 }
202 return false;
203}
204
205
206function betterServerlist($Servers) {
207 $return = array();
208 function cmp($a, $b) {
209 $ab = strcmp($a['cc'], $b['cc']);
210 if ($ab == 0) {
211 return strcmp($a['short'], $b['short']);
212 }
213 return $ab;
214 }
215
216 foreach ($Servers as $sid=>$s) {
217 $name = serverNameFix($s['dumpfile']);
218 if (!preg_match('/\.(\w+)$/i', $name, $cc)) { die("Cannot match cc for ".$name); }
219 if (!preg_match('/^(\w+)\./i', $name, $short)) { die("Cannot match short"); }
220 $return[] = array(
221 'name' => $name,
222 'cc' => $cc[1],
223 'short' => $short[1],
224 'table' => $s['name'],
225 'sid' => $sid
226 );
227 }
228 usort($return, 'cmp');
229 return $return;
230}
231
232function srt_sid ($a, $b) {
233 return strcmp($a['orgname'], $b['orgname']);
234}
235
236function in_whitelist ($id, $WhiteList) {
237 if (isset($WhiteList[$id])) { return 1; } else { return 0; }
238}
239
240function logit ($file, $message) {
241 // Lets give the message a timestamp and a linebreak
242 $message = sprintf("%s - %s\n", date("d.m.Y - H:i:s"), $message);
243 if (is_writable($file)) {
244 if (!$handle = fopen($file, 'a')) {
245 printf("unable to open file for writing");
246 return -1;
247 }
248 if (fwrite($handle, $message) === FALSE) {
249 printf("unable to write");
250 return -1;
251 }
252 fclose($handle);
253
254 } else {
255 printf("file is not writable");
256 return -1;
257 }
258}
259
260?>