def join_game(screen, host, port, team): try: sock = socket.socket() sock.connect((host, port)) mapname = [None] other_team = [None] spawns = [None] def handler(type, data): if type == "MAP": mapname[0] = data if type == "TEAM": other_team[0] = deserialize_team(data) if type == "SPAWNS": spawns[0] = deserialize_spawns(data) conn = Messager(handler, sock) conn.push_message("TEAM", serialize_team(team)) while mapname[0] is None or other_team[0] is None or spawns[0] is None: asyncore.loop(count=1, timeout=0.1) except: sys.excepthook(*sys.exc_info()) return teams = [("Player 1", conn, other_team[0]), ("Player 2", None, team)] game = XadirMain(screen, mapname=mapname[0]) game.load_resources() game.init_teams(teams, spawns[0]) game.main_loop()
def host_game(screen, port, mapname, team): show_message(screen, 'Waiting for connection...') try: serv = socket.socket() serv.bind(('0.0.0.0', port)) serv.listen(1) sock, addr = serv.accept() serv.close() show_message(screen, 'Connection established') show_message(screen, 'Synchronizing game data...') proto = [False] other_team = [None] spawns = [None] def handler(type, data): if type == 'PROTOCOL': if compatible_protocol(data): proto[0] = True assert proto[0], 'Incompatible protocol version' if type == 'TEAM': other_team[0] = deserialize_team(data) conn = Messager(handler, sock) conn.push_message('PROTOCOL', PROTOCOL_VERSION) conn.push_message('MAP', mapname) conn.push_message('TEAM', serialize_team(team)) while other_team[0] is None: asyncore.loop(count=1, timeout=0.1) assert asyncore.socket_map, 'Protocol error or disconnection' except Exception, e: sys.excepthook(*sys.exc_info()) show_message(screen, 'Link failed! (%s: %s)' % (e.__class__.__name__, e.message), True, (255, 0, 0)) return
def join_game(screen, host, port, team): show_message(screen, 'Connecting...') try: sock = socket.socket() sock.connect((host, port)) show_message(screen, 'Connection established') show_message(screen, 'Synchronizing game data...') proto = [False] mapname = [None] other_team = [None] spawns = [None] def handler(type, data): if type == 'PROTOCOL': if compatible_protocol(data): proto[0] = True assert proto[0], 'Incompatible protocol version' if type == 'MAP': mapname[0] = data if type == 'TEAM': other_team[0] = deserialize_team(data) if type == 'SPAWNS': spawns[0] = deserialize_spawns(data) conn = Messager(handler, sock) conn.push_message('PROTOCOL', PROTOCOL_VERSION) conn.push_message('TEAM', serialize_team(team)) while mapname[0] is None or other_team[0] is None or spawns[0] is None: asyncore.loop(count=1, timeout=0.1) assert asyncore.socket_map, 'Protocol error or disconnection' except Exception, e: sys.excepthook(*sys.exc_info()) show_message(screen, 'Link failed! (%s: %s)' % (e.__class__.__name__, e.message), True, (255, 0, 0)) return
def host_game(screen, port, mapname, team): try: serv = socket.socket() serv.bind(("0.0.0.0", port)) serv.listen(1) sock, addr = serv.accept() serv.close() other_team = [None] spawns = [None] def handler(type, data): if type == "TEAM": other_team[0] = deserialize_team(data) conn = Messager(handler, sock) conn.push_message("MAP", mapname) conn.push_message("TEAM", serialize_team(team)) while other_team[0] is None: asyncore.loop(count=1, timeout=0.1) except: sys.excepthook(*sys.exc_info()) return teams = [("Player 1", None, team), ("Player 2", conn, other_team[0])] game = XadirMain(screen, mapname=mapname) game.load_resources() spawns = game.get_spawnpoints(teams) conn.push_message("SPAWNS", serialize_spawns(spawns)) game.init_teams(teams, spawns) game.main_loop()