<?php
class Score
{
	var $bLoaded=false;
	var $score_id = 0;
	var $gid = 0;
	var $uid = 0;
	var $score = 0;
	var $score_date = 0;
	function Score($db_row=false)
	{
		if($db_row)
		{
			$this->score_id = $db_row->score_id;
			$this->gid = $db_row->gid;
			$this->uid = $db_row->uid;
			$this->score = $db_row->score;
			$this->score_date = $db_row->score_date;
			$this->bLoaded=true;
		}
	}
	function &getByID($id)
	{
		global $db;
		$sql = 'SELECT * FROM '.$db->prefix('arcade_scores').' WHERE score_id='.$id;
		$ret = false;
		if($result = $db->query($sql))
		{
			if($row = $db->fetch_object($result))
			{
				$ret = new Score($row);
			}
		}
		return $ret;
	}
	function &getAllWhere($arrConditions=false, $order_by='', $limit=0, $offset=0)
	{
		global $db;
		$sql = 'SELECT * FROM '.$db->prefix('arcade_scores');
		if($arrConditions && count($arrConditions) > 0)	
		{
			$sql .= " WHERE ";
			for ($i=0; $i<count($arrConditions); $i++)
			{
				if ($i < count($arrConditions)-1)
					$sql .= $arrConditions[$i].' AND ';
				else
					$sql .= $arrConditions[$i].' ';
			}
		}
		if($order_by!='')
		{
			$sql .= " ORDER BY $order_by ";
		}
		$ret = array();
		if($result = $db->query($sql, intval($limit), intval($offset)))
		{
			while($row = $db->fetch_object($result))
			{
				$ret[] = new Score($row);
			}
		}
		return $ret;
	}
	function store()
	{
		global $db;
		$sql = '';
		if($this->bLoaded)
		{
			$sql = "UPDATE ".$db->prefix('arcade_scores')." SET ";
			$sql .= " gid='$this->gid',";
			$sql .= " uid='$this->uid',";
			$sql .= " score='$this->score',";
			$sql .= " score_date='$this->score_date'";
			$sql .= " WHERE score_id='$this->score_id'";
		}
		else
		{
			$sql = "INSERT INTO ".$db->prefix('arcade_scores')." SET ";
			$sql .= " score_id='$this->score_id',";
			$sql .= " gid='$this->gid',";
			$sql .= " uid='$this->uid',";
			$sql .= " score='$this->score',";
			$sql .= " score_date='$this->score_date'";
		}
		$result = $db->query($sql);
		if($result) $this->score_id = $db->getInsertId();
		return $result;
	}
	function delete()
	{
		global $db;
		$sql = 'DELETE FROM '.$db->prefix('arcade_scores').' WHERE score_id='.$this->score_id;
		$result = $db->query($sql);
		if($result) $this->bloaded = false;
		return $result;
	}
}
?>
