-
Notifications
You must be signed in to change notification settings - Fork 0
/
RunGameMode.py
83 lines (77 loc) · 3.08 KB
/
RunGameMode.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import glob
from os import path
import os
import sys
import importlib
import itertools
import xml.etree.ElementTree as ET
from itertools import combinations,repeat
from Utils.IniConfigProvider import IniConfigProvider
import os.path
import time
from Engine.Controllers.GameController import GameController
from time import sleep
class RunGameMode(object):
def __init__(self,botfile,inifile,gamespercouple,threads):
self._ConfigProvider=IniConfigProvider(inifile)
self._GamesPerCouple=gamespercouple
botsdataparsed=self._parseBots(botfile)
gameSchedualeBuilt=self._BuildGameScheduale(gamespercouple)
self._Threads = threads
self._Valid=botsdataparsed and self._ConfigProvider.ConfigValid
@property
def valid(self) -> bool:
return self._Valid
def _BuildGameScheduale(self,gamespercouple):
pairs=list(itertools.combinations(self._BotsData.keys(),2))
self._TestsKeys=[]
for i in range(0,gamespercouple):
self._TestsKeys+=pairs
return True
def Run(self):
for gamepair in self._TestsKeys:
try:
sleep(2)
firstPlayer = self._InstanceCreator(gamepair[0], self._BotsData[gamepair[0]])
secondPlayer = self._InstanceCreator(gamepair[1], self._BotsData[gamepair[1]])
gamecontroller = GameController(self._ConfigProvider, firstPlayer, secondPlayer)
if not gamecontroller.valid:
print ("Invalid game controller")
else:
print(f"Game between {gamepair[0]} and gamepair{gamepair[1]}")
starttime = time.time()
gamecontroller.Run()
endtime=time.time()
print(f"Game took {endtime-starttime} seconds Winner={gamecontroller.VictoryReason.winner} WinnignReason={gamecontroller.VictoryReason.winningireason}")
except:
print(sys.exc_info())
def _parseBots(self, botfile):
self._BotsData={};
if path.isfile(botfile):
try:
tree = ET.parse(botfile)
root = tree.getroot()
for child in root:
if 'args' in child.attrib:
self._BotsData[child.attrib['file']]=(child.attrib['module'],child.attrib['classname'],child.attrib['args'])
else:
self._BotsData[child.attrib['file']] = (child.attrib['module'], child.attrib['classname'])
return True
except:
return False
else:
return False
def _InstanceCreator(self,filename,data):
result=None
sys.path.append(os.path.dirname(filename))
try:
module = importlib.import_module(data[0])
class_=getattr(module,data[1])
if (len(data)>2):
result=class_(data[2])
else:
result = class_()
except ImportError:
print ("unable to load module:" + filename)
return (None)
return result