-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathskill.py
More file actions
179 lines (166 loc) · 6.21 KB
/
skill.py
File metadata and controls
179 lines (166 loc) · 6.21 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
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
import os
import re
import math
import uuid
import json
import yaml
import hashlib
from datetime import datetime
CUSTOM_DIR_PATH = 'custom'
DATA_DIR_PATH = 'data'
def process(customDirPath, dataDirPath):
print('--skill--')
configFile = open(os.path.join(customDirPath, 'config.yml'), 'r', encoding='utf-8')
configContent = configFile.read()
configFile.close()
configContent = yaml.safe_load(configContent)
# timeFormat = configContent.get('timeFormat', f'%Y/%m/%d')
skillTreeDataPath = os.path.join(customDirPath, configContent['tabs']['skill']['path'])
dataFile = open(skillTreeDataPath, 'r', encoding='utf-8')
dataContent = dataFile.read()
dataFile.close()
skillTreeData = json.loads(dataContent)
# parse skill tree data to nodes structure
options = configContent['tabs']['skill']['options']
radiusStep = options['level_gap']
tau = 2*math.pi
nodes = [{
'label': skillTreeData['label'],
'level': 0,
'parent': None,
'child': []
}]
currentChain = [{
'data': skillTreeData,
'index': 0,
'node': nodes[0]
}]
levelNodes = [[nodes[0]]]
while True:
currentParent = currentChain[-1]
currentIndex = currentParent['index']
currentData = currentParent['data']['child'][currentIndex]
newNode = {
'label': currentData['label'],
'level': len(currentChain),
'parent': currentParent['node'],
'child': []
}
nodes.append(newNode)
currentParent['node']['child'].append(newNode)
while len(levelNodes)-1 < newNode['level']:
levelNodes.append([])
levelNodes[newNode['level']].append(newNode)
if len(currentData['child']) > 0:
currentChain.append({
'data': currentData,
'index': 0,
'node': newNode
})
else:
while currentIndex == len(currentParent['data']['child'])-1:
currentChain.pop()
if len(currentChain) == 0: break
currentParent = currentChain[-1]
currentIndex = currentParent['index']
if len(currentChain) == 0: break
currentChain[-1]['index'] += 1;
for level in range(0, len(levelNodes)):
count = len(levelNodes[level])
for index, node in enumerate(levelNodes[level]):
node['sameLevelCount'] = count
node['sameLevelIndex'] = index
maxX = 0; minX = 0; maxY = 0; minY = 0
for level in range(0, len(levelNodes)):
if level == 0:
for node in levelNodes[0]:
node['x'] = 0
node['y'] = 0
continue
cCoef = 0; sCoef = 0
for node in levelNodes[level]:
x = node['parent']['x']
y = node['parent']['y']
c = math.cos(tau*node['sameLevelIndex']/node['sameLevelCount'])
s = math.sin(tau*node['sameLevelIndex']/node['sameLevelCount'])
cCoef += x*s-y*c; sCoef += x*c+y*s
theta = 0
if cCoef == 0: theta = 0
else:
s = -math.copysign(1, cCoef) * 1/(1+(sCoef/cCoef)**2)
c = -(sCoef/cCoef)*s
theta = math.atan2(s, c)
for node in levelNodes[level]:
c = math.cos(tau*node['sameLevelIndex']/node['sameLevelCount'] + theta)
s = math.sin(tau*node['sameLevelIndex']/node['sameLevelCount'] + theta)
node['x'] = radiusStep*node['level'] * c
node['y'] = radiusStep*node['level'] * s
if node['x'] > maxX: maxX = node['x']
if node['x'] < minX: minX = node['x']
if node['y'] > maxY: maxY = node['y']
if node['y'] < minY: minY = node['y']
# print(nodes)
# use id system to handle the bi-reference structure
for node in nodes:
node['id'] = str(uuid.uuid4())
for node in nodes:
node['parent'] = node['parent']['id'] if node['parent'] is not None else None
newChild = []
for child in node['child']:
newChild.append(child['id'])
node['child'] = newChild
nodesDict = {}
for node in nodes:
nodeId = node['id']
del node['id']
nodesDict[nodeId] = node
outputData = nodesDict
outputContent = json.dumps(outputData)
outputFile = open(os.path.join(dataDirPath, 'skillTreeNodesDict.json'), 'w+', encoding='utf-8')
outputFile.write(outputContent)
outputFile.close()
md5 = hashlib.md5()
md5.update(outputContent.encode('utf-8'))
md5Value = md5.hexdigest()
md5Content = {}
try:
md5File = open(os.path.join(dataDirPath, 'md5.json'), 'r', encoding='utf-8')
md5Content = md5File.read()
md5File.close()
md5Content = json.loads(md5Content)
except: pass
md5Content['skillTreeNodesDict'] = md5Value
md5Content = json.dumps(md5Content)
md5File = open(os.path.join(dataDirPath, 'md5.json'), 'w+', encoding='utf-8')
md5File.write(md5Content)
md5File.close()
# ---
options['maxX'] = maxX
options['minX'] = minX
options['maxY'] = maxY
options['minY'] = minY
outputData = options
outputContent = json.dumps(outputData)
outputFile = open(os.path.join(dataDirPath, 'skillTreeOptions.json'), 'w+', encoding='utf-8')
outputFile.write(outputContent)
outputFile.close()
md5 = hashlib.md5()
md5.update(outputContent.encode('utf-8'))
md5Value = md5.hexdigest()
md5Content = {}
try:
md5File = open(os.path.join(dataDirPath, 'md5.json'), 'r', encoding='utf-8')
md5Content = md5File.read()
md5File.close()
md5Content = json.loads(md5Content)
except: pass
md5Content['skillTreeOptions'] = md5Value
md5Content = json.dumps(md5Content)
md5File = open(os.path.join(dataDirPath, 'md5.json'), 'w+', encoding='utf-8')
md5File.write(md5Content)
md5File.close()
if __name__ == '__main__':
if len(os.sys.argv) > 1:
process(os.sys.argv[1], DATA_DIR_PATH)
else:
process(CUSTOM_DIR_PATH, DATA_DIR_PATH)