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

// prepare user object
$event = new ReactEvent($db);

// read mandatory $_POST properties
// ensure sessionId, trigger index and event type are passed in $_POST parameters
if (isset($_POST['srcEventIndex']) && $_POST['srcEventIndex'] >= 0)
	$event->srcEventIndex = $_POST['srcEventIndex'];
else trigger_error( missing_parameter_error("srcEventIndex") );

if (isset($_POST['srcEventSessionId']) && $_POST['srcEventSessionId'] > 0)
	$event->srcEventSessionId = $_POST['srcEventSessionId'];
else trigger_error( missing_parameter_error("srcEventSessionId") );

if (isset($_POST['type']))
	$event->reactType = $_POST['type'];
else trigger_error( missing_parameter_error("type") );


// read other $_POST properties
$event->hitUserId = $_POST['hitUserId'];
$event->hitTargetName = $_POST['targetName'];
$event->hitBoneName = $_POST['boneName'];
$event->damage = $_POST['damage'];
$event->targetKilled = (strcasecmp( ($_POST['targetKilled'] ?? false), "true" ) == 0) ? 1 : 0;
$event->objectHitLocationX = $_POST['objectHitLocationX'];
$event->objectHitLocationY = $_POST['objectHitLocationY'];
$event->objectHitTagLocation = $_POST['objectHitTagLocation'];
$event->hitPrecision = $_POST['hitPrecision'];
$event->distance = $_POST['distance'];
$event->reactTime = $_POST['reactTime'];
$event->reactMode = $_POST['reactMode'];
$event->timeStamp = $_POST['timestamp'];

// create reactevent
if ($event->record())
{
	cache_invalidate_session($event->srcEventSessionId);
	invalidate_session_debrief_cache_db($db, $event->srcEventSessionId);
	$evt_arr = $event->getResultArray(true, "Event_Stored_OK");
	print_r(json_encode($evt_arr));	// OK
}
else trigger_error("Error_Occured");
?>