player.py 3.04 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 12 13
from enum import Enum

class Side(Enum):
14 15 16 17 18 19
	'''
	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
20 21 22 23
	
	@property
	def opposite(self):
		return Side.Right if self==Side.Left else Side.Left 
24

25 26
from database import Database, DatabaseError

27
class Player():
Antoine Lima's avatar
Antoine Lima committed
28 29 30 31 32 33
	__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'
	
34
	def __init__(self, id, rfid, fname, lname, pic_path, stats):
35
		self.id = id
36
		self.rfid = rfid
37 38
		self.fname = fname
		self.lname = lname
Antoine Lima's avatar
Antoine Lima committed
39 40 41
		self.pic_path = pic_path if pic_path else Player._placeholder_pic_path # Default pic if None
		self.stats = stats
	
42
	@staticmethod
Antoine Lima's avatar
Antoine Lima committed
43 44 45 46 47 48
	def fromRFID(rfid):
		db = Database.instance()
		
		try:
			# Retrieve generic informations
			id, fname, lname, pic = db.select_one(Player.__query_infos, rfid)
49
			
Antoine Lima's avatar
Antoine Lima committed
50
			# Retrieve stats
Antoine Lima's avatar
Antoine Lima committed
51 52 53
			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
54
			
55 56 57 58 59
			for key, val in stats.items():
				if val==None:
					stats[key] = 0
			
			return Player(id, rfid, fname, lname, pic, stats)
60
		
Antoine Lima's avatar
Antoine Lima committed
61 62 63
		except DatabaseError as e:
			logging.warn('DB Error: {}'.format(e))
			return PlayerGuest
64 65 66
	
	def displayImg(self, containerWidget):
		containerWidget.setStyleSheet('border-image: url({});'.format(self.pic_path))
67 68 69 70 71 72 73 74
	
	def save(self):
		'''
		Update or create the player in database
		'''
	
	@property
	def name(self):
75
		return '{} {}'.format(self.fname, self.lname.upper())
Antoine Lima's avatar
Antoine Lima committed
76
		
77
	@property
Antoine Lima's avatar
Antoine Lima committed
78
	def stats_property(self):
79 80 81 82 83
		'''
		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
84 85 86 87 88 89
		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']
90
		
Antoine Lima's avatar
Antoine Lima committed
91
		return Stat(self.stats)
92 93 94 95
	
	@staticmethod
	def allPlayers():
		return [Player.fromRFID(rfid) for rfid, in Database.instance().select_all_rfid()]
96

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