//=========== (C) Copyright 1999 Valve, L.L.C. All rights reserved. ===========
//
// The copyright to the contents herein is the property of Valve, L.L.C.
// The contents may be used and/or copied only with the written permission of
// Valve, L.L.C., or in accordance with the terms and conditions stipulated in
// the agreement/contract under which the contents have been supplied.
//
// Purpose:  Implementation of CScoreboard
//
// $Workfile:     $
// $Date:         $
//
//------------------------------------------------------------------------------------------------------
// $Log: $
//
// $NoKeywords: $
//=============================================================================
#include "Scoreboard.h"


//------------------------------------------------------------------------------------------------------
// Function:	CScoreboard::init
// Purpose:	initializes the object
//------------------------------------------------------------------------------------------------------
void CScoreboard::init()
{
}

//------------------------------------------------------------------------------------------------------
// Function:	CScoreboard::generate
// Purpose:	generates intermediate data based on match info
//------------------------------------------------------------------------------------------------------
void CScoreboard::generate()
{
}

//------------------------------------------------------------------------------------------------------
// Function:	CScoreboard::writeHTML
// Purpose:	generates html from the intermediate data generated by generate()
// Input:	html - the html file to write the html code into
//------------------------------------------------------------------------------------------------------
void CScoreboard::writeHTML(CHTMLFile& html)
{
	CPlayerListIterator i;
	int t;
	
	bool teamFound=false;
	
	for (t=0;t<MAX_TEAMS;t++)
	{
		if (!g_pMatchInfo->teamExists(t))
			continue;
		
		if (!teamFound)
		{
			html.write("<p>");
			html.write("<img src=\"%s/scores.gif\">",g_pApp->supportHTTPPath.c_str());
			teamFound=true;
		}

		int totalkills=0;
		int totaldeaths=0;
		double teamrank=0;
		int numplrs=0;
		
		html.write("<table cellpadding=3 cellspacing=0 border=0 class=scores>");
		html.write("<tr class=header>");
		html.write("<td ><font class=boardtext>%s</font></td>",g_pMatchInfo->teamName(t).c_str());
		html.write("<td><font class=boardtext>Kills</font></td>");
		html.write("<td><font class=boardtext>Deaths</font></td>");
		html.write("<td><font class=boardtext>Rank</font></td>");
		html.write("</tr>");
		
		multimap<double,CPlayer> ranksort;
		for (i=g_pMatchInfo->playerBegin();i!=g_pMatchInfo->playerEnd();++i)
		{
			pair<PID,CPlayer> plr=(*i);
			PID pid=plr.first;
			CPlayer& p=plr.second;
			
			if (p.teams.contains(t))
			{	
				double rank=p.perteam[t].rank();
				pair<double,CPlayer> insertme(rank,p);
				ranksort.insert(insertme);
				//ranksort[rank]=p;
			}
		}

		multimap<double,CPlayer>::reverse_iterator i2;
		for (i2=ranksort.rbegin();i2!=ranksort.rend();++i2)
		{
			double rank=(*i2).first;
			CPlayer p=(*i2).second;
			
			if (!p.teams.contains(t))
			{
				continue;
			}	

			html.write("<tr>\n");
			html.write("\n");
			html.write("<td width=140><font class=player%s>%s</font></td>\n",Util::teamcolormap[t],p.name.c_str());
			html.write("<td width=100><font class=boardtext>%li</font></td>\n",p.perteam[t].kills);
			html.write("<td width=100><font class=boardtext>%li</font></td>\n",p.perteam[t].deaths);
			html.write("<td width=100><font class=boardtext>%.2lf</font></td>\n",rank);
			
			html.write("</tr>\n");
			totalkills+=p.perteam[t].kills;
			totaldeaths+=p.perteam[t].deaths;
			teamrank+=rank;
			numplrs++;
		}
		
		html.write("<tr>");
		html.write("<td colspan=4><hr></td>");
		html.write("</tr>");
		html.write("<tr>");
		html.write("<td><font class=boardtext>totals</font></td>");
		html.write("<td><font class=boardtext>%li</font></td>",totalkills);
		html.write("<td><font class=boardtext>%li</font></td>",totaldeaths);
		html.write("<td><font class=boardtext>%.2lf (avg)</font></td>",teamrank/numplrs);
		html.write("</tr>");
		html.write("</table>\n<p>\n");		
		
		
	}
	
}


