forked from stefan2200/TWB
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmanager.py
148 lines (134 loc) · 6.12 KB
/
manager.py
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import json
import os
from game.reports import ReportCache
from game.attack import AttackCache
class VillageManager:
@staticmethod
def farm_manager(verbose=False):
with open("config.json", "r") as f:
config = json.load(f)
if verbose:
print("[Farm Manager] Villages: %d" % len(config["villages"]))
attacks = AttackCache.cache_grab()
reports = ReportCache.cache_grab()
if verbose:
print("[Farm Manager] Reports: %d" % len(reports))
print("[Farm Manager] Farms: %d" % len(attacks))
t = {"wood": 0, "iron": 0, "stone": 0}
for farm in attacks:
data = attacks[farm]
num_attack = []
loot = {"wood": 0, "iron": 0, "stone": 0}
total_loss_count = 0
total_sent_count = 0
for rep in reports:
if reports[rep]["dest"] == farm and reports[rep]["type"] == "attack":
for unit in reports[rep]["extra"]["units_sent"]:
total_sent_count += reports[rep]["extra"]["units_sent"][unit]
for unit in reports[rep]["losses"]:
total_loss_count += reports[rep]["losses"][unit]
try:
res = reports[rep]["extra"]["loot"]
for r in res:
loot[r] = loot[r] + int(res[r])
t[r] = t[r] + int(res[r])
num_attack.append(reports[rep])
except:
pass
percentage_lost = 0
if total_sent_count > 0:
percentage_lost = total_loss_count / total_sent_count * 100
perf = "Normal ".rjust(15)
if data["high_profile"]:
perf = "High Profile ".rjust(15)
if "low_profile" in data and data["low_profile"]:
perf = "Low Profile ".rjust(15)
if verbose:
print(
"%sFarm village %s attacked %d times - Total loot: %s - Total units lost: %s (%s)"
% (
perf,
farm,
len(num_attack),
str(loot),
str(total_loss_count),
str(percentage_lost),
)
)
if len(num_attack):
total = 0
for k in loot:
total += loot[k]
if len(num_attack) > 3:
if total / len(num_attack) < 100 and (
"low_profile" not in data or not data["low_profile"]
):
if verbose:
print(
"Farm %s has very low resources (%d avg total), extending farm time"
% (farm, total / len(num_attack))
)
data["low_profile"] = True
AttackCache.set_cache(farm, data)
elif total / len(num_attack) >= 100 and (
"low_profile" in data and data["low_profile"]
):
if verbose:
print(
"Farm %s had very low resources, now back up to normal? (%d avg total), resetting farm time"
% (farm, total / len(num_attack))
)
data["low_profile"] = False
AttackCache.set_cache(farm, data)
elif total / len(num_attack) > 500 and (
"high_profile" not in data or not data["high_profile"]
):
if verbose:
print(
"Farm %s has very high resources (%d avg total), setting to high profile"
% (farm, total / len(num_attack))
)
data["high_profile"] = True
AttackCache.set_cache(farm, data)
if percentage_lost > 20 and not data["low_profile"]:
print(
f"[Farm Manager] Dangerous {percentage_lost} percentage lost units! Extending farm time"
)
data["low_profile"] = True
data["high_profile"] = False
AttackCache.set_cache(farm, data)
# if percentage_lost > 50 and len(num_attack) > 10:
# print("[Farm Manager] Farm seems too dangerous/ unprofitable to farm. Setting safe to false!")
# data["safe"] = False
# AttackCache.set_cache(farm, data)
for report in reports:
r = reports[report]
total_loss_count = 0
if r["type"] == "scout" or r["type"] == "attack":
if r["losses"] != {}:
for unit in r["losses"]:
total_loss_count += (
r["losses"][unit] * 2
if unit == "light"
else r["losses"][unit]
)
if total_loss_count > 10 and verbose:
print(
f"[Farm Manager] Dangerous: {r} -> {total_loss_count} total loss count, extending farm time"
)
if total_loss_count > 10:
data["low_profile"] = True
AttackCache.set_cache(farm, data)
if verbose:
print("[Farm Manager] Total loot: %s" % t)
list_of_files = sorted(
["./cache/reports/" + f for f in os.listdir("./cache/reports/")],
key=os.path.getctime,
)
print(f"Found {len(list_of_files)} files")
while len(list_of_files) > 350:
oldest_file = list_of_files.pop(0)
print(f"[Farm Manager] Delete old report ({oldest_file})")
os.remove(os.path.abspath(oldest_file))
if __name__ == "__main__":
VillageManager.farm_manager(verbose=True)