2016-04-21 16:57:23 +00:00
|
|
|
from telegram.ext import Updater
|
|
|
|
import filemanager
|
|
|
|
|
2016-04-21 20:14:35 +00:00
|
|
|
import logging
|
|
|
|
logger = logging.getLogger()
|
|
|
|
logger.setLevel(logging.INFO)
|
|
|
|
|
2016-04-21 16:57:23 +00:00
|
|
|
token = filemanager.readfile('telegramapi.txt')
|
|
|
|
updater = Updater(token)
|
|
|
|
|
|
|
|
|
2016-04-21 17:52:01 +00:00
|
|
|
# Ruoli possibili per i giocatori
|
|
|
|
# Base di un ruolo
|
|
|
|
class Role:
|
2016-04-22 17:21:50 +00:00
|
|
|
icon = "-"
|
2016-04-21 18:20:26 +00:00
|
|
|
team = 'None' # Squadra: 'None', 'Good', 'Evil'
|
2016-04-22 19:14:14 +00:00
|
|
|
name = "UNDEFINED"
|
2016-04-21 17:52:01 +00:00
|
|
|
haspower = False
|
|
|
|
poweruses = 0
|
|
|
|
|
|
|
|
def power(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def onendday(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class Royal(Role):
|
|
|
|
icon = "\U0001F610"
|
2016-04-21 18:20:26 +00:00
|
|
|
team = 'Good'
|
2016-04-22 19:14:14 +00:00
|
|
|
name = "Royal"
|
2016-04-21 17:52:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Mifioso(Role):
|
|
|
|
icon = "\U0001F47F"
|
2016-04-21 18:20:26 +00:00
|
|
|
team = 'Evil'
|
2016-04-21 17:52:01 +00:00
|
|
|
haspower = True
|
|
|
|
poweruses = 1
|
|
|
|
target = None
|
2016-04-22 19:14:14 +00:00
|
|
|
name = "Mifioso"
|
2016-04-21 17:52:01 +00:00
|
|
|
|
|
|
|
def power(self):
|
|
|
|
# Imposta qualcuno come bersaglio
|
|
|
|
pass
|
|
|
|
|
|
|
|
def onendday(self):
|
|
|
|
# Ripristina il potere
|
|
|
|
self.poweruses = 1
|
|
|
|
# Uccidi il bersaglio
|
|
|
|
|
|
|
|
|
|
|
|
class Investigatore(Role):
|
|
|
|
icon = "\U0001F575"
|
2016-04-21 18:20:26 +00:00
|
|
|
team = 'Good'
|
2016-04-21 17:52:01 +00:00
|
|
|
haspower = True
|
|
|
|
poweruses = 1
|
2016-04-22 19:14:14 +00:00
|
|
|
name = "Investigatore"
|
2016-04-21 17:52:01 +00:00
|
|
|
|
|
|
|
def power(self):
|
|
|
|
# Visualizza il ruolo di qualcuno
|
|
|
|
pass
|
|
|
|
|
|
|
|
def onendday(self):
|
|
|
|
# Ripristina il potere
|
|
|
|
self.poweruses = 1
|
|
|
|
|
|
|
|
|
2016-04-21 21:04:53 +00:00
|
|
|
class Angelo(Role):
|
2016-04-22 18:39:16 +00:00
|
|
|
icon = "\U0001F607"
|
2016-04-21 21:04:53 +00:00
|
|
|
team = 'Good'
|
|
|
|
haspower = True
|
|
|
|
poweruses = 1
|
2016-04-22 19:14:14 +00:00
|
|
|
name = "Angelo"
|
2016-04-21 21:04:53 +00:00
|
|
|
|
|
|
|
def power(self):
|
|
|
|
# Salva qualcuno dalla morte!
|
|
|
|
pass
|
|
|
|
|
|
|
|
def onendday(self):
|
|
|
|
# Ripristina il potere
|
|
|
|
self.poweruses = 1
|
|
|
|
|
|
|
|
|
2016-04-21 18:20:26 +00:00
|
|
|
# Classi per i giocatori
|
|
|
|
class Player:
|
|
|
|
tid = int()
|
|
|
|
tusername = str()
|
|
|
|
role = Role() # Di base, ogni giocatore è un ruolo indefinito
|
|
|
|
alive = True
|
2016-04-22 17:21:50 +00:00
|
|
|
votingfor = None # Diventa un player se ha votato
|
2016-04-22 19:14:14 +00:00
|
|
|
votes = 0 # Voti. Aggiornato da updatevotes()
|
2016-04-21 18:20:26 +00:00
|
|
|
|
|
|
|
def message(self, bot, text):
|
|
|
|
bot.sendMessage(self.tid, text)
|
|
|
|
|
2016-04-22 19:14:14 +00:00
|
|
|
def kill(self):
|
|
|
|
self.alive = False
|
|
|
|
|
2016-04-21 18:20:26 +00:00
|
|
|
def __init__(self, tid, tusername):
|
|
|
|
self.tid = tid
|
|
|
|
self.tusername = tusername
|
|
|
|
|
|
|
|
|
|
|
|
# Classe di ogni partita
|
|
|
|
class Game:
|
|
|
|
adminid = int()
|
|
|
|
groupid = int()
|
|
|
|
players = list()
|
|
|
|
tokill = list() # Giocatori che verranno uccisi all'endday
|
|
|
|
phase = 'Join' # Fase di gioco: 'Join', 'Voting', 'Ended'
|
|
|
|
|
|
|
|
def __init__(self, groupid, adminid):
|
|
|
|
self.groupid = groupid
|
|
|
|
self.adminid = adminid
|
|
|
|
|
|
|
|
def message(self, bot, text):
|
|
|
|
bot.sendMessage(self.groupid, text)
|
|
|
|
|
|
|
|
def adminmessage(self, bot, text):
|
|
|
|
bot.sendMessage(self.adminid, text)
|
|
|
|
|
|
|
|
def mifiamessage(self, bot, text):
|
|
|
|
# Trova tutti i mifiosi nell'elenco dei giocatori
|
|
|
|
for player in self.players:
|
|
|
|
if isinstance(player.role, Mifioso):
|
|
|
|
player.message(bot, text)
|
|
|
|
# Inoltra il messaggio all'admin
|
|
|
|
self.adminmessage(bot, text)
|
|
|
|
|
2016-04-22 17:21:50 +00:00
|
|
|
def findplayerbyid(self, tid) -> Player:
|
|
|
|
# Trova tutti i giocatori con un certo id
|
|
|
|
for player in self.players:
|
|
|
|
if player.tid == tid:
|
|
|
|
return player
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
|
|
|
def findplayerbyusername(self, tusername) -> Player:
|
|
|
|
# Trova tutti i giocatori con un certo username
|
|
|
|
for player in self.players:
|
|
|
|
if player.tusername == tusername:
|
|
|
|
return player
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
2016-04-22 18:43:23 +00:00
|
|
|
def assignroles(self, mifia=2, investigatore=1, angelo=0):
|
2016-04-22 17:55:24 +00:00
|
|
|
import random
|
|
|
|
random.seed()
|
|
|
|
playersleft = self.players.copy()
|
|
|
|
random.shuffle(playersleft)
|
|
|
|
# Seleziona 2 mifiosi
|
|
|
|
while mifia > 0:
|
|
|
|
try:
|
|
|
|
selected = playersleft.pop()
|
|
|
|
except IndexError:
|
|
|
|
raise IndexError("Non ci sono abbastanza giocatori!")
|
|
|
|
else:
|
|
|
|
selected.role = Mifioso()
|
|
|
|
mifia -= 1
|
|
|
|
# Seleziona 1 detective
|
|
|
|
while investigatore > 0:
|
|
|
|
try:
|
|
|
|
selected = playersleft.pop()
|
|
|
|
except IndexError:
|
|
|
|
raise IndexError("Non ci sono abbastanza giocatori!")
|
|
|
|
else:
|
|
|
|
selected.role = Investigatore()
|
|
|
|
investigatore -= 1
|
2016-04-22 18:43:23 +00:00
|
|
|
# Seleziona 1 angelo
|
2016-04-22 18:43:55 +00:00
|
|
|
while angelo > 0:
|
2016-04-22 18:43:23 +00:00
|
|
|
try:
|
|
|
|
selected = playersleft.pop()
|
|
|
|
except IndexError:
|
|
|
|
raise IndexError("Non ci sono abbastanza giocatori!")
|
|
|
|
else:
|
|
|
|
selected.role = Angelo()
|
|
|
|
investigatore -= 1
|
2016-04-22 17:55:24 +00:00
|
|
|
# Assegna il ruolo di Royal a tutti gli altri
|
|
|
|
for player in playersleft:
|
|
|
|
player.role = Royal()
|
|
|
|
|
2016-04-22 19:14:14 +00:00
|
|
|
def updatevotes(self):
|
|
|
|
for player in self.players:
|
|
|
|
player.votes = 0
|
|
|
|
for player in self.players:
|
|
|
|
player.votingfor.votes += 1
|
|
|
|
|
|
|
|
def mostvotedplayer(self) -> Player:
|
|
|
|
mostvoted = None
|
|
|
|
self.updatevotes()
|
|
|
|
for player in self.players:
|
|
|
|
if (mostvoted is None and player.votes >= 1) or (player.votes > mostvoted.votes):
|
|
|
|
mostvoted = player
|
|
|
|
elif player.votes == mostvoted.votes:
|
|
|
|
# Non sono sicuro che questo algoritmo sia effettivamente il più equo. Ma vabbè, non succederà mai
|
|
|
|
import random
|
|
|
|
mostvoted = random.choice([player, mostvoted])
|
|
|
|
return mostvoted
|
|
|
|
|
|
|
|
def endday(self, bot):
|
|
|
|
# Se ce n'è bisogno, si potrebbe rendere casuale l'ordine nelle abilità
|
|
|
|
for player in self.players:
|
|
|
|
player.role.onendday()
|
|
|
|
lynched = self.mostvotedplayer()
|
|
|
|
if lynched is not None:
|
|
|
|
self.message(bot, "{0} è stato ucciso.\n"
|
|
|
|
"Era un {1} {2}.".format(lynched.tusername, lynched.role.icon, lynched.role.name))
|
|
|
|
lynched.kill()
|
|
|
|
|
2016-04-21 20:14:35 +00:00
|
|
|
# Partite in corso
|
|
|
|
inprogress = list()
|
|
|
|
|
2016-04-21 18:20:26 +00:00
|
|
|
|
2016-04-21 21:04:53 +00:00
|
|
|
# Trova una partita con un certo id
|
|
|
|
def findgamebyid(gid) -> Game:
|
|
|
|
for game in inprogress:
|
|
|
|
if game.groupid == gid:
|
|
|
|
return game
|
|
|
|
|
|
|
|
|
2016-04-21 17:52:01 +00:00
|
|
|
# Comandi a cui risponde il bot
|
2016-04-21 16:57:23 +00:00
|
|
|
def ping(bot, update):
|
2016-04-21 20:22:08 +00:00
|
|
|
bot.sendMessage(update.message.chat['id'], "Pong!")
|
2016-04-21 16:57:23 +00:00
|
|
|
|
2016-04-21 20:14:35 +00:00
|
|
|
|
|
|
|
def newgame(bot, update):
|
|
|
|
if update.message.chat['type'] != 'private':
|
|
|
|
g = Game(update.message.chat['id'], update.message.from_user['id'])
|
|
|
|
inprogress.append(g)
|
2016-04-21 21:04:53 +00:00
|
|
|
bot.sendMessage(update.message.chat['id'], "Partita creata: " + repr(g))
|
2016-04-21 20:14:35 +00:00
|
|
|
else:
|
2016-04-21 21:04:53 +00:00
|
|
|
bot.sendMessage(update.message.chat['id'], "Non puoi creare una partita in questo tipo di chat!")
|
|
|
|
|
|
|
|
|
|
|
|
def join(bot, update):
|
|
|
|
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'])
|
|
|
|
game.players.append(p)
|
|
|
|
bot.sendMessage(update.message.chat['id'], "Unito alla partita: " + repr(p))
|
|
|
|
else:
|
|
|
|
bot.sendMessage(update.message.chat['id'], "Ti sei già unito alla partita: " + repr(p))
|
2016-04-22 17:21:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
def status(bot, update):
|
|
|
|
game = findgamebyid(update.message.chat['id'])
|
|
|
|
if game is None:
|
|
|
|
bot.sendMessage(update.message.chat['id'], "In questo gruppo non ci sono partite in corso.")
|
|
|
|
else:
|
|
|
|
text = "Gruppo: {0}\n" \
|
|
|
|
"Creatore: {1}\n" \
|
|
|
|
"Stato: {2}\n" \
|
|
|
|
"Giocatori partecipanti:\n".format(game.groupid, game.adminid, game.phase)
|
|
|
|
# Aggiungi l'elenco dei giocatori
|
|
|
|
for player in game.players:
|
|
|
|
if player.votingfor is not None:
|
|
|
|
text += "{0} {1} ({2})\n".format(player.role.icon, player.tusername, player.votingfor.tusername)
|
|
|
|
else:
|
|
|
|
text += "{0} {1}\n".format(player.role.icon, player.tusername)
|
|
|
|
bot.sendMessage(update.message.chat['id'], text)
|
|
|
|
|
|
|
|
|
|
|
|
def endjoin(bot, update):
|
|
|
|
game = findgamebyid(update.message.chat['id'])
|
2016-04-22 18:43:23 +00:00
|
|
|
if game is not None and game.phase is 'Join' and update.message.from_user['id'] == game.adminid:
|
2016-04-22 17:21:50 +00:00
|
|
|
game.phase = 'Voting'
|
|
|
|
bot.sendMessage(update.message.chat['id'], "La fase di join è terminata.")
|
2016-04-22 18:43:23 +00:00
|
|
|
game.assignroles(1, 1, 1)
|
2016-04-22 17:55:24 +00:00
|
|
|
bot.sendMessage(update.message.chat['id'], "I ruoli sono stati assegnati.")
|
2016-04-22 17:21:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
def vote(bot, update):
|
|
|
|
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:
|
|
|
|
target = game.findplayerbyusername(update.message.text.split(' ')[1])
|
|
|
|
if target is not None:
|
|
|
|
player.votingfor = target
|
|
|
|
bot.sendMessage(update.message.chat['id'], "Hai votato per uccidere {0}.".format(target.tusername))
|
|
|
|
else:
|
|
|
|
bot.sendMessage(update.message.chat['id'], "Il nome utente specificato non esiste.")
|
|
|
|
else:
|
|
|
|
bot.sendMessage(update.message.chat['id'], "Non sei nella partita.")
|
|
|
|
else:
|
|
|
|
bot.sendMessage(update.message.chat['id'], "Nessuna partita in corso trovata.")
|
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):
|
|
|
|
game = findgamebyid(update.message.chat['id'])
|
|
|
|
if game is not None and game.phase is 'Voting' and update.message.from_user['id'] == game.adminid:
|
|
|
|
game.endday(bot)
|
|
|
|
|
|
|
|
|
2016-04-21 16:57:23 +00:00
|
|
|
updater.dispatcher.addTelegramCommandHandler('ping', ping)
|
2016-04-21 20:14:35 +00:00
|
|
|
updater.dispatcher.addTelegramCommandHandler('newgame', newgame)
|
2016-04-21 21:04:53 +00:00
|
|
|
updater.dispatcher.addTelegramCommandHandler('join', join)
|
2016-04-22 17:21:50 +00:00
|
|
|
updater.dispatcher.addTelegramCommandHandler('status', status)
|
|
|
|
updater.dispatcher.addTelegramCommandHandler('endjoin', endjoin)
|
|
|
|
updater.dispatcher.addTelegramCommandHandler('vote', vote)
|
2016-04-22 19:14:14 +00:00
|
|
|
updater.dispatcher.addTelegramCommandHandler('endday', endday)
|
2016-04-21 20:14:35 +00:00
|
|
|
updater.start_polling()
|
2016-04-22 17:21:50 +00:00
|
|
|
updater.idle()
|