-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWarReportLogger.py
96 lines (65 loc) · 3.5 KB
/
WarReportLogger.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
#!/usr/bin/python
import pandas as pd
import datetime
import numpy as np
from collections import defaultdict
import operator
def main_logger(df2,df1):
now = datetime.datetime.now()
#don't understand the following block of code:
ne = (df1 != df2).any(1)
ne_stacked = (df1 != df2).stack()
changed = ne_stacked[ne_stacked]
difference_locations = np.where(df1 != df2)
changed_from = df1.values[difference_locations]
changed_to = df2.values[difference_locations]
changed_df = pd.DataFrame({'from': changed_from, 'to': changed_to}, index=changed.index)
changed_df2 = changed_df.unstack()
# its making some sort of multi-index array but I can't figure out how to slice or work with the data. Unstack atleast gets
#it back into a form I can work with but this seems very inefficient, need to figure out how to work with multi-index df
print(changed_df2)
changed_from2 = changed_df2['from']['type']
changed_to2 = changed_df2['to']['type']
df_changed_from = pd.DataFrame(changed_from2)
df_changed_from = df_changed_from[df_changed_from.type.notnull()]
df_changed_to = pd.DataFrame(changed_to2)
df_changed_to = df_changed_to[df_changed_to.type.notnull()]
war_report_dict = {}
winner_loser_segment_dict = {}
for x in df_changed_to['type'].unique():
personcount = defaultdict(int)
person_segment_list = defaultdict(list)
for y in df_changed_to[df_changed_to['type'] == x].index:
#don't get why the output here is a numpy array:
beatperson_array = df_changed_from[df_changed_from.index == y].values[0]
#converting to string:
beatperson_string = str(beatperson_array[0])
personcount[beatperson_string] += 1
person_segment_list[beatperson_string].append(y)
war_report_dict[x] = personcount
winner_loser_segment_dict[x] = person_segment_list
logfile=open('warlog.csv', 'a+')
for warlord in war_report_dict:
for victim in sorted(war_report_dict[warlord].items(), key=operator.itemgetter(1), reverse=True):
if victim[0] == 'UNCLAIMED':
if victim[1] == 1:
print(warlord,'claimed',victim[1],'unowned territory')
space_list = ' '.join(map(str, winner_loser_segment_dict[warlord][victim[0]]))
line = str(now)+','+warlord+' claimed '+str(victim[1])+' unowned territory,'+space_list
else:
print(warlord,'claimed',victim[1],'unowned territories')
space_list = ' '.join(map(str, winner_loser_segment_dict[warlord][victim[0]]))
line = str(now)+','+warlord+' claimed '+str(victim[1])+' unowned territories,'+space_list
else:
if victim[1] == 1:
print(warlord,'conquered',victim[1],'territory from',victim[0])
space_list = ' '.join(map(str, winner_loser_segment_dict[warlord][victim[0]]))
line = str(now)+','+warlord+' conquered '+str(victim[1])+' territory from '+victim[0]+','+space_list
else:
print(warlord,'conquered',victim[1],'territories from',victim[0])
space_list = ' '.join(map(str, winner_loser_segment_dict[warlord][victim[0]]))
line = str(now)+','+warlord+' conquered '+str(victim[1])+' territories from '+victim[0]+','+space_list
logfile.write(line+'\n')
logfile.close()
if __name__ == "__main__":
main()