forked from aichallenge/aichallenge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd_maps_to_database.py
executable file
·51 lines (43 loc) · 1.66 KB
/
add_maps_to_database.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
#!/usr/bin/python
import os
import sys
import MySQLdb
from server_info import server_info
from sql import sql
def main():
# get list of all map files
maps_path = server_info["maps_path"]
map_files = set()
for root, dirs, files in os.walk(maps_path):
for filepath in files:
if filepath.endswith(".map"):
map_files.add(os.path.join(root, filepath)[len(maps_path)+1:])
# get list of maps in 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()
cursor.execute(sql["select_map_filenames"])
db_maps = set([row[0] for row in cursor.fetchall()])
# get maps not in database
new_maps = map_files.difference(db_maps)
# add new maps to database with top priority
if len(new_maps) > 0:
cursor.execute(sql["update_map_priorities"])
for mapfile in new_maps:
players = 0
with open(os.path.join(maps_path,mapfile), 'r') as f:
for line in f:
if line.startswith('players'):
players = int(line.split()[1])
break
if players:
cursor.execute(sql["insert_map_filenames"], (mapfile, players))
print(mapfile)
connection.commit()
print('{0} maps added to database'.format(len(new_maps)))
else:
print("No maps added, priorities not changed.")
if __name__ == "__main__":
main()