player.py 3.05 KB
Newer Older
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
Antoine Lima's avatar
Antoine Lima committed
10 11
import sqlite3

12 13 14
from enum import Enum

class Side(Enum):
15 16 17 18 19 20
	'''
	Values of the enum are used throughout the code for indexing purposes, not to be changed
	'''
	Undef = -1
	Left  = 0
	Right = 1
Antoine Lima's avatar
Antoine Lima committed
21 22 23 24
	
	@property
	def opposite(self):
		return Side.Right if self==Side.Left else Side.Left 
25

26 27
from database import Database, DatabaseError

28
class Player():
Antoine Lima's avatar
Antoine Lima committed
29 30 31 32 33 34
	__query_infos = 'SELECT id, fname, lname, pic FROM Players WHERE rfid==?'
	__query_time_goals_games = 'SELECT SUM(Matchs.duration) AS timePlayed, SUM(Teams.nGoals) AS goalsScored, COUNT(*) AS gamesPlayed FROM Teams INNER JOIN  Matchs ON (Teams.id==Matchs.winningTeam OR Teams.id==Matchs.losingTeam) WHERE (Teams.player1==? OR player2==?)'
	__query_victories = 'SELECT COUNT(*) AS victories FROM Players INNER JOIN Teams ON (Players.id==Teams.player1 OR Players.id==Teams.player2) INNER JOIN  Matchs ON (Teams.id==Matchs.winningTeam) WHERE Players.id==?'

	_placeholder_pic_path = ':ui/img/placeholder_head.jpg'
	
35
	def __init__(self, id, rfid, fname, lname, pic_path, stats):
36
		self.id = id
37
		self.rfid = rfid
38 39
		self.fname = fname
		self.lname = lname
Antoine Lima's avatar
Antoine Lima committed
40 41 42
		self.pic_path = pic_path if pic_path else Player._placeholder_pic_path # Default pic if None
		self.stats = stats
	
43
	@staticmethod
Antoine Lima's avatar
Antoine Lima committed
44 45 46 47 48 49
	def fromRFID(rfid):
		db = Database.instance()
		
		try:
			# Retrieve generic informations
			id, fname, lname, pic = db.select_one(Player.__query_infos, rfid)
50
			
Antoine Lima's avatar
Antoine Lima committed
51 52 53 54
			# Retrieve stats
			stats = {}
			stats['time_played'], stats['goals_scored'], stats['games_played'] = db.select_one(Player.__query_time_goals_games, id, id)
			stats['victories'], = db.select_one(Player.__query_victories, id)
Antoine Lima's avatar
Antoine Lima committed
55
			
56 57 58 59 60
			for key, val in stats.items():
				if val==None:
					stats[key] = 0
			
			return Player(id, rfid, fname, lname, pic, stats)
61
		
Antoine Lima's avatar
Antoine Lima committed
62 63 64
		except DatabaseError as e:
			logging.warn('DB Error: {}'.format(e))
			return PlayerGuest
65 66 67
	
	def displayImg(self, containerWidget):
		containerWidget.setStyleSheet('border-image: url({});'.format(self.pic_path))
68 69 70 71 72 73 74 75
	
	def save(self):
		'''
		Update or create the player in database
		'''
	
	@property
	def name(self):
76
		return '{} {}'.format(self.fname, self.lname.upper())
Antoine Lima's avatar
Antoine Lima committed
77
		
78
	@property
Antoine Lima's avatar
Antoine Lima committed
79
	def stats_property(self):
80 81 82 83 84
		'''
		Compatibility property allowing to access stats as a object member and not dict
		ex: player.stats['victories'] can be accessed with player.stats_property.victories'
		This is mostly used for sorting players in leaderboard.py
		'''
Antoine Lima's avatar
Antoine Lima committed
85 86 87 88 89 90
		class Stat:
			def __init__(self, stats):
				self.victories = stats['victories']
				self.time_played = stats['time_played']
				self.goals_scored = stats['goals_scored']
				self.games_played = stats['games_played']
91
		
Antoine Lima's avatar
Antoine Lima committed
92
		return Stat(self.stats)
93 94 95 96
	
	@staticmethod
	def allPlayers():
		return [Player.fromRFID(rfid) for rfid, in Database.instance().select_all_rfid()]
97

98
PlayerGuest = Player.fromRFID(-1)
99
PlayerEmpty = Player(-1, -42, '', '', Player._placeholder_pic_path, {'time_played':'', 'goals_scored':'', 'games_played':'', 'victories': ''})