~fkmclane/armagetronad/armagetron.py

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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
<!DOCTYPE html>
<html>
	<head>
		<title>Scripting Library</title>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
		<link href="/res/common.css" rel="stylesheet" type="text/css"/>
		<style>
			.content table {
				width: 100%;
			}

			.content table td:first-child {
				width: 200px;
			}

			.content pre {
				background: rgba(60, 110, 80, 0.6);
				border: 2px solid #000;
				padding: 0px 40px 0px 40px;
			}
		</style>
	</head>
	<body>
		<div class="header">
			<img src="/res/aalogo.png" alt="" class="logo"/>
			<span class="title">Scripting Library</span>
		</div>
		<div class="content">
			To use the library, you must first use <code>import armagetron</code> at the beginning of your python script.  You must also call <code>armagetron.run()</code> after all of your handler initialization.  This function will then go into a loop and parse all of the ladderlog commands for you.  Examples are available at the bottom.
			<br/>
			<br/>
			<h1>Namespace <code>armagetron</code>:</h1>
			<table border="1">
				<tr>
					<td rowspan="2"><code>run()</code></td>
					<td>Description:</td>
					<td>Let the library listen to the ladderlog.</td>
				</tr>
				<tr>
				</tr>
				<tr>
					<td rowspan="2"><code>send_command(command)</code></td>
					<td>Description:</td>
					<td>Sends a command to the server.</td>
				</tr>
				<tr>
					<td>Parameters:</td>
					<td><code>command</code> (string) - Command to send to the server.  A newline is added automatically.</td>
				</tr>
				<tr>
					<td rowspan="2"><code>add_handler(command, handler)</code></td>
					<td>Description:</td>
					<td>Adds a handler that will be called when the specified ladderlog command is received.</td>
				</tr>
				<tr>
					<td>Parameters:</td>
					<td><code>command</code> (string) - Ladderlog command to attach the handler.<br/><br/>
					<code>handler</code> (function) - A callback function in the form of <code>handler(command)</code> where command is a list representing the parameters of the command where the first parameter is the command itself.</td>
				</tr>
				<tr>
					<td rowspan="2"><code>remove_handler(command, handler)</code></td>
					<td>Description:</td>
					<td>Removes a handler from a ladderlog command.</td>
				</tr>
				<tr>
					<td>Parameters:</td>
					<td><code>command</code> (string) - Ladderlog command of the handler.<br/><br/>
					<code>handler</code> (function) - The handler to remove.</td>
				</tr>
				<tr>
					<td rowspan="2"><code>set_chat_handler(command, handler)</code></td>
					<td>Description:</td>
					<td>Sets the handler for a chat command.</td>
				</tr>
				<tr>
					<td>Parameters:</td>
					<td><code>command</code> (string) - Chat command (including the beginning slash) to attach the handler.<br/><br/>
					<code>handler</code> (function) - A callback function in the form of <code>handler(command)</code> where command is a list representing the command where the first element is the command itself (including the beginning slash), the second element is the player's name who issued the command, the third element is the player's IP address, the fourth element is the player's access level, and the rest are the command arguments.</td>
				</tr>
				<tr>
					<td rowspan="2"><code>remove_chat_handler(command)</code></td>
					<td>Description:</td>
					<td>Removes a handler from a chat command (making the command unknown).</td>
				</tr>
				<tr>
					<td>Parameters:</td>
					<td><code>command</code> (string) - Chat command to remove the handler from (including the beginning slash).</td>
				</tr>
				<tr>
					<td rowspan="2"><code>say(message)</code></td>
					<td>Description:</td>
					<td>Sends the SAY command with the message.  Appears as "<code><span style="color: red">Admin</span>: message</code>" in each player's console.</td>
				</tr>
				<tr>
					<td>Parameters:</td>
					<td><code>message</code> (string) - Message to say.</td>
				</tr>
				<tr>
					<td rowspan="2"><code>console_message(message)</code></td>
					<td>Description:</td>
					<td>Prints <code>message</code> to every player's console.</td>
				</tr>
				<tr>
					<td>Parameters:</td>
					<td><code>message</code> (string) - Message to send.</td>
				</tr>
				<tr>
					<td rowspan="2"><code>center_message(message)</code></td>
					<td>Description:</td>
					<td>Prints <code>message</code> on everybody's screens.</td>
				</tr>
				<tr>
					<td>Parameters:</td>
					<td><code>message</code> (string) - Message to send.</td>
				</tr>
				<tr>
					<td rowspan="2"><code>send_message(player, message)</code></td>
					<td>Description:</td>
					<td>Prints <code>message</code> to the specified player's console.</td>
				</tr>
				<tr>
					<td>Parameters:</td>
					<td><code>player</code> (string or Player) - Player to send the message.<br/><br/>
					<code>message</code> (string) - Message to send.</td>
				</tr>
				<tr>
					<td rowspan="2"><code>pause_round()</code></td>
					<td>Description:</td>
					<td>Tell the server to wait before starting a new round.</td>
				</tr>
				<tr>
				</tr>
				<tr>
					<td rowspan="2"><code>continue_round()</code></td>
					<td>Description:</td>
					<td>Tell the server to start the round (if it was paused).</td>
				</tr>
				<tr>
				</tr>
				<tr>
					<td rowspan="2"><code>set_repository(address)</code></td>
					<td>Description:</td>
					<td>Sets the resource server.</td>
				</tr>
				<tr>
					<td>Parameters:</td>
					<td><code>address</code> (string) - URI of the resource server.</td>
				</tr>
				<tr>
					<td rowspan="2"><code>set_map(resource)</code></td>
					<td>Description:</td>
					<td>Sets the current map.</td>
				</tr>
				<tr>
					<td>Parameters:</td>
					<td><code>resource</code> (string) - Resource of the map.</td>
				</tr>
				<tr>
					<td rowspan="2"><code>include(config)</code></td>
					<td>Description:</td>
					<td>Includes a configuration file.</td>
				</tr>
				<tr>
					<td>Parameters:</td>
					<td><code>config</code> (string) - Name of the configuration file to include.</td>
				</tr>
				<tr>
					<td rowspan="2"><code>rinclude(config)</code></td>
					<td>Description:</td>
					<td>Includes a configuration file from a resource.</td>
				</tr>
				<tr>
					<td>Parameters:</td>
					<td><code>config</code> (string) - Resource address of the configuration file to include.</td>
				</tr>
				<tr>
					<td rowspan="2"><code>reload()</code></td>
					<td>Description:</td>
					<td>Reload's the server's configuration by including the default settings.</td>
				</tr>
				<tr>
				</tr>
				<tr>
					<td rowspan="2"><code>end_round()</code></td>
					<td>Description:</td>
					<td>Ends the round by spawning the win/death zone immediately.</td>
				</tr>
				<tr>
				</tr>
			</table>
			<br/>
			<br/>
			<h1>Class <code>Grid</code> (instantiated at <code>armagetron.grid</code>):</h1>
			<table border="1">
				<tr>
					<td rowspan="3"><code>get_team(name)</code></td>
					<td>Description:</td>
					<td>Get a Team object by name.</td>
				</tr>
				<tr>
					<td>Parameters:</td>
					<td><code>name</code> (string) - Name of the team to get.</td>
				</tr>
				<tr>
					<td>Return:</td>
					<td><code>team</code> (Team) - Team object with the given name.</td>
				</tr>
				<tr>
					<td rowspan="3"><code>get_player(name)</code></td>
					<td>Description:</td>
					<td>Get a Player object by name.</td>
				</tr>
				<tr>
					<td>Parameters:</td>
					<td><code>name</code> (string) - Name of the player to get.</td>
				</tr>
				<tr>
					<td>Return:</td>
					<td><code>player</code> (Player) - Player object with the given name.</td>
				</tr>
				<tr>
					<td rowspan="3"><code>get_zone(name)</code></td>
					<td>Description:</td>
					<td>Get a Zone object by name.</td>
				</tr>
				<tr>
					<td>Parameters:</td>
					<td><code>name</code> (string) - Name of the zone to get.</td>
				</tr>
				<tr>
					<td>Return:</td>
					<td><code>zone</code> (Zone) - Zone object with the given name.</td>
				</tr>
				<tr>
					<td rowspan="3"><code>create_zone(name, type, x, y, size, growth=0, xdir=0, ydir=0, interactive=None, r=None, g=None, b=None, target_size=None, rubber=None, player=None, owner=None, target_command=None)</code></td>
					<td>Description:</td>
					<td>Create a new zone.</td>
				</tr>
				<tr>
					<td>Parameters:</td>
					<td><code>name</code> (string) - Name of the new zone.<br/><br/>
					<code>type</code> (string) - Type of the new zone.<br/><br/>
					<code>x</code> (float) - The x coordinate of the new zone.<br/><br/>
					<code>y</code> (float) - The y coordinate of the new zone.<br/><br/>
					<code>size</code> (float) - Size of the new zone.<br/><br/>
					<code>growth</code> (float) - Growth of the new zone. Defaults to 0.<br/><br/>
					<code>xdir</code> (float) - Change in the x direction of the new zone. Defaults to 0.<br/><br/>
					<code>ydir</code> (float) - Change in the y direction of the new zone. Defaults to 0.</td>
				</tr>
				<tr>
					<td>Return:</td>
					<td><code>zone</code> (Zone) - The newly created zone (also stored internally).</td>
				</tr>
				<tr>
					<td><code>round</code> (int)</td>
					<td>Description:</td>
					<td>Current round number.</td>
				</tr>
				<tr>
					<td><code>num_players</code> (int)</td>
					<td>Description:</td>
					<td>Number of people playing.</td>
				</tr>
			</table>
			<br/>
			<br/>
			<h1>Class <code>Team</code>:</h1>
			<table border="1">
				<tr>
					<td><code>name</code> (string)</td>
					<td>Description:</td>
					<td>The team's name.</td>
				</tr>
				<tr>
					<td><code>score</code> (int)</td>
					<td>Description:</td>
					<td>The team's score.</td>
				</tr>
				<tr>
					<td><code>players</code> (Player[])</td>
					<td>Description:</td>
					<td>Dictionary of the team's players indexed by name.</td>
				</tr>
				<tr>
					<td><code>positions</code> (int[])</td>
					<td>Description:</td>
					<td>A list of players on the team representing their position. For example, you can get the player in position with <code>positions[0]</code>.</td>
				</tr>
			</table>
			<br/>
			<br/>
			<h1>Class <code>Player</code>:</h1>
			<table border="1">
				<tr>
					<td rowspan="2"><code>send_message(message)</code></td>
					<td>Description:</td>
					<td>Sends a message to the player's console.</td>
				</tr>
				<tr>
					<td>Parameters:</td>
					<td><code>message</code> (string) - The message to send.</td>
				</tr>
				<tr>
					<td rowspan="2"><code>kill()</code></td>
					<td>Description:</td>
					<td>Kills the player instantly.</td>
				</tr>
				<tr>
				</tr>
				<tr>
					<td rowspan="2"><code>kick(reason=None)</code></td>
					<td>Description:</td>
					<td>Kicks a player for a specified reason.</td>
				</tr>
				<tr>
					<td>Parameters:</td>
					<td><code>reason</code> (string) - The message displayed when the player is kicked.  Defaults to None.</td>
				</tr>
				<tr>
					<td rowspan="2"><code>ban(time=None, reason=None)</code></td>
					<td>Description:</td>
					<td>Bans a player for the specified time and reason.</td>
				</tr>
				<tr>
					<td>Parameters:</td>
					<td><code>time</code> (int) - The amount of minutes to ban the player.  Defaults to Armagetron's decision.<br/><br/>
					<code>reason</code> (string) - The message displayed when the player is banned.  Defaults to None.</td>
				</tr>
				<tr>
					<td rowspan="2"><code>ban_ip(time=None, reason=None)</code></td>
					<td>Description:</td>
					<td>Bans a player's IP address for a specified time and reason.</td>
				</tr>
				<tr>
					<td>Parameters:</td>
					<td><code>time</code> (int) - The amount of minutes to ban the player.  Defaults to Armagetron's decision.<br/><br/>
					<code>reason</code> (string) - The message displayed when the player is banned.  Defaults to None.</td>
				</tr>
				<tr>
					<td rowspan="2"><code>declare_winner()</code></td>
					<td>Description:</td>
					<td>Sets the player as the winner and ends the round.</td>
				</tr>
				<tr>
				</tr>
				<tr>
					<td rowspan="2"><code>teleport(x, y, xdir, ydir)</code></td>
					<td>Description:</td>
					<td>Teleport the player to the specified location.  Note: This function works in sty+ct only.</td>
				</tr>
				<tr>
					<td>Parameters:</td>
					<td><code>x</code> (float) - The x coordinate of the player.<br/><br/>
					<code>y</code> (float) - The y coordinate of the player.<br/><br/>
					<code>xdir</code> (int) - The x direction of the player.  Its value is based on the current axis system.  For example, you could put 1 here in four axes to mean go forward on the x axis.<br/><br/>
					<code>ydir</code> (int) - The y direction of the player.  Its value is based on the current axis system.  For example, you could put 1 here in four axes to mean go forward on the y axis.</td>
				</tr>
				<tr>
					<td rowspan="2"><code>respawn(x, y, xdir, ydir)</code></td>
					<td>Description:</td>
					<td>Respawns the player at the specified location.  Note: This function works in sty+ct only.</td>
				</tr>
				<tr>
					<td>Parameters:</td>
					<td><code>x</code> (float) - The x coordinate of the player.<br/><br/>
					<code>y</code> (float) - The y coordinate of the player.<br/><br/>
					<code>xdir</code> (int) - The x direction of the player.  Its value is based on the current axis system.  For example, you could put 1 here in four axes to mean go forward on the x axis.<br/><br/>
					<code>ydir</code> (int) - The y direction of the player.  Its value is based on the current axis system.  For example, you could put 1 here in four axes to mean go forward on the y axis.</td>
				</tr>
				<tr>
					<td><code>name</code> (string)</td>
					<td>Description:</td>
					<td>The player's internal name or login.</td>
				</tr>
				<tr>
					<td><code>screenname</code> (string)</td>
					<td>Description:</td>
					<td>The player's visible name, UTF-8 (Python's native) encoded in the case of special characters.</td>
				</tr>
				<tr>
					<td><code>ip</code> (string)</td>
					<td>Description:</td>
					<td>The player's IP address.</td>
				</tr>
				<tr>
					<td><code>score</code> (int)</td>
					<td>Description:</td>
					<td>The player's score.</td>
				</tr>
				<tr>
					<td><code>alive</code> (bool)</td>
					<td>Description:</td>
					<td>Whether or not the player is alive.</td>
				</tr>
			</table>
			<br/>
			<br/>
			<h1>Class <code>Zone</code>:</h1>
			<table border="1">
				<tr>
					<td rowspan="2"><code>change_color(r, g, b)</code></td>
					<td>Description:</td>
					<td>Sets the zone's color from an RGB triplet.</td>
				</tr>
				<tr>
					<td>Parameters:</td>
					<td><code>r</code> (int) - Red part of color.<br/><br/>
					<code>g</code> (int) - Green part of color.<br/><br/>
					<code>b</code> (int) - Blue part of color.</td>
				</tr>
				<tr>
					<td rowspan="2"><code>change_expansion(growth)</code></td>
					<td>Description:</td>
					<td>Sets the zone's growth factor.</td>
				</tr>
				<tr>
					<td>Parameters:</td>
					<td><code>growth</code> (float) - The zone's growth factor.</td>
				</tr>
				<tr>
					<td rowspan="2"><code>change_position(x, y)</code></td>
					<td>Description:</td>
					<td>Sets the zone's position.</td>
				</tr>
				<tr>
					<td>Parameters:</td>
					<td><code>x</code> (float) - The x coordinate of zone.<br/><br/>
					<code>y</code> (float) - The y coordinate of zone.</td>
				</tr>
				<tr>
					<td rowspan="2"><code>change_size(size, growth=None)</code></td>
					<td>Description:</td>
					<td>Sets the zone's size and a growth rate to it.</td>
				</tr>
				<tr>
					<td>Parameters:</td>
					<td><code>size</code> (float) - The zone's size.<br/><br/>
					<code>growth</code> (float) - The zone's growth rate to get to that size.  Defaults to instantly.</td>
				</tr>
				<tr>
					<td rowspan="2"><code>change_speed(xdir, ydir)</code></td>
					<td>Description:</td>
					<td>Set the zone's speed by components.</td>
				</tr>
				<tr>
					<td>Parameters:</td>
					<td><code>xdir</code> (float) - The zone's x component of the velocity.<br/><br/>
					<code>ydir</code> (float) - The zone's y component of the velocity.</td>
				</tr>
				<tr>
					<td rowspan="2"><code>set_command(command)</code></td>
					<td>Description:</td>
					<td>Set the command of the target zone.</td>
				</tr>
				<tr>
					<td>Parameters:</td>
					<td><code>command</code> (string) - Armagetron command to be called when the target zone is entered.</td>
				</tr>
				<tr>
					<td><code>name</code> (string)</td>
					<td>Description:</td>
					<td>The zone's name.</td>
				</tr>
				<tr>
					<td><code>type</code> (string)</td>
					<td>Description:</td>
					<td>The zone's type.</td>
				</tr>
				<tr>
					<td><code>x</code> (float)</td>
					<td>Description:</td>
					<td>The (set) x coordinate of the zone.  Note: This will change if xdir is set.</td>
				</tr>
				<tr>
					<td><code>y</code> (float)</td>
					<td>Description:</td>
					<td>The (set) y coordinate of the zone.  Note: This will change if the ydir is set.</td>
				</tr>
				<tr>
					<td><code>size</code> (float)</td>
					<td>Description:</td>
					<td>The (set) size of the zone.</td>
				</tr>
				<tr>
					<td><code>growth</code> (float)</td>
					<td>Description:</td>
					<td>The growth of the zone.</td>
				</tr>
				<tr>
					<td><code>xdir</code> (float)</td>
					<td>Description:</td>
					<td>The x component of the velocity of the zone.</td>
				</tr>
				<tr>
					<td><code>ydir</code> (float)</td>
					<td>Description:</td>
					<td>The y component of the velocity of the zone.</td>
				</tr>
				<tr>
					<td colspan="3">Note: If these parameters are changed outside of the zone's functions (i.e. an Armagetron command), these values will not be accurate.</td>
				</tr>
				<tr>
					<td colspan="3">On destruction, the zone is simply set to collapse.</td>
				</tr>
			</table>
			<br/>
			<br/>
			<h1>Examples:</h1>
			<br/>
			<h2>Reset the settings when everyone leaves the game:</h2>
			<pre><code>
import armagetron

def reload(command):
  armagetron.reload()

armagetron.add_handler("GAME_END", reload)

armagetron.run()
			</code></pre>
			<br/>
			<h2>Simple echo chat command:</h2>
			<pre><code>
import armagetron

def echo(command):
  armagetron.send_message(command[1], " ".join(command[4:]))

armagetron.set_chat_handler("/echo", echo)

armagetron.run()
			</code></pre>
			<br/>
			<h2>Map rotation:</h2>
			<pre><code>
import armagetron

maps = [ "Anonymous/polygon/regular/square-1.0.1.aamap.xml", "Anonymous/polygon/regular/40-gon-0.2.aamap.xml", "kyle/other/maze-0.0.1.aamap.xml" ]

index = 0

def change_map(command):
  global index

  armagetron.pause_round()
  index++
  if index &gt; len(maps):
    index = 0
  armagetron.set_map(maps[index])
  armagetron.continue_round()

armagetron.add_handler("NEW_ROUND", change_map)

armagetron.run()
			</code></pre>
		</div>
	</body>
</html>