-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchamporder.py
More file actions
executable file
·132 lines (112 loc) · 4.23 KB
/
champorder.py
File metadata and controls
executable file
·132 lines (112 loc) · 4.23 KB
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
#!/usr/bin/python
import argparse
import mmap
import pydot
import re
import sys, os
from collections import defaultdict
import Image
LOGS_FOLDER = 'Game - R3d Logs'
''' Search the entire computer recursively for the League Logs Directory '''
def find_log_dir():
# Determine operating system to locate root folder. This feels dirty.
# Please tell me if you find a better way.
rootdir = ''
if os.name == 'posix':
rootdir = '/'
elif os.name == 'nt':
rootdir = 'C:\\'
else:
sys.exit('''Operating system not recognized - please enter the logs
path manually''')
# Recursive find
for root, dirs, files in os.walk('/'):
if LOGS_FOLDER in dirs:
return os.path.join(root, LOGS_FOLDER)
# Nothing found
return None
''' Parse the summoner names input and split it to a list '''
def parse_names(raw_names):
result = raw_names.split(',')
result = map((lambda x: x.strip()), result) # strip whitespace
return result
''' Search the logs in the given path for the given summoner names and record
the order of champions played '''
def get_order(summoners, path):
# Find the acutal log files
logs = os.listdir(path)
logs = filter((lambda x: x.endswith('.txt')), logs)
logs.sort()
champion_order = []
# Iterate over logs
for log in logs:
# Open and mmap the file
fn = os.path.join(path,log)
size = os.stat(fn).st_size
f = open(fn)
data = mmap.mmap(f.fileno(), size, access=mmap.ACCESS_READ)
pattern = re.compile(r'(\w+)\(\d+\) created for ' + summoners[0])
result = pattern.search(data)
if result:
champion = result.groups()[0]
champion_order.append(champion)
return champion_order
''' Parse through the champion order, returning a DOT file showing a flowchart
of champion selections and a frequency associated with each edge'''
def parse_order(champion_order):
# This is kinda ugly...
ordermap = defaultdict(lambda: defaultdict(lambda: 0))
last = '<<start>>'
total = 0.0
result = ''
for champion in champion_order:
ordermap[last][champion] += 1.0
ordermap[last]['<total>'] += 1.0
last = champion
result += 'digraph champions {\n'
for left,leftmap in ordermap.iteritems():
for right,count in leftmap.iteritems():
if right is not '<total>':
result += '\t' + left + ' -> ' + right + '[label="%.2f%%"]' % \
(count/ordermap[left]['<total>']) + '\n'
result += '}\n'
return result
''' Entry point for the program '''
if __name__ == '__main__':
# argument parsing
#parser = argparse.ArgumentParser()
#parser.add_argument('SUMMONER_NAMES',
# help='''a comma-separated list of all your summoner
# names (if you wish to track more than one)''')
#parser.add_argument('OUTPUT_FILENAME',
# help='''the name of the file to save the graph to''')
#parser.add_argument('-i', '--input',
# help='''path of your logs folder (Game - R3d Logs).
# Leave empty to find automatically (may be slower than
# manual input)''')
#args = parser.parse_args()
args_SUMMONER_NAMES = raw_input('Enter Summoner names (comma-separated): ')
args_OUTPUT_FILENAME = 'temp'
args_input = None
# Get the log directory (or error out appropriately)
log_dir = ''
if args_input:
log_dir = args_input
if not os.path.exists(log_dir):
sys.exit('directory not found')
else:
log_dir = find_log_dir()
if not log_dir:
sys.exit('logs folder not found')
# Parse summoner names
summoner_list = parse_names(args_SUMMONER_NAMES)
print '// Logs Direcoty: %s' % log_dir
print '// Player Summoners: %s' % summoner_list
# Get champion order
champion_order = get_order(summoner_list, log_dir)
print '// Champion Order: %s' % champion_order
dot_data = parse_order(champion_order)
graph = pydot.graph_from_dot_data(dot_data)
graph.write_png(args_OUTPUT_FILENAME + '.png')
img = Image.open(args_OUTPUT_FILENAME + '.png')
img.show()