/**********************************************************************
 *
 *   FreeDoko a Doppelkopf-Game
 *    
 *   Copyright (C) 2001-2004  by Diether Knof and Borg Enders
 *
 *   This program is free software; you can redistribute it and/or 
 *   modify it under the terms of the GNU General Public License as 
 *   published by the Free Software Foundation; either version 2 of 
 *   the License, or (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details. 
 *   You can find this license in the file 'gpl.txt'.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, 
 *   MA  02111-1307  USA
 *
 *  Contact:
 *    Diether Knof dknof@gmx.de
 *    Borg Enders  borg@borgsoft.de
 *
 **********************************************************************/

#include "playersDb.h"

/**
 **
 ** Constructor
 **
 ** @param	-
 **
 ** @return	-
 ** 
 ** @author	Borg Enders
 **
 ** @version	0.6.1
 **
 **/
PlayersDb::PlayersDb()
  : games_(),
  specialpoints_(),
  averageGamePoints_( 0 ),
  averageGameTrickPoints_( 0 ),
totalGames_( 0 ) 
{
  games_.resize( GAMETYPE::LAST + 1 );
  specialpoints_.resize( SPECIALPOINT::LAST + 1 );
}

/**
 **
 ** Constructor
 **
 ** @param	p	players db to copy
 **
 ** @return	-
 ** 
 ** @author	Borg Enders
 **
 ** @version	0.6.1
 **
 **/
PlayersDb::PlayersDb( PlayersDb& p )
  : games_( p.games_ ),
  specialpoints_( p.specialpoints_ ),
  averageGamePoints_( p.averageGamePoints_ ),
  averageGameTrickPoints_( p.averageGameTrickPoints_ ),
totalGames_( p.totalGames_ ) 
{
}

/**
 **
 ** adds the game to the db
 **
 ** @param	p	player
 ** @param	g	gamepoints
 **
 ** @return	-
 ** 
 ** @author	Borg Enders
 **
 ** @version	0.6.1
 **
 **/
void
PlayersDb::evaluate( const Player& p, const GamePoints& g )
{
  if( g.winnerteam() == p.team() )
  {

    games_[g.gametype()].hasWon();
    rank.add( Ranking( 1.0 ) );
    averageGamePoints_ = (( averageGamePoints_ * totalGames_ ) + g.points() )
      / ( totalGames_ + 1 );
  } else
  {
    games_[g.gametype()].hasLost();
    rank.add( Ranking( -1.0 ) );
  }

  averageGameTrickPoints_ = 
    ( ( averageGameTrickPoints_ * totalGames_ ) + g.playerspoints( p.no() ) ) 
    / (totalGames_+1);
  totalGames_ ++;

  Specialpointsvector spv = g.pointlist();  

  for( unsigned i = 0; i < spv.size(); i++ ) {
    if ((spv[i].player_get_no == p.no() )
	|| (spv[i].player_get_no == UINT_MAX) )
      if( spv[i].counting_team == p.team() ) {
	specialpoints_[ spv[i].type ].hasWon();
      }
    if ((spv[i].player_of_no == p.no() )
	|| (spv[i].player_of_no == UINT_MAX) )
      if( spv[i].counting_team != p.team() ) {
	specialpoints_[ spv[i].type ].hasLost();
      }
  }

  return ;
} // virtual void PlayerDb::evaluate( const Player& p, const GamePoints& g )
