start adjustment to new cli interface: better parsing and default values

This commit is contained in:
Maximilian Keßler 2023-11-11 23:09:13 +01:00
parent f0263ab250
commit eb0699e775
Signed by: max
GPG Key ID: BCC5A619923C0BA5

View File

@ -15,23 +15,26 @@ with open(DATA_FILE, 'r') as f:
def analyze_game(game_id: int): def analyze_game(game_id: int):
max_draw_pile_size = 15
probabilities = {} probabilities = {}
raw_output = None raw_output = None
try: try:
result = subprocess.run(['./endgame-analyzer', '-g', str(game_id), '-d', '16', '--interactive', '0', '--quiet', '-r'], stdout=subprocess.PIPE, timeout=30) result = subprocess.run(['./endgame-analyzer', '-g', str(game_id), '-d', str(max_draw_pile_size), '--interactive', '0', '--quiet', '-r'], stdout=subprocess.PIPE, timeout=10)
raw_output = result.stdout raw_output = result.stdout
except subprocess.TimeoutExpired as time_err: except subprocess.TimeoutExpired as time_err:
raw_output = time_err.stdout raw_output = time_err.stdout
output = raw_output.decode('utf-8') output = raw_output.decode('utf-8')
print(output)
# Check if the game was just over before reaching the specified draw pile size # Check if the game was just over before reaching the specified draw pile size
if re.match("This given draw pile size \(.*\) cannot be obtained with the specified replay.", output): if re.search(r'The given draw pile size \({}\) cannot be obtained with the specified replay.'.format(max_draw_pile_size), output):
for i in range(0,16): print('detected empty output')
probabilities[str(i)] = 0 return {str(size): 0 for size in range(1, max_draw_pile_size + 1)}
return probabilities
# Now, parse all results that we obtained (unclear how many depending on whether we ran into the timeout) # Now, parse all results that we obtained (unclear how many depending on whether we ran into the timeout)
for m in re.finditer('Probability with ([0-9]+) cards left in the deck: .*/.* ~ ([0-9.]+)', output): x = 'Probability with 13 cards left in deck: 5855/6237 ~ 93.875%'
for m in re.finditer('Probability with ([0-9]+) cards left in deck: .*/.* ~ ([0-9.]+)', output):
probabilities[str(m.group(1))] = m.group(2) probabilities[str(m.group(1))] = m.group(2)
return probabilities return probabilities
@ -64,10 +67,12 @@ def full_analyze_game_cached(game_id: int):
return result return result
def analyze_game_cached(game_id: int): def analyze_game_cached(game_id: int):
cached = DATA['normal'].get(str(game_id), None) cached = DATA.get('normal', {}).get(str(game_id), None)
if cached is not None: if cached is not None:
return cached return cached
result = analyze_game(game_id) result = analyze_game(game_id)
if 'normal' not in DATA.keys():
DATA['normal'] = {}
DATA['normal'][game_id] = result DATA['normal'][game_id] = result
save_cache() save_cache()
return result return result