Compare commits
7 Commits
fb91302032
...
5f9ae3e145
Author | SHA1 | Date | |
---|---|---|---|
5f9ae3e145 | |||
83093f8c81 | |||
e1b5d3abb0 | |||
ef70128552 | |||
73554214f7 | |||
97776d7c2d | |||
e597fedc2f |
@ -18,3 +18,9 @@ usage: hhirlstats [-h] [-v] [-c] [-Q {gametime,moves,nicknames,playtime,usage}]
|
|||||||
Recommended to cache replays (`-c`) to save issuing requests to Showdown on every query.
|
Recommended to cache replays (`-c`) to save issuing requests to Showdown on every query.
|
||||||
|
|
||||||
Run the program once with all your replays (or once for each replay), then start querying.
|
Run the program once with all your replays (or once for each replay), then start querying.
|
||||||
|
|
||||||
|
## Future work
|
||||||
|
|
||||||
|
- include timestamps in logs to correlate KOs with the mon that KOed
|
||||||
|
- calculate gametime based on active turns rather than moves used
|
||||||
|
- also solves the issue where paralyzed/confused turns are not counted
|
||||||
|
@ -12,3 +12,4 @@ https://replay.pokemonshowdown.com/dl-gen9paldeadexposthomedraft-29698
|
|||||||
https://replay.pokemonshowdown.com/dl-gen9paldeadexposthomedraft-30488
|
https://replay.pokemonshowdown.com/dl-gen9paldeadexposthomedraft-30488
|
||||||
https://replay.pokemonshowdown.com/dl-gen9paldeadexposthomedraft-30858
|
https://replay.pokemonshowdown.com/dl-gen9paldeadexposthomedraft-30858
|
||||||
https://replay.pokemonshowdown.com/dl-gen9paldeadexposthomedraft-31241
|
https://replay.pokemonshowdown.com/dl-gen9paldeadexposthomedraft-31241
|
||||||
|
https://replay.pokemonshowdown.com/dl-gen9paldeadexposthomedraft-31809
|
||||||
|
179
main.py
179
main.py
@ -46,17 +46,21 @@ def _init_db(conn: sqlite3.Connection):
|
|||||||
UNIQUE(game, turn, player, user)
|
UNIQUE(game, turn, player, user)
|
||||||
);
|
);
|
||||||
CREATE TABLE IF NOT EXISTS switches(
|
CREATE TABLE IF NOT EXISTS switches(
|
||||||
game, turn, name,
|
game, turn, player, name,
|
||||||
UNIQUE(game, turn, name)
|
UNIQUE(game, turn, player, name)
|
||||||
);
|
);
|
||||||
CREATE TABLE IF NOT EXISTS nicknames(
|
CREATE TABLE IF NOT EXISTS nicknames(
|
||||||
game, player, name, specie,
|
game, player, name, specie,
|
||||||
UNIQUE(game,player, specie)
|
UNIQUE(game, player, specie)
|
||||||
);
|
);
|
||||||
"""
|
CREATE TABLE IF NOT EXISTS knockouts(
|
||||||
)
|
game, turn, player, name,
|
||||||
conn.execute(
|
UNIQUE(game, turn, player)
|
||||||
"""
|
);
|
||||||
|
CREATE TABLE IF NOT EXISTS games(
|
||||||
|
id, p1, p2, format, uploadtime,
|
||||||
|
UNIQUE(id)
|
||||||
|
)
|
||||||
"""
|
"""
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -96,11 +100,11 @@ def parse_log(game: str, log: str, into: sqlite3.Connection):
|
|||||||
player, name = resolve_mon(name)
|
player, name = resolve_mon(name)
|
||||||
conn.execute(
|
conn.execute(
|
||||||
"""
|
"""
|
||||||
INSERT INTO switches(game, turn, name)
|
INSERT INTO switches(game, turn, player, name)
|
||||||
VALUES (?, ?, ?)
|
VALUES (?, ?, ?, ?)
|
||||||
ON CONFLICT DO NOTHING
|
ON CONFLICT DO NOTHING
|
||||||
""",
|
""",
|
||||||
(game, turn, name),
|
(game, turn, player, name),
|
||||||
)
|
)
|
||||||
conn.execute(
|
conn.execute(
|
||||||
"""
|
"""
|
||||||
@ -110,116 +114,20 @@ def parse_log(game: str, log: str, into: sqlite3.Connection):
|
|||||||
""",
|
""",
|
||||||
(game, player, name, specie.split(", ")[0]),
|
(game, player, name, specie.split(", ")[0]),
|
||||||
)
|
)
|
||||||
|
case ["faint", mon]:
|
||||||
|
player, mon = resolve_mon(mon)
|
||||||
|
conn.execute(
|
||||||
|
"""
|
||||||
|
INSERT INTO knockouts(game, turn, player, name)
|
||||||
|
VALUES(?, ?, ?, ?)
|
||||||
|
ON CONFLICT DO NOTHING
|
||||||
|
""",
|
||||||
|
(game, turn, player, mon),
|
||||||
|
)
|
||||||
case _:
|
case _:
|
||||||
debug(f"unhandled message {chunks[0]}")
|
debug(f"unhandled message {chunks[0]}")
|
||||||
|
|
||||||
|
|
||||||
QUERIES = ["gametime", "moves", "nicknames", "playtime", "usage"]
|
|
||||||
|
|
||||||
|
|
||||||
def query(type: str, conn: sqlite3.Connection):
|
|
||||||
match type:
|
|
||||||
case "gametime":
|
|
||||||
print("Longest games")
|
|
||||||
print("=============")
|
|
||||||
for row in conn.execute(
|
|
||||||
"""
|
|
||||||
SELECT game, MAX(turn) AS n
|
|
||||||
FROM moves
|
|
||||||
GROUP BY game
|
|
||||||
ORDER BY n DESC
|
|
||||||
LIMIT 5
|
|
||||||
"""
|
|
||||||
):
|
|
||||||
replay = fetch(row.game, cache=True)
|
|
||||||
print(f"{replay.p1} vs {replay.p2}: {row.n} turns")
|
|
||||||
|
|
||||||
case "moves":
|
|
||||||
print("Move usage overall")
|
|
||||||
print("==================")
|
|
||||||
for row in conn.execute(
|
|
||||||
"""
|
|
||||||
SELECT name, COUNT(*) AS n
|
|
||||||
FROM moves
|
|
||||||
GROUP BY name
|
|
||||||
ORDER BY n DESC, name
|
|
||||||
LIMIT 10
|
|
||||||
"""
|
|
||||||
):
|
|
||||||
print(f"{row.name}: {row.n}")
|
|
||||||
|
|
||||||
case "nicknames":
|
|
||||||
print("Nickname usage per player")
|
|
||||||
print("=========================")
|
|
||||||
for row_p in conn.execute("SELECT DISTINCT player FROM nicknames"):
|
|
||||||
print(row_p.player)
|
|
||||||
for row_s in conn.execute(
|
|
||||||
"SELECT DISTINCT specie FROM nicknames WHERE player = ?",
|
|
||||||
(row_p.player,),
|
|
||||||
):
|
|
||||||
print(f" {row_s.specie}: ", end="")
|
|
||||||
names = []
|
|
||||||
for row in conn.execute(
|
|
||||||
"""
|
|
||||||
SELECT player, specie, name, count(game) AS n
|
|
||||||
FROM nicknames
|
|
||||||
WHERE player = ? AND specie = ?
|
|
||||||
GROUP BY player, specie, name
|
|
||||||
ORDER BY player, specie, name
|
|
||||||
""",
|
|
||||||
(row_p.player, row_s.specie),
|
|
||||||
):
|
|
||||||
names.append(f"{row.name} (x{row.n})")
|
|
||||||
print(*names, sep=", ")
|
|
||||||
|
|
||||||
case "playtime":
|
|
||||||
print("Active playtime per Pokemon")
|
|
||||||
print("===========================")
|
|
||||||
for row in conn.execute(
|
|
||||||
"""
|
|
||||||
SELECT m.player, k.specie, COUNT(m.name) AS n
|
|
||||||
FROM moves m
|
|
||||||
LEFT JOIN nicknames k ON (m.game, m.player, m.user) = (k.game, k.player, k.name)
|
|
||||||
GROUP BY k.specie, m.player
|
|
||||||
ORDER BY n DESC, k.specie, m.player
|
|
||||||
LIMIT 10
|
|
||||||
"""
|
|
||||||
):
|
|
||||||
print(f"{row.specie} ({row.player}): {row.n} turns")
|
|
||||||
|
|
||||||
case "usage":
|
|
||||||
print("Pokemon usage per player")
|
|
||||||
print("========================")
|
|
||||||
games = {
|
|
||||||
r.player: r.n
|
|
||||||
for r in conn.execute(
|
|
||||||
"""
|
|
||||||
SELECT player, COUNT(m.game) AS n
|
|
||||||
FROM (SELECT DISTINCT player, game FROM moves) m
|
|
||||||
GROUP BY player
|
|
||||||
"""
|
|
||||||
)
|
|
||||||
}
|
|
||||||
for row_p in conn.execute("SELECT DISTINCT player FROM nicknames"):
|
|
||||||
print(row_p.player)
|
|
||||||
for row_s in conn.execute(
|
|
||||||
"""
|
|
||||||
SELECT specie, COUNT(game) AS n
|
|
||||||
FROM nicknames
|
|
||||||
WHERE player = ?
|
|
||||||
GROUP BY specie
|
|
||||||
ORDER BY n DESC, specie
|
|
||||||
""",
|
|
||||||
(row_p.player,),
|
|
||||||
):
|
|
||||||
print(
|
|
||||||
f" {row_s.specie}: {row_s.n}"
|
|
||||||
f" ({row_s.n / games[row_p.player] * 100:.2f}%)"
|
|
||||||
)
|
|
||||||
case _:
|
|
||||||
error(f"unknown query {type}")
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass(frozen=True)
|
@dataclass(frozen=True)
|
||||||
class Replay:
|
class Replay:
|
||||||
id: str
|
id: str
|
||||||
@ -266,13 +174,7 @@ def main():
|
|||||||
"-v", "--verbose", action="store_true", help="add debugging info"
|
"-v", "--verbose", action="store_true", help="add debugging info"
|
||||||
)
|
)
|
||||||
parser.add_argument("-c", "--cache", action="store_true", help="cache replays")
|
parser.add_argument("-c", "--cache", action="store_true", help="cache replays")
|
||||||
parser.add_argument(
|
parser.add_argument("replay", nargs="+", help="replay ID or URL")
|
||||||
"-Q",
|
|
||||||
"--query",
|
|
||||||
choices=QUERIES,
|
|
||||||
help="run query instead of download",
|
|
||||||
)
|
|
||||||
parser.add_argument("replay", nargs="*", help="replay ID or URL")
|
|
||||||
|
|
||||||
args = parser.parse_args(sys.argv[1:])
|
args = parser.parse_args(sys.argv[1:])
|
||||||
logging.basicConfig(level=logging.DEBUG if args.verbose else logging.INFO)
|
logging.basicConfig(level=logging.DEBUG if args.verbose else logging.INFO)
|
||||||
@ -281,23 +183,24 @@ def main():
|
|||||||
db = sqlite3.connect("data.db")
|
db = sqlite3.connect("data.db")
|
||||||
_init_db(db)
|
_init_db(db)
|
||||||
|
|
||||||
if args.query:
|
for r in args.replay:
|
||||||
query(args.query, db)
|
try:
|
||||||
else:
|
replay = fetch(r, cache=args.cache)
|
||||||
if not args.replay:
|
except Exception as e:
|
||||||
parser.print_usage()
|
error(f"bad replay {r}")
|
||||||
print(f"{APP}: error: either query or replay arguments are required")
|
continue
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
for r in args.replay:
|
db.execute(
|
||||||
try:
|
"""
|
||||||
replay = fetch(r, cache=args.cache)
|
INSERT INTO games(id, p1, p2, format, uploadtime)
|
||||||
except Exception as e:
|
VALUES (?, ?, ?, ?, ?)
|
||||||
error(f"bad replay {r}")
|
ON CONFLICT DO NOTHING
|
||||||
continue
|
""",
|
||||||
|
(replay.id, replay.p1, replay.p2, replay.format, replay.uploadtime),
|
||||||
|
)
|
||||||
|
|
||||||
parse_log(replay.id, replay.log, into=db)
|
parse_log(replay.id, replay.log, into=db)
|
||||||
db.commit()
|
db.commit()
|
||||||
|
|
||||||
finally:
|
finally:
|
||||||
db.close()
|
db.close()
|
||||||
|
Loading…
Reference in New Issue
Block a user