<?php

// COMMONS + DB

require "include/common.php";

// POST DATA CHECKS

$chapter_name = get_post("chapter_name", false, "");
$variant_name = get_post("variant_name", false, "");
$screen_name = get_post("screen_name", false, "");

$values_json = get_post("values_json");

// SESSION MUST BE ACTIVE

$session_id = check_session_post();

$user_id = get_session_user($session_id);

// DECODE values_json

$values = json_decode($values_json);

$user_action_log = "";
$first = true;

foreach($values as $key => $value) {
  if (strlen($value) > 0 and $value[0] == "\"" and $value[-1] == "\"") $value = substr($value, 1, strlen($value)-2);
	$msg = player_set_state($user_id, $key, $value);
	if ($first) $first = false;
	else $user_action_log = $user_action_log . ", ";
	$user_action_log = $user_action_log . $msg;
}

player_action($session_id, $chapter_name, $variant_name, $screen_name, "SET STATE", $user_action_log);		

// RESPOND

response_json_ok(array()); // will exit()