Skip to content

_war_fsm

_war_fsm

War state machine for tracking war transitions.

WarStateMachine

WarStateMachine(initial_state: WarState = NOT_IN_WAR)

Tracks war state transitions for a single clan.

The CoC API returns war state as a string in clan_current_war(). This FSM detects when the state actually changes (vs. the same state being reported on consecutive polls).

Source code in cocapi/events/_war_fsm.py
16
17
def __init__(self, initial_state: WarState = WarState.NOT_IN_WAR) -> None:
    self.state = initial_state

transition

transition(raw_state: str) -> WarState | None

Attempt a state transition.

PARAMETER DESCRIPTION
raw_state

The state field from clan_current_war() response.

TYPE: str

RETURNS DESCRIPTION
WarState | None

The new WarState if a transition occurred, or None if the state

WarState | None

is unchanged or the raw_state is unrecognized.

Source code in cocapi/events/_war_fsm.py
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
def transition(self, raw_state: str) -> WarState | None:
    """Attempt a state transition.

    Args:
        raw_state: The ``state`` field from ``clan_current_war()`` response.

    Returns:
        The new WarState if a transition occurred, or None if the state
        is unchanged or the raw_state is unrecognized.
    """
    try:
        new_state = WarState(raw_state)
    except ValueError:
        return None

    if new_state == self.state:
        return None

    self.state = new_state
    return new_state