forked from rrenaud/dominionstats
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgoal_stats.py
79 lines (64 loc) · 2.52 KB
/
goal_stats.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
#!/usr/bin/python
# Module-level logging instance
import logging
log = logging.getLogger(__name__)
log.addHandler(logging.NullHandler())
import collections
import operator
import dominionstats.utils.log
import goals
import incremental_scanner
import utils
def main(parsed_args):
db = utils.get_mongo_database()
goal_db = db.goals
gstats_db = db.goal_stats
all_goals = goals.goal_check_funcs.keys()
total_pcount = collections.defaultdict(int)
goal_scanner = incremental_scanner.IncrementalScanner('goals', db)
stat_scanner = incremental_scanner.IncrementalScanner('goal_stats', db)
if not parsed_args.incremental:
log.warning('resetting scanner and db')
stat_scanner.reset()
gstats_db.remove()
log.info("Starting run: %s", stat_scanner.status_msg())
# TODO: The following logic doesn't work now that goal calculation doesn't happen with a scanner.
# if goal_scanner.get_max_game_id() == stat_scanner.get_max_game_id():
# log.info("Stats already set! Skip")
# exit(0)
log.info('all_goals %s', all_goals)
for goal_name in all_goals:
log.info("Working on %s", goal_name)
found_goals_cursor = goal_db.find({'goals.goal_name': goal_name},
{'goals.player': 1, '_id': 0})
total = found_goals_cursor.count()
log.info("Found %d instances of %s", total, goal_name)
pcount = collections.defaultdict(int)
for goal in found_goals_cursor:
player = goal['goals'][0]['player']
pcount[player] += 1
total_pcount[player] += 1
psorted = sorted(pcount.iteritems(), key=operator.itemgetter(1),
reverse=True)
top = []
leaders = 0
i = 0
while leaders < 3 and i < len(psorted):
(player, count) = psorted[i]
players = [player]
i += 1
while i < len(psorted) and psorted[i][1] == count:
players.append(psorted[i][0])
i += 1
leaders += len(players)
top.append((players, count))
mongo_val = {'_id': goal_name, 'count': total, 'top': top}
gstats_db.save(mongo_val)
stat_scanner.set_max_game_id(goal_scanner.get_max_game_id())
stat_scanner.save()
log.info("Ending run: %s", stat_scanner.status_msg())
if __name__ == '__main__':
parser = utils.incremental_max_parser()
args = parser.parse_args()
dominionstats.utils.log.initialize_logging(args.debug)
main(args)