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 run(self, speed=config.speed): if self.record_replay: self.create_replay() self.world = World(self.default_triggers, self.seed) if self.world.load_and_build_map(self.map): style.load(res.get_text("ui/style", append=True, locale=True), self.map.campaign_style, self.map.additional_style) sounds.enter_map(self.map.mapfile) update_orders_list() # when style has changed self.pre_run() self.interface = clientgame.GameInterface(self.me, speed=speed) self.interface.load_bindings( res.get_text("ui/bindings", append=True, locale=True) + "\n" + self.map.get_campaign("ui/bindings.txt") + "\n" + self.map.get_additional("ui/bindings.txt")) self.world.populate_map(self.players, self.alliances, self.races) self.nb_human_players = self.world.current_nb_human_players() t = threading.Thread(target=self.world.loop) t.daemon = True t.start() self.interface.loop() self._record_stats(self.world) self.post_run() sounds.exit_map() self.world.clean() else: voice.alert([1029]) # hostile sound voice.alert([self.world.map_error]) if self.record_replay: self._replay_file.close()
def run(self, speed=config.speed): if self.record_replay: self.create_replay() self.world = World(self.default_triggers, self.seed) if self.world.load_and_build_map(self.map): self.map.load_style(res) try: self.map.load_resources() update_orders_list() # when style has changed self.pre_run() self.interface = clientgame.GameInterface(self.me, speed=speed) self.interface.load_bindings( res.get_text_file("ui/bindings", append=True, localize=True) + "\n" + self.map.get_campaign("ui/bindings.txt") + "\n" + self.map.get_additional("ui/bindings.txt")) self.world.populate_map(self.players, self.alliances, self.factions) self.nb_human_players = self.world.current_nb_human_players() t = threading.Thread(target=self.world.loop) t.daemon = True t.start() if PROFILE: import cProfile cProfile.runctx("self.interface.loop()", globals(), locals(), "interface_profile.tmp") else: self.interface.loop() self._record_stats(self.world) self.post_run() finally: self.map.unload_resources() self.world.clean() else: voice.alert([1029]) # hostile sound voice.alert([self.world.map_error]) if self.record_replay: self._replay_file.close()
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 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 == 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 reload_all(): update_display_caption() res.update_mods_list() sounds.load_default() rules.load(res.get_text("rules", append=True)) load_ai(res.get_text("ai", append=True)) # just in case style.load(res.get_text("ui/style", append=True, locale=True)) while(res.alerts): voice.alert(res.alerts.pop(0))
def multiplayer_menu(): if config.login == "player": voice.alert(mp.ENTER_NEW_LOGIN) modify_login() menu = Menu(mp.MAKE_A_SELECTION, [ (mp.CHOOSE_SERVER_IN_LIST, choose_server_ip_in_a_list), (mp.ENTER_SERVER_IP, enter_server_ip), (mp.CANCEL, None), ]) menu.run()
def launch_manual(): if os.path.exists("doc/en"): p = "doc" else: p = join(tempfile.gettempdir(), "soundrts/build/doc") try: lang = best_language_match(preferred_language, os.listdir(p)) except OSError: voice.alert(mp.BEEP) else: webbrowser.open(join(p, lang, "help-index.htm"))
def multiplayer_menu(self): revision_checker.start_if_needed() if config.login == "player": voice.alert([4235]) # type your new login self.modify_login() menu = Menu([4030], [ ([4119], self.choose_server_ip_in_a_list), ([4120], self.enter_server_ip), ([4048], None), ]) menu.run()
def run(self, speed=config.speed): if self.record_replay: self.create_replay() self.world = World(self.default_triggers, self.seed, must_apply_equivalent_type=self.must_apply_equivalent_type) if self.world.load_and_build_map(self.map): self.map.load_style(res) try: self.map.load_resources() update_orders_list() # when style has changed self.pre_run() if self.world.objective: voice.confirmation(mp.OBJECTIVE + self.world.objective) self.interface = clientgame.GameInterface(self.me, speed=speed) b = res.get_text_file("ui/bindings", append=True, localize=True) b += "\n" + self.map.get_campaign("ui/bindings.txt") b += "\n" + self.map.get_additional("ui/bindings.txt") try: b += "\n" + open(CUSTOM_BINDINGS_PATH, "U").read() except IOError: pass self.interface.load_bindings(b) self.world.populate_map(self.players) self.nb_human_players = self.world.current_nb_human_players() t = threading.Thread(target=self.world.loop) t.daemon = True t.start() if PROFILE: import cProfile cProfile.runctx("self.interface.loop()", globals(), locals(), "interface_profile.tmp") import pstats for n in ("interface_profile.tmp", ): p = pstats.Stats(n) p.strip_dirs() p.sort_stats('time', 'cumulative').print_stats(30) p.print_callers(30) p.print_callees(20) p.sort_stats('cumulative').print_stats(50) else: self.interface.loop() self._record_stats(self.world) self.post_run() finally: self.map.unload_resources() self.world.stop() else: voice.alert(mp.BEEP + [self.world.map_error]) if self.record_replay: self._replay_file.close()
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 save(self): f = open(SAVE_PATH, "w") i = stats.Stats(None, None)._get_weak_user_id() f.write("%s\n" % i) self.world.remove_links_for_savegame() self._rules = rules self._ai = definitions._ai self._style = style if self.record_replay: self._replay_file.flush() os.fsync(self._replay_file.fileno()) # just to be sure self._replay_file_content = open(self._replay_file.name).read() try: pickle.dump(self, f) voice.info(mp.OK) except: exception("save game failed") voice.alert(mp.BEEP) self.world.restore_links_for_savegame()
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 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 modify_login(self): login = input_string([4235, 4236], "^[a-zA-Z0-9]$") # type your new # login ; use alphanumeric characters if login == None: voice.alert([4238]) # current login kept elif (len(login) < 1) or (len(login) > 20): voice.alert([4237, 4238]) # incorrect login ; current login kept else: voice.alert([4239, login]) # new login: config.login = login
def modify_login(): login = input_string(mp.ENTER_NEW_LOGIN + mp.USE_LETTERS_AND_NUMBERS_ONLY, "^[a-zA-Z0-9]$") if login == None: voice.alert(mp.CURRENT_LOGIN_KEPT) elif (len(login) < 1) or (len(login) > 20): voice.alert(mp.BAD_LOGIN + mp.CURRENT_LOGIN_KEPT) else: voice.alert(mp.NEW_LOGIN + [login]) config.login = login config.save()
def restore_game(self): n = SAVE_PATH if not os.path.exists(n): voice.alert([1029]) # hostile sound return f = open(n) try: i = int(stats.Stats(None, None)._get_weak_user_id()) j = int(f.readline()) except: i = 0 j = "error" if i == j: try: game_session = pickle.load(f) except: exception("cannot load savegame file") voice.alert([1029]) # hostile sound return game_session.run_on() else: warning("savegame file is not from this machine") voice.alert([1029]) # hostile sound