-
Notifications
You must be signed in to change notification settings - Fork 0
/
metagame.py
413 lines (341 loc) · 14.8 KB
/
metagame.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
# -*- coding: utf-8 -*-
import math
import os
import pathos.multiprocessing as mp
import re
import requests
import sqlite3
import sys
import time
from bs4 import BeautifulSoup
from collections import namedtuple
from collections import OrderedDict
from scipy.stats import hypergeom
sys.setrecursionlimit(1000000)
CardsRow = namedtuple("CardsRow", "cardname")
DecklistsRow = namedtuple("DecklistsRow", "id_deck, cardname, quantity, percent, sideboard")
FormatsRow = namedtuple("FormatsRow", "formatname")
MetagamesRow = namedtuple("MetagamesRow", "deckname, formatname, rawshare, url")
MetagamesRowNumbered = namedtuple("MetagamesRowNumbered", "id_deck, deckname, formatname, rawshare, url")
class MetagameMaster(object):
def __init__(self, database="metagame.db"):
self.database = database
self.formats = ["Standard", "Modern", "Legacy", "Vintage", "Pauper"]
self.connection = sqlite3.connect(self.database)
self.cursor = self.connection.cursor()
self.create_function_LOG()
self.create_function_EXP()
self.create_aggregate_PROD()
def update(self):
open(self.database, 'w').close() # Wipe database
self.create_decklists()
self.create_metagames()
order = int(math.ceil(math.log(len(self.formats), 10))) # Least power of 10 greater than the number of formats.
metagame_rows = [MetagamesRowNumbered(id_deck=i * 10**order + id_format,
deckname=mr.deckname,
formatname=mr.formatname,
rawshare=mr.rawshare,
url=mr.url)
for id_format, format_ in enumerate(self.formats)
for i, mr in enumerate(self.scrape_metagame(format_))
]
pool = mp.Pool()
decklist_rows = [decklist_row
for decklist in pool.imap(self.scrape_decklist, metagame_rows, chunksize=40)
for decklist_row in decklist
]
pool.close()
pool.join()
for row in metagame_rows:
self.insert_to_metagames(id_deck=row.id_deck,
formatname=row.formatname,
deckname=row.deckname,
rawshare=row.rawshare,
url=row.url)
for row in decklist_rows:
self.insert_to_decklists(id_deck=row.id_deck,
cardname=row.cardname,
quantity=row.quantity,
percent=row.percent,
sideboard=row.sideboard)
self.connection.commit()
# Scraping #########################################################################################################
def scrape_metagame(self, formatname):
url = "https://www.mtggoldfish.com/metagame/{}/full#online".format(formatname)
page = requests.get(url).content
fullsoup = BeautifulSoup(page, "lxml")
soup = fullsoup.find("div", class_="metagame-list-full-content")
decklists = []
for decklist_tile in soup.find_all(class_="archetype-tile"):
try:
rawshare = self.string_try_to_float(
decklist_tile.find(class_="percentage col-freq").contents[0].strip('\n')
)
url_short = decklist_tile.find('a', class_="card-image-tile-link-overlay")["href"]
url = "https://www.mtggoldfish.com" + url_short
deckname = decklist_tile.find('a', href=url_short + "#online").contents[0]
decklists.append(MetagamesRow(deckname, formatname, rawshare, url))
except TypeError:
pass
print decklists
return decklists
def scrape_decklist(self, decklist_row):
url = decklist_row.url
page = requests.get(url).content
fullsoup = BeautifulSoup(page, "lxml")
soup = fullsoup.find('div', class_="archetype-details")
cards = []
for card_type_subsection in soup.find_all('div', class_="archetype-breakdown-section"):
for featured in card_type_subsection.find_all('div', class_="archetype-breakdown-featured-card"):
try:
cardname = self.clean_name(featured.find('img')['alt'])
percent_quantity = featured.find('p', class_="archetype-breakdown-featured-card-text").contents[0]
percent = self.clean_number(re.search("[0-9]+%", percent_quantity).group(0))
quantity = int(self.clean_number(re.search("[0-9]+x", percent_quantity).group(0)))
sideboard = card_type_subsection.find('h4').contents[0] == "Sideboard"
cards.append(DecklistsRow(decklist_row.id_deck, cardname, quantity, percent, sideboard))
except AttributeError:
pass
for unfeatured in card_type_subsection.find_all('tr'):
try:
cardname = self.clean_name(unfeatured.find('a').contents[0])
percent = self.clean_number(unfeatured.find('td', class_="deck-col-frequency").contents[0])
quantity = int(self.clean_number(unfeatured.find('td', class_="deck-col-qty").contents[0]))
sideboard = card_type_subsection.find('h4').contents[0] == "Sideboard"
cards.append(DecklistsRow(decklist_row.id_deck, cardname, quantity, percent, sideboard))
except AttributeError:
pass
print cards
return cards
# Formatting #######################################################################################################
@staticmethod
def string_try_to_float(x):
# Converts MTGGoldfish reported data into floats if they're numbers.
try:
return float(x)
except ValueError:
try:
return float(x.strip('%')) / 100
except ValueError:
return x
@staticmethod
def clean_name(name):
# Takes a cardname with art specification and returns just the card's name. (Removes everything in parentheses.)
regex_del = "\(([^\)]+)\)"
return str(re.sub(regex_del, "", name).strip())
def clean_number(self, number):
# Cleans up whitespace and NUMx from MTGGoldfish HTML and converts to float.
return self.string_try_to_float(number.strip().strip('x'))
@staticmethod
def percent(num, decimals=0):
"""
:param num: Float to be rewritten as a percentage.
:param decimals: Number of decimals to display.
:return: 'N%' string.
"""
return "{0:.{1}f}%".format(num * 100, decimals)
@staticmethod
def humanform(number, decimals=1):
"""
:param number: Float to be rewritten with specified significant digits.
:param decimals: Number of decimals to display.
:return: 'N.M' string.
"""
if number == int(number):
return str(number)
else:
return "{0:.{1}f}".format(number, decimals)
def plausibile(self, intable, maxrows=5, threshold=.05):
"""
:param intable: Table of archetypes and associated probabilities.
:param maxrows: Maximum number of archetypes to display.
:param threshold: Minimum likelihood to display.
:return: outtable is intable rows which satisfy max and threshold restrictions
"""
outtable = [(self.percent(probability), archetype) for probability, archetype in intable[:maxrows] if
probability > threshold]
return outtable
# SQL setup ########################################################################################################
def create_function_LOG(self):
self.connection.create_function("LOG", 1, math.log)
def create_function_EXP(self):
self.connection.create_function("EXP", 1, math.exp)
def create_aggregate_PROD(self):
class P(object):
def __init__(self):
self.prod = 1
def step(self, value):
self.prod *= value
def finalize(self):
return self.prod
self.connection.create_aggregate("PROD", 1, P)
def create_aggregate_THISDECK(self, carddict):
def hg(cardname, quantity, percent):
try:
actualsuccesses = carddict[cardname]
decksize = 60
successdraws = quantity
cardsdrawn = sum(carddict.values())
return hypergeom.pmf(k=actualsuccesses, M=decksize, n=successdraws, N=cardsdrawn) * percent
except KeyError:
return 0
class TD(object):
def __init__(self):
self.prod = 0
self.accounted = set()
def step(self, rawshare, cardname, quantity, percent):
if cardname in carddict:
self.accounted.add(cardname)
if self.prod is 0:
self.prod = rawshare
self.prod *= hg(cardname, quantity, percent)
def finalize(self):
if self.accounted == set(carddict.keys()):
return self.prod
else:
return 0
self.connection.create_aggregate("THISDECK", 4, TD)
def create_decklists(self):
command = """
CREATE TABLE decklists (
id INTEGER PRIMARY KEY,
id_deck INTEGER,
cardname VARCHAR(100),
quantity INTEGER,
percent FLOAT,
sideboard BOOL
)
"""
self.cursor.execute(command)
def create_metagames(self):
command = """
CREATE TABLE metagames (
id INTEGER PRIMARY KEY,
id_deck INTEGER,
formatname VARCHAR(100),
deckname VARCHAR(100),
rawshare FLOAT,
url VARCHAR(100)
)
"""
self.cursor.execute(command)
def insert_to_decklists(self, id_deck, cardname, quantity, percent, sideboard):
command = """
INSERT INTO decklists (id_deck, cardname, quantity, percent, sideboard)
VALUES (?,?,?,?,?)
"""
self.cursor.execute(command, [id_deck, cardname, quantity, percent, sideboard])
def insert_to_metagames(self, id_deck, formatname, deckname, rawshare, url):
command = """
INSERT INTO metagames (id_deck, formatname, deckname, rawshare, url)
VALUES (?,?,?,?,?)
"""
self.cursor.execute(command, [id_deck, formatname, deckname, rawshare, url])
# SQL queries ######################################################################################################
def decknames(self, formatname=None, show_formats=False):
if formatname:
formatname_specification = "WHERE formatname = ?"
args = [formatname]
else:
formatname_specification = ""
args = []
command = """
SELECT DISTINCT deckname, formatname
FROM metagames
{}
ORDER BY rawshare DESC
""".format(formatname_specification)
self.cursor.execute(command, args)
result = self.cursor.fetchall()
if show_formats:
output = OrderedDict()
for i in result:
output[i[0]] = i[1]
else:
output = [i[0] for i in result]
return output
def whatdeck(self, formatname, carddict):
self.create_aggregate_THISDECK(carddict)
table_thisdeck = """
SELECT d.id_deck, deckname, THISDECK(rawshare, cardname, quantity, percent) AS thisdeck
FROM decklists d
JOIN metagames m
ON d.id_deck = m.id_deck
WHERE sideboard = 0 AND formatname = ?
GROUP BY d.id_deck
"""
command = """
SELECT SUM(thisdeck) / (SELECT SUM(thisdeck) FROM ({})) AS probability, deckname
FROM({})
GROUP BY deckname
HAVING probability > .05
ORDER BY probability DESC
LIMIT 5
""".format(table_thisdeck, table_thisdeck)
self.cursor.execute(command, [formatname, formatname])
result = self.cursor.fetchall()
return self.plausibile(result)
def running(self, formatname, deckname, cardname):
command = """
SELECT SUM(ev * rawshare) / SUM(rawshare), sideboard
FROM(
SELECT SUM(CASE WHEN cardname = ? THEN d.quantity * d.percent ELSE 0 END) AS ev, rawshare, sideboard
FROM decklists d
JOIN metagames m
ON m.id_deck = d.id_deck
WHERE formatname = ? AND deckname = ?
GROUP BY d.id_deck, sideboard
)
GROUP BY sideboard
"""
self.cursor.execute(command, [cardname, formatname, deckname])
result = self.cursor.fetchall()
outdict = {'maindeck': '0', 'sideboard': '0'}
for board in result:
outdict[('maindeck', 'sideboard')[board[1]]] = self.humanform(board[0]) # board[1] determines the board
return outdict
def decklist(self, deckname, formatname=None):
if formatname:
format_restriction = "AND formatname = ?"
args = [deckname, formatname]
else:
format_restriction = ""
args = [deckname]
command = """
SELECT m.url
FROM metagames m
JOIN decklists d
ON d.id_deck = m.id_deck
WHERE deckname = ? {}
ORDER BY m.rawshare DESC
LIMIT 1
""".format(format_restriction)
self.cursor.execute(command, args)
result = self.cursor.fetchall()
return result[0][0]
def update():
master = MetagameMaster()
master.update()
def load():
master = MetagameMaster()
return master
if __name__ == "__main__":
m = MetagameMaster()
update = False
if update:
t0 = time.time()
m.update()
t1 = time.time()
print "timing:", t1 - t0
print "formats:", m.formats
print "decknames:", m.decknames()
print "decknames:", m.decknames(show_formats=True)
print "decknames standard:", m.decknames("Standard")
print "decklist:", m.decklist(u"Dredge")
print "decklist:", m.decklist(deckname=u"Dredge", formatname="Vintage")
print "running:", m.running("Modern", u"Dredge", "Ancient Grudge")
print "running:", m.running("Modern", "Affinity", "Etched Champion")
print "running:", m.running("Modern", "Abzan", "Tarmogoyf")
print "whatdeck:", m.whatdeck("Modern", {"Death's Shadow": 1, "Bloodstained Mire": 2})
print "whatdeck:", m.whatdeck("Modern", {"Eldrazi Temple": 1, "Chalice of the Void": 2})
print "whatdeck:", m.whatdeck("Modern", {"Tarmogoyf": 2, "Forest": 1})