def __init__(self): self.client = Client(self) self.finished_event = threading.Event() self.games_left = 1 self.max_mass = -1 self.mass = -1
def test_connect(self): nick = 'Dummy' address, token = find_server() subscriber = SubscriberMock() client = Client(subscriber) self.assertEqual('', client.player.nick) client.player.nick = nick self.assertFalse(client.connected) self.assertEqual('', client.address) self.assertEqual('', client.server_token) try: success = client.connect(address, token) except ConnectionResetError: # sometimes connection gets closed on first attempt success = client.connect(address, token) self.assertTrue(success) self.assertTrue(client.connected) self.assertEqual(address, client.address) self.assertEqual(token, client.server_token) self.assertEqual(nick, client.player.nick) self.assertEqual('sock_open', subscriber.events[-1]) client.disconnect() self.assertFalse(client.connected) self.assertFalse(client.ingame) self.assertEqual(address, client.address) self.assertEqual(token, client.server_token) self.assertEqual(nick, client.player.nick) self.assertEqual('sock_closed', subscriber.events[-1])
class Bot(Subscriber): def __init__(self): self.client = Client(self) self.finished_event = threading.Event() self.games_left = 1 self.max_mass = -1 self.mass = -1 def main(self): nick = random.choice(special_names) self.client.player.nick = nick address, token = find_server() try: self.client.connect(address, token) except ConnectionResetError: # sometimes connection gets closed on first attempt print('Connection got closed on first attempt, retrying') self.client.connect(address, token) player = self.client.player self.client.send_respawn() self.client.send_target(*player.center) print('Connected, players name is {}, coords are {}.'.format( player.nick, player.center)) self.finished_event.wait() def on_world_update_post(self): # Main bot logic player = self.client.player if player.total_mass <= self.mass: self.mass = player.total_mass print(self.mass) if self.mass < self.max_mass: self.max_mass = self.mass def on_cell_eaten(self, eater_id, eaten_id): cells = self.client.world.cells if eaten_id in cells and eater_id in cells: print('{} ate {}'.format(cells[eater_id].name, cells[eaten_id].name)) player = self.client.player if eaten_id in player.own_ids: if len(player.own_ids) <= 1: self.games_left -= 1 if self.games_left < 1: self.client.disconnect() self.finished_event.set()
def __init__(self, address, token=None, nick=None): if nick is None: nick = random.choice(special_names) # connect the subscribers # order is important, first subscriber gets called first self.multi_sub = MultiSubscriber(self) def key(keycode, *subs, disabled=False): # subscribe all these subscribers, toggle them when key is pressed if isinstance(keycode, str): keycode = ord(keycode) self.multi_sub.sub(KeyToggler(keycode, *subs, disabled=disabled)) self.client = client = Client(self.multi_sub) self.tagar_client = tagar_client = TagarClient(client) self.native_control = NativeControl(client) self.multi_sub.sub(self.native_control) # background key(Gdk.KEY_F2, SolidBackground()) key(Gdk.KEY_F2, SolidBackground(WHITE), disabled=True) key('b', WorldBorderDrawer(), FieldOfView()) key('g', GridDrawer()) self.multi_sub.sub(CellsDrawer()) # cell overlay key('k', CellSkins()) key('n', CellNames()) key( 'i', CellHostility(), CellMasses(), RemergeTimes(), ForceFields(), ) key('m', MovementLines()) # HUD key( Gdk.KEY_F1, SplitCounter(), Minimap(), Leaderboard(), ExperienceMeter(), Logger(client), MassGraph(client), ) # Team Overlay key('t', TeamOverlay(tagar_client)) key(Gdk.KEY_F3, FpsMeter(50), disabled=True) client.player.nick = nick connected = False while not connected: try: client.connect(address, token) except ConnectionResetError as e: print("Error while connecting:", e) print("Trying again...") client.disconnect() continue except gaierror as e: print("Can't connect to agario.io:", e) print("Trying again...") client.disconnect() continue connected = True gtk_watch_client(client) self.world_viewer = wv = WorldViewer(client.world) wv.button_subscriber = wv.draw_subscriber = wv.input_subscriber = self.multi_sub wv.focus_player(client.player)
def test_connect(self): nick = 'Dummy' address, token = find_server() subscriber = SubscriberMock() client = Client(subscriber) self.assertEqual('', client.player.nick) client.player.nick = nick self.assertFalse(client.connected) self.assertEqual('', client.address) self.assertEqual('', client.server_token) try: success = client.connect(address, token) except ConnectionResetError: print('Connection got closed on first attempt, retrying') success = client.connect(address, token) self.assertTrue(success) self.assertTrue(client.connected) self.assertEqual(address, client.address) self.assertEqual(token, client.server_token) self.assertEqual(nick, client.player.nick) self.assertEqual(1, len(subscriber.events)) self.assertEqual('sock_open', subscriber.events[0]) # the following tests cover expected server behavior and responses def poll_select(client): """ Waits until the client receives a packet then calls its on_message() method to receive one packet. """ r = False while not r: r, w, e = select.select((client.ws.sock,), (), ()) assert not e, 'Error while receiving from socket' client.on_message() def pop_and_check_keys(event, *keys): received_event = subscriber.events.pop(0) received_data = subscriber.data.pop(0) self.assertEqual(event, received_event) if keys: self.assertSetEqual(set(keys), set(received_data.keys())) else: self.assertDictEqual({}, received_data) # receive world_rect packet subscriber.reset() poll_select(client) pop_and_check_keys('ingame') pop_and_check_keys('world_rect', 'left', 'top', 'right', 'bottom') if len(subscriber.events) > 2: # optional, might be removed later pop_and_check_keys('server_version', 'number', 'text') # receive at least one leaderboard and world_update received = { 'leaderboard': False, 'world_update': False, 'cell_info': False, 'cell_eaten': False, 'cell_removed': True, # not required, but checked } for i in range(30): # assuming <30tps and >1 leaderboard per second # receive any packet subscriber.reset() poll_select(client) if subscriber.events[0] == 'leaderboard_names': received['leaderboard'] = True pop_and_check_keys('leaderboard_names', 'leaderboard') elif subscriber.events[0] == 'world_update_pre': received['world_update'] = True self.assertEqual('world_update_post', subscriber.events[-1]) self.assertDictEqual({}, subscriber.data[0]) self.assertDictEqual({}, subscriber.data[-1]) for evt, data in zip(subscriber.events, subscriber.data): if evt == 'cell_info': received['cell_info'] = True self.assertSetEqual({'cid', 'x', 'y', 'size', 'name', 'color', 'is_virus', 'is_agitated' }, set(data.keys())) elif evt == 'cell_eaten': received['cell_eaten'] = True self.assertSetEqual({'eater_id', 'eaten_id'}, set(data.keys())) elif evt == 'cell_removed': received['cell_removed'] = True self.assertSetEqual({'cid'}, set(data.keys())) elif evt not in ('world_update_pre', 'world_update_post'): self.fail('Got unexpected event during world_update:' ' %s %s' % (evt, data)) else: self.fail('Unexpected event %s %s' % (subscriber.events, subscriber.data)) if all(received.values()): break # we received all packets we want to check else: self.fail('Did not receive all wanted packets, missing: %s' % ', '.join(e for e, v in received.items() if not v)) # TODO test spectating # TODO test movement # TODO test split/shoot # do not test respawn, we might not get eaten for a long time client.disconnect() self.assertFalse(client.connected) self.assertFalse(client.ingame) self.assertEqual(address, client.address) self.assertEqual(token, client.server_token) self.assertEqual(nick, client.player.nick) self.assertEqual('sock_closed', subscriber.events[-1])
def test_connect(self): nick = 'Dummy' address, token = find_server() subscriber = SubscriberMock() client = Client(subscriber) self.assertEqual('', client.player.nick) client.player.nick = nick self.assertFalse(client.connected) self.assertEqual('', client.address) self.assertEqual('', client.server_token) try: success = client.connect(address, token) except ConnectionResetError: print('Connection got closed on first attempt, retrying') success = client.connect(address, token) self.assertTrue(success) self.assertTrue(client.connected) self.assertEqual(address, client.address) self.assertEqual(token, client.server_token) self.assertEqual(nick, client.player.nick) self.assertEqual(1, len(subscriber.events)) self.assertEqual('sock_open', subscriber.events[0]) # the following tests cover expected server behavior and responses def poll_select(client): """ Waits until the client receives a packet then calls its on_message() method to receive one packet. """ r = False while not r: r, w, e = select.select((client.ws.sock, ), (), ()) assert not e, 'Error while receiving from socket' client.on_message() def pop_and_check_keys(event, *keys): received_event = subscriber.events.pop(0) received_data = subscriber.data.pop(0) self.assertEqual(event, received_event) if keys: self.assertSetEqual(set(keys), set(received_data.keys())) else: self.assertDictEqual({}, received_data) # receive world_rect packet subscriber.reset() poll_select(client) pop_and_check_keys('ingame') pop_and_check_keys('world_rect', 'left', 'top', 'right', 'bottom') if len(subscriber.events) > 2: # optional, might be removed later pop_and_check_keys('server_version', 'number', 'text') # receive at least one leaderboard and world_update received = { 'leaderboard': False, 'world_update': False, 'world_rect': False, 'cell_info': False, 'cell_eaten': False, 'cell_removed': True, # not required, but checked } ignored_events = ('world_update_pre', 'world_update_post', 'cell_skin') for i in range(30): # assuming <30tps and >1 leaderboard per second # receive any packet subscriber.reset() poll_select(client) if subscriber.events[0] == 'leaderboard_names': received['leaderboard'] = True pop_and_check_keys('leaderboard_names', 'leaderboard') elif subscriber.events[0] == 'world_rect': received['world_rect'] = True pop_and_check_keys('world_rect', 'left', 'top', 'right', 'bottom') elif subscriber.events[0] == 'world_update_pre': received['world_update'] = True self.assertEqual('world_update_post', subscriber.events[-1]) self.assertDictEqual({}, subscriber.data[0]) self.assertDictEqual({}, subscriber.data[-1]) for evt, data in zip(subscriber.events, subscriber.data): if evt == 'cell_info': received['cell_info'] = True self.assertSetEqual( { 'cid', 'x', 'y', 'size', 'name', 'color', 'is_virus', 'is_agitated' }, set(data.keys())) elif evt == 'cell_eaten': received['cell_eaten'] = True self.assertSetEqual({'eater_id', 'eaten_id'}, set(data.keys())) elif evt == 'cell_removed': received['cell_removed'] = True self.assertSetEqual({'cid'}, set(data.keys())) elif evt not in ignored_events: self.fail('Got unexpected event during world_update:' ' %s %s' % (evt, data)) else: self.fail('Unexpected event %s %s' % (subscriber.events, subscriber.data)) if all(received.values()): break # we received all packets we want to check else: print('Did not receive all wanted ingame packets, missing: %s' % ', '.join(e for e, v in received.items() if not v)) # TODO test spectating # TODO test movement # TODO test split/shoot # do not test respawn, we might not get eaten for a long time client.disconnect() self.assertFalse(client.connected) self.assertFalse(client.ingame) self.assertEqual(address, client.address) self.assertEqual(token, client.server_token) self.assertEqual(nick, client.player.nick) self.assertEqual('sock_closed', subscriber.events[-1])
def __init__(self, address, token=None, nick=None): if nick is None: nick = random.choice(special_names) # connect the subscribers # order is important, first subscriber gets called first self.multi_sub = MultiSubscriber(self) def key(keycode, *subs, disabled=False): # subscribe all these subscribers, toggle them when key is pressed if isinstance(keycode, str): keycode = ord(keycode) self.multi_sub.sub(KeyToggler(keycode, *subs, disabled=disabled)) self.client = client = Client(self.multi_sub) self.multi_sub.sub(NativeControl(client)) self.multi_sub.sub(Bot(client)) # background key(Gdk.KEY_F2, SolidBackground()) key(Gdk.KEY_F2, SolidBackground(WHITE), disabled=True) key('b', WorldBorderDrawer()) key('g', GridDrawer()) self.multi_sub.sub(CellsDrawer()) # cell overlay key('k', CellSkins()) key('n', CellNames()) key( 'i', CellHostility(), CellMasses(), RemergeTimes(client), ForceFields(), ) key('m', MovementLines()) # HUD key( Gdk.KEY_F1, Minimap(), Leaderboard(), ExperienceMeter(), Logger(client), MassGraph(client), ) key(Gdk.KEY_F3, FpsMeter(50), disabled=True) client.player.nick = nick client.connect(address, token) # use AkiraYasha's Facebook token to start with more mass (> 43, lvl 56) self.client.send_facebook( 'g2gDYQFtAAAAEKO6L3c8C8/eXtbtbVJDGU5tAAAAUvOo7JuWAVSczT5Aj0eo0CvpeU8ijGzKy/gXBVCxhP5UO+ERH0jWjAo9bU1V7dU0GmwFr+SnzqWohx3qvG8Fg8RHlL17/y9ifVWpYUdweuODb9c=' ) print("Using AkiraYasha's Facebook token") gtk_watch_client(client) self.world_viewer = wv = WorldViewer(client.world) wv.draw_subscriber = wv.input_subscriber = self.multi_sub wv.focus_player(client.player)