-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtournament_manager.py
executable file
·187 lines (176 loc) · 6.22 KB
/
tournament_manager.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
import engine
import logging
import logging.handlers
import MySQLdb
import os
import random
from server_info import server_info
import subprocess
import sys
import time
import zlib
# Set up logging
logger = logging.getLogger('tm_logger')
logger.setLevel(logging.INFO)
handler = logging.handlers.RotatingFileHandler("tm.log",
maxBytes=1000000,
backupCount=5)
logger.addHandler(handler)
tm_pid = os.getpid()
def log_message(message):
logger.info(str(tm_pid) + ": " + message)
print message
log_message("started. time is " + str(time.time()))
log_message("parsing command-line arguments")
if len(sys.argv) != 2:
print "USAGE: python tournament_manager.py time_limit_in_seconds"
sys.exit(1)
time_limit = int(sys.argv[1])
log_message("time_limit: " + str(time_limit))
log_message("connecting to db")
# Connect to the database.
connection = MySQLdb.connect(host = server_info["db_host"],
user = server_info["db_username"],
passwd = server_info["db_password"],
db = server_info["db_name"])
cursor = connection.cursor(MySQLdb.cursors.DictCursor)
log_message("db connection established")
log_message("getting active submissions from db")
# Get the list of currently active submissions.
cursor.execute("""
SELECT s.*, l.*
FROM submissions s
INNER JOIN (
SELECT
MAX(submission_id) AS submission_id
FROM submissions
GROUP BY user_id
) AS o ON o.submission_id = s.submission_id
INNER JOIN languages l ON l.language_id = s.language_id
WHERE s.status = 40
""")
submissions = list(cursor.fetchall())
log_message("found " + str(len(submissions)) + " active submissions")
# Get the list of maps.
log_message("fetching map list from db")
cursor.execute("SELECT * FROM maps")
maps = cursor.fetchall()
log_message("found " + str(len(maps)) + " maps")
# Get the rankings
cursor.execute("""
SELECT r.* from rankings r WHERE leaderboard_id = (select max(leaderboard_id) from leaderboards);
""")
rankings = cursor.fetchall()
log_message("found " + str(len(maps)) + " rankings")
# Order submissions by ranking
rankings_by_submission_id = {}
for ranking in rankings:
rankings_by_submission_id[ranking['submission_id']] = ranking['rank']
default_rank = int(len(rankings)/2)
for submission in submissions:
submission['rank'] = rankings_by_submission_id.get(submission['submission_id'],default_rank)
submissions.sort(key=lambda x: x['rank'])
# Are there enough players? Are there enough maps?
if len(submissions) < 2:
log_message("There are fewer than two active submissions. No games can " + \
"be played")
sys.exit(0)
if len(maps) < 1:
log_message("There are no maps in the database. No games can be played")
sys.exit(0)
log_message("starting tournament. time is " + str(time.time()))
start_time = time.time()
while time.time() - start_time < time_limit:
log_message("attempting to match two bots. time is " + str(time.time()))
map = random.choice(maps)
# Choose probably near bots to fight
pickable_spread = random.choice([5,10,10,20,40,100,200,1000])
player_one = random.choice(submissions)
player_two = player_one
while player_one == player_two:
offset = random.randint(-pickable_spread,pickable_spread) + submissions.index(player_one)
if offset < 0:
continue
if offset >= len(submissions):
continue
player_two = submissions[offset]
log_message("%s (rank %d) vs %s (rank %d) on %s" % (
player_one["submission_id"],
player_one["rank"],
player_two["submission_id"],
player_two["rank"],
map["name"]))
# Invoke the game engine.
player_one_path = "../submissions/" + str(player_one["submission_id"]) + "/."
player_two_path = "../submissions/" + str(player_two["submission_id"]) + "/."
map_path = "../maps/" + map["path"]
players = [
{"path" : player_one_path, "command" : player_one["command"]},
{"path" : player_two_path, "command" : player_two["command"]}
]
log_message("starting game")
outcome = engine.play_game(map_path, 1000, 200, players, debug=False)
log_message("game finished")
# Store the game outcome in the database
winner = "NULL"
loser = "NULL"
map_id = map["map_id"]
draw = 1
timestamp = "CURRENT_TIMESTAMP"
playback_string = ""
errors = ""
if "error" in outcome:
log_message("the game engine reported an error: " + outcome["error"])
if "winner" in outcome:
log_message("winner:" + str(outcome["winner"]))
if outcome["winner"] == 0:
pass
elif outcome["winner"] == 1:
winner = player_one["submission_id"]
loser = player_two["submission_id"]
draw = 0
elif outcome["winner"] == 2:
winner = player_two["submission_id"]
loser = player_one["submission_id"]
draw = 0
else:
errors += "Game engine reported invalid winner value: " + \
str(outcome["winner"]) + "\n"
else:
errors += "The engine did not report a winner."
if "playback" in outcome:
playback_string = outcome["playback"]
if len(errors) == 0:
log_message("inserting game outcome into the db")
cursor.execute("INSERT INTO games (winner,loser,map_id,draw,timestamp," + \
"player_one,player_two) VALUES (" + \
str(winner) + "," + \
str(loser) + "," + \
str(map_id) + "," + \
str(draw) + "," + \
str(timestamp) + "," + \
str(player_one["submission_id"]) + "," + \
str(player_two["submission_id"]) + ")")
game_id = connection.insert_id()
compressed_playback_string = zlib.compress(playback_string,9)
cursor = connection.cursor()
cursor.execute(
"INSERT INTO playback SET game_id = %s, playback_string = %s",
(game_id, compressed_playback_string)
)
game_id = connection.insert_id()
compressed_playback_string = zlib.compress(playback_string,9)
cursor = connection.cursor()
cursor.execute(
"INSERT INTO playback SET game_id = %s, playback_string = %s",
(game_id, compressed_playback_string)
)
log_message("finished inserting")
else:
log_message(errors)
# Close the database connection
log_message("closing db connection")
cursor.close()
connection.close()
log_message("db connection closed")
log_message("exiting")