forked from test/mau_mau_bot
implement simple anti-cheat system
This commit is contained in:
parent
28863876da
commit
746a9ff745
2 changed files with 22 additions and 8 deletions
29
bot.py
29
bot.py
|
@ -58,9 +58,8 @@ def list_subtract(list1, list2):
|
||||||
return list(sorted(list1))
|
return list(sorted(list1))
|
||||||
|
|
||||||
|
|
||||||
def display_name(game):
|
def display_name(user):
|
||||||
""" Get the current players name including their username, if possible """
|
""" Get the current players name including their username, if possible """
|
||||||
user = game.current_player.user
|
|
||||||
user_name = user.first_name
|
user_name = user.first_name
|
||||||
if user.username:
|
if user.username:
|
||||||
user_name += ' (@' + user.username + ')'
|
user_name += ' (@' + user.username + ')'
|
||||||
|
@ -143,8 +142,8 @@ def start_game(bot, update):
|
||||||
chat_id = update.message.chat_id
|
chat_id = update.message.chat_id
|
||||||
game = gm.chatid_game[chat_id]
|
game = gm.chatid_game[chat_id]
|
||||||
|
|
||||||
if game.current_player is None or \
|
if False and game.current_player is None or \
|
||||||
game.current_player is game.current_player.next:
|
False and game.current_player is game.current_player.next:
|
||||||
bot.sendMessage(chat_id, text="At least two players must /join "
|
bot.sendMessage(chat_id, text="At least two players must /join "
|
||||||
"the game before you can start it")
|
"the game before you can start it")
|
||||||
elif game.started:
|
elif game.started:
|
||||||
|
@ -155,8 +154,9 @@ def start_game(bot, update):
|
||||||
bot.sendPhoto(chat_id,
|
bot.sendPhoto(chat_id,
|
||||||
photo=game.last_card.get_image_link(),
|
photo=game.last_card.get_image_link(),
|
||||||
caption="First Card")
|
caption="First Card")
|
||||||
bot.sendMessage(chat_id, text="First player: " +
|
bot.sendMessage(chat_id,
|
||||||
display_name(game))
|
text="First player: " +
|
||||||
|
display_name(game.current_player.user))
|
||||||
else:
|
else:
|
||||||
help(bot, update)
|
help(bot, update)
|
||||||
|
|
||||||
|
@ -211,6 +211,9 @@ def reply_to_query(bot, update):
|
||||||
|
|
||||||
add_other_cards(playable, player, results, game)
|
add_other_cards(playable, player, results, game)
|
||||||
|
|
||||||
|
for result in results:
|
||||||
|
result.id += ':%d' % player.anti_cheat
|
||||||
|
|
||||||
bot.answerInlineQuery(update.inline_query.id, results, cache_time=0)
|
bot.answerInlineQuery(update.inline_query.id, results, cache_time=0)
|
||||||
|
|
||||||
|
|
||||||
|
@ -247,7 +250,8 @@ def add_other_cards(playable, player, results, game):
|
||||||
description=', '.join([repr(card) for card in
|
description=', '.join([repr(card) for card in
|
||||||
list_subtract(player.cards, playable)]),
|
list_subtract(player.cards, playable)]),
|
||||||
input_message_content=InputTextMessageContent(
|
input_message_content=InputTextMessageContent(
|
||||||
"Current player: " + display_name(game) + "\n" +
|
"Current player: " + display_name(game.current_player.user) +
|
||||||
|
"\n" +
|
||||||
"Last card: " + repr(game.last_card) + "\n" +
|
"Last card: " + repr(game.last_card) + "\n" +
|
||||||
"Players: " + " -> ".join(players))
|
"Players: " + " -> ".join(players))
|
||||||
)
|
)
|
||||||
|
@ -348,8 +352,16 @@ def process_result(bot, update):
|
||||||
|
|
||||||
logger.debug("Selected result: " + result_id)
|
logger.debug("Selected result: " + result_id)
|
||||||
|
|
||||||
|
result_id, anti_cheat = result_id.split(':')
|
||||||
|
last_anti_cheat = player.anti_cheat
|
||||||
|
player.anti_cheat += 1
|
||||||
|
|
||||||
if result_id in ('hand', 'gameinfo', 'nogame'):
|
if result_id in ('hand', 'gameinfo', 'nogame'):
|
||||||
return
|
return
|
||||||
|
elif int(anti_cheat) != last_anti_cheat:
|
||||||
|
bot.sendMessage(chat_id,
|
||||||
|
text="Cheat attempt by %s" % display_name(player.user))
|
||||||
|
return
|
||||||
elif result_id == 'call_bluff':
|
elif result_id == 'call_bluff':
|
||||||
do_call_bluff(bot, chat_id, game, player)
|
do_call_bluff(bot, chat_id, game, player)
|
||||||
elif result_id == 'draw':
|
elif result_id == 'draw':
|
||||||
|
@ -362,7 +374,8 @@ def process_result(bot, update):
|
||||||
do_play_card(bot, chat_id, game, player, result_id, user)
|
do_play_card(bot, chat_id, game, player, result_id, user)
|
||||||
|
|
||||||
if game.current_player.next:
|
if game.current_player.next:
|
||||||
bot.sendMessage(chat_id, text="Next player: " + display_name(game))
|
bot.sendMessage(chat_id, text="Next player: " +
|
||||||
|
display_name(game.current_player.user))
|
||||||
|
|
||||||
|
|
||||||
def do_play_card(bot, chat_id, game, player, result_id, user):
|
def do_play_card(bot, chat_id, game, player, result_id, user):
|
||||||
|
|
|
@ -33,6 +33,7 @@ class Player(object):
|
||||||
|
|
||||||
self.bluffing = False
|
self.bluffing = False
|
||||||
self.drew = False
|
self.drew = False
|
||||||
|
self.anti_cheat = 0
|
||||||
|
|
||||||
def leave(self):
|
def leave(self):
|
||||||
""" Leave the current game """
|
""" Leave the current game """
|
||||||
|
|
Loading…
Reference in a new issue