-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeagueNight.py
More file actions
309 lines (227 loc) · 10.9 KB
/
LeagueNight.py
File metadata and controls
309 lines (227 loc) · 10.9 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
import sys
import json
import re
import time
import configparser
import requests
from PySide2.QtWidgets import QApplication, QMessageBox, QListWidgetItem
from PySide2.QtUiTools import QUiLoader
from PySide2.QtCore import QFile
from LeagueNightClasses import *
def decode_summoner_dto_json(dct):
return SummonerDTO(
dct['profileIconId'],
dct['name'],
dct['puuid'],
dct['summonerLevel'],
dct['revisionDate'],
dct['id'],
dct['accountId'])
class LeagueNight:
# before_main() but not actually main() ¯\_(ツ)_/¯
def __init__(self):
super().__init__()
config = configparser.ConfigParser()
config.read('config.ini')
self.api_key = config['DEFAULT']['RIOT_API_KEY']
self.match_list = []
self.initUI()
# main()
def initUI(self):
app = QApplication(sys.argv)
ui_file = QFile("UI/ui_mainwindow.ui")
ui_file.open(QFile.ReadOnly)
loader = QUiLoader()
self.window = loader.load(ui_file)
ui_file.close()
self.window.usernameEdit.textEdited.connect(self.enable_add_player)
self.window.usernameEdit.returnPressed.connect(self.add_player)
self.window.addPlayerButton.clicked.connect(self.add_player)
self.window.putTeam1Button.clicked.connect(self.add_player_team1)
self.window.putTeam2Button.clicked.connect(self.add_player_team2)
self.window.removePlayerTeam1Button.clicked.connect(self.remove_player_team1)
self.window.removePlayerTeam2Button.clicked.connect(self.remove_player_team2)
self.window.teamAddButton.clicked.connect(self.add_points)
# Other Stuff
self.start_time = time.time()
self.window.show()
sys.exit(app.exec_())
def enable_add_player(self):
if self.window.usernameEdit.text() is not None and self.window.usernameEdit.text() != "":
self.window.addPlayerButton.setEnabled(True)
else:
QMessageBox.warning(
self.window, "Error", "Enter Summoner Name", QMessageBox.Ok)
# Creates player widget for player pool list
def create_player(self, parent, summoner):
loader = QUiLoader()
file = QFile("UI/ui_playerpoolwidget.ui")
file.open(QFile.ReadOnly)
new_player = loader.load(file, parent)
file.close()
# Set the QWidget's summoner
new_player.summoner = summoner
return new_player
# Adds a new player to the player pool list
def add_player(self):
summoner = self.get_summoner(self.window.usernameEdit.text())
if summoner is None:
QMessageBox.warning(self.window, "League Night", "Invalid Summoner Name", QMessageBox.Ok, QMessageBox.NoButton)
return
item = QListWidgetItem(self.window.playerPoolList)
playerWidget = self.create_player(self.window.playerPoolList, summoner)
playerWidget.playerLabel.setText(summoner.name)
playerWidget.pointLabel.setText(str(summoner.points))
item.setSizeHint(playerWidget.sizeHint())
self.window.playerPoolList.addItem(item)
self.window.playerPoolList.setItemWidget(item, playerWidget)
self.window.usernameEdit.setText("")
self.window.addPlayerButton.setEnabled(False)
# Creates player widget for team lists
def add_player_to_team(self, parent, summoner):
file = QFile("UI/ui_playerteamwidget.ui")
file.open(QFile.ReadOnly)
loader = QUiLoader()
PlayerTeamWidget = loader.load(file, parent)
PlayerTeamWidget.summoner = summoner
file.close()
return PlayerTeamWidget
# Check if a Summoner exists on a specific Team List
def check_player_exists_on_team(self, list, summoner):
for i in range(list.count()):
player_widget = list.itemWidget(list.item(i))
if player_widget.summoner.name == summoner.name:
return True
return False
# Checks if players any players are in Summoner Pool
def check_for_players(self):
if self.window.playerPoolList.count() > 0:
return True
return False
# Adds a player from the Player Pool to the Team 1 List
def add_player_team1(self):
if not self.check_for_players():
QMessageBox.warning(self.window, "League Night", "There are currently no Players", QMessageBox.Ok,
QMessageBox.NoButton)
return
team1List = self.window.team1List
playerPoolList = self.window.playerPoolList
players = playerPoolList.selectedItems()
for index in players:
player = playerPoolList.itemWidget(index)
if self.check_player_exists_on_team(self.window.team1List, player.summoner):
QMessageBox.warning(self.window, "League Night", "Player is already on this Team", QMessageBox.Ok,
QMessageBox.NoButton)
elif self.check_player_exists_on_team(self.window.team2List, player.summoner):
QMessageBox.warning(self.window, "League Night", "Player is already on the other Team", QMessageBox.Ok,
QMessageBox.NoButton)
else:
item = QListWidgetItem(team1List)
team1Player = self.add_player_to_team(team1List, player.summoner)
team1Player.playerLabel.setText(player.summoner.name)
item.setSizeHint(team1Player.sizeHint())
team1List.addItem(item)
team1List.setItemWidget(item, team1Player)
# Adds a player from the Player Pool to the Team 2 List
def add_player_team2(self):
if not self.check_for_players():
QMessageBox.warning(self.window, "League Night", "There are currently no Players", QMessageBox.Ok,
QMessageBox.NoButton)
return
team2List = self.window.team2List
playerPoolList = self.window.playerPoolList
players = playerPoolList.selectedItems()
for index in players:
player = playerPoolList.itemWidget(index)
if self.check_player_exists_on_team(self.window.team2List, player.summoner):
QMessageBox.warning(self.window, "League Night", "Player is already on this Team", QMessageBox.Ok,
QMessageBox.NoButton)
elif self.check_player_exists_on_team(self.window.team1List, player.summoner):
QMessageBox.warning(self.window, "League Night", "Player is already on the other Team", QMessageBox.Ok,
QMessageBox.NoButton)
else:
item = QListWidgetItem(team2List)
team2Player = self.add_player_to_team(team2List, player.summoner)
team2Player.playerLabel.setText(player.summoner.name)
item.setSizeHint(team2Player.sizeHint())
team2List.addItem(item)
team2List.setItemWidget(item, team2Player)
# Removes a player from the Team 1 List
def remove_player_team1(self):
team1List = self.window.team1List
for item in team1List.selectedItems():
team1List.takeItem(team1List.row(item))
# Removes a player from the Team 2 List
def remove_player_team2(self):
team2List = self.window.team2List
for item in team2List.selectedItems():
team2List.takeItem(team2List.row(item))
# Tally the points for each team and update the player pool list
def add_points(self):
if not self.check_for_players():
QMessageBox.warning(self.window, "League Night", "There are currently no Players", QMessageBox.Ok,
QMessageBox.NoButton)
return
if QMessageBox.question(self.window, "League Night", "Are all Players correct?", QMessageBox.Yes,
QMessageBox.No) == QMessageBox.No:
return
def add_points_for_team(team_list, team_num) -> List[SummonerStats]:
summoner_stat_list = []
for i in range(team_list.count()):
summoner_widget = team_list.itemWidget(team_list.item(i))
summoner_first_game = False
summoner_support = False
summoner_honor = False
summoner_points = 0
# TODO: Determine point weights
# Make point values configurable
if summoner_widget.checkFirstGame.isChecked():
summoner_first_game = True
summoner_points += 3
summoner_widget.summoner.points += 3
if summoner_widget.checkSupport.isChecked():
summoner_support = True
summoner_points += 3
summoner_widget.summoner.points += 3
if summoner_widget.checkHonor.isChecked():
summoner_honor = True
summoner_points += 3
summoner_widget.summoner.points += 3
summoner_stat = SummonerStats(
summoner_widget.summoner,
summoner_support,
summoner_first_game,
summoner_honor,
summoner_points,
team_num
)
summoner_stat_list.append(summoner_stat)
return summoner_stat_list
# TODO: Go through current match list and check all points from there(for the whole day, since the
# original launch of the program), instead of calculating the last match
def update_points_labels(player_pool_list):
for i in range(player_pool_list.count()):
summoner_widget = player_pool_list.itemWidget(player_pool_list.item(i))
summoner_widget.pointLabel.setText(str(summoner_widget.summoner.points))
team1_stat_list = add_points_for_team(self.window.team1List, 1)
team2_stat_list = add_points_for_team(self.window.team2List, 2)
total_stat_list = team1_stat_list + team2_stat_list
self.match_list.append(Match(time.time(), total_stat_list))
update_points_labels(self.window.playerPoolList)
def get_summoner(self, username: str):
summoner_regex = re.compile('^[0-9\w+ _\.]+$', re.UNICODE)
summoner_name = username
url = "https://na1.api.riotgames.com/lol/summoner/v4/summoners/by-name/" + summoner_name
payload = {'api_key': self.api_key}
if not summoner_regex.match(summoner_name) and len(summoner_name) <= 16:
return None
else:
summoner_name_request = requests.get(url, params=payload)
if summoner_name_request.status_code == 200:
summoner = json.loads(
summoner_name_request.text, object_hook=decode_summoner_dto_json)
return summoner
else:
return None
if __name__ == '__main__':
LeagueNight()