example.py 1.3 KB
Newer Older
1
#!/usr/bin/env python2
m!nus's avatar
m!nus committed
2

3 4

from teeworlds.teeworlds import Teeworlds
m!nus's avatar
m!nus committed
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

# set up stuff
tw = Teeworlds(timeout=2)

# ask the masters for servers
tw.query_masters()

# query servers, wait for responses
# stops if no packet is received for `timeout` seconds
tw.run_loop()

# filter the serverlist
servers = tw.serverlist.find(name="^C", # Server whose name begins with "C", regex style
                            gametype="CTF", # gametype contains "CTF"
                            maxping=0.1) # ping is lower or equal to 100ms

# sort by ping
servers.sort(key=lambda s: s.latency)

# display a nice list
for server in servers:
        print("{server: <64} [{gametype: ^16}] on {master}: {clients: >2}/{max_clients: <2} - {latency: >4.0f} ms" \
        .format(server=server.name, gametype=server.gametype, master=server.master.name, clients=server.clients, \
                max_clients=server.max_clients, latency=server.latency*1000))

# check if m!nus is currently playing
minus_list = tw.playerlist.find(name="^m!nus$")
if minus_list.players:
    minus = minus_list.players[0]
    print("m!nus is currently playing on {server} ({address}) with {players} other player(s)."
          .format(server=minus.server.name, address=minus.server.address, players=(minus.server.players-1)))
else:
    print("m!nus isn't playing at the moment.")