<?php

/*
 * Loto.
 */

	define('INSIDE'  , true);
	define('INSTALL' , false);
	require_once dirname(__FILE__) .'/common.php';
	
	$smetal    = 50000000000;
	$scristal  = 10000000000;
	$sdeuterio = 5000000000;
	
	$TraiteJoueurLoto = doquery("SELECT * FROM {{table}}","loterie");
		
	$DejaJoue = false;
		
	$parse['list_player'] = "";
	
	while( $JoueurLoto = mysql_fetch_array($TraiteJoueurLoto) ) 
	{
		$parse['list_player'] .= " - ". $JoueurLoto['username'] .": ";
		$parse['list_player'] .= $JoueurLoto['num1'] ." / ";
		$parse['list_player'] .= $JoueurLoto['num2'] ." / ";
		$parse['list_player'] .= $JoueurLoto['num3'] ." / ";
		$parse['list_player'] .= $JoueurLoto['num4'] ." / ";
		$parse['list_player'] .= $JoueurLoto['num5'] ." / ";
		$parse['list_player'] .= $JoueurLoto['num6'] ." / ";
		$parse['list_player'] .= $JoueurLoto['num7'] ." . <br /> ";
		
		if ($JoueurLoto['id_owner'] == $user['id'])
		{
			$DejaJoue = true;
		}
	}
	
	if (!$DejaJoue) {
		$parse['input_num']  = "	<form action=\"loterie.php\" method=\"POST\"> ";
		$parse['input_num'] .= "		<input type=\"text\" name=\"num1\" size=\"3\" maxlength=\"2\">&nbsp; ";
		$parse['input_num'] .= "		<input type=\"text\" name=\"num2\" size=\"3\" maxlength=\"2\">&nbsp; ";
		$parse['input_num'] .= "		<input type=\"text\" name=\"num3\" size=\"3\" maxlength=\"2\">&nbsp; ";
		$parse['input_num'] .= "		<input type=\"text\" name=\"num4\" size=\"3\" maxlength=\"2\">&nbsp; ";
		$parse['input_num'] .= "		<input type=\"text\" name=\"num5\" size=\"3\" maxlength=\"2\">&nbsp; ";
		$parse['input_num'] .= "		<input type=\"text\" name=\"num6\" size=\"3\" maxlength=\"2\">&nbsp; ";
		$parse['input_num'] .= "		<input type=\"text\" name=\"num7\" size=\"3\" maxlength=\"2\">&nbsp; ";
		$parse['input_num'] .= "	<br /> <br /> <br />";
		$parse['input_num'] .= "		<input type=\"submit\" name=\"action\" value=\"Terminer\"> ";
		$parse['input_num'] .= " </form> ";
	}else{
		$parse['input_num']  = " Vous avez d&eacute;j&agrave; jou&eacute; au loto.";
	}

	$parse['etat'] = "";
	
	if (isset($_POST['action']))
	{
		$chiffre_incorrect = false;
		$double_chiffre = false;
		
		for($i = 1; $i <= 7; $i++) {
			if (intval($_POST['num'.$i]) < 1 OR intval($_POST['num'.$i]) > 49) {
				$chiffre_incorrect = true;
			}
			
			$num[$i] = intval($_POST['num'.$i]);
			for($j = 1; $j <= 7; $j++) {
				if ($num[$i] == $num[$j] AND $i != $j) $double_chiffre = true;
			}
		}
		
		if ($chiffre_incorrect) {
			$parse['etat'] .= "- Un de vos chiffres est incorrect, inf&eacute;rieur &agrave; 1 ou sup&eacute;rieur &agrave; 49. <br />";
		}
		if ($double_chiffre) {
			$parse['etat'] .= "- Il y a au moins deux chiffres identiques! <br />";
		}
		
		if (!$chiffre_incorrect AND !$double_chiffre){
			$QryLoto  = "INSERT INTO {{table}} SET";
			$QryLoto .= "`id_owner` = '".$user['id']."', ";
			$QryLoto .= "`username` = '".$user['username']."', ";
			$QryLoto .= "`num1` = '". $num[1] ."', ";
			$QryLoto .= "`num2` = '". $num[2] ."', ";
			$QryLoto .= "`num3` = '". $num[3] ."', ";
			$QryLoto .= "`num4` = '". $num[4] ."', ";
			$QryLoto .= "`num5` = '". $num[5] ."', ";
			$QryLoto .= "`num6` = '". $num[6] ."', ";
			$QryLoto .= "`num7` = '". $num[7] ."' ";
			doquery($QryLoto, "loterie");
			
			doquery("UPDATE {{table}} SET `metal` = `metal` - ".$smetal.", `crystal` = `crystal` - ".$scristal.", `deuterium` = `deuterium` - ".$sdeuterio." WHERE `id`='".$planetrow['id']."' ", "planets");
			
			$parse['etat'] .= "- Vos chiffres ont &eacute;t&eacute; enregistr&eacute;s avec succ&egrave;s. <br />";
		}
	}

	
$loteria = gettemplate('loto_body');
$lote = parsetemplate( $loteria, $parse);
display ($lote, "Loto", true, '', false);

?>