2021-06-15 12:34:48 +02:00
|
|
|
from datetime import datetime
|
2021-08-18 23:33:29 +02:00
|
|
|
from discord_webhook import DiscordWebhook, DiscordEmbed # type: ignore
|
2021-08-20 06:43:59 +02:00
|
|
|
from typing import Dict
|
2021-08-18 23:33:29 +02:00
|
|
|
|
|
|
|
from bemani.common.constants import GameConstants, BroadcastConstants
|
2021-08-20 06:43:59 +02:00
|
|
|
from bemani.data.config import Config
|
2021-08-18 23:33:29 +02:00
|
|
|
from bemani.data.types import Song
|
2021-06-15 12:34:48 +02:00
|
|
|
|
|
|
|
|
|
|
|
class Triggers:
|
|
|
|
"""
|
|
|
|
Class for broadcasting data to some outside service
|
|
|
|
"""
|
2022-10-15 20:56:30 +02:00
|
|
|
|
2021-08-20 06:43:59 +02:00
|
|
|
def __init__(self, config: Config) -> None:
|
2021-06-15 12:34:48 +02:00
|
|
|
self.config = config
|
|
|
|
|
2021-08-19 21:21:22 +02:00
|
|
|
def __gameconst_to_series(self, game: GameConstants) -> str:
|
2021-08-18 23:33:29 +02:00
|
|
|
return {
|
2022-10-15 20:56:30 +02:00
|
|
|
GameConstants.BISHI_BASHI: "Bishi Bashi",
|
|
|
|
GameConstants.DANCE_EVOLUTION: "Dance Evolution",
|
|
|
|
GameConstants.DDR: "Dance Dance Revolution",
|
|
|
|
GameConstants.IIDX: "Beatmania IIDX",
|
|
|
|
GameConstants.JUBEAT: "Jubeat",
|
|
|
|
GameConstants.MGA: "Metal Gear Arcade",
|
|
|
|
GameConstants.MUSECA: "MÚSECA",
|
|
|
|
GameConstants.POPN_MUSIC: "Pop'n Music",
|
|
|
|
GameConstants.REFLEC_BEAT: "Reflec Beat",
|
|
|
|
GameConstants.SDVX: "Sound Voltex",
|
|
|
|
}.get(game, "Unknown")
|
2021-08-18 23:33:29 +02:00
|
|
|
|
2021-09-06 21:42:38 +02:00
|
|
|
def has_broadcast_destination(self, game: GameConstants) -> bool:
|
|
|
|
# For now we only support discord
|
|
|
|
if self.config.webhooks.discord[game] is not None:
|
|
|
|
return True
|
|
|
|
|
|
|
|
# Nothing is hooked up for this game, so there is no destination.
|
|
|
|
return False
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
def broadcast_score(
|
|
|
|
self, data: Dict[BroadcastConstants, str], game: GameConstants, song: Song
|
|
|
|
) -> None:
|
2021-06-15 12:34:48 +02:00
|
|
|
# For now we only support discord
|
2021-08-20 06:43:59 +02:00
|
|
|
if self.config.webhooks.discord[game] is not None:
|
2021-06-15 12:34:48 +02:00
|
|
|
self.broadcast_score_discord(data, game, song)
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
def broadcast_score_discord(
|
|
|
|
self, data: Dict[BroadcastConstants, str], game: GameConstants, song: Song
|
|
|
|
) -> None:
|
2021-09-06 02:16:06 +02:00
|
|
|
if game in {GameConstants.IIDX, GameConstants.POPN_MUSIC}:
|
2021-06-15 12:34:48 +02:00
|
|
|
now = datetime.now()
|
|
|
|
|
2021-08-20 06:43:59 +02:00
|
|
|
webhook = DiscordWebhook(url=self.config.webhooks.discord[game])
|
2022-10-15 20:56:30 +02:00
|
|
|
scoreembed = DiscordEmbed(
|
|
|
|
title=f"New {self.__gameconst_to_series(game)} Score!", color="fbba08"
|
|
|
|
)
|
|
|
|
scoreembed.set_footer(
|
|
|
|
text=(now.strftime("Score was recorded on %m/%d/%y at %H:%M:%S"))
|
|
|
|
)
|
2021-06-15 12:34:48 +02:00
|
|
|
|
|
|
|
# lets give it an author
|
2022-10-15 20:56:30 +02:00
|
|
|
song_url = (
|
|
|
|
f"{self.config.server.uri}/{game.value}/topscores/{song.id}"
|
|
|
|
if self.config.server.uri is not None
|
|
|
|
else None
|
|
|
|
)
|
2021-08-20 06:43:59 +02:00
|
|
|
scoreembed.set_author(name=self.config.name, url=song_url)
|
2021-08-18 23:33:29 +02:00
|
|
|
for item, value in data.items():
|
2021-06-15 12:34:48 +02:00
|
|
|
inline = True
|
2022-10-15 20:56:30 +02:00
|
|
|
if item in {
|
|
|
|
BroadcastConstants.DJ_NAME,
|
|
|
|
BroadcastConstants.PLAYER_NAME,
|
|
|
|
BroadcastConstants.SONG_NAME,
|
|
|
|
BroadcastConstants.ARTIST_NAME,
|
|
|
|
BroadcastConstants.PLAY_STATS_HEADER,
|
|
|
|
}:
|
2021-06-15 12:34:48 +02:00
|
|
|
inline = False
|
2021-08-18 23:33:29 +02:00
|
|
|
scoreembed.add_embed_field(name=item.value, value=value, inline=inline)
|
2021-06-15 12:34:48 +02:00
|
|
|
webhook.add_embed(scoreembed)
|
|
|
|
webhook.execute()
|