<?php
include 'dbconn.php';
include 'func.php';

if (!isset($_SESSION['LoggedIn'.$session_app])) {
	echo '{"status":false, "message":"'.$lang['session-timeout'].'", "session":false}';	
	exit();
}

if(!empty($_REQUEST['member_id']) && !empty($_REQUEST['token']) && isset($_REQUEST['member_chance']) && isset($_REQUEST['member_point']) && isset($_REQUEST['member_score']) && isset($_REQUEST['bet']) && isset($_REQUEST['win']) && !empty($_REQUEST['status']))
{
	$token = mysqli_real_escape_string($conn, $_REQUEST['token']);
	if ($token == $_SESSION['Token']){
		$member_id = mysqli_real_escape_string($conn, $_REQUEST['member_id']);
		$member_chance = mysqli_real_escape_string($conn, $_REQUEST['member_chance']);
		$member_point = mysqli_real_escape_string($conn, $_REQUEST['member_point']);
		$member_score = mysqli_real_escape_string($conn, $_REQUEST['member_score']);
		$player_bet = mysqli_real_escape_string($conn, $_REQUEST['bet']);
		$win_point = mysqli_real_escape_string($conn, $_REQUEST['win']);
		$status = mysqli_real_escape_string($conn, $_REQUEST['status']);

		//members data
		$check_member_points = checkExistPoints($_SESSION['Member_ID'], $gameType);
		$last_member_point = $check_member_points[0]['member_point'];
		$last_member_chance = $check_member_points[0]['member_chance'];
		$last_member_score = $check_member_points[0]['member_score'];

		$game_log_data = '{"type":"client", "status":"'.$status.'", "current":{"member_point":'.$last_member_point.', "member_chance":'.$last_member_chance.', "member_score":'.$last_member_score.'}, "update":{"member_point":'.$member_point.', "member_chance":'.$member_chance.', "member_score":'.$member_score.'}, "bet":'.$player_bet.', "win":'.$win_point.'}';
			
		if($testMode){
			echo '{"status":true, "token":"'.$_SESSION['Token'].'", "member_point":'.$member_point.', "member_chance":'.$member_chance.', "member_score":'.$member_score.', "message":"'.$lang['server-points-updated'].'"}';
			exit;
		}else{
			if(updateMemberPoints($member_point, $member_chance, $member_score, $member_id, $gameType)){
				createGameLogs($gameType, $_SESSION['Member_ID'], $game_log_data);

				$_SESSION['Token'] = getToken(30);
				echo '{"status":true, "token":"'.$_SESSION['Token'].'", "member_point":'.$member_point.', "member_chance":'.$member_chance.', "member_score":'.$member_score.', "message":"'.$lang['server-points-updated'].'"}';
			}else{
				echo '{"status":false, "message":"'.$lang['server-no-record'].'"}';
			}
		}
	}else{
		echo '{"status":false, "message":"'.$lang['server-error-undefined-token'].'"}';	
	}
}else{
	echo '{"status":false, "message":"'.$lang['server-error'].'"}';
}

// Close connection
mysqli_close($conn);
?>