def __init__(self, replay): self._file = open(replay) game_type_name = self.replay_read() if game_type_name in ("multiplayer", "training"): self.default_triggers = _MultiplayerGame.default_triggers game_name = self.replay_read() version = self.replay_read() mods = self.replay_read() _compatibility_version = self.replay_read() if _compatibility_version != compatibility_version(): voice.alert([1029, 4012]) # hostile sound "version error" warning("Version mismatch. Version should be: %s. Mods should be: %s.", version, mods) self.map = Map() self.map.unpack(self.replay_read()) players = self.replay_read().split() self.alliances = map(int, self.replay_read().split()) self.races = self.replay_read().split() self.seed = int(self.replay_read()) self.me = ReplayClient(players[0], self) self.players = [self.me] for x in players[1:]: if x in ["aggressive", "easy"]: # the "ai_" prefix wasn't recorded self.players += [DummyClient(x)] else: self.players += [HalfDummyClient(x)] self.me.nb_humans += 1
def __init__(self, replay): self._file = open(replay) game_type_name = self.replay_read() if game_type_name in ("multiplayer", "training"): self.default_triggers = _MultiplayerGame.default_triggers game_name = self.replay_read() voice.alert([game_name]) version = self.replay_read() mods = self.replay_read() if mods != config.mods: config.mods = mods reload_all() _compatibility_version = self.replay_read() if _compatibility_version != compatibility_version(): voice.alert([1029, 4012]) # hostile sound "version error" warning( "Version mismatch. Version should be: %s. Mods should be: %s.", version, mods) self.map = Map() self.map.unpack(self.replay_read()) players = self.replay_read().split() self.alliances = map(int, self.replay_read().split()) self.factions = self.replay_read().split() self.seed = int(self.replay_read()) self.me = ReplayClient(players[0], self) self.players = [self.me] for x in players[1:]: if x in ["aggressive", "easy"]: # the "ai_" prefix wasn't recorded self.players += [DummyClient(x)] else: self.players += [HalfDummyClient(x)] self.me.nb_humans += 1
def choose_server_ip_in_a_list(self): servers_list = None try: f = urllib.urlopen(SERVERS_LIST_URL) if f.read(len(SERVERS_LIST_HEADER)) == SERVERS_LIST_HEADER: servers_list = f.readlines() except: pass if servers_list is None: voice.alert([1029]) # hostile sound warning("couldn't get the servers list from the metaserver" " => using the default servers list") servers_list = DEFAULT_SERVERS nb = 0 menu = Menu() for s in servers_list: try: ip, version, login, port = s.split()[1:] # ignore the first parameter (time) except: warning("line not recognized from the metaserver: %s", s) continue nb += 1 if version == compatibility_version(): menu.append([login, 4073, login], (connect_and_play, ip, port)) menu.title = nb2msg(len(menu.choices)) + [4078] + nb2msg(nb) + [4079] menu.append([4075, 4076], None) menu.run()
def __init__(self, replay): self._file = open(replay) game_type_name = self.replay_read() if game_type_name in ("multiplayer", "training"): self.default_triggers = _MultiplayerGame.default_triggers game_name = self.replay_read() voice.alert([game_name]) version = self.replay_read() mods = self.replay_read() res.set_mods(mods) _compatibility_version = self.replay_read() if _compatibility_version != compatibility_version(): voice.alert([1029, 4012]) # hostile sound "version error" warning("Version mismatch. Version should be: %s. Mods should be: %s.", version, mods) campaign_path_or_packed_map = self.replay_read() if game_type_name == "mission" and "***" not in campaign_path_or_packed_map: from campaign import Campaign self.map = Campaign(campaign_path_or_packed_map)._get(int(self.replay_read())) else: self.map = Map() self.map.unpack(campaign_path_or_packed_map) players = self.replay_read().split() self.alliances = map(int, self.replay_read().split()) self.factions = self.replay_read().split() self.seed = int(self.replay_read()) self.me = ReplayClient(players[0], self) self.players = [self.me] for x in players[1:]: if x in ["aggressive", "easy"]: # the "ai_" prefix wasn't recorded self.players += [DummyClient(x)] else: self.players += [HalfDummyClient(x)] self.me.nb_humans += 1
def connect_and_play(host="127.0.0.1", port=options.port, auto=False): try: server = ConnectionToServer(host, port) ServerMenu(server, auto=auto).loop() server.close() # without this, the server isn't closed after a game except UnreachableServerError: voice.alert(mp.SERVER_UNREACHABLE) except WrongServerError: voice.alert(mp.UNEXPECTED_REPLY + [compatibility_version()]) except CompatibilityOrLoginError: voice.alert(mp.CONNECTION_REJECTED + [compatibility_version()] + mp.OR_LOGIN_REJECTED) except ConnectionAbortedError: voice.alert(mp.CONNECTION_INTERRUPTED) except SystemExit: raise except: voice.alert(mp.ERROR_DURING_CONNECTION) exception("error during connection to server")
def pre_run(self): nb_human_players = len([p for p in self.players if p.login != "ai"]) if nb_human_players > 1: if compatibility_version() != COMPATIBILITY_VERSION: warning("rules.txt or ai.txt has been modified" " after the program started: exit...") sys.exit() send_platform_version_to_metaserver(self.map.get_name(), nb_human_players) self._countdown()
def _register(self): try: s = urllib.urlopen(REGISTER_URL + "?version=%s&login=%s&ip=%s&port=%s" % (compatibility_version(), self.login, self.ip, config.port)).read() except: s = "couldn't access to the metaserver" if s: warning("couldn't register to the metaserver (%s)", s[:80]) else: info("server registered")
def connect_and_play(host="127.0.0.1", port=config.port): try: server = ConnectionToServer(host, port) ServerMenu(server).loop() server.close() # without this, the server isn't closed after a game except UnreachableServerError: # "failure: the server unreachable. The server is closed or behind a firewall or behind a router." voice.alert([4081]) except WrongServerError: # "failure: unexpected reply from the server. The server is not a SoundRTS server" (version) voice.alert([4082, compatibility_version()]) except CompatibilityOrLoginError: # "failure: connexion rejected the server. The server is not a SoundRTS server" (version) # "or your login has been rejected" voice.alert([4083, compatibility_version(), 4084]) except ConnectionAbortedError: voice.alert([4102]) # connection aborted except SystemExit: raise except: voice.alert([4085]) # "error during connexion to server" exception("error during connection to server")
def create_replay(self): self._replay_file = open(os.path.join(REPLAYS_PATH, "%s.txt" % int(time.time())), "w") self.replay_write(self.game_type_name) players = " ".join([p.login for p in self.players]) self.replay_write(self.map.get_name() + " " + players) self.replay_write(VERSION) self.replay_write(config.mods) self.replay_write(compatibility_version()) self.replay_write(self.map.pack()) self.replay_write(players) self.replay_write(" ".join(map(str, self.alliances))) self.replay_write(" ".join(self.races)) self.replay_write(str(self.seed))
def create_replay(self): self._replay_file = open( os.path.join(REPLAYS_PATH, "%s.txt" % int(time.time())), "w") self.replay_write(self.game_type_name) players = " ".join([p.login for p in self.players]) self.replay_write(self.map.get_name() + " " + players) self.replay_write(VERSION) self.replay_write(config.mods) self.replay_write(compatibility_version()) self.replay_write(self.map.pack()) self.replay_write(players) self.replay_write(" ".join(map(str, self.alliances))) self.replay_write(" ".join(self.factions)) self.replay_write(str(self.seed))
def open(self): try: self.tn = telnetlib.Telnet(self.host, self.port) except socket.error: raise UnreachableServerError try: if self.tn.read_until(":", 3) != ":": raise WrongServerError self.tn.write("login " + compatibility_version() + " %s\n" % config.login) except (EOFError, socket.error): raise WrongServerError try: self.tn.read_until("ok!", 5) except EOFError: raise CompatibilityOrLoginError
def create_replay(self): self._replay_file = open(os.path.join(REPLAYS_PATH, "%s.txt" % int(time.time())), "w") self.replay_write(self.game_type_name) players = " ".join([p.login for p in self.players]) self.replay_write(self.map.get_name() + " " + players) self.replay_write(VERSION) self.replay_write(res.mods) self.replay_write(compatibility_version()) if self.game_type_name == "mission": self.replay_write(self.map.campaign.path) self.replay_write(str(self.map.id)) else: self.replay_write(self.map.pack()) self.replay_write(players) alliances = [p.alliance for p in self.players] self.replay_write(" ".join(map(str, alliances))) factions = [p.faction for p in self.players] self.replay_write(" ".join(factions)) self.replay_write(str(self.seed))
def __init__(self, replay): self._file = open(replay) game_type_name = self.replay_read() if game_type_name in ("multiplayer", "training"): self.default_triggers = _MultiplayerGame.default_triggers self.must_apply_equivalent_type = True game_name = self.replay_read() voice.alert([game_name]) version = self.replay_read() mods = self.replay_read() res.set_mods(mods) _compatibility_version = self.replay_read() if _compatibility_version != compatibility_version(): voice.alert(mp.BEEP + mp.VERSION_ERROR) warning( "Version mismatch. Version should be: %s. Mods should be: %s.", version, mods) campaign_path_or_packed_map = self.replay_read() if game_type_name == "mission" and "***" not in campaign_path_or_packed_map: from campaign import Campaign self.map = Campaign(campaign_path_or_packed_map)._get( int(self.replay_read())) else: self.map = Map() self.map.unpack(campaign_path_or_packed_map) players = self.replay_read().split() alliances = self.replay_read().split() factions = self.replay_read().split() self.seed = int(self.replay_read()) self.me = ReplayClient(players[0], self) self.players = [self.me] for x in players[1:]: if x.startswith("ai_"): x = x[3:] if x in ["aggressive", "easy", "ai2"]: self.players += [DummyClient(x)] else: self.players += [RemoteClient(x)] self.me.nb_humans += 1 for p, a, f in zip(self.players, alliances, factions): p.alliance = a p.faction = f
def create_replay(self): self._replay_file = open( os.path.join(REPLAYS_PATH, "%s.txt" % int(time.time())), "w") self.replay_write(self.game_type_name) players = " ".join([p.login for p in self.players]) self.replay_write(self.map.get_name() + " " + players) self.replay_write(VERSION) self.replay_write(res.mods) self.replay_write(compatibility_version()) if self.game_type_name == "mission": self.replay_write(self.map.campaign.path) self.replay_write(str(self.map.id)) else: self.replay_write(self.map.pack()) self.replay_write(players) alliances = [p.alliance for p in self.players] self.replay_write(" ".join(map(str, alliances))) factions = [p.faction for p in self.players] self.replay_write(" ".join(factions)) self.replay_write(str(self.seed))
def open(self): try: self.tn = telnetlib.Telnet(self.host, self.port) except socket.error: raise UnreachableServerError try: # Activate TCP Keepalive to prevent connections from silently dropping self.tn.get_socket().ioctl(socket.SIO_KEEPALIVE_VALS, (1, 60000, 2000)) except: # platform different than Windows pass try: if self.tn.read_until(":", 3) != ":": raise WrongServerError self.tn.write("login " + compatibility_version() + " %s\n" % config.login) except (EOFError, socket.error): raise WrongServerError try: self.tn.read_until("ok!", 5) except EOFError: raise CompatibilityOrLoginError
def __init__(self, replay): self._file = open(replay) game_type_name = self.replay_read() if game_type_name in ("multiplayer", "training"): self.default_triggers = _MultiplayerGame.default_triggers self.must_apply_equivalent_type = True game_name = self.replay_read() voice.alert([game_name]) version = self.replay_read() mods = self.replay_read() res.set_mods(mods) _compatibility_version = self.replay_read() if _compatibility_version != compatibility_version(): voice.alert(mp.BEEP + mp.VERSION_ERROR) warning("Version mismatch. Version should be: %s. Mods should be: %s.", version, mods) campaign_path_or_packed_map = self.replay_read() if game_type_name == "mission" and "***" not in campaign_path_or_packed_map: from campaign import Campaign self.map = Campaign(campaign_path_or_packed_map)._get(int(self.replay_read())) else: self.map = Map() self.map.unpack(campaign_path_or_packed_map) players = self.replay_read().split() alliances = self.replay_read().split() factions = self.replay_read().split() self.seed = int(self.replay_read()) self.me = ReplayClient(players[0], self) self.players = [self.me] for x in players[1:]: if x.startswith("ai_"): x = x[3:] if x in ["aggressive", "easy", "ai2"]: self.players += [DummyClient(x)] else: self.players += [RemoteClient(x)] self.me.nb_humans += 1 for p, a, f in zip(self.players, alliances, factions): p.alliance = a p.faction = f
from definitions import style from game import TrainingGame, ReplayGame, reload_all from lib.log import exception from multimaps import worlds_multi from msgs import nb2msg from package import get_packages, get_all_packages_paths from paths import REPLAYS_PATH, SAVE_PATH import res from singlemaps import campaigns import stats from version import compatibility_version _ds = open("cfg/default_servers.txt").readlines() _ds = [_x.split() for _x in _ds] DEFAULT_SERVERS = [" ".join(["0"] + _x[:1] + [compatibility_version()] + _x[1:]) for _x in _ds] SERVERS_LIST_HEADER = "SERVERS_LIST" SERVERS_LIST_URL = MAIN_METASERVER_URL + "servers.php?header=%s&include_ports=1" % SERVERS_LIST_HEADER class Application(object): def choose_server_ip_in_a_list(self): servers_list = None try: f = urllib.urlopen(SERVERS_LIST_URL) if f.read(len(SERVERS_LIST_HEADER)) == SERVERS_LIST_HEADER: servers_list = f.readlines() except: pass if servers_list is None: