Skip to content

Commit e7659f1

Browse files
authored
Improvements to replay script: (#119)
- Check problem IDs. - Check contest data. - Allow to completely randomize team IDs. - Unify some error output.
1 parent 0e59a47 commit e7659f1

File tree

1 file changed

+34
-10
lines changed

1 file changed

+34
-10
lines changed

provision-contest/replay.py

+34-10
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
parser.add_argument('-s', '--submissionid', help='submission to start at.')
3030
parser.add_argument('--insecure', help='do not verify SSL certificate', action='store_true')
3131
parser.add_argument('-r', '--no_remap_teams', help='do not remap team ID\'s to team ID\'s of contest from API.', action='store_true')
32+
parser.add_argument('-I', '--ignore_teamids', help='Completely randomize teamids during replay, not storing any mapping.', action='store_true')
3233
parser.add_argument('-i', '--internal_data_source', help='The API uses an internal API source.', action='store_true')
3334
parser.add_argument('-f', '--simulation_speed', help='Speed up replay speed by this factor.')
3435

@@ -53,7 +54,23 @@
5354
submissions = json.load(open('submissions.json'))
5455
logging.info(f'Loaded {len(submissions)} submissions.')
5556

57+
problem_data = requests.get(f'{api_url}/contests/{contest}/problems', verify=verify).json()
58+
known_problem_ids = set([p['id'] for p in problem_data])
59+
used_problem_ids = set([s['problem_id'] for s in submissions])
60+
unknown_problem_ids = used_problem_ids.difference(known_problem_ids)
61+
if unknown_problem_ids:
62+
if len(unknown_problem_ids) == len(used_problem_ids):
63+
logging.critical('None of the used problem IDs is known.')
64+
sys.exit(-1)
65+
logging.error(f'Some problem IDs are used but not known: {unknown_problem_ids}')
66+
5667
contest_data = requests.get(f'{api_url}/contests/{contest}', verify=verify).json()
68+
if 'code' in contest_data and 'message' in contest_data and contest_data['code'] != 200:
69+
code = contest_data['code']
70+
msg = contest_data['message']
71+
logging.critical(f'Failed to retrieve contest, HTTP status code: {code}, message: {msg}')
72+
sys.exit(-1)
73+
5774
while not contest_data['start_time']:
5875
logging.info(f'Start time unknown - contest delayed.')
5976
time_diff = 30
@@ -67,18 +84,22 @@
6784
contest_start_obj = datetime.strptime(contest_data['start_time'], '%Y-%m-%dT%H:%M:%S%z')
6885
contest_duration = (datetime.strptime(contest_data['duration'], '%H:%M:%S.000') - datetime(1900, 1, 1)).total_seconds()
6986

70-
if not args.no_remap_teams:
87+
if args.no_remap_teams:
88+
if args.ignore_teamids:
89+
logging.critical('Cannot specify --no_remap_teams and --ignore_teamids at the same time.')
90+
sys.exit(-1)
91+
92+
logging.info('Keeping original team IDs.')
93+
else:
7194
# Get the teams from the contest
7295
team_data = requests.get(f'{api_url}/contests/{contest}/teams', verify=verify).json()
7396
team_ids = [team['id'] for team in team_data if not team['hidden']]
7497

7598
if not team_ids:
76-
print('Contest has no teams, can\'t submit')
99+
logging.critical('Contest has no teams, can\'t submit.')
77100
sys.exit(-1)
78101

79-
logging.info(f'Remapping teams to {len(team_ids)} teams from API')
80-
else:
81-
logging.info('Keeping original teams')
102+
logging.info(f'Remapping teams to {len(team_ids)} teams from API.')
82103

83104
now = time.time()
84105
orig_contest_duration = 5 * 60 * 60
@@ -151,11 +172,14 @@
151172
else:
152173
problem_label = problem_id
153174
if not args.no_remap_teams:
154-
if team_id not in team_problem_team_map:
155-
team_problem_team_map[team_id] = dict()
156-
if problem_label not in team_problem_team_map[team_id]:
157-
team_problem_team_map[team_id][problem_label] = random.choice(team_ids)
158-
team_id = team_problem_team_map[team_id][problem_label]
175+
if args.ignore_teamids:
176+
team_id = random.choice(team_ids)
177+
else:
178+
if team_id not in team_problem_team_map:
179+
team_problem_team_map[team_id] = dict()
180+
if problem_label not in team_problem_team_map[team_id]:
181+
team_problem_team_map[team_id][problem_label] = random.choice(team_ids)
182+
team_id = team_problem_team_map[team_id][problem_label]
159183
else:
160184
team_id = submission['team_id']
161185

0 commit comments

Comments
 (0)