-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfindTargets.php
224 lines (198 loc) · 5.38 KB
/
findTargets.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?
function minutesToStr($minutes)
{
if ($minutes < 60)
return sprintf("%dm", $minutes);
else if ($minutes < 1440)
return sprintf("%dh %dm",
floor($minutes / 60),
$minutes % 60
);
else
return sprintf("%dd %dh %dm",
floor($minutes / 1440),
floor(($minutes % 1440) / 60),
($minutes % 1440) % 60
);
}
?>
<html>
<head>
<title>P&W targets finder</title>
<style>
table {border-collapse: collapse;}
td
{
padding-left: 1em;
padding-right: 1em;
padding-top: 1ex;
padding-bottom: 1ex;
text-align: center;
}
</style>
</head>
<body>
<h1>P&W targets finder</h1>
<p>The cached list of nations was last updated
<?
$cacheAge = (time() - filemtime("nations.txt")) / 60;
$cacheAgeStr = minutesToStr($cacheAge);
echo($cacheAgeStr);
?>
ago.</p>
<?php
if (isset($_REQUEST["apiKey"]))
$apiKey = $_REQUEST["apiKey"];
else
$apiKey = "";
if (isset($_REQUEST["myNationId"]))
$myNationId = $_REQUEST["myNationId"];
else
$myNationId = 142203;
if (isset($_REQUEST["targetAlliances"]) && $_REQUEST["targetAlliances"] != "")
$targetAlliances = explode("\n", $_REQUEST["targetAlliances"]);
else
$targetAlliances = array("None");
$targetAlliances = array_map( trim, $targetAlliances);
$apiUrl = sprintf("http://politicsandwar.com/api/nation/id=%d/&key=%s", urlencode($myNationId), urlencode($apiKey));
// var_dump($apiUrl);
$f = fopen($apiUrl, "r");
$myNation = json_decode(fgets($f), TRUE);
//var_dump($myNation);
if ($myNation["success"] == true)
{
fclose($f);
$scoreLow = $myNation["score"]*0.75;
$scoreHigh = $myNation["score"]*1.75;
/* var_dump($targetAlliances); */
$f = fopen("nations.txt", "r");
$json = fgets($f);
fclose($f);
$targets = json_decode($json, TRUE)["nations"];
// var_dump(count($targets));
// var_dump($targetAlliances);
$targets = array_filter( $targets,
function($n){
global $targetAlliances;
return in_array($n["alliance"], $targetAlliances);
}
);
// var_dump(count($targets));
$targets = array_map(
function($n){
$n["score"] = (float)($n["score"]);
return $n;
}, $targets);
$targets = array_filter( $targets,
function($n){
global $scoreHigh, $scoreLow;
return $n["score"] >= $scoreLow
&& $n["score"] <= $scoreHigh
&& $n["vacmode"] == 0
; } );
?>
<p>Here are the nations that <a href="<?printf("https://politicsandwar.com/nation/id=%d", $myNationId)?>">
<?=$myNation["name"]?></a>
(score: <?=$myNation["score"]?>, alliance: <?=$myNation["alliance"]?>) could attack,
from the following alliances:
<ul>
<?
foreach ($targetAlliances as $alliance)
{
printf("<li>%s</li>\n", htmlentities($alliance));
}
?>
</ul>
<!-- Nations that are either beiged or out of defensive slots are shown in grey. -->
</p>
<table style="margin-left: auto; margin-right: auto;">
<thead style="text-align: center; font-weight: 600;">
<tr>
<td>nation</td>
<td>alliance</td>
<td>colour</td>
<td>score</td>
<td>cities</td>
<td>offensive<br/>wars</td>
<td>defensive<br/>wars</td>
<td>inactive</td>
</tr>
</thead>
<tbody>
<?
// var_dump($targets);
$rowCtr = 0;
foreach ($targets as $id=>$n)
{
$inactivity = $n["minutessinceactive"] + $cacheAge;
$isInactive = $inactivity > 20160;
$isBeige = $n["color"] == "beige";
$isFull = $n["defensivewars"] == 3;
if ($isBeige || $isFull)
// printf("<tr style=\"color: #AAAAAA;\">");
continue;
else
{
$rowCtr++;
$rowCtr % 2 == 0 ?
printf("<tr style=\"background-color:peachpuff\">")
:
printf("<tr style=\"background-color:lavender\">")
;
?>
<td style="text-align: right;">
<a href="https://politicsandwar.com/nation/id=<?=$n["nationid"]?>"
style="text-decoration: none; font-weight: bold;"
target="_blank">
<?=$n["nation"]?>
</a>
</td>
<td style="text-align: center;">
<a href="<?="https://politicsandwar.com/alliance/id=".$n["allianceid"]?>">
<?=$n["alliance"]?>
</a>
</td>
<td style="text-align: center;"><?=$n["color"]?></td>
<td style="text-align: right;"><?=sprintf("%.2f", $n["score"])?></td>
<td style="text-align: center;"><?=$n["cities"]?></td>
<td><?=$n["offensivewars"]?></td>
<td><?=$n["defensivewars"]?></td>
<td style="text-align: right;">
<?=minutesToStr($inactivity)?></td>
</tr>
<?
}
}
?>
</tbody>
</table>
<?
}
else
{
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
?>
<p>(No nation specified or JSON request failed. Try checking your API key?)</p>
<?
}
}
?>
<h2>New search</h2>
<form action="<?=$_SERVER["PHP_SELF"]?>" method="post">
Nation ID: <input type="text" name="myNationId" value="<?=$myNationId?>"/><br/>
Target alliances (one per line): <br/>
<textarea name="targetAlliances"
rows="24" cols="80"
style="width: 600px">
<?=isset($targetAlliances) ? implode("\n", $targetAlliances) : ""?>
</textarea>
<br/>
API key: <input type="text" name="apiKey" value="<?=$apiKey?>"/><br/>
<input type="submit" value="Search"/>
</form>
<p>You can find your API key on your <a
href="https://politicsandwar.com/account/#7" target="_blank">Account
Settings</a> page.</p>
</body>
</html>