leaderboard.py 4.42 KB
Newer Older
Antoine Lima's avatar
Antoine Lima committed
1
2
3
4
5
6
7
8
9
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Apr 18 18:34:40 2018

@author: Antoine Lima, Leo Reynaert, Domitille Jehenne
"""

import logging
10
from operator import attrgetter
Antoine Lima's avatar
Antoine Lima committed
11

12
13
from PyQt5 import QtWidgets
from PyQt5.QtCore import QTime, Qt, QSize, QItemSelectionModel
Antoine Lima's avatar
Antoine Lima committed
14
15
16

from module import Module
import modules
17
from player import PlayerGuest, Player
Antoine Lima's avatar
Antoine Lima committed
18
from ui.leaderboard_ui import Ui_Form as LeaderboardWidget
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from ui.playerlist_ui import Ui_Form as PlayerListWidget

class LeaderboardItemWidget(QtWidgets.QWidget):
	def __init__(self, parent, player):
		QtWidgets.QWidget.__init__(self, parent)
		self.ui = PlayerListWidget()
		self.ui.setupUi(self)
		
		self.ui.picHolder.setStyleSheet('border-image: url({});'.format(player.pic_path))
		self.ui.lblFName.setText(player.fname)
		self.ui.lblLName.setText(player.lname)
		
		self.ui.lblVictories.setText    (self.ui.lblVictories.text().replace('####',     str(player.stats.victories)))
		self.ui.lblGamesPlayed.setText  (self.ui.lblGamesPlayed.text().replace('####',   str(player.stats.games_played)))
		self.ui.lblGoalsScored.setText  (self.ui.lblGoalsScored.text().replace('####',   str(player.stats.goals_scored)))
		self.ui.lblMinutesPlayed.setText(self.ui.lblMinutesPlayed.text().replace('####', str(player.stats.time_played)))
		
		self.ui.pushButton.clicked.connect(lambda: logging.debug('clicked'))
Antoine Lima's avatar
Antoine Lima committed
37
38
39
40

class LeaderboardModule(Module):
	def __init__(self, parent):
		super().__init__(parent, LeaderboardWidget())
41
42
43
44
45
46
47
48
49
50
51
52
53
		self.players = []
		
		self.ui.rbName.clicked.connect(lambda: self.changeSort(self.ui.rbName))
		self.ui.rbVictories.clicked.connect(lambda: self.changeSort(self.ui.rbVictories))
		self.ui.rbScore.clicked.connect(lambda: self.changeSort(self.ui.rbScore))
		self.ui.rbGamesPlayed.clicked.connect(lambda: self.changeSort(self.ui.rbGamesPlayed))
		self.ui.rbTimePlayed.clicked.connect(lambda: self.changeSort(self.ui.rbTimePlayed))
		
		self.selectedSort = 0
		self.sortMethodRB = [self.ui.rbName, self.ui.rbVictories, self.ui.rbScore, self.ui.rbGamesPlayed, self.ui.rbTimePlayed]
		self.sortMethodAttr = ['lname', 'stats.victories', 'stats.goals_scored', 'stats.games_played', 'stats.time_played']
		
		self.sortMethodRB[self.selectedSort].setChecked(True)
Antoine Lima's avatar
Antoine Lima committed
54
55
56

	def load(self):
		logging.debug('Loading LeaderboardModule')
57
		self.loadList()
Antoine Lima's avatar
Antoine Lima committed
58
59
60

	def unload(self):
		logging.debug('Unloading LeaderboardModule')
61
		self.players = []
Antoine Lima's avatar
Antoine Lima committed
62
63
64

	def other(self, **kwargs):
		logging.debug('Other LeaderboardModule')
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
	
	def changeSort(self, rbSort):
		self.selectedSort = self.sortMethodRB.index(rbSort)
		self.loadList()
	
	def loadList(self):
		if self.players:
			self.ui.listWidget.clear()
		else:
			# Load from DB
			dummy1 = Player(0, 'A', 'E', ':ui/img/placeholder_head.jpg')
			dummy2 = Player(1, 'B', 'D', ':ui/img/placeholder_head.jpg')
			dummy3 = Player(2, 'C', 'C', ':ui/img/placeholder_head.jpg')
			dummy4 = Player(3, 'D', 'B', ':ui/img/placeholder_head.jpg')
			dummy5 = Player(4, 'E', 'A', ':ui/img/placeholder_head.jpg')
			
			dummy2.stats.victories    = 1
			dummy3.stats.time_played  = 1
			dummy4.stats.games_played = 1
			dummy5.stats.goals_scored = 1
			
			self.players = [PlayerGuest, dummy1, dummy2, dummy3, dummy4, dummy5]
		
		self.players.sort(key=attrgetter(self.sortMethodAttr[self.selectedSort]), reverse=True)
		
		for player in self.players:
			item = QtWidgets.QListWidgetItem()
			playerWidget = LeaderboardItemWidget(self.ui.listWidget, player)
			item.setSizeHint(playerWidget.size())
			self.ui.listWidget.addItem(item)
			self.ui.listWidget.setItemWidget(item, playerWidget)
		
		self.ui.listWidget.setCurrentRow(0, QItemSelectionModel.Select)
Antoine Lima's avatar
Antoine Lima committed
98

99
	def keyPressEvent(self, e):
100
101
102
		curRow = self.ui.listWidget.currentRow()
		curSort = self.selectedSort
		
103
		if e.key() == Qt.Key_Escape:
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
			self.handleExit()
			
		elif e.key() == Qt.Key_Up:
			newRow = curRow-1 if curRow!=0 else self.ui.listWidget.count()-1
			self.ui.listWidget.setCurrentRow(newRow, QItemSelectionModel.SelectCurrent)
			
		elif e.key() == Qt.Key_Down:
			newRow = curRow+1 if curRow!=self.ui.listWidget.count()-1 else 0
			self.ui.listWidget.setCurrentRow(newRow, QItemSelectionModel.SelectCurrent)
			
		elif e.key() == Qt.Key_Left:
			newSort = curSort-1 if curSort!=0 else len(self.sortMethodRB)-1
			self.sortMethodRB[newSort].animateClick()
			
		elif e.key() == Qt.Key_Right:
			newSort = curSort+1 if curSort!=len(self.sortMethodRB)-1 else 0
			self.sortMethodRB[newSort].animateClick()
121

122
	def handleExit(self):
Antoine Lima's avatar
Antoine Lima committed
123
		self.switchModule(modules.MenuModule)