-
Notifications
You must be signed in to change notification settings - Fork 0
/
logic.py
295 lines (234 loc) · 8.61 KB
/
logic.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import math
from datetime import datetime, timedelta
from operator import itemgetter
import os
#
SPEED_OF_NOBLE_MAN = 21875 / 10 # s/p
SPEED_OF_RAM = 18750 / 10 # s/p
X = 0 # space for x coordinate
Y = 1 # space for y coordinate
# for all in time type
DAYS = 0
HOURS = 1
MIN = 2
SEC = 3
# for attack list
TYP_ATTACK = 0
ATTACKER_VILLAGE_CORDS = 1
DATE_AND_TIME = 2
ATTACKER_NICK = 3
DEFENDER_VILLAGE_CORDS = 4
DEFENDER_NICK = 5
# added after calculate
WHEN_SENT = 6
DISTANCE = 7
HOW_MANY_ATTACKS = 8
def get_players_list(directory) -> list:
"""
:param directory: to files where are all txt files
:return: list off players
"""
files_list = os.listdir(directory)
files_name = []
for i in files_list:
files_name.append(i[:-4])
return files_name
def get_how_many_attacks_from_attacker_village(list_of_attacks):
"""
:param list_of_attacks: all attacks list
:return: dict {attacker village: int - how many attacks from this village]
"""
attack_dict = {}
for attack in list_of_attacks:
villages_attacks_keys = list(attack_dict.keys())
if attack[ATTACKER_VILLAGE_CORDS] in villages_attacks_keys:
attack_dict[attack[ATTACKER_VILLAGE_CORDS]] += 1
else:
attack_dict[attack[ATTACKER_VILLAGE_CORDS]] = 1
return attack_dict
def more_then_one_attack(dict_attacks_from_village):
"""
:param dict_attacks_from_village:
:return: list villages under attack more than once
"""
more_then_one_attack_list = []
for i in list(dict_attacks_from_village.keys()):
if dict_attacks_from_village[i] > 1:
more_then_one_attack_list.append(i)
return more_then_one_attack_list
def add_sent_time_and_sort_by_this(dict_attacks_from_village, data):
"""
:param dict_attacks_from_village:
:param data: list of all attacks
:return: list sorted by when the attack was sent
"""
list_with_all_information = []
for attack in data:
distance = distance_counting(attack[ATTACKER_VILLAGE_CORDS],
attack[DEFENDER_VILLAGE_CORDS]) # return distance a -> b
attack_travel_time_list = attack_travel_time(distance,
attack[TYP_ATTACK]) # returns list [D, h, m, s]
end_time_object = datetime.strptime(attack[DATE_AND_TIME], '%d.%m.%y %H:%M:%S:%f')
when_sent = end_time_object - timedelta(days=attack_travel_time_list[DAYS],
hours=attack_travel_time_list[HOURS],
minutes=attack_travel_time_list[MIN],
seconds=attack_travel_time_list[SEC])
when_sent = change_time_object_to_string(when_sent)
end_time = change_time_object_to_string(end_time_object)
how_many_attacks = dict_attacks_from_village[attack[ATTACKER_VILLAGE_CORDS]]
list_with_all_information.append([attack[TYP_ATTACK],
attack[ATTACKER_VILLAGE_CORDS],
end_time,
attack[ATTACKER_NICK],
attack[DEFENDER_VILLAGE_CORDS],
attack[DEFENDER_NICK],
when_sent,
str(int(distance)),
how_many_attacks])
sorted_by_send_time = sort_by_date(list_with_all_information)
return sorted_by_send_time
def sort_by_date(list_of_attacks):
"""
:param sort_key: sort key
:param list_of_attacks: list all of atatcks
:return: list sorted my key
"""
sorted_by_send_time = sorted(list_of_attacks, key=itemgetter(DATE_AND_TIME))
return sorted_by_send_time
def distance_counting(start, target):
"""
:param start: attacker village coords
:param target: defender village coords
:return: distance 1 -> 2
"""
start = start.split('|')
target = target.split('|')
y_dif = math.fabs(int(start[X]) - int(target[X]))
x_dif = math.fabs(int(start[Y]) - int(target[Y]))
distance = math.hypot(x_dif, y_dif)
return distance
def attack_travel_time(distance, attack_type):
"""
:param distance:
:param attack_type:
:return: attack travel time for Szlachcic, zwiad and all others like "taran"
"""
if attack_type == "Szlachcic":
duration = distance * SPEED_OF_NOBLE_MAN
time_list = seconds_to_datetime(duration)
return time_list
else:
duration = distance * SPEED_OF_RAM
time_list = seconds_to_datetime(duration)
return time_list
def seconds_to_datetime(secs):
"""
:param secs: time travel in seconds
:return: list [days, hours, minutes, seconds]
"""
days = secs // 86400
hours = (secs - days * 86400) // 3600
minutes = (secs - days * 86400 - hours * 3600) // 60
seconds = secs - days * 86400 - hours * 3600 - minutes * 60
time_list = [days, hours, minutes, seconds]
return time_list
def sort_by_entry_time(list_of_list):
"""
:param list_of_list:
:return:
"""
sorted_by_send_time = sorted(list_of_list, key=itemgetter(WHEN_SENT))
return sorted_by_send_time
def get_list_of_keys_from_list(list_of_list, key):
"""
:param list_of_list:
:param key:
:return:
"""
attacked_villages_list = []
for attack in list_of_list:
if attack[key] in attacked_villages_list:
pass
else:
attacked_villages_list.append(attack[key])
return attacked_villages_list
def add_information_send_no(list_of_list):
"""
change information [how many attacks from attacker_village to with one
:param list_of_list:
:return:
"""
data = get_key_attack_dict(list_of_list, ATTACKER_VILLAGE_CORDS)
data_keys_list = list(data.keys())
for key in data_keys_list:
attack_list = data[key]
for attack_no in range(len(attack_list)):
how_many = attack_list[attack_no][HOW_MANY_ATTACKS]
new_information = str(attack_no + 1) + "/" + str(how_many)
attack_list[attack_no][HOW_MANY_ATTACKS] = new_information
list_of_list = dict_to_list_of_list(data)
return list_of_list
def dict_to_list_of_list(dict_with_list):
"""
change to list of the list from format dict {key: attack list, key: attack list}
:param dict_with_list: key is one part of list
for example {attack_type : [[attack type, 1, 2, 3], [attack type, 2, 2, 3], [attack type, 2, 2, 3]]
:return: return list of the list
for example [[attack type, 1, 2, 3], [attack type, 2, 2, 3], [attack type, 2, 2, 3]]
"""
list_of_list = []
for key in dict_with_list:
attacks_list = dict_with_list[key]
for attack in attacks_list:
list_of_list.append(attack)
return list_of_list
def get_key_attack_dict(list_of_list, key):
"""
:param list_of_list:
:param key:
:return:
"""
attacked_villages_list = get_list_of_keys_from_list(list_of_list, key)
attacks_dict = {}
for i in attacked_villages_list:
attacks_dict[i] = []
for attack in list_of_list:
attacks_dict_keys_list = list(attacks_dict.keys())
if attack[key] in attacks_dict_keys_list:
list_for_def = attacks_dict[attack[key]]
list_for_def.append(attack)
attacks_dict[attack[key]] = list_for_def
else:
attacks_dict[attack[key]] = [[attacks_dict[attack]]]
return attacks_dict
def change_time_object_to_string(time_object):
"""
convert datetime object to str
:param time_object: date in time object
:return: string Y/M/D H:M:S
"""
return time_object.strftime("%Y/%m/%d %H:%M:%S")
def get_sorted_attacks_by_nickname_and_villages_and_sent_time(list_of_list):
"""
:param list_of_list: list of all attacks
:return:
"""
end_format = {}
defenders_nicks_list = get_list_of_keys_from_list(list_of_list, DEFENDER_NICK)
for nick in defenders_nicks_list:
end_format[nick] = []
for attack in list_of_list:
if attack[DEFENDER_NICK] == nick:
end_format[nick].append(attack[DEFENDER_VILLAGE_CORDS])
list_of_end_format = list(end_format.keys())
for i in list_of_end_format:
end_format[i] = list(dict.fromkeys(end_format[i]))
return end_format
def how_many_attacks_by(data):
attackers_dict = {}
for i in data:
if i[3] in attackers_dict:
attackers_dict[i[3]] += 1
else:
attackers_dict[i[3]] = 1
return attackers_dict