def check_restriction(user): player = get_player(user.id) now = int(time.time()) if now >= player.restricted_until: return False else: return player.restricted_until - now
def gen_reward(user, negative=True): ''' Reward the player :) ''' def __chance(percentage): if randrange(0, 10000) / 10000 < percentage: return True else: return False def __floating(value): return randrange(8000, 12000) / 10000 * value def __lose_cards(cardnum): if cardnum <= 6: return 1 else: return int(__floating(log(cardnum, 2))) def __get_cards(cardnum): if cardnum >= 2: cards = __floating(1 / log(cardnum, 100)) if cards > 1.0: return int(cards) else: return int(__chance(cards)) else: return int(__floating(8.0)) # Negative rewards def restrict_mining(player): lost_cards = __lose_cards(player.immunity_cards) player.immunity_cards -= lost_cards if player.immunity_cards >= 0: ret = "用去{}张免疫卡,还剩{}张".format(lost_cards, player.immunity_cards) else: now = int(time.time()) seconds = randint(30, 120) player.restricted_until = now + seconds ret = "没有免疫卡了,被限制扫雷{}秒".format(seconds) return ret # Positive rewards def give_immunity_cards(player): rewarded_cards = __get_cards(player.immunity_cards) player.immunity_cards += rewarded_cards if rewarded_cards == 0: return "共有{}张免疫卡".format(player.immunity_cards) else: return "被奖励了{}张免疫卡,共有{}张".format(rewarded_cards, player.immunity_cards) player = get_player(user.id) try: if negative: player.death += 1 return restrict_mining(player) else: player.wins += 1 return give_immunity_cards(player) finally: player.save()
def player_statistics(update, context): logger.info("Statistics from {0}".format(update.message.from_user.id)) user = update.message.from_user player = get_player(user.id) mines = player.mines death = player.death wins = player.wins cards = player.immunity_cards TEMPLATE = "一共玩了{mines}局,爆炸{death}次,赢了{wins}局\n" \ "口袋里有{cards}张免疫卡" update.message.reply_text(TEMPLATE.format(mines=mines, death=death, wins=wins, cards=cards))
def game_count(user): player = get_player(user.id) player.mines += 1 player.save()