42 lines
1.2 KiB
Python
42 lines
1.2 KiB
Python
import subprocess
|
|
from typing import Final
|
|
|
|
import diceplayer.dice.dice_input as dice_input
|
|
|
|
from pathlib import Path
|
|
|
|
from diceplayer.config import DiceConfig
|
|
|
|
|
|
DICE_FLAG_LINE: Final[int] = -2
|
|
DICE_END_FLAG: Final[str] = "End of simulation"
|
|
|
|
|
|
class DiceWrapper:
|
|
def __init__(self, dice_config: DiceConfig, working_directory: Path):
|
|
self.dice_config = dice_config
|
|
self.working_directory = working_directory
|
|
|
|
def run(self, dice_config: dice_input.BaseConfig) -> None:
|
|
input_path = dice_config.write(self.working_directory)
|
|
output_path = input_path.parent / (input_path.name + ".out")
|
|
|
|
with open(output_path, "w") as outfile, open(input_path, "r") as infile:
|
|
exit_status = subprocess.call(
|
|
self.dice_config.progname, stdin=infile, stdout=outfile
|
|
)
|
|
|
|
if exit_status != 0:
|
|
raise RuntimeError(f"Dice simulation failed with exit status {exit_status}")
|
|
|
|
with open(output_path, "r") as outfile:
|
|
line = outfile.readlines()[DICE_FLAG_LINE]
|
|
if line.strip() == DICE_END_FLAG:
|
|
return
|
|
|
|
raise RuntimeError(f"Dice simulation failed with exit status {exit_status}")
|
|
|
|
def extract_results(self) -> dict:
|
|
return {}
|
|
|