File: _fleet_combat_stats.py

package info (click to toggle)
freeorion 0.5.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 194,940 kB
  • sloc: cpp: 186,508; python: 40,969; ansic: 1,164; xml: 719; makefile: 32; sh: 7
file content (47 lines) | stat: -rw-r--r-- 1,833 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import freeOrionAIInterface as fo

from CombatRatingsAI._ship_combat_stats import ShipCombatStats, get_ship_combat_stats
from common.fo_typing import FleetId
from freeorion_tools import combine_ratings
from freeorion_tools.caching import cache_for_current_turn


class FleetCombatStats:
    """Stores combat related stats of the fleet."""

    def __init__(self, fleet_id: FleetId, max_stats=False):
        self._fleet_id = fleet_id
        self._max_stats = max_stats
        self._ship_stats = self._get_stats_from_fleet()

    def get_ship_combat_stats(self) -> list[ShipCombatStats]:
        """Returns list of ShipCombatStats of the fleet."""
        return list(self._ship_stats)

    def get_rating(self, enemy_stats: ShipCombatStats = None) -> float:
        """Calculates the rating of the fleet by combining all its ships ratings.

        :param enemy_stats: enemy to be rated against
        :return: Rating of the fleet
        """
        return combine_ratings(x.get_rating(enemy_stats) for x in self._ship_stats)

    def get_rating_vs_planets(self) -> float:
        return combine_ratings(x.get_rating_vs_planets() for x in self._ship_stats)

    def _get_stats_from_fleet(self):
        """Calculate fleet combat stats (i.e. the stats of all its ships)."""
        universe = fo.getUniverse()
        fleet = universe.getFleet(self._fleet_id)
        if not fleet:
            return []
        return [get_ship_combat_stats(ship_id=ship_id, max_stats=self._max_stats) for ship_id in fleet.shipIDs]


@cache_for_current_turn
def get_fleet_combat_stats(fleet_id, max_stats=False):
    return FleetCombatStats(fleet_id, max_stats=max_stats)


def get_ships_stats_for_fleet(fleet_id, max_stats=False) -> list[ShipCombatStats]:
    return get_fleet_combat_stats(fleet_id, max_stats=max_stats).get_ship_combat_stats()