Skip to content

Commit

Permalink
Merge pull request #568 from rafsaf/fix-export-troops
Browse files Browse the repository at this point in the history
Fix for export troops
  • Loading branch information
rafsaf authored Dec 2, 2024
2 parents 81e78c7 + 634d2bf commit 0dc22b3
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 56 deletions.
12 changes: 6 additions & 6 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
authors = ["rafsaf <[email protected]>"]
description = "Tribal Wars Planer django app, professional tool for creating outlines for off-game coordinators."
name = "tribal_wars_planer"
version = "5.8.1"
version = "5.8.2"

[tool.poetry.dependencies]
python = ">=3.13,<3.14"
Expand All @@ -18,6 +18,10 @@ beautifulsoup4 = "^4.12.3"
botocore = "^1.35.25"
cython = "^3.0.11"
diskcache = "^5.6.3"
django-otp-yubikey = "^1.1.0"
django-timezone-field = "^7.0"
django-two-factor-auth = { extras = ["phonenumberslite"], version = "^1.17.0" }
drf-spectacular = "^0.27.2"
fpdf = "^1.7.2"
numpy = "^2.1.1"
prometheus-client = "^0.21.0"
Expand All @@ -27,14 +31,10 @@ python-dateutil = "^2.9.0"
python-dotenv = "^1.0.1"
schedule = "^1.2.1"
scipy = "^1.14.1"
sentry-sdk = { extras = ["django"], version = "^2.18.0" }
setuptools = "^75.1.0"
stripe = "^11.1.0"
uwsgi = "^2.0.27"
django-otp-yubikey = "^1.1.0"
django-two-factor-auth = { extras = ["phonenumberslite"], version = "^1.17.0" }
sentry-sdk = { extras = ["django"], version = "^2.18.0" }
drf-spectacular = "^0.27.2"
django-timezone-field = "^7.0"

[tool.poetry.group.dev.dependencies]
coverage = "^7.6.1"
Expand Down
20 changes: 16 additions & 4 deletions utils/basic/army.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ class WorldEvidence(NamedTuple):


class ArmyIndexDict(TypedDict):
coord: int
village_points: int
enroute_text: int | None
spear: int
swordsman: int
axeman: int
Expand All @@ -64,7 +67,7 @@ class ArmyIndexDict(TypedDict):

class TroopsIndex:
def __init__(self, army: Literal["army", "defence"]) -> None:
self.result_mapping: dict[WorldEvidence, dict[str, int | None]] = {
self.result_mapping: dict[WorldEvidence, ArmyIndexDict] = { # type: ignore
WorldEvidence(1, 1, 1): {},
WorldEvidence(1, 1, 0): {},
WorldEvidence(0, 1, 1): {},
Expand All @@ -75,11 +78,16 @@ def __init__(self, army: Literal["army", "defence"]) -> None:
WorldEvidence(0, 0, 0): {},
}
for evidence in self.result_mapping:
troops_indexes = self.result_mapping[evidence]
troops_indexes["coord"] = 0
troops_indexes["village_points"] = 1
if army == "army":
start_index = 2
troops_indexes["enroute_text"] = None
else:
start_index = 3
troops_indexes = self.result_mapping[evidence]
troops_indexes["enroute_text"] = 2

for troop in TROOPS_TYPES:
if not evidence.archer and troop in {"archer", "mounted_archer"}:
troops_indexes[troop] = None
Expand Down Expand Up @@ -201,7 +209,7 @@ def army_value(self, index: int | None):
@property
def coord(self) -> str:
"""Coords of village"""
return self.text_army[0]
return self.text_army[self.index_dict["coord"]]

@cached_property
def nobleman(self):
Expand Down Expand Up @@ -290,7 +298,11 @@ def __init__(self, text_army: str, evidence):

@property
def deff_collection_text(self) -> str:
return self.text_army[1]
if self.index_dict["enroute_text"] is None:
raise RuntimeError(
"index dict cannot be none: %s %s", self.text_army, self.world_evidence
)
return self.text_army[self.index_dict["enroute_text"]]

def clean_init(
self,
Expand Down
66 changes: 20 additions & 46 deletions utils/basic/info_generatation.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,10 @@

from base import models
from utils import basic
from utils.basic.army import ARMY_INDEX, DEFENCE_INDEX, Army, ArmyIndexDict


class OutlineInfo:
evidence_dictionary: dict[tuple[int, int, int], int] = {
(1, 1, 1): 16,
(1, 1, 0): 15,
(0, 1, 1): 15,
(1, 0, 1): 14,
(1, 0, 0): 13,
(0, 0, 1): 13,
(0, 1, 0): 14,
(0, 0, 0): 12,
}

def __init__(self, outline: models.Outline) -> None:
"""
Generate basic informations about outline like targets coords
Expand All @@ -47,9 +37,7 @@ def __init__(self, outline: models.Outline) -> None:
self.ruin_message: dict[int, str] = defaultdict(str)
self.fake_message: dict[int, str] = defaultdict(str)
self.target_message: dict[int, str] = defaultdict(str)
self.world_evidence: tuple[int, int, int] = basic.world_evidence(
self.outline.world
)
self.world_evidence = basic.world_evidence(self.outline.world)
self.order_counter = {
"ruins": 0,
"fakes": 0,
Expand Down Expand Up @@ -186,57 +174,44 @@ def show_sum_up(self):
def parse_weight_to_army_line(
weight_max: models.WeightMaximum,
line_lst: list[str],
noble_index: int,
catapult_index: int,
index: ArmyIndexDict,
deff_collection_text: str | None = None,
) -> str:
army_line_lst = line_lst.copy()
army_line_lst[0] = str(weight_max.start) + ","
army_line_lst[3] = str(weight_max.off_left - weight_max.catapult_left * 8) + ","
army_line_lst[noble_index] = str(weight_max.nobleman_left) + ","
army_line_lst[catapult_index] = str(weight_max.catapult_left) + ","
army_line_lst[index["coord"]] = weight_max.start
army_line_lst[index["axeman"]] = str(
weight_max.off_left - weight_max.catapult_left * 8
)
army_line_lst[index["nobleman"]] = str(weight_max.nobleman_left)
army_line_lst[index["catapult"]] = str(weight_max.catapult_left)

if deff_collection_text is not None:
army_line_lst.insert(1, f"{deff_collection_text},")
army_line_lst.pop()
if deff_collection_text is not None and index["enroute_text"] is not None:
army_line_lst[index["enroute_text"]] = deff_collection_text

return "".join(army_line_lst)
return ",".join(army_line_lst) + ","

def show_export_troops(self) -> str:
sum_text: str = ""
line_length: int = self.evidence_dictionary[self.world_evidence]

line_lst: list[str] = ["0," for _ in range(line_length)]

catapult_index: int
noble_index: int
army_index_dict = ARMY_INDEX.get_index_dict(self.world_evidence)
defence_index_dict = DEFENCE_INDEX.get_index_dict(self.world_evidence)

if self.world_evidence[1] == 0:
catapult_index = 8
if self.world_evidence[0] == 0:
noble_index = 9
else:
noble_index = 10
elif self.world_evidence[0] == 0:
catapult_index = 10
noble_index = 11
else:
catapult_index = 10
noble_index = 12
line_lst: list[str] = [
"0" for _ in range(min(Army.EVIDENCE_DICTIONARY[self.world_evidence]))
]

for weight_max in models.WeightMaximum.objects.filter(
outline=self.outline
).iterator():
if self.outline.input_data_type == self.outline.ARMY_COLLECTION:
sum_text += self.parse_weight_to_army_line(
weight_max, line_lst, noble_index, catapult_index
weight_max, line_lst, index=army_index_dict
)
else:
sum_text += self.parse_weight_to_army_line(
weight_max,
line_lst,
noble_index,
catapult_index,
index=defence_index_dict,
deff_collection_text=self.outline.deff_collection_text_in_village,
)
sum_text += "\r\n"
Expand All @@ -250,8 +225,7 @@ def show_export_troops(self) -> str:
sum_text += self.parse_weight_to_army_line(
weight_max,
line_lst,
noble_index,
catapult_index,
index=defence_index_dict,
deff_collection_text=self.outline.deff_collection_text_enroute,
)

Expand Down

0 comments on commit 0dc22b3

Please sign in to comment.