def main(): print("Copyright (C) 2015 Gjum <*****@*****.**>\n" "This program comes with ABSOLUTELY NO WARRANTY.\n" "This is free software, and you are welcome to redistribute it\n" "under certain conditions; see LICENSE.txt for details.\n" "Project homepage: https://github.com/Gjum/gagar\n" "Version: 0.1.1\n") if len(sys.argv) > 1 and sys.argv[1] in ('-h', '--help'): print("Usage: %s [nick]" % sys.argv[0]) print(" %s party <token> [nick]" % sys.argv[0]) print(" %s <IP:port> <token> [nick]" % sys.argv[0]) return address, token, nick, *_ = sys.argv[1:] + ([None] * 3) if token is None: nick = address address = None if address and address[0] in 'Pp': address = get_party_address(token) if not address: address, token, *_ = find_server() GtkControl(address, token, nick) gtk_main_loop()
def main(): print("Copyright (C) 2015 Gjum <*****@*****.**>\n" "This program comes with ABSOLUTELY NO WARRANTY.\n" "This is free software, and you are welcome to redistribute it\n" "under certain conditions; see LICENSE.txt for details.\n" "Project homepage: https://github.com/Gjum/gagar\n" "Version: 0.1.4\n") if len(sys.argv) > 1 and sys.argv[1] in ('-h', '--help'): print("Usage: %s [nick]" % sys.argv[0]) print(" %s party <token> [nick]" % sys.argv[0]) print(" %s <IP:port> <token> [nick]" % sys.argv[0]) return address, token, nick, *_ = sys.argv[1:] + ([None] * 3) if token is None: nick = address address = None if address and address[0] in 'Pp': address = get_party_address(token) if not address: address, token = find_server() GtkControl(address, token, nick) gtk_main_loop()
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])
def on_key_press(symbol, modifiers): try: print("key", chr(symbol), hex(symbol), symbol) except: pass if symbol == key.S: client.send_spectate() elif symbol == key.Q: client.send_spectate_toggle() elif symbol == key.R: client.send_respawn() mass_graph.clear() elif symbol == key.W: client.send_target(*screen_to_world(target_win)) client.send_shoot() elif symbol == key.SPACE: client.send_split() elif symbol == key.C: client.disconnect() client.connect(*find_server()) elif symbol == key.N: global show_cell_names show_cell_names = not show_cell_names elif symbol == key.I: global show_cell_info show_cell_info = not show_cell_info elif symbol == key.F3: global show_fps show_fps = not show_fps
def test_find_server(self): address_regex = r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\:\d{1,5}' for i in range(3): # TODO make sure to not get above a rate limit address, token = find_server(region='EU-London', mode='teams') self.assertNotEqual('0.0.0.0:0', address) self.assertRegex(address, address_regex) self.assertGreater(len(token), 0, 'no token received')
def on_key_pressed(self, val, char): if val == Gdk.KEY_Escape: self.client.disconnect() Gtk.main_quit() elif char == 'c': # reconnect to any server self.client.disconnect() address, token = find_server() self.client.connect(address, token) self.world_viewer.focus_player(self.client.player)
def on_key_pressed(self, val, char): if val == Gdk.KEY_Tab: self.native_control.toggle_sending_mouse() if val == Gdk.KEY_Escape: self.client.disconnect() Gtk.main_quit() elif char == 'c': # reconnect to any server self.client.disconnect() address, token, *_ = find_server() self.client.connect(address, token) self.world_viewer.focus_player(self.client.player)
def on_key_pressed(self, val, char): if char == "q" or val == Gdk.KEY_Escape: self.client.disconnect() Gtk.main_quit() elif char == "s": self.client.send_spectate() elif char == "r" or val == Gdk.KEY_Return: self.client.send_respawn() elif char == "c": # reconnect to any server self.client.disconnect() address, token, *_ = find_server() self.client.connect(address, token) self.world_viewer.focus_player(self.client.player)
def on_key_pressed(self, val, char): if char == 'q' or val == Gdk.KEY_Escape: self.client.disconnect() Gtk.main_quit() elif char == 's': self.client.send_spectate() elif char == 'r' or val == Gdk.KEY_Return: self.client.send_respawn() elif char == 'c': # reconnect to any server self.client.disconnect() address, token, *_ = find_server() self.client.connect(address, token) self.world_viewer.focus_player(self.client.player)
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()
while True: r, w, e = select.select((client.ws.sock,), (), (), timeout) if r: client.on_message() elif e: client.subscriber.on_sock_error(e) else: break # all waiting packets processed def on_tick(dt): client.send_target(*screen_to_world(target_win)) process_packets(client) sub = Subscriber() client = agarnet.client.Client(sub) client.world.cell_class = CustomCell client.player.nick = random.choice(special_names) print("player name:", client.player.nick) try: client.connect(*find_server()) except ConnectionResetError: client.connect(*find_server()) pyglet.clock.schedule_interval(on_tick, 1.0 / 60.0) window.set_visible(True) pyglet.app.run()
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 test_get_party_address(self): address, token = find_server(mode='party') party_address = get_party_address(token) self.assertEqual(address, party_address, 'party mode broke')