def test_no_move_without_start(self): game.join(self.r, 'game', 'max') game.join(self.r, 'game', 'jenny') game.start(self.r, 'game', 'jenny') self.assertFalse(game.move(self.r, 'game', 'jenny', 'han')) info = game.info(self.r, 'game', 'jenny').next() self.assertEquals('camp', info['you']['state'])
def test_chat(self): game.join(self.r, 'game', 'loner') self.assertTrue(game.chat(self.r, 'game', 'loner', 'just me myself and i')) info = game.info(self.r, 'game').next() self.assertDictContainsSubset({'chat': { 'speaker': 'loner', 'message': 'just me myself and i'}}, info['updates'].pop(0))
def test_join(self): self.assertTrue(game.join(self.r, 'game', 'dave')) info = game.info(self.r, 'game', 'dave').next() self.assertDictContainsSubset({ 'state': 'joined', 'name': 'dave' }, info['you']) self.assertEquals([{'name': 'dave', 'state': 'joined' }], info['state']['players'])
def test_start_statuses(self): game.join(self.r, 'game', 'dave') self.assertTrue(game.start(self.r, 'game', 'dave')) info = game.info(self.r, 'game', 'dave').next() self.assertDictContainsSubset({ 'state': 'camp', 'name': 'dave' }, info['you']) self.assertEquals([{'name': 'dave', 'state': 'camp' }], info['state']['players'])
def test_all_must_approve_start(self): game.join(self.r, 'game', 'alpha') game.join(self.r, 'game', 'beta') game.join(self.r, 'game', 'gaga') game.start(self.r, 'game', 'alpha') game.start(self.r, 'game', 'beta') info = game.info(self.r, 'game').next() self.assertNotIn('round', info['state'])
def test_move(self): game.join(self.r, 'game', 'max') game.join(self.r, 'game', 'jenny') game.start(self.r, 'game', 'max') game.start(self.r, 'game', 'jenny') self.assertTrue(game.move(self.r, 'game', 'jenny', 'han')) info = game.info(self.r, 'game', 'jenny').next() self.assertEquals([{ 'name': 'jenny', 'state': 'moved'}, { 'name': 'max', 'state': 'undecided'}], info['state']['players']) self.assertEquals('han', info['you']['state'])
def test_start_game(self): game.join(self.r, 'game', 'alpha') game.join(self.r, 'game', 'beta') game.start(self.r, 'game', 'alpha') game.start(self.r, 'game', 'beta') info = game.info(self.r, 'game').next() self.assertEquals('1', info['state']['round']) self.assertEquals([{ 'name': 'alpha', 'state': 'undecided'}, { 'name': 'beta', 'state': 'undecided'}], info['state']['players'])
def test_updates_most_recent_first(self): """ The first update should be the most recent, then they work backwards. """ game.join(self.r, 'game', 'first') game.join(self.r, 'game', 'second') game.join(self.r, 'game', 'third') info = game.info(self.r, 'game').next() self.assertDictContainsSubset({'join': 'third'}, info['updates'].pop(0)) self.assertDictContainsSubset({'join': 'second'}, info['updates'].pop(0)) self.assertDictContainsSubset({'join': 'first'}, info['updates'].pop(0))
def test_info_advances(self): info = game.info(self.r, 'blocked') info.next() # pull out the not_exists info t = Thread(target=info.next) t.start() self.assertTrue(t.is_alive()) time.sleep(0.5) self.assertTrue(t.is_alive()) game.join(self.r, 'blocked', 'some dude') t.join(1) self.assertFalse(t.is_alive())
def test_info_advances_beyond_id(self): info = game.info(self.r, 'game') game.join(self.r, 'game', 'thing one') t_all_info = Thread(target=info.next) t_all_info.start() t_all_info.join(1) self.assertFalse(t_all_info.is_alive()) t_partial_info = Thread(target=info.next) t_partial_info.start() self.assertTrue(t_partial_info.is_alive()) time.sleep(0.5) self.assertTrue(t_partial_info.is_alive()) self.assertTrue(game.join(self.r, 'game', 'some dude')) t_partial_info.join(1) self.assertFalse(t_partial_info.is_alive())
def test_info_waits_for_id(self): """ The ID passed to info should cause it to hold on a response until there are more than that number of updates. """ info = game.info(self.r, 'game', start_info_id=3) t = Thread(target=info.next) t.start() game.join(self.r, 'game', 'first') game.join(self.r, 'game', 'second') game.join(self.r, 'game', 'third') self.assertTrue(t.is_alive()) game.join(self.r, 'game', 'fourth') t.join(1) self.assertFalse(t.is_alive())
def test_superuser_chat(self): game.join(self.r, 'game', 'betty') game.join(self.r, 'game', 'susie') game.join(self.r, 'game', 'martha') self.assertTrue(game.chat(self.r, 'game', 'hero', "what up gals", True)) self.assertFalse(game.chat(self.r, 'game', 'zero', "me too!")) game.chat(self.r, 'game', 'betty', "who's that dude?") game.chat(self.r, 'game', 'martha', "no clue") info = game.info(self.r, 'game').next() self.assertEquals({ 'speaker': 'martha', 'message': 'no clue'}, info['updates'].pop(0)['chat']) self.assertEquals({ 'speaker': 'betty', 'message': "who's that dude?"}, info['updates'].pop(0)['chat']) self.assertEquals({ 'speaker': 'hero', 'message': 'what up gals'}, info['updates'].pop(0)['chat'])
def run_player(r, k, name, wait_for_n_players, sleeper, ai=random_ai, sleep_time=0): """ A self-contained player. Joins game, plays when it can. """ game.join(r, k, name) for info in game.info(r, k, name): sleeper.sleep(sleep_time) player_state = info['you']['state'] if player_state == 'joined': if len(info['state']['players']) == wait_for_n_players: game.start(r, k, name) else: pass elif player_state == 'undecided': game.move(r, k, name, ai()) elif player_state in ['won', 'lost']: # done! return True sleeper.sleep(sleep_time)
def test_info_id_is_zero_to_start(self): info = game.info(self.r, 'game') self.assertEquals(0, info.next()['id'])
def test_info_not_exists(self): info = game.info(self.r, 'nonexistent').next() self.assertEquals({ 'not_exists': True }, info['state'])
def test_cannot_join_twice(self): game.join(self.r, 'game', 'dave') self.assertFalse(game.join(self.r, 'game', 'dave')) info = game.info(self.r, 'game', 'dave').next() self.assertEquals(1, len(info['state']['players']))
def test_needs_multiple_players_to_start(self): game.join(self.r, 'game', 'hermit') game.start(self.r, 'game', 'hermit') info = game.info(self.r, 'game').next() self.assertNotIn('round', info['state'])
def test_single_player_starting_gets_more_players_update(self): game.join(self.r, 'game', 'hermit') game.start(self.r, 'game', 'hermit') info = game.info(self.r, 'game').next() self.assertDictContainsSubset({'more_players': True}, info['updates'].pop(0))