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
|
|
|
|
"""
|
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 {
|
|
|
|
GameConstants.BISHI_BASHI: 'Bishi Bashi',
|
|
|
|
GameConstants.DANCE_EVOLUTION: 'Dance Evolution',
|
|
|
|
GameConstants.DDR: 'Dance Dance Revolution',
|
|
|
|
GameConstants.IIDX: 'Beatmania IIDX',
|
|
|
|
GameConstants.JUBEAT: 'Jubeat',
|
|
|
|
GameConstants.MUSECA: 'MÚSECA',
|
|
|
|
GameConstants.POPN_MUSIC: 'Pop\'n Music',
|
|
|
|
GameConstants.REFLEC_BEAT: 'Reflec Beat',
|
|
|
|
GameConstants.SDVX: 'Sound Voltex',
|
|
|
|
}.get(game, 'Unknown')
|
|
|
|
|
2021-08-19 21:21:22 +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)
|
|
|
|
|
2021-08-19 21:21:22 +02:00
|
|
|
def broadcast_score_discord(self, data: Dict[BroadcastConstants, str], game: GameConstants, song: Song) -> None:
|
2021-06-15 12:34:48 +02:00
|
|
|
if game == GameConstants.IIDX:
|
|
|
|
now = datetime.now()
|
|
|
|
|
2021-08-20 06:43:59 +02:00
|
|
|
webhook = DiscordWebhook(url=self.config.webhooks.discord[game])
|
2021-08-18 23:33:29 +02:00
|
|
|
scoreembed = DiscordEmbed(title=f'New {self.__gameconst_to_series(game)} Score!', color='fbba08')
|
2021-06-15 12:34:48 +02:00
|
|
|
scoreembed.set_footer(text=(now.strftime('Score was recorded on %m/%d/%y at %H:%M:%S')))
|
|
|
|
|
|
|
|
# lets give it an author
|
2021-08-20 06:43:59 +02:00
|
|
|
song_url = f"{self.config.server.uri}/{game.value}/topscores/{song.id}" if self.config.server.uri is not None else None
|
|
|
|
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
|
2021-08-18 23:33:29 +02:00
|
|
|
if item in {BroadcastConstants.DJ_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()
|