File: pilot_rating.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 (69 lines) | stat: -rw-r--r-- 1,780 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from collections.abc import Mapping, Sequence
from logging import info

from common.fo_typing import PlanetId
from empire.survey_lock import survey_universe_lock
from freeorion_tools.caching import cache_for_current_turn


def set_pilot_rating_for_planet(pid: PlanetId, pilot_rating: float):
    """
    Set pilot rating for planet.

    Warning! Temporal coupling.
    All calls of this function should be done before using of this information.
    """
    _get_pilot_ratings()[pid] = pilot_rating


@survey_universe_lock
def get_rating_for_planet(pid: PlanetId) -> float:
    return get_pilot_ratings().get(pid, 0)


@survey_universe_lock
def get_pilot_ratings() -> Mapping[PlanetId, float]:
    return _get_pilot_ratings()


@cache_for_current_turn
def _get_pilot_ratings() -> dict[PlanetId, float]:
    """
    Return mutable state.
    """
    return {}


class _Summary:
    def __init__(self):
        self.best_pilot_rating = 1e-8
        self.medium_pilot_rating = 1e-8


@cache_for_current_turn
def _get_pilot_summary() -> _Summary:
    summary = _Summary()
    rating_list = sorted(get_pilot_ratings().values(), reverse=True)
    _summarize_pilot_ratings(summary, rating_list)
    return summary


@survey_universe_lock
def best_pilot_rating() -> float:
    return _get_pilot_summary().best_pilot_rating


@survey_universe_lock
def medium_pilot_rating() -> float:
    return _get_pilot_summary().medium_pilot_rating


def _summarize_pilot_ratings(summary: _Summary, ratings: Sequence[float]):
    if not ratings:
        return
    info("_summarize_pilot_ratings")
    summary.best_pilot_rating = ratings[0]
    if len(ratings) == 1:
        summary.medium_pilot_rating = ratings[0]
    else:
        summary.medium_pilot_rating = ratings[(1 + int(len(ratings) // 5))]