-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimulator.py
210 lines (162 loc) · 7.32 KB
/
simulator.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
from operator import itemgetter
import re
import resultsDatabase
import subprocess
# this path is relative to the working directory; you should
# call these scripts from the same directory that contains the simulator
simulatorPath = "iteratedecks-cli.exe"
def simulatorArgsBase(attackHash = None, defenseHash = None, path = simulatorPath):
args = [path]
if(attackHash):
args.append(attackHash)
if(defenseHash):
args.append(defenseHash)
return args
def simulatorArgsAddHash(args, deckHash):
args.append(deckHash)
return args
def simulatorArgsAddMission(args, missionId):
args.extend(["-m", str(missionId)])
return args
def simulatorArgsAddNumSims(args, n):
args.extend(["-n", str(n)])
return args
def simulatorArgsAddOrdered(args):
args.extend(["-o"])
return args
def simulatorArgsAddQuest(args, questId):
args.extend(["-Q", str(questId)])
return args
def simulatorArgsAddRaid(args, raidId):
args.extend(["-r", str(raidId)])
return args
def simulatorArgsAddSeed(args):
args.extend(["--seed"])
return args
def simulatorArgsAddSurge(args):
args.extend(["-s"])
return args
def simulatorArgsAddVersus(args, versusType, versusId):
if(versusType == "quest"):
args = simulatorArgsAddQuest(args, versusId)
elif(versusType == "mission"):
args = simulatorArgsAddMission(args, versusId)
elif(versusType == "raid"):
args = simulatorArgsAddRaid(args, versusId)
elif(versusType == "hash"):
args = simulatorArgsAddHash(args, versusId)
return args
def getVersusScores(resultsDb, evolvedHashes, versus, defense = False):
attackKeys = evolvedHashes
defenseKeys = resultsDatabase.getVersusKeys(versus)
resultScores = None
if("hash" in versus and len(versus["hash"]) > 0):
if(defense):
attackKeys = defenseKeys
defenseKeys = evolvedHashes
resultScores = getAttackScores(resultsDb, defenseKeys, attackKeys, defense)
resultScores = sorted(resultScores, key=itemgetter(1), reverse=True)
return resultScores
def getAttackScores(resultsDb, defenseHashes, attackHashes, scoreDefense = False):
useAllAttackHashes = (attackHashes is None)
if(defenseHashes is None):
defenseHashes = resultsDb.keys()
attackScores = {}
resultHash = None
for defenseHash in defenseHashes:
if(not defenseHash in resultsDb):
print("Warning: " + defenseHash + " is not in results")
continue
#print("defenseHash: '" + defenseHash + "'")
results = resultsDb[defenseHash]
if(scoreDefense):
resultHash = defenseHash
if(useAllAttackHashes):
attackHashes = results.keys()
for attackHash in attackHashes:
if(not attackHash in results):
print("Warning: " + attackHash + " has no results for " + defenseHash)
continue
#print("attackHash: '" + attackHash + "'")
if(not scoreDefense):
resultHash = attackHash
total = results[attackHash][0]
wins = results[attackHash][1]
losses = results[attackHash][2]
draws = results[attackHash][3]
score = wins
if(scoreDefense):
score = losses + draws
if(not resultHash in attackScores):
attackScores[resultHash] = [resultHash, 0, 0, 0, 0, 0, 0]
attackScores[resultHash][2] += score
attackScores[resultHash][3] += total
attackScores[resultHash][4] += wins
attackScores[resultHash][5] += losses
attackScores[resultHash][6] += draws
#TODO could probably move this after all the numbers were tallied
attackScores[resultHash][1] = float(attackScores[resultHash][2]) / attackScores[resultHash][3]
#attackScores[resultHash].append(score / total)
#print("attackScore: '" + "\t".join(map(str, attackScores[resultHash])) + "'")
return attackScores.values()
def runMatrix(attackHashes, defenseMatrix, n, ordered = False, surge = False, resultsDb = None):
if(resultsDb is None):
resultsDb = {}
args = simulatorArgsBase()
args = simulatorArgsAddSeed(args)
args = simulatorArgsAddNumSims(args, n)
if(ordered):
args = simulatorArgsAddOrdered(args)
if(surge):
args = simulatorArgsAddSurge(args)
attackCount = len(attackHashes)
# wins total losses draws
regexString = "\D+(\d+)\D+(\d+)" # Wins 123 / 200
regexString += "\D+(\d+)\D+\d+" # Losses 123 / 200
regexString += "\D+(\d+)\D+\d+" # Draws 123 / 200
resultRegex = re.compile(regexString)
simulationCap = 10000
for defenseType in defenseMatrix:
print("starting " + defenseType + " matrix... ")
defenseIds = defenseMatrix[defenseType]
defenseCounter = 0
defenseCount = len(defenseIds)
# attack loop must go first because the simulator takes ATTACKHASH DEFENSEHASH
for attackHash in attackHashes:
attackKey = resultsDatabase.deckKey("hash", attackHash)
attackArgs = list(args)
attackArgs = simulatorArgsAddHash(attackArgs, attackHash)
#print("\tstarting attack group with attackKey " + attackKey)
for defenseId in defenseIds:
#totalSimulations = attackCount * (defenseCount - defenseCounter) * n
#print("\t" + str(defenseCount - defenseCounter) + "x" + str(attackCount) + "x" + str(n) + "=" + str(totalSimulations) + " simulations left")
defenseArgs = list(attackArgs)
defenseArgs = simulatorArgsAddVersus(defenseArgs, defenseType, defenseId)
defenseKey = resultsDatabase.deckKey(defenseType, defenseId)
if((defenseKey in resultsDb) and (attackKey in resultsDb[defenseKey]) and (resultsDb[defenseKey][attackKey][0] >= simulationCap)):
print("Skipping " + attackKey + " \tdefense " + defenseKey)
continue
#print("\t\trunning " + attackKey + " vs \t" + defenseKey)
result = runSimulation(defenseArgs)
simResults = resultRegex.match(result).groups()
resultsDatabase.recordResults(defenseKey, attackKey, simResults, resultsDb)
#defenseCounter = defenseCounter + 1
return resultsDb
def runSimulation(args):
result = subprocess.check_output(args)
return result.decode()
def runVersusMatrix(versus, evolvedHashes, resultsDb, numSims, defense = False, ordered = False, surge = False):
attackKeys = evolvedHashes
defenseVersus = versus
if("hash" in versus and len(versus["hash"]) > 0):
if(defense):
attackKeys = versus["hash"]
defenseVersus = { "hash": evolvedHashes }
# attackKeys = evolvedHashes
# defenseVersus = versus
#resultsDb = simulator.runSimulationMatrix(attackKeys, defenseHashes, args.numSims, resultsDb)
resultsDb = runMatrix(attackKeys, defenseVersus, numSims, ordered, surge, resultsDb)
#defenseKeys = resultsDatabase.getVersusKeys(defenseVersus)
#resultScores = getAttackScores(resultsDb, defenseKeys, attackKeys, defense)
#resultScores = sorted(resultScores, key=itemgetter(1), reverse=True)
return getVersusScores(resultsDb, None, versus, defense)