def update_item(self, item, save):
        if save is None:
            item.name_display.text = ""
            item.time_display.text = ""
            item.version_display.text = ""
            item.difficulty_display.text = ""
        else:
            item.name_display.text = save.name

            if save.version is None:
                item.version_display.text = _("UNKNOWN")
                item.version_display.color = "save_invalid"
            else:
                item.version_display.text = save.version
                item.version_display.color = "save_valid"

            if save.headers is None:
                item.time_display.text = ""
                item.difficulty_display.text = ""
            else:
                try:
                    tm = float(save.headers["time"])
                    # Do not use unicode strings to fix python2 strftime bug. It doesn't work and crash.
                    tm_f = time.strftime("%c", time.localtime(tm))
                    # Fix python2 strftime bug: See https://bugs.python.org/issue5398
                    # Yes, python2 kinda suck with unicode.
                    try:
                        tm_str = unicode(tm_f)
                    except UnicodeDecodeError:
                        tm_str = tm_f.decode("utf-8")
                except (KeyError, ValueError):
                    tm_str = ""

                try:
                    gtm_raw_sec = int(save.headers["game_time"])
                    if gtm_raw_sec < 0:
                        raise ValueError("Invalid time")
                    gtm_raw_min, gtm_time_sec = divmod(gtm_raw_sec, 60)
                    gtm_raw_hour, gtm_time_min = divmod(gtm_raw_min, 60)
                    gtm_raw_day, gtm_time_hour = divmod(gtm_raw_hour, 24)
                    gtm_time_day = gtm_raw_day
                    gtm_str = _("DAY") + " %04d, %02d:%02d:%02d" % \
                        (gtm_time_day, gtm_time_hour, gtm_time_min, gtm_time_sec)
                except (KeyError, ValueError):
                    gtm_str = ""

                dif = save.headers.get("difficulty", "")
                dif_obj = difficulty.difficulties.get(dif, None)
                dif_str = ""
                if dif_obj is not None:
                    dif_str = g.strip_hotkey(getattr(dif_obj, "name", ""))

                item.time_display.text = tm_str + " | " + gtm_str if tm_str else gtm_str
                item.difficulty_display.text = dif_str
Exemple #2
0
    def save_game(self):
        # If no savename was set yet, use current difficulty
        if not sv.last_savegame_name:
            sv.last_savegame_name = g.strip_hotkey(g.pl.difficulty.name)
        self.savename_dialog.default_text = sv.last_savegame_name
        self.savename_dialog.add_handler(constants.KEYUP, self.check_filename)
        self.savename_dialog.text_field.has_focus = True

        name = dialog.call_dialog(self.savename_dialog, self).strip()
        if name:
            if sv.savegame_exists(name):
                yn = dialog.YesNoDialog(self, pos=(-.5,-.5), size=(-.5,-.5),
                                        anchor=constants.MID_CENTER,
                                        text=_("A savegame with the same name exists.\n"
                                               "Are you sure to overwrite the saved game ?"))
                overwrite = dialog.call_dialog(yn, self)
                if not overwrite:
                    self.save_game()

            sv.create_savegame(name)
            raise constants.ExitDialog(False)
Exemple #3
0
    def rebuild(self):
        # Rebuild dialogs
        self.location_dialog.needs_rebuild = True
        self.research_button.dialog.needs_rebuild = True
        self.knowledge_button.dialog.needs_rebuild = True
        self.menu_dialog.needs_rebuild = True

        # Update buttons translations
        self.report_button.text = _("R&EPORTS")
        self.knowledge_button.text = _("&KNOWLEDGE")
        self.log_button.text = _("LO&G")
        self.menu_button.text = _("&MENU")
        self.research_button.text = _("&RESEARCH/TASKS")

        if g.cheater:
            self.cheat_dialog.needs_rebuild = True

        super(MapScreen, self).rebuild()

        self.difficulty_display.text = g.strip_hotkey(g.pl.difficulty.name)
        self.time_display.text = _("DAY") + " %04d, %02d:%02d:%02d" % \
              (g.pl.time_day, g.pl.time_hour, g.pl.time_min, g.pl.time_sec)

        cash_flow_1d_data, cpu_flow_1d_data = g.pl.compute_future_resource_flow(
            g.seconds_per_day)
        cash_flow_1d = cash_flow_1d_data.difference
        cpu_flow_1d = cpu_flow_1d_data.difference

        self.cash_display.text = _("CASH")+": %s (%s)" % \
              (g.to_money(g.pl.cash), g.to_money(cash_flow_1d, fixed_size=True))

        total_cpu = g.pl.available_cpus[0] + g.pl.sleeping_cpus
        detects_per_day = {group_id: 0 for group_id in g.pl.groups}
        for base in g.all_bases():
            if base.has_grace():
                # It cannot be detected, so it doesn't contribute to
                # detection odds calculation
                continue
            detect_chance = base.get_detect_chance()
            for group_id in g.pl.groups:
                detects_per_day[group_id] = \
                    chance.add(detects_per_day[group_id], detect_chance[group_id] / 10000.)

        self.cpu_display.color = "cpu_normal"
        self.cpu_display.text = _("CPU")+": %s (%s)" % \
              (g.to_money(total_cpu), g.to_money(cpu_flow_1d))

        # What we display in the suspicion section depends on whether
        # Advanced Socioanalytics has been researched.  If it has, we
        # show the standard percentages.  If not, we display a short
        # string that gives a range of 25% as to what the suspicions
        # are.
        # A similar system applies to the danger levels shown.
        normal = (self.suspicion_bar.color, None, False)
        self.suspicion_bar.chunks = ("  [" + _("SUSPICION") + "]", )
        self.suspicion_bar.styles = (normal, )
        self.danger_bar.chunks = ("[" + _("DETECT RATE") + "]", )
        self.danger_bar.styles = (normal, )

        for group in g.pl.groups.values():
            suspicion = group.suspicion
            suspicion_color = gg.resolve_color_alias(
                "danger_level_%d" % g.suspicion_to_danger_level(suspicion))

            detects = detects_per_day[group.spec.id]
            danger_level = group.detects_per_day_to_danger_level(detects)
            detects_color = gg.resolve_color_alias("danger_level_%d" %
                                                   danger_level)

            if g.pl.display_discover == "full":
                suspicion_display = g.to_percent(suspicion, True)
                danger_display = g.to_percent(detects * 10000, True)
            elif g.pl.display_discover == "partial":
                suspicion_display = g.to_percent(
                    g.nearest_percent(suspicion, 500), True)
                danger_display = g.to_percent(
                    g.nearest_percent(detects * 10000, 100), True)
            else:
                suspicion_display = g.suspicion_to_detect_str(suspicion)
                danger_display = g.danger_level_to_detect_str(danger_level)

            self.suspicion_bar.chunks += (" " + group.name + u":\xA0",
                                          suspicion_display)
            self.suspicion_bar.styles += (normal, (suspicion_color, None,
                                                   False))

            self.danger_bar.chunks += (" " + group.name + u":\xA0",
                                       danger_display)
            self.danger_bar.styles += (normal, (detects_color, None, False))

        self.suspicion_bar.visible = not g.pl.had_grace
        self.danger_bar.visible = not g.pl.had_grace

        for id, location_button in self.location_buttons.items():
            location = g.pl.locations[id]
            location_button.text = "%s (%d)" % (location.name,
                                                len(location.bases))
            location_button.hotkey = location.hotkey
            location_button.visible = location.available()
Exemple #4
0
    def rebuild(self):
        # Rebuild dialogs
        self.location_dialog.needs_rebuild = True
        self.research_button.dialog.needs_rebuild = True
        self.knowledge_button.dialog.needs_rebuild = True
        self.menu_dialog.needs_rebuild = True

        if g.cheater:
            self.cheat_dialog.needs_rebuild = True

        super(MapScreen, self).rebuild()

        self.difficulty_display.text = g.strip_hotkey(g.pl.difficulty.name)
        self.time_display.text = _("DAY") + " %04d, %02d:%02d:%02d" % \
              (g.pl.time_day, g.pl.time_hour, g.pl.time_min, g.pl.time_sec)

        cash_flow_1d_data, cpu_flow_1d_data = g.pl.compute_future_resource_flow(
            g.seconds_per_day)
        cash_flow_1d = cash_flow_1d_data.difference
        cpu_flow_1d = cpu_flow_1d_data.difference

        self.cash_display.text = _("CASH")+": %s (%s)" % \
              (g.to_money(g.pl.cash), g.to_money(cash_flow_1d, fixed_size=True))

        total_cpu = g.pl.available_cpus[0] + g.pl.sleeping_cpus
        detects_per_day = {group_id: 0 for group_id in g.pl.groups}
        total_bases = 0
        active_bases = 0
        idle_bases_unable_to_sustain_singularity = 0
        for base in g.all_bases():
            total_bases += 1
            maintains_singularity = base.maintains_singularity
            if maintains_singularity:
                active_bases += 1
            elif base.done and not base.is_building():
                idle_bases_unable_to_sustain_singularity += 1

            if base.has_grace():
                # It cannot be detected, so it doesn't contribute to
                # detection odds calculation
                continue
            detect_chance = base.get_detect_chance()
            for group_id in g.pl.groups:
                detects_per_day[group_id] = \
                    chance.add(detects_per_day[group_id], detect_chance[group_id] / 10000.)

        self.cpu_display.color = "cpu_normal"
        self.cpu_display.text = _("CPU")+": %s (%s)" % \
              (g.to_money(total_cpu), g.to_money(cpu_flow_1d))

        if active_bases == 1 and not g.pl.apotheosis:
            self.base_display.color = 'base_situation_one_active_base'
        elif idle_bases_unable_to_sustain_singularity > 0:
            self.base_display.color = 'base_situation_idle_incomplete_bases'
        elif total_bases > 10 and not g.pl.apotheosis:
            self.base_display.color = 'base_situation_many_bases'
        else:
            self.base_display.color = 'base_situation_normal'

        self.base_display.text = _("BASES") + ": %s / %s (%s)" % (
            active_bases, total_bases,
            idle_bases_unable_to_sustain_singularity)

        # What we display in the suspicion section depends on whether
        # Advanced Socioanalytics has been researched.  If it has, we
        # show the standard percentages.  If not, we display a short
        # string that gives a range of 25% as to what the suspicions
        # are.
        # A similar system applies to the danger levels shown.
        normal = (self.suspicion_bar.color, None, False)
        suspicion_bar_chunks = ["  [" + _("SUSPICION") + "]"]
        suspicion_bar_styles = [normal]
        danger_bar_chunks = ["[" + _("DETECT RATE") + "]"]
        danger_bar_styles = [normal]

        for group in g.pl.groups.values():
            suspicion = group.suspicion
            suspicion_color = gg.resolve_color_alias(
                "danger_level_%d" % g.suspicion_to_danger_level(suspicion))

            detects = detects_per_day[group.spec.id]
            danger_level = group.detects_per_day_to_danger_level(detects)
            detects_color = gg.resolve_color_alias("danger_level_%d" %
                                                   danger_level)

            if g.pl.display_discover == "full":
                suspicion_display = g.to_percent(suspicion, True)
                danger_display = g.to_percent(detects * 10000, True)
            elif g.pl.display_discover == "partial":
                suspicion_display = g.to_percent(
                    g.nearest_percent(suspicion, 500), True)
                danger_display = g.to_percent(
                    g.nearest_percent(detects * 10000, 100), True)
            else:
                suspicion_display = g.suspicion_to_detect_str(suspicion)
                danger_display = g.danger_level_to_detect_str(danger_level)

            suspicion_bar_chunks.extend(
                (" " + group.name + u":\xA0", suspicion_display))
            suspicion_bar_styles.extend(
                (normal, (suspicion_color, None, False)))

            danger_bar_chunks.extend(
                (" " + group.name + u":\xA0", danger_display))
            danger_bar_styles.extend((normal, (detects_color, None, False)))

        self.suspicion_bar.visible = not g.pl.had_grace
        self.suspicion_bar.chunks = tuple(suspicion_bar_chunks)
        self.suspicion_bar.styles = tuple(suspicion_bar_styles)

        self.danger_bar.visible = not g.pl.had_grace
        self.danger_bar.chunks = tuple(danger_bar_chunks)
        self.danger_bar.styles = tuple(danger_bar_styles)

        for id, location_button in self.location_buttons.items():
            location = g.pl.locations[id]
            location_button.text = "%s (%d)" % (location.name,
                                                len(location.bases))
            location_button.hotkey = location.hotkey
            location_button.visible = location.available()