1
Fork 0
mirror of https://github.com/RYGhub/royal-mifia.git synced 2024-11-22 05:44:19 +00:00
royal-mifia/main.py

939 lines
37 KiB
Python
Raw Normal View History

2016-05-26 09:52:01 +00:00
#!/usr/bin/env python3.5
2016-04-25 16:13:16 +00:00
# -*- coding: utf-8 -*-
2016-05-29 15:12:23 +00:00
import pickle
2016-04-25 16:21:38 +00:00
from telegram.ext import Updater, CommandHandler
2016-12-04 18:41:59 +00:00
from telegram import ParseMode, TelegramError
import filemanager
import random
2016-05-25 11:57:15 +00:00
import strings as s
2016-08-10 14:05:30 +00:00
import logging
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
2016-04-21 20:14:35 +00:00
token = filemanager.readfile('telegramapi.txt')
updater = Updater(token)
freenames = s.names_list.copy()
2016-05-27 22:13:34 +00:00
2016-04-21 17:52:01 +00:00
# Ruoli possibili per i giocatori
# Base di un ruolo
class Role:
"""Classe base di un ruolo. Da qui si sviluppano tutti gli altri ruoli."""
2016-06-02 15:09:07 +00:00
icon = "-" # Icona del ruolo, da visualizzare di fianco al nome
team = 'None' # Squadra: 'None', 'Good', 'Evil'
name = "UNDEFINED" # Nome del ruolo, viene visualizzato dall'investigatore e durante l'assegnazione
powerdesc = None # Ha un potere? Se sì, inviagli le info su come usarlo.
2016-12-05 18:07:18 +00:00
def __init__(self, player):
self.player = player
2016-05-27 09:46:54 +00:00
def __repr__(self) -> str:
2016-05-27 11:42:25 +00:00
r = "<undefined Role>"
2016-05-26 09:08:50 +00:00
return r
2016-04-21 17:52:01 +00:00
2016-12-05 18:07:18 +00:00
def power(self, bot, game, arg):
2016-05-25 09:52:53 +00:00
"""Il potere del ruolo. Si attiva quando il bot riceve un /power in chat privata."""
2016-04-21 17:52:01 +00:00
pass
2016-05-26 09:30:31 +00:00
def onendday(self, bot, game):
"""Metodo chiamato alla fine di ogni giorno."""
pass
2016-12-05 18:09:07 +00:00
def ondeath(self, bot, game):
"""Metodo chiamato alla morte del giocatore."""
2016-04-21 17:52:01 +00:00
pass
class Royal(Role):
2016-05-25 09:52:53 +00:00
"""Un membro della Royal Games. Il ruolo principale, non ha alcun potere se non quello di votare."""
2016-06-02 15:09:07 +00:00
icon = s.royal_icon
team = 'Good'
name = s.royal_name
2016-12-05 18:07:18 +00:00
def __init__(self, player):
super().__init__(player)
2016-04-21 17:52:01 +00:00
2016-05-27 09:46:54 +00:00
def __repr__(self) -> str:
2016-05-27 11:42:25 +00:00
r = "<Role: Royal>"
2016-05-26 09:08:50 +00:00
return r
2016-04-21 17:52:01 +00:00
class Mifioso(Role):
2016-05-25 11:57:15 +00:00
"""Il nemico globale. Può impostare come bersaglio una persona al giorno, per poi ucciderla alla fine."""
2016-06-02 15:09:07 +00:00
icon = s.mifia_icon
team = 'Evil'
name = s.mifia_name
powerdesc = s.mifia_power_description
2016-12-05 18:07:18 +00:00
def __init__(self, player):
super().__init__(player)
2016-05-22 15:13:26 +00:00
self.target = None
2016-05-27 09:46:54 +00:00
def __repr__(self) -> str:
2016-05-26 09:08:50 +00:00
if self.target is None:
2016-05-27 11:42:25 +00:00
r = "<Role: Mifioso>"
2016-05-26 09:08:50 +00:00
else:
2016-05-27 11:42:25 +00:00
r = "<Role: Mifioso, targeting {target}>".format(target=self.target.tusername)
2016-05-26 09:08:50 +00:00
return r
2016-04-21 17:52:01 +00:00
2016-12-05 18:07:18 +00:00
def power(self, bot, game, arg):
2016-05-25 09:52:53 +00:00
# Imposta una persona come bersaglio da uccidere.
selected = game.findplayerbyusername(arg)
2016-05-27 08:39:12 +00:00
if selected is not None:
self.target = selected
2016-12-05 18:07:18 +00:00
self.player.message(bot, s.mifia_target_selected.format(target=self.target.tusername))
else:
2016-12-05 18:07:18 +00:00
self.player.message(bot, s.error_username)
2016-04-21 17:52:01 +00:00
2016-05-26 09:30:31 +00:00
def onendday(self, bot, game):
2016-05-26 13:12:49 +00:00
if not game.votingmifia:
# Uccidi il bersaglio se non è protetto da un Angelo.
if self.target is not None:
if self.target.protectedby is None:
self.target.kill(bot, game)
2016-05-27 11:42:25 +00:00
game.message(bot, s.mifia_target_killed.format(target=self.target.tusername,
icon=self.target.role.icon,
role=self.target.role.name))
2016-05-26 13:12:49 +00:00
else:
2016-05-27 11:42:25 +00:00
game.message(bot, s.mifia_target_protected.format(target=self.target.tusername,
icon=self.target.protectedby.role.icon,
protectedby=self.target.protectedby.tusername))
2016-05-26 13:12:49 +00:00
self.target = None
else:
2016-05-03 11:29:29 +00:00
self.target = None
2016-04-21 17:52:01 +00:00
class Investigatore(Role):
2016-05-25 09:52:53 +00:00
"""L'investigatore può indagare sul vero ruolo di una persona una volta al giorno."""
2016-06-02 15:09:07 +00:00
icon = s.detective_icon
team = 'Good'
name = s.detective_name
powerdesc = s.detective_power_description
refillpoweruses = 1
2016-12-05 18:07:18 +00:00
def __init__(self, player):
super().__init__(player)
self.poweruses = self.refillpoweruses
2016-05-27 09:46:54 +00:00
def __repr__(self) -> str:
2016-05-27 11:42:25 +00:00
r = "<Role: Investigatore, {uses} uses left>".format(uses=self.poweruses)
2016-05-26 09:08:50 +00:00
return r
2016-04-21 17:52:01 +00:00
2016-12-05 18:07:18 +00:00
def power(self, bot, game, arg):
2016-05-25 09:52:53 +00:00
# Indaga sul vero ruolo di una persona, se sono ancora disponibili usi del potere.
2016-04-23 15:12:52 +00:00
if self.poweruses > 0:
target = game.findplayerbyusername(arg)
if target is not None:
self.poweruses -= 1
2016-12-05 18:07:18 +00:00
self.player.message(bot, s.detective_discovery.format(target=target.tusername,
2016-05-27 11:42:25 +00:00
icon=target.role.icon,
role=target.role.name,
left=self.poweruses))
2016-04-23 15:12:52 +00:00
else:
2016-12-05 18:07:18 +00:00
self.player.message(bot, s.error_username)
2016-04-23 15:12:52 +00:00
else:
2016-12-05 18:07:18 +00:00
self.player.message(bot, s.error_no_uses)
2016-04-21 21:04:53 +00:00
2016-05-26 09:30:31 +00:00
def onendday(self, bot, game):
2016-04-21 21:04:53 +00:00
# Ripristina il potere
self.poweruses = self.refillpoweruses
2016-04-21 21:04:53 +00:00
2016-05-03 11:29:29 +00:00
class Angelo(Role):
2016-05-27 11:42:25 +00:00
"""L'angelo può proteggere una persona al giorno dalla Mifia.
Se ha successo nella protezione, il suo ruolo sarà rivelato a tutti."""
2016-06-02 15:09:07 +00:00
icon = s.angel_icon
team = 'Good' # Squadra: 'None', 'Good', 'Evil'
name = s.angel_name
powerdesc = s.angel_power_description
2016-12-05 18:07:18 +00:00
def __init__(self, player):
super().__init__(player)
2016-05-25 09:52:53 +00:00
self.protecting = None # La persona che questo angelo sta proteggendo
2016-05-27 09:46:54 +00:00
def __repr__(self) -> str:
2016-05-27 22:13:34 +00:00
if self.protecting is None:
2016-05-27 11:42:25 +00:00
r = "<Role: Angelo>"
2016-05-26 09:08:50 +00:00
else:
2016-05-27 11:42:25 +00:00
r = "<Role: Angelo, protecting {target}>".format(target=self.protecting.tusername)
2016-05-26 09:08:50 +00:00
return r
2016-12-05 18:07:18 +00:00
def power(self, bot, game, arg):
2016-05-25 09:52:53 +00:00
# Imposta qualcuno come protetto
2016-05-03 11:29:29 +00:00
selected = game.findplayerbyusername(arg)
if selected is not None:
if selected is not Player:
# Togli la protezione a quello che stavi proteggendo prima
if self.protecting is not None:
self.protecting.protectedby = None
2016-12-05 18:07:18 +00:00
selected.protectedby = self.player
self.protecting = selected
2016-12-05 18:07:18 +00:00
self.player.message(bot, s.angel_target_selected.format(target=self.protecting.tusername))
else:
2016-12-05 18:07:18 +00:00
self.player.message(bot, s.error_angel_no_selfprotect)
else:
2016-12-05 18:07:18 +00:00
self.player.message(bot, s.error_username)
2016-05-26 09:30:31 +00:00
def onendday(self, bot, game):
2016-05-03 11:29:29 +00:00
# Resetta la protezione
if self.protecting is not None:
self.protecting.protectedby = None
2016-05-03 11:29:29 +00:00
self.protecting = None
2016-05-03 19:49:39 +00:00
2016-08-10 15:02:30 +00:00
class Terrorista(Role):
"""Il terrorista è un mifioso che può uccidere in un solo modo: facendosi uccidere dai Royal.
Se riesce, vince la partita e uccide tutti quelli che lo hanno votato."""
icon = s.terrorist_icon
team = "Evil"
name = s.terrorist_name
powerdesc = s.terrorist_power_description
def __repr__(self) -> str:
r = "<Role: Terrorista>"
return r
2016-12-05 18:09:07 +00:00
def ondeath(self, bot, game):
2016-08-10 15:02:30 +00:00
# Se è stato ucciso da una votazione, attiva il suo potere
2016-12-05 18:09:07 +00:00
if self.player == game.lastlynch:
2016-08-10 15:02:30 +00:00
game.message(bot, s.terrorist_kaboom)
for selectedplayer in game.players:
2016-12-05 18:16:15 +00:00
if selectedplayer.votingfor == self.player:
2016-08-10 15:02:30 +00:00
game.message(bot, s.terrorist_target_killed.format(target=selectedplayer.tusername,
icon=selectedplayer.role.icon,
role=selectedplayer.role.name))
selectedplayer.kill(bot, game)
2016-12-04 18:32:40 +00:00
class Derek(Role):
"""Derek muore. Quando gli pare."""
icon = s.derek_icon
team = "Good"
name = s.derek_name
powerdesc = s.derek_power_description
2016-12-05 18:07:18 +00:00
def __init__(self, player):
super().__init__(player)
2016-12-04 18:32:40 +00:00
# Per qualche motivo assurdo ho deciso di tenere l'oggetto Player qui
self.deathwish = None
def __repr__(self) -> str:
r = "<Role: Derek>"
return r
2016-12-05 18:07:18 +00:00
def power(self, bot, game, arg):
2016-12-04 18:32:40 +00:00
# Attiva / disattiva la morte alla fine del round
if self.deathwish is not None:
self.deathwish = None
2016-12-05 18:07:18 +00:00
self.player.message(bot, s.derek_deathwish_unset)
2016-12-04 18:32:40 +00:00
else:
2016-12-05 18:07:18 +00:00
self.deathwish = self.player
self.player.message(bot, s.derek_deathwish_set)
2016-12-04 18:32:40 +00:00
def onendday(self, bot, game):
if self.deathwish is not None:
game.message(bot, s.derek_deathwish_successful.format(icon=s.derek_icon,
role=s.derek_name,
name=self.deathwish.tusername))
2016-12-04 19:18:35 +00:00
self.deathwish.kill(bot, game)
2016-12-04 18:32:40 +00:00
class Disastro(Role):
"""L'investigatore sbadato investiga, ma giunge a conclusioni sbagliate..."""
icon = s.detective_icon
team = 'Good'
name = s.detective_name
powerdesc = s.detective_power_description
refillpoweruses = 1
2016-12-05 18:07:18 +00:00
def __init__(self, player):
super().__init__(player)
self.poweruses = self.refillpoweruses
def __repr__(self) -> str:
r = "<Role: Investigatore, {uses} uses left>".format(uses=self.poweruses)
return r
2016-12-05 18:07:18 +00:00
def power(self, bot, game, arg):
# Indaga sul vero ruolo di una persona, se sono ancora disponibili usi del potere.
if self.poweruses > 0:
target = game.findplayerbyusername(arg)
if target is not None:
self.poweruses -= 1
randomrole = random.sample(rolepriority, 1)[0]
2016-12-05 18:07:18 +00:00
self.player.message(bot, s.detective_discovery.format(target=target.tusername,
icon=randomrole.role.icon,
role=randomrole.role.name,
left=self.poweruses))
else:
2016-12-05 18:07:18 +00:00
self.player.message(bot, s.error_username)
else:
2016-12-05 18:07:18 +00:00
self.player.message(bot, s.error_no_uses)
def onendday(self, bot, game):
# Ripristina il potere
self.poweruses = self.refillpoweruses
2016-12-05 18:09:07 +00:00
def ondeath(self, bot, game):
game.message(bot, s.disaster_revealed.format(icon=s.disaster_icon,
role=s.disaster_name,
2016-12-05 18:09:07 +00:00
name=self.player.tusername))
rolepriority = [Mifioso, Investigatore, Disastro, Angelo, Derek, Terrorista, Royal]
2016-08-10 12:55:58 +00:00
2016-04-21 18:20:26 +00:00
class Player:
2016-05-27 11:42:25 +00:00
"""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."""
2016-05-26 09:30:31 +00:00
def __init__(self, tid, tusername):
2016-05-25 09:52:53 +00:00
self.tid = tid # ID di Telegram
self.tusername = tusername # Username di Telegram
2016-12-05 18:07:18 +00:00
self.role = Role(self) # Di base, ogni giocatore è un ruolo indefinito
2016-05-22 15:13:26 +00:00
self.alive = True
self.votingfor = None # Diventa un player se ha votato
2016-05-25 09:52:53 +00:00
self.votes = 0 # Voti che sta ricevendo questo giocatore. Aggiornato da updatevotes()
self.protectedby = None # Protettore. Oggetto player che protegge questo giocatore dalla mifia.
2016-05-26 13:12:49 +00:00
self.mifiavotes = 0 # Voti che sta ricevendo questo giocatore dalla mifia. Aggiornato da updatemifiavotes()
2016-04-21 18:20:26 +00:00
2016-05-27 09:46:54 +00:00
def __repr__(self) -> str:
2016-05-27 11:42:25 +00:00
r = "<Player {username}>".format(username=self.tusername)
2016-05-27 09:46:54 +00:00
return r
2016-05-26 09:30:31 +00:00
def message(self, bot, text):
"""Manda un messaggio privato al giocatore."""
2016-12-04 18:43:19 +00:00
try:
bot.sendMessage(self.tid, text, parse_mode=ParseMode.MARKDOWN)
except TelegramError:
pass
def kill(self, bot, game):
"""Uccidi il giocatore."""
2016-12-05 18:09:07 +00:00
self.role.ondeath(bot, game)
self.alive = False
2016-05-25 09:52:53 +00:00
2016-05-27 22:13:34 +00:00
2016-04-21 18:20:26 +00:00
class Game:
2016-05-27 11:42:25 +00:00
"""Classe di una partita, contenente parametri riguardanti stato della partita
e informazioni sul gruppo di Telegram."""
2016-12-04 18:54:33 +00:00
def __init__(self, groupid):
2016-05-25 09:52:53 +00:00
self.groupid = groupid # ID del gruppo in cui si sta svolgendo una partita
2016-12-04 18:54:33 +00:00
self.admin = None # ID telegram dell'utente che ha creato la partita con /newgame
2016-05-25 09:52:53 +00:00
self.players = list() # Lista dei giocatori in partita
2016-05-22 15:13:26 +00:00
self.tokill = list() # Giocatori che verranno uccisi all'endday
self.phase = 'Join' # Fase di gioco: 'Join', 'Config', 'Voting'
self.configstep = 0 # Passo attuale di configurazione
2016-08-10 12:55:58 +00:00
self.roleconfig = dict() # Dizionario con le quantità di ruoli da aggiungere
2016-05-26 13:12:49 +00:00
self.votingmifia = False # Seguire le regole originali della mifia che vota?
# Liste di ruoli in gioco, per velocizzare gli endday
2016-08-10 12:55:58 +00:00
self.playersinrole = dict()
for currentrole in rolepriority:
self.playersinrole[currentrole.__name__] = list()
# Trova un nome per la partita
if len(freenames) > 0:
2016-05-26 09:49:40 +00:00
random.shuffle(freenames)
2016-05-26 09:08:50 +00:00
self.name = freenames.pop()
else:
2016-05-26 09:08:50 +00:00
self.name = str(groupid)
2016-08-10 15:02:30 +00:00
self.lastlynch = None # Ultima persona uccisa dai Royal, diventa un player
def __del__(self):
2016-05-26 09:08:50 +00:00
# Rimetti il nome che si è liberato in disponibili.
try:
int(self.name)
2016-05-26 09:10:52 +00:00
except ValueError:
2016-05-26 09:08:50 +00:00
freenames.append(self.name)
2016-05-26 09:08:50 +00:00
def __repr__(self):
2016-05-27 11:42:25 +00:00
r = "<Game {name} in group {groupid} with {nplayers} players in phase {phase}>" \
.format(name=self.name, groupid=self.groupid, nplayers=len(self.players), phase=self.phase)
2016-05-26 09:08:50 +00:00
return r
2016-04-21 18:20:26 +00:00
2016-05-26 09:30:31 +00:00
def message(self, bot, text):
2016-05-25 09:52:53 +00:00
"""Manda un messaggio nel gruppo."""
bot.sendMessage(self.groupid, text, parse_mode=ParseMode.MARKDOWN)
2016-04-21 18:20:26 +00:00
2016-05-26 09:30:31 +00:00
def adminmessage(self, bot, text):
2016-05-25 09:52:53 +00:00
"""Manda un messaggio privato al creatore della partita."""
2016-12-04 18:54:33 +00:00
self.admin.message(bot, text)
2016-04-21 18:20:26 +00:00
2016-05-26 09:30:31 +00:00
def mifiamessage(self, bot, text):
2016-05-25 09:52:53 +00:00
"""Manda un messaggio privato a tutti i Mifiosi nella partita."""
2016-04-21 18:20:26 +00:00
# Trova tutti i mifiosi nell'elenco dei giocatori
for player in self.players:
if isinstance(player.role, Mifioso):
player.message(bot, text)
2016-05-26 09:30:31 +00:00
def findplayerbyid(self, tid) -> Player:
2016-05-25 09:52:53 +00:00
"""Trova il giocatore con un certo id."""
for player in self.players:
if player.tid == tid:
return player
else:
return None
2016-05-26 09:30:31 +00:00
def findplayerbyusername(self, tusername) -> Player:
2016-05-25 09:52:53 +00:00
"""Trova il giocatore con un certo username."""
for player in self.players:
2016-04-23 15:07:09 +00:00
if player.tusername.lower() == tusername.lower():
return player
else:
return None
def assignroles(self, bot):
2016-05-25 09:52:53 +00:00
"""Assegna ruoli casuali a tutti i giocatori."""
2016-04-22 17:55:24 +00:00
random.seed()
playersleft = self.players.copy()
2016-04-23 16:26:32 +00:00
# Seleziona mifiosi
2016-08-10 12:55:58 +00:00
for currentrole in rolepriority:
for player in random.sample(playersleft, self.roleconfig[currentrole.__name__]):
self.playersinrole[currentrole.__name__].append(player)
2016-12-05 18:07:18 +00:00
player.role = currentrole(self)
2016-08-10 12:55:58 +00:00
playersleft.remove(player)
2016-04-22 17:55:24 +00:00
# Assegna il ruolo di Royal a tutti gli altri
for player in playersleft:
2016-12-05 18:07:18 +00:00
player.role = Royal(self)
2016-04-23 16:26:32 +00:00
# Manda i ruoli assegnati a tutti
for player in self.players:
2016-05-25 11:57:15 +00:00
player.message(bot, s.role_assigned.format(icon=player.role.icon, name=player.role.name))
2016-05-26 09:40:59 +00:00
if player.role.powerdesc is not None:
player.message(bot, player.role.powerdesc.format(gamename=self.name))
2016-06-01 12:21:51 +00:00
# Manda ai mifiosi l'elenco dei loro compagni di squadra
text = s.mifia_team_intro
2016-08-10 12:55:58 +00:00
for player in self.playersinrole['Mifioso']:
2016-06-01 12:21:51 +00:00
text += s.mifia_team_player.format(icon=player.role.icon, name=player.tusername)
2016-08-10 12:55:58 +00:00
for player in self.playersinrole['Mifioso']:
2016-06-01 12:21:51 +00:00
player.message(bot, text)
2016-04-22 19:14:14 +00:00
def updatevotes(self):
2016-05-25 09:52:53 +00:00
"""Aggiorna il conteggio dei voti di tutti i giocatori."""
2016-04-22 19:14:14 +00:00
for player in self.players:
player.votes = 0
for player in self.players:
2016-05-25 14:27:11 +00:00
if player.votingfor is not None and player.alive:
2016-04-23 15:37:43 +00:00
player.votingfor.votes += 1
2016-04-22 19:14:14 +00:00
2016-05-27 08:51:52 +00:00
def updatemifiavotes(self):
2016-05-26 13:12:49 +00:00
"""Aggiorna il conteggio dei voti mifiosi di tutti i giocatori."""
for player in self.players:
player.mifiavotes = 0
2016-08-10 12:55:58 +00:00
for player in self.playersinrole['Mifioso']:
if player.alive:
2016-05-26 13:12:49 +00:00
if player.role.target is not None:
player.role.target.mifiavotes += 1
2016-05-25 14:27:11 +00:00
def mostvotedplayer(self) -> list:
2016-05-25 09:52:53 +00:00
"""Trova il giocatore più votato."""
2016-05-25 14:27:11 +00:00
mostvoted = list()
currenttop = 0
2016-04-22 19:14:14 +00:00
self.updatevotes()
for player in self.players:
2016-05-25 14:27:11 +00:00
if player.votes > currenttop:
2016-05-27 09:51:57 +00:00
mostvoted = list()
mostvoted.append(player)
currenttop = player.votes
2016-05-25 14:27:11 +00:00
elif player.votes == currenttop:
mostvoted.append(player)
2016-05-26 13:12:49 +00:00
if currenttop > 0:
return mostvoted
else:
2016-05-27 08:51:52 +00:00
return list()
2016-05-26 13:12:49 +00:00
def mostvotedmifia(self) -> list:
"""Trova il giocatore più votato dalla mifia."""
mostvoted = list()
currenttop = 0
2016-05-27 08:51:52 +00:00
self.updatemifiavotes()
2016-05-26 13:12:49 +00:00
for player in self.players:
2016-05-27 09:51:57 +00:00
if player.mifiavotes > currenttop:
mostvoted = list()
mostvoted.append(player)
currenttop = player.mifiavotes
2016-05-27 09:51:57 +00:00
elif player.votes == currenttop:
2016-05-26 13:12:49 +00:00
mostvoted.append(player)
if currenttop > 0:
return mostvoted
else:
2016-05-27 08:51:52 +00:00
return list()
2016-05-26 13:12:49 +00:00
2016-04-22 19:14:14 +00:00
def endday(self, bot):
2016-05-25 14:27:11 +00:00
"""Finisci la giornata, uccidi il più votato del giorno ed esegui gli endday di tutti i giocatori."""
# Conta i voti ed elimina il più votato.
topvotes = self.mostvotedplayer()
if len(topvotes) > 0:
# In caso di pareggio, elimina un giocatore casuale.
random.seed()
random.shuffle(topvotes)
lynched = topvotes.pop()
2016-05-26 09:59:58 +00:00
if lynched.alive:
2016-05-27 11:42:25 +00:00
self.message(bot, s.player_lynched.format(name=lynched.tusername,
icon=lynched.role.icon,
role=lynched.role.name))
2016-08-10 15:02:30 +00:00
self.lastlynch = lynched
lynched.kill(bot, self)
2016-05-25 14:27:11 +00:00
else:
self.message(bot, s.no_players_lynched)
# Fai gli endday in un certo ordine.
# Si potrebbe fare più velocemente, credo.
2016-05-25 14:27:11 +00:00
# Ma non sto ho voglia di ottimizzare ora.
# Mifiosi
2016-05-26 13:12:49 +00:00
if self.votingmifia:
# Trova il più votato dai mifiosi e uccidilo
2016-05-27 09:16:02 +00:00
killlist = self.mostvotedmifia()
2016-05-26 13:12:49 +00:00
if len(killlist) > 0:
# In caso di pareggio, elimina un giocatore casuale.
random.seed()
random.shuffle(killlist)
killed = killlist.pop()
if killed.alive:
2016-05-27 22:40:30 +00:00
if killed.protectedby is None:
killed.kill(bot, self)
2016-05-27 22:40:30 +00:00
self.message(bot, s.mifia_target_killed.format(target=killed.tusername,
icon=killed.role.icon,
role=killed.role.name))
else:
self.message(bot, s.mifia_target_protected.format(target=killed.tusername,
icon=killed.protectedby.role.icon,
protectedby=killed.protectedby.tusername))
2016-08-10 12:55:58 +00:00
# Attiva gli onendday
for currentrole in rolepriority:
for player in self.playersinrole[currentrole.__name__]:
if player.alive:
player.role.onendday(bot, self)
# Cancella tutti i voti
2016-04-23 15:37:43 +00:00
for player in self.players:
player.votingfor = None
2016-08-10 15:42:57 +00:00
# Controlla se qualcuno ha vinto
self.victoryconditions(bot)
2016-04-23 16:26:32 +00:00
2016-05-27 13:53:06 +00:00
def endconfig(self, bot):
2016-05-27 13:34:23 +00:00
"""Fine della fase di config, inizio assegnazione ruoli"""
# Controlla che ci siano abbastanza giocatori per avviare la partita
requiredplayers = 0
2016-08-10 13:53:55 +00:00
for selectedrole in self.roleconfig:
requiredplayers += self.roleconfig[selectedrole]
# Se non ce ne sono abbastanza, torna alla fase di join
if requiredplayers > len(self.players):
self.phase = 'Join'
self.configstep = 0
2016-08-10 13:53:55 +00:00
self.message(bot, s.error_not_enough_players)
2016-05-27 13:34:23 +00:00
else:
self.phase = 'Voting'
self.assignroles(bot)
2016-05-27 13:39:17 +00:00
self.message(bot, s.roles_assigned_successfully)
2016-05-27 13:34:23 +00:00
2016-04-23 16:26:32 +00:00
def endgame(self):
inprogress.remove(self)
2016-04-22 19:14:14 +00:00
2016-05-29 15:12:23 +00:00
def save(self):
# Crea il file.
2016-06-03 20:04:37 +00:00
try:
file = open(str(self.groupid) + ".p", 'x')
except FileExistsError:
pass
else:
file.close()
2016-05-29 15:12:23 +00:00
# Scrivi sul file.
file = open(str(self.groupid) + ".p", 'wb')
pickle.dump(self, file)
file.close()
2016-08-10 15:42:57 +00:00
def victoryconditions(self, bot):
# Condizioni di vittoria
good = 0
evil = 0
for player in self.players:
if player.alive and player.role.team == 'Evil':
evil += 1
elif player.alive and player.role.team == 'Good':
good += 1
# Distruzione atomica!
if good == 0 and evil == 0:
self.message(bot, s.end_game_wiped)
for player in self.players:
player.message(bot, s.end_game_wiped + s.tie)
# Mifiosi più dei Royal
2016-08-10 15:44:36 +00:00
elif evil >= good:
2016-08-10 15:42:57 +00:00
self.message(bot, s.end_mifia_outnumber + s.victory_mifia)
for player in self.players:
if player.role.team == 'Good':
player.message(bot, s.end_mifia_outnumber + s.defeat)
elif player.role.team == 'Evil':
player.message(bot, s.end_mifia_outnumber + s.victory)
self.endgame()
# Male distrutto
elif evil == 0:
self.message(bot, s.end_mifia_killed + s.victory_royal)
for player in self.players:
if player.role.team == 'Good':
player.message(bot, s.end_mifia_killed + s.victory)
elif player.role.team == 'Evil':
player.message(bot, s.end_mifia_killed + s.defeat)
self.endgame()
2016-08-10 12:55:58 +00:00
2016-04-21 20:14:35 +00:00
# Partite in corso
inprogress = list()
2016-04-21 18:20:26 +00:00
2016-05-26 09:30:31 +00:00
def findgamebyid(gid) -> Game:
2016-05-25 09:52:53 +00:00
"""Trova una partita con un certo id."""
2016-04-21 21:04:53 +00:00
for game in inprogress:
if game.groupid == gid:
return game
2016-05-26 09:30:31 +00:00
def findgamebyname(name) -> Game:
2016-05-26 09:08:50 +00:00
"""Trova una partita con un certo nome."""
for game in inprogress:
if game.name.lower() == name.lower():
2016-05-26 10:10:07 +00:00
return game
2016-05-27 22:13:34 +00:00
2016-04-21 17:52:01 +00:00
# Comandi a cui risponde il bot
def ping(bot, update):
2016-05-25 09:52:53 +00:00
"""Ping!"""
bot.sendMessage(update.message.chat['id'], s.pong, parse_mode=ParseMode.MARKDOWN)
2016-04-21 20:14:35 +00:00
def newgame(bot, update):
2016-05-25 09:52:53 +00:00
"""Crea una nuova partita."""
2016-04-21 20:14:35 +00:00
if update.message.chat['type'] != 'private':
game = findgamebyid(update.message.chat['id'])
if game is None:
2016-12-04 18:54:33 +00:00
game = Game(update.message.chat['id'])
inprogress.append(game)
game.message(bot, s.new_game.format(groupid=game.groupid, name=game.name))
2016-12-04 18:54:33 +00:00
join(bot, update)
else:
bot.sendMessage(update.message.chat['id'], s.error_game_in_progress, parse_mode=ParseMode.MARKDOWN)
2016-04-21 20:14:35 +00:00
else:
bot.sendMessage(update.message.chat['id'], s.error_chat_type, parse_mode=ParseMode.MARKDOWN)
2016-04-21 21:04:53 +00:00
def join(bot, update):
2016-05-25 09:52:53 +00:00
"""Unisciti a una partita."""
2016-04-21 21:04:53 +00:00
game = findgamebyid(update.message.chat['id'])
if game is not None:
if game.phase == 'Join':
2016-04-22 18:39:16 +00:00
p = game.findplayerbyid(update.message.from_user['id'])
if p is None:
p = Player(update.message.from_user['id'], update.message.from_user['username'])
2016-12-04 18:41:59 +00:00
try:
2016-12-04 19:18:35 +00:00
p.message(bot, s.you_joined.format(game=game.name))
2016-12-04 18:41:59 +00:00
except TelegramError:
game.message(bot, s.error_chat_unavailable)
else:
game.message(bot, s.player_joined.format(name=p.tusername))
2016-12-04 18:54:33 +00:00
if len(game.players) == 0:
game.admin = p
2016-12-04 18:41:59 +00:00
game.players.append(p)
2016-04-22 18:39:16 +00:00
else:
game.message(bot, s.error_player_already_joined)
else:
2016-05-26 09:08:50 +00:00
game.message(bot, s.error_join_phase_ended)
else:
bot.sendMessage(update.message.chat['id'], s.error_no_games_found, parse_mode=ParseMode.MARKDOWN)
def status(bot, update):
2016-05-25 09:52:53 +00:00
"""Visualizza lo stato della partita."""
game = findgamebyid(update.message.chat['id'])
if game is not None:
2016-12-04 18:54:33 +00:00
text = s.status_header.format(name=game.name, admin=game.admin.tusername, phase=game.phase)
if __debug__:
text += s.debug_mode
2016-04-23 15:37:43 +00:00
game.updatevotes()
# Aggiungi l'elenco dei giocatori
for player in game.players:
2016-04-23 15:07:09 +00:00
if not player.alive:
2016-05-26 09:52:01 +00:00
text += s.status_dead_player.format(name=player.tusername)
else:
2016-06-02 15:09:07 +00:00
text += s.status_alive_player.format(icon="\U0001F610",
name=player.tusername,
votes=str(player.votes))
game.message(bot, text)
else:
bot.sendMessage(update.message.chat['id'], s.error_no_games_found, parse_mode=ParseMode.MARKDOWN)
def endjoin(bot, update):
2016-05-25 09:52:53 +00:00
"""Termina la fase di join e inizia quella di votazione."""
game = findgamebyid(update.message.chat['id'])
2016-06-03 20:54:09 +00:00
if game is not None and game.phase == 'Join':
2016-12-04 18:54:33 +00:00
if update.message.from_user['id'] == game.admin.tid:
# Inizio fase di configurazione
game.phase = 'Config'
2016-05-26 09:08:50 +00:00
game.message(bot, s.join_phase_ended)
game.message(bot, s.config_list[0])
2016-05-26 09:08:50 +00:00
else:
game.message(bot, s.error_not_admin)
else:
bot.sendMessage(update.message.chat['id'], s.error_no_games_found, parse_mode=ParseMode.MARKDOWN)
2016-05-25 11:57:15 +00:00
2016-05-27 22:13:34 +00:00
def config(bot, update):
"""Configura il parametro richiesto."""
game = findgamebyid(update.message.chat['id'])
if game is not None and game.phase is 'Config':
2016-12-04 18:54:33 +00:00
if update.message.from_user['id'] == game.admin.tid:
cmd = update.message.text.split(' ', 1)
2016-05-27 09:46:54 +00:00
if len(cmd) >= 2:
if game.configstep == 0:
try:
game.roleconfig["Mifioso"] = int(cmd[1])
except ValueError:
game.message(bot, s.error_invalid_config)
else:
game.configstep += 1
game.message(bot, s.config_list[game.configstep])
elif game.configstep == 1:
try:
2016-08-10 13:53:55 +00:00
game.roleconfig["Investigatore"] = int(cmd[1])
except ValueError:
game.message(bot, s.error_invalid_config)
else:
game.configstep += 1
game.message(bot, s.config_list[game.configstep])
elif game.configstep == 2:
try:
game.roleconfig["Angelo"] = int(cmd[1])
except ValueError:
game.message(bot, s.error_invalid_config)
2016-05-26 13:12:49 +00:00
else:
game.configstep += 1
game.message(bot, s.config_list[game.configstep])
elif game.configstep == 3:
2016-08-10 15:02:30 +00:00
try:
game.roleconfig["Terrorista"] = int(cmd[1])
except ValueError:
game.message(bot, s.error_invalid_config)
else:
game.configstep += 1
game.message(bot, s.config_list[game.configstep])
elif game.configstep == 4:
2016-12-04 18:54:33 +00:00
try:
game.roleconfig["Derek"] = int(cmd[1])
except ValueError:
game.message(bot, s.error_invalid_config)
else:
game.configstep += 1
game.message(bot, s.config_list[game.configstep])
elif game.configstep == 5:
2016-12-05 17:57:08 +00:00
try:
game.roleconfig["Disastro"] = int(cmd[1])
except ValueError:
game.message(bot, s.error_invalid_config)
else:
game.configstep += 1
game.message(bot, s.config_list[game.configstep])
elif game.configstep == 6:
2016-05-27 13:16:47 +00:00
if cmd[1].lower() == 'testa':
game.votingmifia = False
2016-05-27 13:53:06 +00:00
game.endconfig(bot)
2016-05-27 13:16:47 +00:00
elif cmd[1].lower() == 'unica':
game.votingmifia = True
2016-05-27 13:53:06 +00:00
game.endconfig(bot)
else:
2016-05-27 13:34:23 +00:00
game.message(bot, s.error_invalid_config)
else:
game.message(bot, s.config_list[game.configstep])
2016-05-26 11:50:16 +00:00
else:
game.message(bot, s.error_not_admin)
else:
2016-08-10 15:02:30 +00:00
bot.sendMessage(update.message.chat['id'], s.error_no_games_found, parse_mode=ParseMode.MARKDOWN)
def vote(bot, update):
2016-05-25 09:52:53 +00:00
"""Vota per uccidere una persona."""
game = findgamebyid(update.message.chat['id'])
if game is not None and game.phase is 'Voting':
player = game.findplayerbyid(update.message.from_user['id'])
2016-05-26 09:18:07 +00:00
if player is not None:
2016-05-26 09:08:50 +00:00
if player.alive:
target = game.findplayerbyusername(update.message.text.split(' ')[1])
if target is not None:
player.votingfor = target
game.message(bot, s.vote.format(voted=target.tusername))
else:
game.message(bot, s.error_username)
else:
game.message(bot, s.error_dead)
else:
game.message(bot, s.error_not_in_game)
else:
bot.sendMessage(update.message.chat['id'], s.error_no_games_found, parse_mode=ParseMode.MARKDOWN)
2016-04-21 21:04:53 +00:00
2016-04-21 20:14:35 +00:00
2016-04-22 19:14:14 +00:00
def endday(bot, update):
2016-05-25 09:52:53 +00:00
"""Termina la giornata attuale."""
2016-04-22 19:14:14 +00:00
game = findgamebyid(update.message.chat['id'])
2016-12-04 18:54:33 +00:00
if game is not None and game.phase is 'Voting' and update.message.from_user['id'] == game.admin.tid:
2016-04-22 19:14:14 +00:00
game.endday(bot)
2016-04-23 15:07:09 +00:00
def power(bot, update):
2016-05-25 09:52:53 +00:00
"""Attiva il potere del tuo ruolo."""
2016-04-23 15:07:09 +00:00
if update.message.chat['type'] == 'private':
cmd = update.message.text.split(' ', 2)
game = findgamebyname(cmd[1])
# Se non lo trovi con il nome, prova con l'id
if game is None:
2016-05-26 09:08:50 +00:00
game = findgamebyid(int(cmd[1]))
2016-04-23 15:07:09 +00:00
if game is not None:
2016-05-26 10:12:14 +00:00
player = game.findplayerbyid(int(update.message.from_user['id']))
if player is not None:
2016-05-26 09:08:50 +00:00
if player.alive:
2016-12-05 18:07:18 +00:00
player.role.power(bot, game, cmd[2])
2016-05-26 09:08:50 +00:00
else:
player.message(bot, s.error_dead)
else:
bot.sendMessage(update.message.chat['id'], s.error_not_in_game, parse_mode=ParseMode.MARKDOWN)
2016-04-23 15:07:09 +00:00
else:
bot.sendMessage(update.message.chat['id'], s.error_no_games_found, parse_mode=ParseMode.MARKDOWN)
2016-04-23 15:07:09 +00:00
else:
bot.sendMessage(update.message.chat['id'], s.error_private_required, parse_mode=ParseMode.MARKDOWN)
2016-04-23 15:07:09 +00:00
2016-05-27 22:13:34 +00:00
def role(bot, update):
2016-05-26 09:08:50 +00:00
"""Visualizza il tuo ruolo."""
game = findgamebyid(update.message.chat['id'])
if game is not None and game.phase is 'Voting':
player = game.findplayerbyid(update.message.from_user['id'])
if player is not None:
2016-05-26 09:08:50 +00:00
if player.alive:
2016-06-01 07:19:37 +00:00
player.message(bot, s.role_assigned.format(icon=player.role.icon, name=player.role.name))
2016-05-26 09:08:50 +00:00
game.message(bot, s.check_private)
else:
game.message(bot, s.error_dead)
else:
bot.sendMessage(update.message.chat['id'], s.error_not_in_game, parse_mode=ParseMode.MARKDOWN)
else:
bot.sendMessage(update.message.chat['id'], s.error_no_games_found, parse_mode=ParseMode.MARKDOWN)
2016-05-26 09:49:40 +00:00
def kill(bot, update):
2016-05-26 09:08:50 +00:00
"""Uccidi un giocatore in partita."""
2016-06-01 11:52:00 +00:00
if __debug__:
game = findgamebyid(update.message.chat['id'])
if game is not None and game.phase is 'Voting':
2016-12-04 18:54:33 +00:00
if update.message.from_user['id'] == game.admin.tid:
2016-06-01 11:52:00 +00:00
target = game.findplayerbyusername(update.message.text.split(' ')[1])
if target is not None:
target.kill(bot, game)
game.message(bot, s.admin_killed.format(name=target.tusername,
icon=target.role.icon,
2016-06-01 11:52:00 +00:00
role=target.role.name))
else:
game.message(bot, s.error_username)
2016-05-26 09:08:50 +00:00
else:
2016-06-01 11:52:00 +00:00
game.message(bot, s.error_not_admin)
2016-05-26 09:08:50 +00:00
else:
2016-06-01 11:52:00 +00:00
bot.sendMessage(update.message.chat['id'], s.error_no_games_found, parse_mode=ParseMode.MARKDOWN)
2016-04-25 17:44:40 +00:00
2016-06-01 11:39:41 +00:00
def delete(bot, update):
"""Elimina una partita in corso."""
if update.message.chat['type'] == 'private':
if update.message.from_user['username'] == "Steffo":
cmd = update.message.text.split(' ', 2)
game = findgamebyname(cmd[1])
# Se non lo trovi con il nome, prova con l'id
if game is None:
game = findgamebyid(int(cmd[1]))
if game is not None:
2016-06-02 15:09:07 +00:00
game.message(bot, s.owner_ended)
2016-06-01 11:39:41 +00:00
game.endgame()
else:
game.message(bot, s.error_no_games_found)
else:
bot.sendMessage(update.message.chat['id'], s.error_not_owner, parse_mode=ParseMode.MARKDOWN)
else:
bot.sendMessage(update.message.chat['id'], s.error_chat_type, parse_mode=ParseMode.MARKDOWN)
2016-05-26 12:55:02 +00:00
def fakerole(bot, update):
"""Manda un finto messaggio di ruolo."""
if update.message.chat['type'] == 'private':
2016-12-05 17:56:34 +00:00
for singlerole in rolepriority:
bot.sendMessage(update.message.chat['id'], s.role_assigned.format(icon=singlerole.icon, name=singlerole.name),
parse_mode=ParseMode.MARKDOWN)
2016-05-26 12:55:02 +00:00
else:
bot.sendMessage(update.message.chat['id'], s.error_private_required, parse_mode=ParseMode.MARKDOWN)
2016-05-26 12:55:02 +00:00
2016-05-29 15:12:23 +00:00
def load(bot, update):
"""Carica una partita salvata."""
file = open(str(update.message.chat['id']) + ".p", "rb")
game = pickle.load(file)
inprogress.append(game)
game.message(bot, s.game_loaded)
def save(bot, update):
"""Salva una partita su file."""
game = findgamebyid(update.message.chat['id'])
if game is not None:
game.save()
else:
bot.sendMessage(update.message.chat['id'], s.error_no_games_found, parse_mode=ParseMode.MARKDOWN)
def debug(bot, update):
"""Visualizza tutti i ruoli e gli id."""
if __debug__:
game = findgamebyid(update.message.chat['id'])
if game is not None:
2016-12-04 18:54:33 +00:00
if game.admin.tid == update.message.from_user['id']:
text = s.status_header.format(name=game.groupid, admin=game.admin.tid, phase=game.phase)
game.updatevotes()
# Aggiungi l'elenco dei giocatori
for player in game.players:
if not player.alive:
text += s.status_dead_player.format(name=player.tusername)
else:
text += s.status_alive_player.format(icon=player.role.icon,
2016-06-02 15:09:07 +00:00
name=player.tusername,
votes=str(player.votes))
game.adminmessage(bot, text)
game.message(bot, s.check_private)
else:
game.message(bot, s.error_not_admin)
else:
bot.sendMessage(update.message.chat['id'], s.error_no_games_found, parse_mode=ParseMode.MARKDOWN)
def debuggameslist(bot, update):
"""Visualizza l'elenco delle partite in corso."""
if __debug__:
bot.sendMessage(update.message.from_user['id'], repr(inprogress), parse_mode=ParseMode.MARKDOWN)
2016-08-10 14:03:11 +00:00
2016-06-03 20:04:37 +00:00
updater.dispatcher.add_handler(CommandHandler('ping', ping))
updater.dispatcher.add_handler(CommandHandler('newgame', newgame))
updater.dispatcher.add_handler(CommandHandler('join', join))
updater.dispatcher.add_handler(CommandHandler('endjoin', endjoin))
updater.dispatcher.add_handler(CommandHandler('vote', vote))
updater.dispatcher.add_handler(CommandHandler('endday', endday))
updater.dispatcher.add_handler(CommandHandler('power', power))
updater.dispatcher.add_handler(CommandHandler('status', status))
updater.dispatcher.add_handler(CommandHandler('role', role))
updater.dispatcher.add_handler(CommandHandler('debug', debug))
updater.dispatcher.add_handler(CommandHandler('debuggameslist', debuggameslist))
updater.dispatcher.add_handler(CommandHandler('kill', kill))
updater.dispatcher.add_handler(CommandHandler('config', config))
updater.dispatcher.add_handler(CommandHandler('fakerole', fakerole))
updater.dispatcher.add_handler(CommandHandler('save', save))
updater.dispatcher.add_handler(CommandHandler('load', load))
updater.dispatcher.add_handler(CommandHandler('delete', delete))
2016-04-21 20:14:35 +00:00
updater.start_polling()
2016-05-27 09:34:06 +00:00
print("Bot avviato!")
if __name__ == "__main__":
updater.idle()