<?php
// include database and object files
include_once '../config/init.php';	// contains $db
include_once '../config/error.php';
include_once '../objects/db_session.php';

// prepare user object
$gameSession = new GameSession($db);

// read mandatory $_POST properties : ensure sessionType and sessionName are passed in $_POST parameters
if (isset($_POST['sessionId']))
	$gameSession->id = $_POST['sessionId'];
else trigger_error( missing_parameter_error("sessionId") );

// read other $_POST properties for game session
$gameSession->timeToFinish = $_POST['duration'];
$gameSession->success = (strcasecmp( ($_POST['success'] ?? false), "true" ) == 0) ? 1 : 0;
$gameSession->score = $_POST['score'];

// close the game session
if ($gameSession->stop())
{
	cache_invalidate_session($gameSession->id);
	rebuild_session_debrief_cache_db($db, $gameSession->id);
	$session_arr = $gameSession->getResultArray(true, "Session_Stop_OK");
	print_r(json_encode($session_arr));	// OK
}
else trigger_error("Error_Occured");
?>