-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
134 lines (119 loc) · 6.87 KB
/
main.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
from pywebio import start_server, config, session
from pywebio.input import input, FLOAT
from pywebio.output import *
from pywebio.output import *
import json
from functools import partial
PORT = 8080
config(theme="sketchy", title="LOL Team Challenge Helper", description="英雄聯盟挑戰系統陣容小幫手",)
def printJSON(data):
print(json.dumps(data, ensure_ascii=False, indent=4))
def main():
with open("data.json", "r", encoding='utf-8') as f:
chalList = json.load(f)
with open("champ.json", "r", encoding='utf-8') as f:
champDict = json.load(f, object_hook=lambda d: {int(k) if k.isdigit() else k: v for k, v in d.items()})
chalNameList = [chal['name'] for chal in chalList]
selectList = []
isMobile = session.info.user_agent.is_mobile
COLNUM = 2 if isMobile else 4
def onSelect(action, choice):
clear(scope='display')
# 點擊反應
if action == "select":
if choice not in selectList:
selectList.append(choice)
if action == "remove":
if choice in selectList:
selectList.remove(choice)
# 產生新的選單,並顯示
putList = []
for i in range(len(chalList)):
if i % COLNUM ==0: curList = []
if i in selectList:
curList += [f"{chalList[i]['name']}{'' if chalList[i]['all5'] else '³'}", put_buttons([dict(label='✔', value='remove', color='dark')], onclick=partial(onSelect, choice=i), scope='select')]
else:
curList += [f"{chalList[i]['name']}{'' if chalList[i]['all5'] else '³'}", put_buttons([dict(label='✔', value='select', color='light')], onclick=partial(onSelect, choice=i), scope='select')]
if i % COLNUM == COLNUM-1 or i==29: putList.append(curList)
clear('select')
put_table([['名稱', '選擇']*COLNUM]+putList, scope='select')
# 產生交集列表
if selectList:
champSet, avaiSet= -1, -1
for select in selectList:
if champSet == -1:
champSet = set(chalList[select]['idList'])
else:
champSet = champSet & set(chalList[select]['idList'])
if chalList[select]['all5']:
if avaiSet == -1:
avaiSet = set(chalList[select]['idList'])
else:
avaiSet = avaiSet & set(chalList[select]['idList'])
champList = sorted(list(champSet), key= lambda x: (champDict[x][1], x)) # 照英文排序
champListZH = [ champDict[champ][0] for champ in champList ]
#put_text(f"{','.join(champListZH)}", scope="display")
if selectList:
for select in selectList:
imageUrl = chalList[select]['icon'].replace("/lol-game-data/assets", "https://raw.communitydragon.org/pbe/plugins/rcp-be-lol-game-data/global/default")
put_image(imageUrl, scope='display', width= ("20%" if isMobile else "10%") )
put_markdown(f"## 目前選擇:{'、'.join([chalList[i]['name'] for i in selectList])}", scope='display')
put_markdown(f"## 符合「所有條件」的英雄({len(champListZH)}):`{','.join(champListZH)}`", scope='display')
put_collapse('顯示/隱藏英雄圖示', [put_image(f"https://raw.communitydragon.org/pbe/plugins/rcp-be-lol-game-data/global/default/v1/champion-icons/{champId}.png", scope='display',
width= ("18%" if isMobile else "13.5%")) for champId in champList ] , open=True, scope="display")
if avaiSet != -1 and (avaiSet - champSet):
diffChampList = sorted(list(avaiSet - champSet), key= lambda x: (champDict[x][1], x)) # 照英文排序
diffChampListZH = [ champDict[champ][0] for champ in diffChampList ]
put_markdown(f"## 符合「必選條件」,但不符合所有條件的英雄({len(diffChampListZH)}):`{','.join(diffChampListZH)}`", scope='display')
put_collapse('顯示/隱藏英雄圖示', [put_image(f"https://raw.communitydragon.org/pbe/plugins/rcp-be-lol-game-data/global/default/v1/champion-icons/{champId}.png", scope='display',
width= ("18%" if isMobile else "13.5%")) for champId in diffChampList ] , open=True, scope="display")
if len(selectList) > 1:
countDict = {key: [] for key in champDict.keys()}
selectCnt = 0
for select in selectList:
if not chalList[select]['all5']:
tmpSet = avaiSet & set(chalList[select]['idList']) if avaiSet != -1 else set(chalList[select]['idList'])
tmpList = sorted(list(tmpSet), key= lambda x: (champDict[x][1], x)) # 照英文排序
tmpListZH = [ champDict[champ][0] for champ in tmpList ]
put_markdown(f"### `{chalList[select]['name']}` 需要從以下{len(tmpListZH)}英雄中選擇至少3個英雄:`{','.join(tmpListZH)}`", scope='display')
put_collapse('顯示/隱藏英雄圖示', [put_image(f"https://raw.communitydragon.org/pbe/plugins/rcp-be-lol-game-data/global/default/v1/champion-icons/{champId}.png", scope='display',
width= ("18%" if isMobile else "13.5%")) for champId in tmpList ] , open=False, scope="display")
for champId in tmpList:
countDict[champId].append(chalList[select]['name'])
#put_image(f"https://raw.communitydragon.org/pbe/plugins/rcp-be-lol-game-data/global/default/v1/champion-icons/{champId}.png", scope='display')
selectCnt += 1
availDict = {count: [] for count in range(selectCnt-1, 1, -1)}
for champId in countDict:
if len(countDict[champId]) in availDict.keys():
availDict[len(countDict[champId])].append(champId)
for availCnt in availDict:
tmpList = sorted(list(availDict[availCnt]), key= lambda x: (champDict[x][1], x)) # 照英文排序
tmpListZH = [ champDict[champ][0] for champ in tmpList ]
if len(tmpList):
put_markdown(f"### 以下{len(tmpList)}個英雄符合{selectCnt}個「可選條件」中的{availCnt}個條件,推薦使用:`{','.join(tmpListZH)}`", scope='display')
put_collapse('顯示/隱藏英雄圖示', [put_image(f"https://raw.communitydragon.org/pbe/plugins/rcp-be-lol-game-data/global/default/v1/champion-icons/{champId}.png", scope='display',
width= ("18%" if isMobile else "13.5%")) for champId in tmpList ] , open=True, scope="display")
putList = []
for i in range(len(chalList)):
if i % COLNUM ==0: curList = []
curList += [f"{chalList[i]['name']}{'' if chalList[i]['all5'] else '³'}", put_buttons([dict(label='✔', value='select', color='light')], onclick=partial(onSelect, choice=i))]
if i % COLNUM == COLNUM-1 or i==29: putList.append(curList)
clear()
put_scope('select')
put_table([['名稱', '選擇']*COLNUM]+putList, scope='select')
put_scope('display')
if __name__ == '__main__':
import argparse
from pywebio.platform.tornado_http import start_server as start_http_server
from pywebio import start_server as start_ws_server
parser = argparse.ArgumentParser()
parser.add_argument("-p", "--port", type=int, default=8080)
parser.add_argument("--http", action="store_true", default=False, help='Whether to enable http protocol for communicates')
args = parser.parse_args()
if args.http:
start_http_server(main, port=args.port)
else:
# Since some cloud server may close idle connections (such as heroku),
# use `websocket_ping_interval` to keep the connection alive
start_ws_server(main, port=args.port, websocket_ping_interval=30)
#start_server(main)