Skip to content

Commit

Permalink
Make the path to matchmaking.json configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
Torom committed May 18, 2023
1 parent e00e004 commit b434362
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,4 @@ dmypy.json

# Project specific
*config.yml
matchmaking.json
*matchmaking.json
4 changes: 2 additions & 2 deletions game_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@


class Game_Manager(Thread):
def __init__(self, config: dict, api: API) -> None:
def __init__(self, config: dict, api: API, matchmaking_path: str) -> None:
Thread.__init__(self)
self.config = config
self.api = api
Expand All @@ -24,7 +24,7 @@ def __init__(self, config: dict, api: API) -> None:
self.started_game_ids: deque[Game_ID] = deque()
self.challenge_requests: deque[Challenge_Request] = deque()
self.changed_event = Event()
self.matchmaking = Matchmaking(self.config, self.api)
self.matchmaking = Matchmaking(self.config, self.api, matchmaking_path)
self.is_matchmaking_allowed = False
self.current_matchmaking_game_id: Game_ID | None = None
self.challenger = Challenger(self.config, self.api)
Expand Down
6 changes: 4 additions & 2 deletions matchmaking.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@


class Matchmaking:
def __init__(self, config: dict, api: API) -> None:
def __init__(self, config: dict, api: API, matchmaking_path: str) -> None:
self.api = api
self.next_update = datetime.now()
self.initial_time: int = config['matchmaking']['initial_time']
Expand All @@ -21,8 +21,10 @@ def __init__(self, config: dict, api: API) -> None:
self.max_rating_diff: int = config['matchmaking'].get('max_rating_diff', float('inf'))
self.estimated_game_duration = timedelta(seconds=(self.initial_time + self.increment * 80) * 2)
self.perf_types = [self._variant_to_perf_type(variant) for variant in config['matchmaking']['variants']]
matchmaking_delay = config['matchmaking'].get('delay', 10)
matchmaking_multiplier = config['matchmaking'].get('multiplier', 15)
self.opponents = Opponents(self.perf_types, self.estimated_game_duration,
config['matchmaking']['delay'], config['matchmaking'].get('multiplier', 15))
matchmaking_delay, matchmaking_multiplier, matchmaking_path)
self.challenger = Challenger(config, self.api)

self.game_start_time: datetime = datetime.now()
Expand Down
17 changes: 13 additions & 4 deletions opponents.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,19 @@ def __eq__(self, __o: object) -> bool:


class Opponents:
def __init__(self, perf_types: list[Perf_Type], estimated_game_duration: timedelta, delay: int, matchmaking_multiplier: int) -> None:
def __init__(
self,
perf_types: list[Perf_Type],
estimated_game_duration: timedelta,
delay: int,
matchmaking_multiplier: int,
matchmaking_path: str
) -> None:
self.perf_types = perf_types
self.estimated_game_duration = estimated_game_duration
self.delay = timedelta(seconds=delay)
self.matchmaking_multiplier = matchmaking_multiplier
self.matchmaking_path = matchmaking_path
self.opponent_list = self._load()
self.busy_bots: list[Bot] = []
self.last_opponent: tuple[Bot, Perf_Type, Challenge_Color] | None = None
Expand Down Expand Up @@ -132,12 +140,13 @@ def _find(self, perf_type: Perf_Type, username: str) -> Opponent:
return Opponent(username, {perf_type: Matchmaking_Data()})

def _load(self) -> list[Opponent]:
if os.path.isfile('matchmaking.json'):
with open('matchmaking.json', encoding='utf-8') as json_input:
if os.path.isfile(self.matchmaking_path):
with open(self.matchmaking_path, encoding='utf-8') as json_input:
return [Opponent.from_dict(opponent) for opponent in json.load(json_input)]
else:
print(f'Matchmaking file "{self.matchmaking_path}" not found. Create new one.')
return []

def _save(self) -> None:
with open('matchmaking.json', 'w', encoding='utf-8') as json_output:
with open(self.matchmaking_path, 'w', encoding='utf-8') as json_output:
json.dump([opponent.__dict__() for opponent in self.opponent_list], json_output, indent=4)
8 changes: 5 additions & 3 deletions user_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@


class UserInterface:
def __init__(self, config_path: str, start_matchmaking: bool, allow_upgrade: bool) -> None:
def __init__(self, config_path: str, matchmaking_path: str, start_matchmaking: bool, allow_upgrade: bool) -> None:
self.start_matchmaking = start_matchmaking
self.allow_upgrade = allow_upgrade
self.config = load_config(config_path)
self.api = API(self.config)
self.is_running = True
self.game_manager = Game_Manager(self.config, self.api)
self.game_manager = Game_Manager(self.config, self.api, matchmaking_path)
self.event_handler = Event_Handler(self.config, self.api, self.game_manager)

def main(self) -> None:
Expand Down Expand Up @@ -266,6 +266,8 @@ def complete(self, text: str, state: int) -> str | None:
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--config', '-c', default='config.yml', type=str, help='Path to config.yml.')
parser.add_argument('--matchmaking_file', '-mf', default='matchmaking.json',
type=str, help='Path to matchmaking.json.')
parser.add_argument('--matchmaking', '-m', action='store_true', help='Start matchmaking mode.')
parser.add_argument('--upgrade', '-u', action='store_true', help='Upgrade account to BOT account.')
parser.add_argument('--debug', '-d', action='store_const', const=logging.DEBUG,
Expand All @@ -274,5 +276,5 @@ def complete(self, text: str, state: int) -> str | None:

logging.basicConfig(level=args.debug)

ui = UserInterface(args.config, args.matchmaking, args.upgrade)
ui = UserInterface(args.config, args.matchmaking_file, args.matchmaking, args.upgrade)
ui.main()

0 comments on commit b434362

Please sign in to comment.