-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
107 lines (94 loc) · 3.34 KB
/
main.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
import sys
import os
import argparse
from exceptions import PlayersException
'''
This horrible code parse arguments and import config and config_helpers,
with function initialize_game_environment it add game_path to sys.path
'''
import config_helpers
if __name__ == '__main__':
'''
These strings add project_root to sys.path
'''
project_root = os.path.normpath(os.path.join(os.path.dirname(__file__)))
if project_root not in sys.path:
sys.path.insert(0, project_root)
'''
Argument parseing:
'''
arg_parser = argparse.ArgumentParser(description='''
Framework runs game, placed in "game_path".
Players in this tournament read from file
"players_config" file, placed in "game_path"
path. Other settings of tournament should be written
in file "config.py", placed in "game_path".''')
arg_parser.add_argument(
'game_path',
help='Directory containing game'
)
arg_parser.add_argument(
'-tid', '--tournament-id', type=int,
help='''
Tournament id which is used for saving logs,
if you don't set tournament id it will be least non-used one'''
)
args = arg_parser.parse_args()
'''
If tournament id isn't setted it will be least non-used one
'''
config_helpers.initialize_game_environment(args.game_path)
import config
from tournament_stages.tournament import Tournament
from utils import print_tournament_system_results
import bot
class Main:
def __init__(self, game_path, tournament_id):
self._game_path = game_path
self._players_list = None
self._tournament_id = tournament_id
self.tournament = None
def _load_players(self):
'''
Load players_list with players_parse(it parses
file containing player's information), filename is there in config
'''
players_config_path = os.path.join(self._game_path,
config.players_config)
self._players_list = config_helpers.players_parse(players_config_path)
if self._players_list is None:
raise PlayersException('Players list doesn\'t exist')
def _run_tournament(self):
'''
Run tournament and get it's results
'''
self.tournament = Tournament(self._players_list,
self._tournament_id)
self.tournament.run()
self.tournament_results = self.tournament.get_results()
def show_result(self):
return self.tournament_results
def _print_tournament_results(self, ts):
print_tournament_system_results(ts)
def _get_free_dirname(self, path, dirname_begin):
for i in range(1, 1024):
name = os.path.normpath(os.path.join(path, dirname_begin + str(i)))
if not os.path.exists(name):
return i
def _make_good_tournament_id(self):
if not self._tournament_id:
self._tournament_id = self._get_free_dirname(
path='logs/',
dirname_begin='tournament'
)
def main(self):
self._load_players()
self._make_good_tournament_id()
try:
self._run_tournament()
self._print_tournament_results(self.tournament.tournament_system)
except bot.ExecuteError:
pass
if __name__ == '__main__':
main = Main(args.game_path, args.tournament_id)
main.main()