<?php
$a=3;
$postuser = $_POST['user_id']; 
if(isset($postuser) && (isset($_POST['func']) && ($_POST['func'] == 'getWheels'))) {
	
	$sumfingIncl=true;
    include 'config.thing.php';
	
	// Get options from admin table
	$set = get_adm_opts();
	
	include 'incl/translate.php';
	
	// Get user id
	$user = is_user_fakeid($postuser);
	
	// Not user, DIE!
	if($user->fake_id != $postuser) {
		die(_("You are not registered app user"));
	}
	
	// Check user from spins table. If exists, return data
	$spins = get_spins($user->fb_id);
	
	// For any case
	if(is_object($spins)) {
		if($spins->done == 1)
		die( _("No access") );
	}	
	
	// Check time for cheaters. If posted too often
	if(isset($spins) && (($spins->last_spin +3) > time())) die( _("Weird... I think you are cheating!") );
	
	// Spin interval if set
	$spininterval = $set->slot_ticket_int * 3600;
	
	// Check for spin interval and delete from table when interval is over
	
	
	// Get reels from db
	$reel1 = explode(",",$set->reel_1); $reel2 = explode(",",$set->reel_2); $reel3 = explode(",",$set->reel_3);
	
	// Wheel elements, comes from database
	$wheel = array($reel1,$reel2,$reel3); 
	$result = "";
	
	// Spin the wheels
	for($x=0;$x<=2;$x++) {
		shuffle($wheel[$x]);		
		$result['wheel'.($x+1)] = $wheel[$x][0];
	}
	
	// Get wins
	$win = 0;
	// Clovers
	if(($result['wheel1'] == $result['wheel2']) && ($result['wheel1'] == 'clover')) {
		$win = $set->clover_2_win;
	}
	if(($result['wheel1'] == $result['wheel2']) && ($result['wheel2'] == $result['wheel3']) && ($result['wheel1'] == 'clover')) {
		$win = $set->clover_3_win;
	}
	// Hearts
	if(($result['wheel1'] == $result['wheel2']) && ($result['wheel1'] == 'heart')) {
		$win = $set->heart_2_win;
	}
	if(($result['wheel1'] == $result['wheel2']) && ($result['wheel2'] == $result['wheel3']) && ($result['wheel1'] == 'heart')) {
		$win = $set->heart_3_win;
	}
	// Stars
	if(($result['wheel1'] == $result['wheel2']) && ($result['wheel1'] == 'star')) {
		$win = $set->star_2_win;
	}
	if(($result['wheel1'] == $result['wheel2']) && ($result['wheel2'] == $result['wheel3']) && ($result['wheel1'] == 'star')) {
		$win = $set->star_3_win;
	}
	
	$result['win']=$win;
	$spinsleft = $set->slot_free_spins - $spins->spins - 1;
	$result['spinsleft'] = $spinsleft;
	$result['win_total'] = ($spins->tickets + $win);
	
	// Update and save results into db
	if($spins->done == 0)
	save_and_update_spin($user->fb_id,$win,($spins->tickets + $win),($spins->spins+1),$set->slot_free_spins);
	
	// Echo results for slot file	
	header('Content-Type: application/json');
	echo json_encode($result);

die;	 
}

if(isset($postuser) && (isset($_POST['func']) && ($_POST['func'] == 'collect'))) {
	$sumfingIncl=true;
    include 'config.thing.php';
	
	// Get user id
	$user = is_user_fakeid($postuser);
	
	// Get options from admin table
	$set = get_adm_opts();
	
	// Not user, DIE!
	if($user->fake_id != $postuser) {
		die(_("You are not registered app user"));
	}
	
	// Check user from spins table. If exists, return data
	$spins = get_spins($user->fb_id);
	
	// Check if all spins done
	if($spins->done == 1) {
		if(slot_give_tickets($user->fb_id))
		$result['ok']=1;
	}
	// Echo results for slot file	
	header('Content-Type: application/json');
	echo json_encode($result);
	
	die;
}
?>