def give(game, params, user_id, user_name): """ Give one of your dice to someone else """ if len(params) < 2: return SlackResponse("Usage: /fiasco die slack_name") from_player = game.get_user(user_id) if not from_player: return SlackResponse( "You are not registered as a player. Please type /fiasco register your_game_name" ) # Load up our user and desired die slack_name = params[-1].replace('@', '') # Allow use of @ if slack_name == 'pool': to_player_id = 'pool' else: to_player_id = game.get_user_id_for_slack_name(slack_name) if not to_player_id: return SlackResponse('No player found with slack name "%s"' % slack_name) try: die = Die(params=params[0:-1]) except InvalidDie: return SlackResponse('Format is w5 or white 5 (or b1 or black 1)') if not game.take_die_from(die, user_id): return SlackResponse(u'%s does not have a %s' % (user_name, die)) if slack_name == 'pool': dice = game.dice dice.append(die) game.dice = dice else: game.give_die_to(die, to_player_id) return SlackResponse( u"%s gave %s to %s" % (user_name, die.to_emoji(), slack_name), True)
def give(game,params,user_id,user_name): """ Give one of your dice to someone else """ if len(params) < 2: return SlackResponse("Usage: /fiasco die slack_name") from_player = game.get_user(user_id) if not from_player: return SlackResponse("You are not registered as a player. Please type /fiasco register your_game_name") # Load up our user and desired die slack_name = params[-1].replace('@','') # Allow use of @ if slack_name == 'pool': to_player_id = 'pool' else: to_player_id = game.get_user_id_for_slack_name(slack_name) if not to_player_id: return SlackResponse('No player found with slack name "%s"' % slack_name) try: die = Die(params=params[0:-1]) except InvalidDie: return SlackResponse('Format is w5 or white 5 (or b1 or black 1)') if not game.take_die_from(die,user_id): return SlackResponse(u'%s does not have a %s' % (user_name, die)) if slack_name == 'pool': dice = game.dice dice.append(die) game.dice = dice else: game.give_die_to(die,to_player_id) return SlackResponse(u"%s gave %s to %s" % (user_name,die.to_emoji(),slack_name),True)
def spend(game,params,user_id,user_name): """ Let a user spend one of their dice """ dice = game.get_user_dice(user_id) try: die = Die(params=params) except InvalidDie: return SlackResponse('Format is w5 or white 5 (or b1 or black 1)') for i,d in enumerate(dice): if die.to_json() == d.to_json(): del dice[i] game.set_user_dice(user_id,dice) return SlackResponse("%s spent %s" % (user_name, die.to_emoji()),True) return SlackResponse("You don't have a %s" % die.to_emoji())
def spend(game, params, user_id, user_name): """ Let a user spend one of their dice """ dice = game.get_user_dice(user_id) try: die = Die(params=params) except InvalidDie: return SlackResponse('Format is w5 or white 5 (or b1 or black 1)') for i, d in enumerate(dice): if die.to_json() == d.to_json(): del dice[i] game.set_user_dice(user_id, dice) return SlackResponse("%s spent %s" % (user_name, die.to_emoji()), True) return SlackResponse("You don't have a %s" % die.to_emoji())
def test_user_dice(self): self.assertEquals([], self.game.get_user_dice('12456')) self.game.set_user_dice('12456', [Die(number=5, color='w')]) self.assertEquals([{ 'n': 5, 'c': 'white' }], [x.to_json() for x in self.game.get_user_dice('12456')])
def test_dice(self): self.assertEquals([], self.game.dice) self.game.dice = [Die(number=5, color='w')] self.assertEquals([{ 'n': 5, 'c': 'white' }], [x.to_json() for x in self.game.dice])
def test_take_die_from(self): self.game.set_user_dice('12456', [Die(params=['b1'])]) self.assertTrue(self.game.take_die_from(Die(params=['b1']), '12456')) self.assertFalse( self.game.take_die_from(Die(params=['b1']), '12456none')) self.assertFalse(self.game.take_die_from(Die(params=['w1']), '12456')) self.assertEquals( { 'game': { 'users': { '12456': { 'name': 'Test', 'slack_name': 'Bar', 'dice': [] } } } }, self.game.game_state.data)
def take(game,params,user_id,user_name): """ Take a specific die from the pool """ if len(params) < 1: return SlackResponse("Usage: /fiasco take color number") # Validate try: die = Die(params=params) except InvalidDie: return SlackResponse('Format is w5 or white 5 (or b1 or black 1') # Find die user_dice = game.get_user_dice(user_id) or [] if game.take_die_from_pool(die): user_dice.append(die) game.set_user_dice(user_id,user_dice) return SlackResponse ("%s claimed %s.\nPool: %s" % (user_name, die.to_emoji(),game.format_dice_pool(game.dice) or 'Empty'),True) return SlackResponse(u"Could not find a %s" % die)
def take(game, params, user_id, user_name): """ Take a specific die from the pool """ if len(params) < 1: return SlackResponse("Usage: /fiasco take color number") # Validate try: die = Die(params=params) except InvalidDie: return SlackResponse('Format is w5 or white 5 (or b1 or black 1') # Find die user_dice = game.get_user_dice(user_id) or [] if game.take_die_from_pool(die): user_dice.append(die) game.set_user_dice(user_id, user_dice) return SlackResponse( "%s claimed %s.\nPool: %s" % (user_name, die.to_emoji(), game.format_dice_pool(game.dice) or 'Empty'), True) return SlackResponse(u"Could not find a %s" % die)
def test_give_die_to(self): self.assertTrue(self.game.give_die_to(Die(params=['b1']), '12456')) self.assertEquals( { 'game': { 'users': { '12456': { 'dice': [{ 'c': 'black', 'n': 1 }], 'name': 'Test', 'slack_name': 'Bar' } } } }, self.game.game_state.data)
def pool(game, params, user_id, user_name): """ Output the dice pool. Based on parameters, optionally reset or reroll. """ dice = game.dice if params == ['reset']: # Reset dice dice = [] user_count = len(game.users) if not user_count: return SlackResponse( "No registered users so no dice rolled. /fiasco register Your Name to register yourself." ) for color in ('black', 'white'): for i in range((user_count * 2)): dice.append(Die(color=color, number=random.randint(1, 6))) game.dice = dice elif params == ['reroll']: # Reroll dice still in pool dice = game.dice for die in dice: die.roll() game.dice = dice return SlackResponse( game.format_dice_pool(dice) or "No dice in pool", True)
def test_json(self): self.assertEquals(u':d6-3-black:', Die(json={ 'c': 'black', 'n': '3' }).to_emoji())
def test_default(self): self.assertEquals(u':d6-5:', Die(color='white', number=5).to_emoji()) self.assertEquals(u':d6-1-black:', Die(color='black', number=1).to_emoji())
def test_clear(self): self.game.dice = [Die(number=5, color='w')] self.game.set_user('abc', 'Slack', 'Test') self.game.clear() self.assertEquals({'game': {}}, self.game.game_state.data)
def test_format_dice_pool(self): self.assertEquals( u':d6-1: :d6-5-black: :d6-6-black:', self.game.format_dice_pool( (Die(number=5, color='black'), Die(number=1, color='white'), Die(number=6, color='black'))))
def test_take_die_from_pool(self): self.game.dice = [Die(number=1, color='b')] self.game.take_die_from_pool(Die(number=1, color='b')) self.assertEquals([], self.game.dice)
def test_unicode(self): self.assertEquals(u'black 2', unicode(Die(params=['b2'])))
def test_to_json(self): self.assertEquals({'c': 'white', 'n': 1}, Die(params=['w1']).to_json())
def test_params(self): self.assertEquals(u':d6-1:', Die(params=['w1']).to_emoji()) self.assertEquals(u':d6-3-black:', Die(params=['b3']).to_emoji()) self.assertEquals(u':d6-6:', Die(params=['white 6']).to_emoji()) self.assertEquals(u':d6-1-black:', Die(params=['black 1']).to_emoji())
def test_roll(self): die = Die(color='w', number=1) self.assertEquals('white', die.color) self.assertEquals(1, die.number) new_number = die.roll() self.assertEquals(die.number, new_number)
def test_roll(self): die = Die(color='w',number=1) self.assertEquals('white',die.color) self.assertEquals(1, die.number) new_number = die.roll() self.assertEquals(die.number,new_number)