2020-07-07 10:06:25 -04:00
|
|
|
import json
|
|
|
|
import os, os.path
|
2020-07-22 14:28:42 -04:00
|
|
|
import pathlib
|
2020-07-07 10:06:25 -04:00
|
|
|
|
|
|
|
import requests
|
|
|
|
|
2020-07-22 12:08:49 -04:00
|
|
|
from . import characters_ssbu
|
2020-07-19 09:39:39 -04:00
|
|
|
|
2020-07-06 18:48:14 -04:00
|
|
|
# =============================================================================
|
|
|
|
API_HOST = "api.smash.gg"
|
|
|
|
API_ENDPOINT = "gql/alpha"
|
|
|
|
API_SCHEME = "https"
|
|
|
|
API_URL = "{scheme}://{host}/{endpoint}".format(
|
|
|
|
scheme = API_SCHEME,
|
|
|
|
host = API_HOST,
|
|
|
|
endpoint = API_ENDPOINT,
|
|
|
|
)
|
|
|
|
|
2020-07-20 12:44:49 -04:00
|
|
|
CHARACTERS = { c.smashggid : c.name for c in characters_ssbu.EVERYONE }
|
|
|
|
|
2020-12-20 13:25:59 -05:00
|
|
|
GET_PLAYERDATA = lambda tag: {"tag": tag,}
|
2020-07-20 12:44:49 -04:00
|
|
|
|
2020-07-06 19:52:16 -04:00
|
|
|
# =============================================================================
|
|
|
|
# -----------------------------------------------------------------------------
|
|
|
|
class Player():
|
|
|
|
"""A Player, as registered by the smash.gg API, and their characters
|
|
|
|
choices and placement in a tournament"""
|
|
|
|
|
|
|
|
# -------------------------------------------------------------------------
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
id,
|
|
|
|
prefix,
|
|
|
|
gamerTag,
|
|
|
|
placement,
|
|
|
|
seeding,
|
|
|
|
twitterHandle = None,
|
2020-07-07 10:07:47 -04:00
|
|
|
chars = None
|
|
|
|
):
|
|
|
|
self.id = int(id) # actually intended to store an Entrant id
|
2020-07-09 12:33:30 -04:00
|
|
|
self.prefix = ( "" if prefix is None else prefix )
|
2020-07-06 19:52:16 -04:00
|
|
|
self.gamerTag = gamerTag
|
2020-07-07 10:07:47 -04:00
|
|
|
self.placement = int(placement)
|
|
|
|
self.seeding = int(seeding)
|
2020-07-09 12:33:30 -04:00
|
|
|
self.twitterHandle = ( "" if twitterHandle is None else twitterHandle )
|
2020-07-06 19:52:16 -04:00
|
|
|
|
2020-12-20 13:25:59 -05:00
|
|
|
try:
|
|
|
|
self.playerdata = GET_PLAYERDATA(self.gamerTag)
|
|
|
|
except:
|
|
|
|
self.playerdata = {}
|
|
|
|
|
2020-09-17 11:29:08 -04:00
|
|
|
# Check if there's a redirect in the players DB
|
|
|
|
try:
|
2020-12-20 13:25:59 -05:00
|
|
|
self.gamerTag = self.playerdata["tag"]
|
2020-09-17 11:29:08 -04:00
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
2020-09-02 03:07:36 -04:00
|
|
|
# Prefix is superseeded by sponsors infos in DB
|
2020-08-21 05:34:02 -04:00
|
|
|
try:
|
2020-12-20 13:25:59 -05:00
|
|
|
self.prefix = self.playerdata["sponsor"]
|
2020-08-21 05:34:02 -04:00
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
2020-07-07 10:07:47 -04:00
|
|
|
if chars is None:
|
|
|
|
self.chars = {}
|
|
|
|
else:
|
|
|
|
self.chars = chars
|
2020-07-06 19:52:16 -04:00
|
|
|
|
|
|
|
# -------------------------------------------------------------------------
|
|
|
|
def add_character_selection(self, character, win):
|
2020-07-20 12:44:49 -04:00
|
|
|
|
|
|
|
if type(character) != tuple:
|
|
|
|
try:
|
|
|
|
charname = CHARACTERS[character]
|
|
|
|
except KeyError:
|
|
|
|
charname = character # Unknown char -> sgg id
|
|
|
|
|
|
|
|
try:
|
2020-12-20 13:25:59 -05:00
|
|
|
skin = self.playerdata["skins"][charname]
|
2020-07-20 12:44:49 -04:00
|
|
|
except KeyError:
|
|
|
|
skin = "00" # default skin
|
|
|
|
|
|
|
|
character = ( charname, skin )
|
|
|
|
|
2020-07-07 10:07:06 -04:00
|
|
|
|
2020-07-06 19:52:16 -04:00
|
|
|
try:
|
|
|
|
self.chars[character] += ( 1.01 if win else 1.00 )
|
|
|
|
# This 1.01 / 1.00 tricks should hold until the player loses 101
|
|
|
|
# matches with one character and wins 100 matches with another...
|
|
|
|
# during one tournament.
|
|
|
|
except KeyError:
|
|
|
|
self.chars[character] = ( 1.01 if win else 1.00 )
|
|
|
|
|
2020-07-07 10:09:22 -04:00
|
|
|
# -------------------------------------------------------------------------
|
|
|
|
def get_mains(self):
|
|
|
|
return [ cv[0] for cv in sorted(
|
|
|
|
self.chars.items(),
|
|
|
|
key = lambda cv: cv[1],
|
|
|
|
reverse = True,
|
|
|
|
) ]
|
|
|
|
|
2020-07-06 19:52:16 -04:00
|
|
|
# -------------------------------------------------------------------------
|
|
|
|
def conf(self):
|
|
|
|
|
2020-07-09 03:52:31 -04:00
|
|
|
# The char list looks like 'character1_skin1 (12.08), character2_skin2
|
|
|
|
# (3.02)' where the number between parenthesis is the number of time
|
|
|
|
# the character was played
|
|
|
|
charslist = ", ".join( [ "{}_{} ({:.2f})".format(c,s,n) for (c,s),n in
|
|
|
|
sorted( self.chars.items(), key = lambda cv: cv[1], reverse =
|
|
|
|
True,) ])
|
2020-07-06 19:52:16 -04:00
|
|
|
|
|
|
|
return """
|
|
|
|
[player {tag}]
|
|
|
|
tag: {tag}
|
|
|
|
team: {pfx}
|
|
|
|
seeding: {seed}
|
|
|
|
placement: {plc}
|
|
|
|
twitter: {twi}
|
|
|
|
characters: {charslist}""" \
|
|
|
|
.format(
|
|
|
|
tag = self.gamerTag,
|
|
|
|
pfx = self.prefix,
|
|
|
|
seed = self.seeding,
|
|
|
|
plc = self.placement,
|
|
|
|
twi = self.twitterHandle,
|
|
|
|
charslist = charslist,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-12-20 13:25:59 -05:00
|
|
|
# -------------------------------------------------------------------------
|
|
|
|
def __str__(self):
|
|
|
|
# The char list looks like 'character1_skin1 (12.08), character2_skin2
|
|
|
|
# (3.02)' where the number between parenthesis is the number of time
|
|
|
|
# the character was played
|
|
|
|
charslist = ", ".join( [ "{}_{} ({:.2f})".format(c,s,n) for (c,s),n in
|
|
|
|
sorted( self.chars.items(), key = lambda cv: cv[1], reverse =
|
|
|
|
True,) ])
|
|
|
|
return "{plc}/ [{pfx}] {tag} -- {charslist}" \
|
|
|
|
.format(
|
|
|
|
tag = self.gamerTag,
|
|
|
|
pfx = self.prefix,
|
|
|
|
plc = self.placement,
|
|
|
|
charslist = charslist,
|
|
|
|
)
|
2020-07-06 19:52:16 -04:00
|
|
|
|
|
|
|
# -----------------------------------------------------------------------------
|
|
|
|
class Tournament():
|
|
|
|
"""A Tournament, as registered by the smash.gg API"""
|
|
|
|
|
|
|
|
# -------------------------------------------------------------------------
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
id,
|
|
|
|
name,
|
|
|
|
slug,
|
|
|
|
startAt,
|
|
|
|
numEntrants,
|
|
|
|
venueAddress = None,
|
|
|
|
venueName = None,
|
|
|
|
city = None,
|
|
|
|
countryCode = None,
|
|
|
|
hashtag = None,
|
|
|
|
):
|
|
|
|
self.id = id
|
|
|
|
self.name = name
|
2020-07-07 10:10:14 -04:00
|
|
|
self.slug = slug.split("/")[-1]
|
2020-07-06 19:52:16 -04:00
|
|
|
self.startAt = startAt
|
|
|
|
self.numEntrants = numEntrants
|
|
|
|
self.venueAddress = venueAddress
|
|
|
|
self.venueName = venueName
|
|
|
|
self.city = city
|
|
|
|
self.countryCode = countryCode
|
|
|
|
self.hashtag = hashtag
|
|
|
|
|
|
|
|
# -------------------------------------------------------------------------
|
|
|
|
def conf(self):
|
|
|
|
return """
|
|
|
|
[Tournament]
|
|
|
|
name: {name}
|
|
|
|
date: {date}
|
2020-07-07 10:11:12 -04:00
|
|
|
location: {loc} - {addr} ({city}, {ctry})
|
2020-07-06 19:52:16 -04:00
|
|
|
slug: {slug}
|
|
|
|
numEntrants: {nbe}""" \
|
|
|
|
.format(
|
|
|
|
id = self.id,
|
|
|
|
name = self.name,
|
|
|
|
date = self.startAt,
|
|
|
|
loc = self.venueName,
|
2020-07-07 10:11:51 -04:00
|
|
|
addr = self.venueAddress,
|
|
|
|
city = self.city,
|
|
|
|
ctry = self.countryCode,
|
2020-07-06 19:52:16 -04:00
|
|
|
slug = self.slug,
|
|
|
|
nbe = self.numEntrants,
|
|
|
|
)
|
|
|
|
|
2020-07-19 16:26:34 -04:00
|
|
|
# -------------------------------------------------------------------------
|
|
|
|
def clean_name(self, name_seo_delimiter):
|
|
|
|
|
2020-12-20 13:25:59 -05:00
|
|
|
if name_seo_delimiter is not None and name_seo_delimiter != "":
|
2020-07-19 16:26:34 -04:00
|
|
|
self.name = self.name.split(name_seo_delimiter)[0].strip()
|
|
|
|
|
|
|
|
return self
|
|
|
|
|
2020-12-20 13:25:59 -05:00
|
|
|
# -------------------------------------------------------------------------
|
|
|
|
def __str__(self):
|
|
|
|
return "Tournament {name} ({date} at {loc}), {nbe} entrants" \
|
|
|
|
.format(
|
|
|
|
id = self.id,
|
|
|
|
name = self.name,
|
|
|
|
date = self.startAt,
|
|
|
|
loc = self.venueName,
|
|
|
|
addr = self.venueAddress,
|
|
|
|
city = self.city,
|
|
|
|
ctry = self.countryCode,
|
|
|
|
slug = self.slug,
|
|
|
|
nbe = self.numEntrants,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-07-06 19:52:16 -04:00
|
|
|
|
2020-07-06 18:48:14 -04:00
|
|
|
# =============================================================================
|
|
|
|
def run_query(
|
|
|
|
query_name,
|
|
|
|
variables = {},
|
2020-07-07 10:11:51 -04:00
|
|
|
token = "",
|
|
|
|
proxy = None,
|
2020-07-22 14:28:42 -04:00
|
|
|
query_dir = pathlib.Path("queries"),
|
2020-07-06 18:48:14 -04:00
|
|
|
query_extension = "gql",
|
|
|
|
api_url = API_URL,
|
|
|
|
log = None,
|
|
|
|
):
|
|
|
|
|
|
|
|
# Load query
|
2020-07-22 14:28:42 -04:00
|
|
|
query_path = query_dir / "{}.{}".format(
|
|
|
|
query_name,
|
|
|
|
query_extension,
|
2020-07-06 18:48:14 -04:00
|
|
|
)
|
|
|
|
query = ""
|
|
|
|
|
2020-07-22 14:28:42 -04:00
|
|
|
with query_path.open("r") as query_file:
|
2020-07-06 18:48:14 -04:00
|
|
|
query = query_file.read()
|
|
|
|
|
|
|
|
# Build payload
|
|
|
|
payload = {
|
|
|
|
"query": query,
|
|
|
|
"variables": json.dumps(variables),
|
|
|
|
}
|
|
|
|
|
|
|
|
# Build headers
|
|
|
|
headers = {
|
2020-07-07 10:11:51 -04:00
|
|
|
"Authorization": "Bearer {token}".format(token = token),
|
2020-07-06 18:48:14 -04:00
|
|
|
"Accept": "application/json",
|
|
|
|
"Content-Type": "application/json"
|
|
|
|
}
|
|
|
|
|
|
|
|
# Send the query
|
|
|
|
try:
|
|
|
|
log.debug("Sending query '{}' with variables:".format(query_name))
|
|
|
|
log.debug(json.dumps(variables))
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
rv = requests.post(
|
|
|
|
API_URL,
|
|
|
|
json.dumps(payload).encode("utf-8"),
|
|
|
|
headers = headers,
|
2020-07-07 10:11:51 -04:00
|
|
|
proxies = {"http": proxy, "https": proxy},
|
2020-07-06 18:48:14 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
try:
|
|
|
|
rv_json = rv.json()
|
|
|
|
except Exception as e:
|
|
|
|
log.error("HTTP request failed")
|
|
|
|
log.error(e)
|
|
|
|
log.debug(e, exc_info=True)
|
|
|
|
log.debug(rv)
|
|
|
|
|
|
|
|
# Try to return the data, or log the error and return None
|
|
|
|
try:
|
|
|
|
log.debug("query complexity : {}" \
|
|
|
|
.format(
|
|
|
|
rv_json["extensions"]["queryComplexity"],
|
|
|
|
))
|
|
|
|
return rv_json["data"]
|
|
|
|
except KeyError:
|
|
|
|
try:
|
|
|
|
log.error("GraphQL error")
|
|
|
|
log.error(rv_json)
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
return None
|
|
|
|
|