player.py 2.59 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
from enum import Enum
Antoine Lima's avatar
Antoine Lima committed
13
from database import Database, DatabaseError
14 15

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

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

83
PlayerGuest = Player.fromRFID(-1)
Antoine Lima's avatar
Antoine Lima committed
84
PlayerEmpty = Player(-1, '', '', Player._placeholder_pic_path, {'time_played':'', 'goals_scored':'', 'games_played':'', 'victories': ''})