1
Fork 0
mirror of https://github.com/RYGhub/royal-mifia.git synced 2024-11-23 22:34:19 +00:00

Added sentry integration and ondeath() method

This commit is contained in:
Steffo 2016-08-09 13:51:56 +02:00
parent 881f426b43
commit 2f97fe7eb0
2 changed files with 67 additions and 24 deletions

4
.gitignore vendored
View file

@ -112,4 +112,6 @@ $RECYCLE.BIN/
# La roba di Pycharm # La roba di Pycharm
.pyc .pyc
.idea/* .idea/*
*.p *.p
.idea/
sentrykey.txt

87
main.py
View file

@ -7,13 +7,50 @@ from telegram import ParseMode
import filemanager import filemanager
import random import random
import strings as s import strings as s
import logging.config
from raven.handlers.logging import SentryHandler
import logging sentrykey = filemanager.readfile("sentrykey.txt")
logger = logging.getLogger()
logger.setLevel(logging.WARN)
logging.basicConfig(level=logging.WARN, handler = SentryHandler(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s') sentrykey)
logging.config.dictConfig({
'version': 1,
'disable_existing_loggers': True,
'formatters': {
'console': {
'format': '[%(asctime)s][%(levelname)s] %(name)s '
'%(filename)s:%(funcName)s:%(lineno)d | %(message)s',
'datefmt': '%H:%M:%S',
},
},
'handlers': {
'console': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
'formatter': 'console'
},
'sentry': {
'level': 'WARNING',
'class': 'raven.handlers.logging.SentryHandler',
'dsn': sentrykey,
},
},
'loggers': {
'': {
'handlers': ['console', 'sentry'],
'level': 'DEBUG',
'propagate': False,
},
'your_app': {
'level': 'DEBUG',
'propagate': True,
},
}
})
token = filemanager.readfile('telegramapi.txt') token = filemanager.readfile('telegramapi.txt')
updater = Updater(token) updater = Updater(token)
@ -39,7 +76,11 @@ class Role:
pass pass
def onendday(self, bot, game): def onendday(self, bot, game):
"""Metodo chiamato alla fine di ogni giorno, per attivare o ripristinare allo stato iniziale il potere.""" """Metodo chiamato alla fine di ogni giorno."""
pass
def ondeath(self, bot, game):
"""Metodo chiamato alla morte del giocatore."""
pass pass
@ -89,7 +130,7 @@ class Mifioso(Role):
# Uccidi il bersaglio se non è protetto da un Angelo. # Uccidi il bersaglio se non è protetto da un Angelo.
if self.target is not None: if self.target is not None:
if self.target.protectedby is None: if self.target.protectedby is None:
self.target.kill() self.target.kill(bot, game)
game.message(bot, s.mifia_target_killed.format(target=self.target.tusername, game.message(bot, s.mifia_target_killed.format(target=self.target.tusername,
icon=self.target.role.icon, icon=self.target.role.icon,
role=self.target.role.name)) role=self.target.role.name))
@ -156,7 +197,7 @@ class Angelo(Role):
else: else:
r = "<Role: Angelo, protecting {target}>".format(target=self.protecting.tusername) r = "<Role: Angelo, protecting {target}>".format(target=self.protecting.tusername)
return r return r
def power(self, bot, game, player, arg): def power(self, bot, game, player, arg):
# Imposta qualcuno come protetto # Imposta qualcuno come protetto
selected = game.findplayerbyusername(arg) selected = game.findplayerbyusername(arg)
@ -172,7 +213,7 @@ class Angelo(Role):
player.message(bot, s.error_angel_no_selfprotect) player.message(bot, s.error_angel_no_selfprotect)
else: else:
player.message(bot, s.error_username) player.message(bot, s.error_username)
def onendday(self, bot, game): def onendday(self, bot, game):
# Resetta la protezione # Resetta la protezione
if self.protecting is not None: if self.protecting is not None:
@ -182,7 +223,7 @@ class Angelo(Role):
class Player: class Player:
"""Classe di un giocatore. Contiene tutti i dati riguardanti un giocatore all'interno di una partita, come il ruolo, """Classe di un giocatore. Contiene tutti i dati riguardanti un giocatore all'interno di una partita, come il ruolo,
e i dati riguardanti telegram, come ID e username.""" e i dati riguardanti telegram, come ID e username."""
def __init__(self, tid, tusername): def __init__(self, tid, tusername):
self.tid = tid # ID di Telegram self.tid = tid # ID di Telegram
self.tusername = tusername # Username di Telegram self.tusername = tusername # Username di Telegram
@ -201,9 +242,9 @@ class Player:
"""Manda un messaggio privato al giocatore.""" """Manda un messaggio privato al giocatore."""
bot.sendMessage(self.tid, text, parse_mode=ParseMode.MARKDOWN) bot.sendMessage(self.tid, text, parse_mode=ParseMode.MARKDOWN)
def kill(self): def kill(self, bot, game):
"""Uccidi il giocatore.""" """Uccidi il giocatore."""
# Perchè questo esiste? self.role.ondeath(bot, game)
self.alive = False self.alive = False
@ -355,7 +396,7 @@ class Game:
if player.mifiavotes > currenttop: if player.mifiavotes > currenttop:
mostvoted = list() mostvoted = list()
mostvoted.append(player) mostvoted.append(player)
currenttop = player.mifiavotes currenttop = player.mifiavotes
elif player.votes == currenttop: elif player.votes == currenttop:
mostvoted.append(player) mostvoted.append(player)
if currenttop > 0: if currenttop > 0:
@ -376,7 +417,7 @@ class Game:
self.message(bot, s.player_lynched.format(name=lynched.tusername, self.message(bot, s.player_lynched.format(name=lynched.tusername,
icon=lynched.role.icon, icon=lynched.role.icon,
role=lynched.role.name)) role=lynched.role.name))
lynched.kill() lynched.kill(bot, self)
else: else:
self.message(bot, s.no_players_lynched) self.message(bot, s.no_players_lynched)
# Fai gli endday in un certo ordine. # Fai gli endday in un certo ordine.
@ -393,7 +434,7 @@ class Game:
killed = killlist.pop() killed = killlist.pop()
if killed.alive: if killed.alive:
if killed.protectedby is None: if killed.protectedby is None:
killed.kill() killed.kill(bot, self)
self.message(bot, s.mifia_target_killed.format(target=killed.tusername, self.message(bot, s.mifia_target_killed.format(target=killed.tusername,
icon=killed.role.icon, icon=killed.role.icon,
role=killed.role.name)) role=killed.role.name))
@ -682,9 +723,9 @@ def kill(bot, update):
if update.message.from_user['id'] == game.adminid: if update.message.from_user['id'] == game.adminid:
target = game.findplayerbyusername(update.message.text.split(' ')[1]) target = game.findplayerbyusername(update.message.text.split(' ')[1])
if target is not None: if target is not None:
target.kill() target.kill(bot, game)
game.message(bot, s.admin_killed.format(name=target.tusername, game.message(bot, s.admin_killed.format(name=target.tusername,
icon=target.role.icon, icon=target.role.icon,
role=target.role.name)) role=target.role.name))
else: else:
game.message(bot, s.error_username) game.message(bot, s.error_username)
@ -719,12 +760,12 @@ def fakerole(bot, update):
if update.message.chat['type'] == 'private': if update.message.chat['type'] == 'private':
bot.sendMessage(update.message.chat['id'], s.role_assigned.format(icon=s.royal_icon, name=s.royal_name), bot.sendMessage(update.message.chat['id'], s.role_assigned.format(icon=s.royal_icon, name=s.royal_name),
parse_mode=ParseMode.MARKDOWN) parse_mode=ParseMode.MARKDOWN)
bot.sendMessage(update.message.chat['id'], s.role_assigned.format(icon=s.mifia_icon, name=s.mifia_name), bot.sendMessage(update.message.chat['id'], s.role_assigned.format(icon=s.mifia_icon, name=s.mifia_name),
parse_mode=ParseMode.MARKDOWN) parse_mode=ParseMode.MARKDOWN)
bot.sendMessage(update.message.chat['id'], s.role_assigned.format(icon=s.detective_icon, name=s.detective_name), bot.sendMessage(update.message.chat['id'], s.role_assigned.format(icon=s.detective_icon, name=s.detective_name),
parse_mode=ParseMode.MARKDOWN)
bot.sendMessage(update.message.chat['id'], s.role_assigned.format(icon=s.angel_icon, name=s.angel_name),
parse_mode=ParseMode.MARKDOWN) parse_mode=ParseMode.MARKDOWN)
bot.sendMessage(update.message.chat['id'], s.role_assigned.format(icon=s.angel_icon, name=s.angel_name),
parse_mode=ParseMode.MARKDOWN)
else: else:
bot.sendMessage(update.message.chat['id'], s.error_private_required, parse_mode=ParseMode.MARKDOWN) bot.sendMessage(update.message.chat['id'], s.error_private_required, parse_mode=ParseMode.MARKDOWN)
@ -759,7 +800,7 @@ def debug(bot, update):
if not player.alive: if not player.alive:
text += s.status_dead_player.format(name=player.tusername) text += s.status_dead_player.format(name=player.tusername)
else: else:
text += s.status_alive_player.format(icon=player.role.icon, text += s.status_alive_player.format(icon=player.role.icon,
name=player.tusername, name=player.tusername,
votes=str(player.votes)) votes=str(player.votes))
game.adminmessage(bot, text) game.adminmessage(bot, text)