<commit_before><?php
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');

$lock = fopen('lock', 'r+');
if (!flock($lock, LOCK_EX | LOCK_NB))
{
	sendMessage(time(),"Instance already running");
    die("Instance already running");
}
sendMessage(time(),"Script started");

function sendMessage($id , $message)
{
	echo "id: $id" . PHP_EOL;
	echo "data:".$message."\n";
	echo PHP_EOL;
	ob_flush();
	flush();
}
include "hand_evaluation.php";
include "card_deck.php";
$deck = new CardDeck(range(0,51), "backImage");
$deck->shuffleDeck(1000);

include "dbaccess.php";
$params = new Entities();
$dbObj = new Database("pokerdb",'localhost',"root","");

function renewLU()
{
	global $params;// = new Entities();
	$date = new DateTime();
	$fdate = $date->format('Y-m-d H:i:s');
	$params->setParam("lastupdate",$fdate);
}

function mergeCards($d_cards,$p_cards)
{
	$d_cards[5]=$p_cards[0];
	$d_cards[6]=$p_cards[1];
	return $d_cards;
}

function subtractFunds($dbObj, $player_id, $sub_funds)
{
	$playerFunds = $dbObj->select("SELECT funds FROM player WHERE id='$player_id'");
	
	if ($playerFunds[0]->funds - $sub_funds < 0) //jei nepakanka pinigu zaidejo saskaitoje, pridedam naujus 5000
	{
		$dbObj->executeSqlCommand("UPDATE player SET funds='5000' WHERE id='$player_id'");
	}
	else //jei saskaitoje pinigu uztektinai, atimame is jos tiek, kiek nurodyta, viska irasome duombazeje
	{
		$newFunds = $playerFunds[0]->funds - $sub_funds;
		$dbObj->executeSqlCommand("UPDATE player SET funds='$newFunds' WHERE id='$player_id'");
	}
}

$hand = [];
$dealercards = [];
$playerlist = [];
$deckcounter = 0;
$playercount = 0;
$resetdata = json_encode(array( "action" => 0, "confirmed" => 0, "raise" => 0));
$dbObj->executeSqlCommand("UPDATE player SET sid = '', hand = '', eval = '', bet = 0, data = '".$resetdata."', quit = 0"); //justincase reset player sids in db
$params ->setParam("dealercards","");
$params->setParam("stage","0");
$params->setParam("handbrake","0");
$params->setParam("abort","0");

$maxlifetime = 15; //max session age, seconds

$handbrake = intval($params->getParam('handbrake')[0]->value);
$sessions = $dbObj->select("SELECT * FROM session");
//$players = $dbObj->select("SELECT * FROM player");

$timer = 0; //for timing game stages
$stage = 0;
$nextstage = 0;

while(true)
{
	while (empty($sessions)) //wait for clients
	{
		sleep(5);
		sendMessage(time(),"No clients, waiting");
		$sessions = $dbObj->select("SELECT * FROM session");
	}
	
	if ((time() % 10) % 5 == 0) //cleanup old sessions
	{
		$sqlCommand = 'SELECT * FROM session
						WHERE TIMESTAMPDIFF(second,session.lastupdate,:time) > :seconds';
		$ddate = new DateTime();
		$data = array (":seconds" => $maxlifetime, ":time" => $ddate->format('Y-m-d H:i:s'));
		$removedsessions = $dbObj->parameterizedSelect($sqlCommand, $data);
		
		if (!empty($removedsessions))
		{
			foreach ($removedsessions as $removedsession)//the actual removal
			{
				$date = new DateTime();
				$lastupdate = $date->format('Y-m-d H:i:s');
				$fold = json_encode(array( "action" => 2, "confirmed" => 1, "raise" => 0));
				$sid_data = array (":sid" => $removedsession->sid, ":fold" => $fold,  ":lastupdate" => $lastupdate);
				
				$sqlCommand = 'DELETE FROM session
						WHERE sid = :sid';
				$dbObj->executePreparedStatement($sqlCommand, $sid_data);
				
				$sqlCommand = "UPDATE player SET data = :fold, quit = 1, lastupdate = :lastupdate WHERE sid = :sid"; //set leaver to fold and quit if he's a player
				$dbObj->executePreparedStatement($sqlCommand, $sid_data);
			}
			
			sendMessage(time(),json_encode($removedsessions)." removing session");
			$sessions = $dbObj->select("SELECT * FROM session"); //incidentally that might've been the last online session
		}
	}
	
	/*if ((time() % 60) % 30 == 0)
	{
		for ($i = 0; $i < 5; $i++)
		{
			$dealercards[$i]=array("color" => $deck[$i]->getColor(), "weight" => $deck[$i]->getWeight(), "frontImage" => $deck[$i]->getFrontImage());
		}
		$deck->shuffleDeck(1000);
		$date = new DateTime();
		$fdate = $date->format('Y-m-d H:i:s');
		$params->setParam("lastupdate",$fdate);
		$params->setParam("dealercards",json_encode($dealercards));

		//json_encode($dealercards)
		sendMessage(time(),json_encode($dealercards)." <- inserting to DB");
	}*/
	if ($timer==0 || $timer % 5 == 0) //test handbrake more often
	{
		$handbrake = intval($params->getParam('handbrake')[0]->value);
		while ($handbrake)//handbrake
		{
			sleep(15);
			sendMessage(time(),"Paused.");<commit_msg>session cleanup sql fix<commit_after><?php
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');

$lock = fopen('lock', 'r+');
if (!flock($lock, LOCK_EX | LOCK_NB))
{
	sendMessage(time(),"Instance already running");
    die("Instance already running");
}
sendMessage(time(),"Script started");

function sendMessage($id , $message)
{
	echo "id: $id" . PHP_EOL;
	echo "data:".$message."\n";
	echo PHP_EOL;
	ob_flush();
	flush();
}
include "hand_evaluation.php";
include "card_deck.php";
$deck = new CardDeck(range(0,51), "backImage");
$deck->shuffleDeck(1000);

include "dbaccess.php";
$params = new Entities();
$dbObj = new Database("pokerdb",'localhost',"root","");

function renewLU()
{
	global $params;// = new Entities();
	$date = new DateTime();
	$fdate = $date->format('Y-m-d H:i:s');
	$params->setParam("lastupdate",$fdate);
}

function mergeCards($d_cards,$p_cards)
{
	$d_cards[5]=$p_cards[0];
	$d_cards[6]=$p_cards[1];
	return $d_cards;
}

function subtractFunds($dbObj, $player_id, $sub_funds)
{
	$playerFunds = $dbObj->select("SELECT funds FROM player WHERE id='$player_id'");
	
	if ($playerFunds[0]->funds - $sub_funds < 0) //jei nepakanka pinigu zaidejo saskaitoje, pridedam naujus 5000
	{
		$dbObj->executeSqlCommand("UPDATE player SET funds='5000' WHERE id='$player_id'");
	}
	else //jei saskaitoje pinigu uztektinai, atimame is jos tiek, kiek nurodyta, viska irasome duombazeje
	{
		$newFunds = $playerFunds[0]->funds - $sub_funds;
		$dbObj->executeSqlCommand("UPDATE player SET funds='$newFunds' WHERE id='$player_id'");
	}
}

$hand = [];
$dealercards = [];
$playerlist = [];
$deckcounter = 0;
$playercount = 0;
$resetdata = json_encode(array( "action" => 0, "confirmed" => 0, "raise" => 0));
$dbObj->executeSqlCommand("UPDATE player SET sid = '', hand = '', eval = '', bet = 0, data = '".$resetdata."', quit = 0"); //justincase reset player sids in db
$params ->setParam("dealercards","");
$params->setParam("stage","0");
$params->setParam("handbrake","0");
$params->setParam("abort","0");

$maxlifetime = 15; //max session age, seconds

$handbrake = intval($params->getParam('handbrake')[0]->value);
$sessions = $dbObj->select("SELECT * FROM session");
//$players = $dbObj->select("SELECT * FROM player");

$timer = 0; //for timing game stages
$stage = 0;
$nextstage = 0;

while(true)
{
	while (empty($sessions)) //wait for clients
	{
		sleep(5);
		sendMessage(time(),"No clients, waiting");
		$sessions = $dbObj->select("SELECT * FROM session");
		$abort = intval($params->getParam('abort')[0]->value);
			
			if ($abort)
			{
				sendMessage(time(),"=abort=");
				exit();
			}
	}
	
	if ((time() % 10) % 5 == 0) //cleanup old sessions
	{
		$sqlCommand = 'SELECT * FROM session
						WHERE TIMESTAMPDIFF(second,session.lastupdate,:time) > :seconds';
		$ddate = new DateTime();
		$data = array (":seconds" => $maxlifetime, ":time" => $ddate->format('Y-m-d H:i:s'));
		$removedsessions = $dbObj->parameterizedSelect($sqlCommand, $data);
		
		if (!empty($removedsessions))
		{
			foreach ($removedsessions as $removedsession)//the actual removal
			{
				$date = new DateTime();
				$lastupdate = $date->format('Y-m-d H:i:s');
				$fold = json_encode(array( "action" => 2, "confirmed" => 1, "raise" => 0));
				$sid_data = array (":sid" => $removedsession->sid);
				$fold_data = array (":sid" => $removedsession->sid, ":fold" => $fold,  ":lastupdate" => $lastupdate);
				
				$sqlCommand = 'DELETE FROM session
						WHERE sid = :sid';
				$dbObj->executePreparedStatement($sqlCommand, $sid_data);
				
				$sqlCommand = "UPDATE player SET data = :fold, quit = 1, lastupdate = :lastupdate WHERE sid = :sid"; //set leaver to fold and quit if he's a player
				$dbObj->executePreparedStatement($sqlCommand, $fold_data);
			}
			
			sendMessage(time(),json_encode($removedsessions)." removing session");
			$sessions = $dbObj->select("SELECT * FROM session"); //incidentally that might've been the last online session
		}
	}
	
	/*if ((time() % 60) % 30 == 0)
	{
		for ($i = 0; $i < 5; $i++)
		{
			$dealercards[$i]=array("color" => $deck[$i]->getColor(), "weight" => $deck[$i]->getWeight(), "frontImage" => $deck[$i]->getFrontImage());
		}
		$deck->shuffleDeck(1000);
		$date = new DateTime();
		$fdate = $date->format('Y-m-d H:i:s');
		$params->setParam("lastupdate",$fdate);
		$params->setParam("dealercards",json_encode($dealercards));

		//json_encode($dealercards)
		sendMessage(time(),json_encode($dealercards)." <- inserting to DB");
	}*/
	if ($timer==0 || $timer % 5 == 0) //test handbrake more often
	{
		$handbrake = intval($params->getParam('handbrake')[0]->value);
		while ($handbrake)//handbrake
		{
			sleep(15);
			sendMessage(time(),"Paused.");
{'fraction_non_alphanumeric': 0.20517560073937152, 'fraction_numerical': 0.014026312928128738, 'mean_word_length': 4.877316293929712, 'pattern_counts': {'":': 11, '<': 11, '<?xml version=': 0, '>': 105, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3049514', 'n_tokens_mistral': 3643, 'n_tokens_neox': 3306, 'n_words': 716}