#!/home/lindahl/.pyenv/versions/3.6.0/bin/python

'''
Run a simulation of player orders.

Expects:
- lib.NNN.tar.gz, the output of build-player-lib
- spool.NNN, a directory containing orders
- an executable for the Olympia C code plus helper scripts
'''

import os
import sys
import subprocess

from olypy.oid import to_oid

turn = sys.argv[1]
turn = int(turn) - 1

subprocess.run('rm -rf lib'.split(), check=True)
subprocess.run('tar xf lib.{}.tar.gz'.format(turn).split(), check=True)
subprocess.run('cp -p spool.{}/m.* lib/spool'.format(turn), shell=True, check=True)

subprocess.run('./olympia -E'.split(), check=True)
subprocess.run('./olympia -rS'.split(), check=True)

# iterate over all possible turn outputs in lib/save/NNN, and generate reports
turn += 1
names = os.listdir(os.path.join('lib', 'save', str(turn)))
for n in names:
    fullname = os.path.join('lib', 'save', str(turn), n)
    print('considering processing', fullname)
    if n.isdigit() and int(n) > 50000:
        subprocess.run('./g2rep {} > {}.{}'.format(fullname, to_oid(n), str(turn)), shell=True, check=True)

subprocess.run('tar czf lib-next.{}.tar.gz lib'.format(turn).split(), check=True)
