<?php
error_reporting ( E_ALL );
require_once(dirname(__FILE__) . '/../common/db.class.php');
require_once(dirname(__FILE__) . '/../common/function.php');
$action = $_GET ['action'];
switch ($action) {
	case 'getstatus':
		getstatus();
		# code...
		break;
	case 'run':
		run();
		break;
	default:
		# code...
		break;
}
//记录摇晃数据
function run(){
	$id=isset($_POST['id'])?intval($_POST['id']):0;
	$openid=isset($_POST['openid'])?strval($_POST['openid']):'';
	$updatingscore=1;//isset($_POST['updatingscore'])?intval($_POST['updatingscore']):0;
	if ($id==0) {
		$returndata=array('code'=>-1,'msg'=>"数据错误");
		echo json_encode($returndata);
		return;
	}
	if ($openid=='') {
		$returndata=array('code'=>-2,'msg'=>"用户数据错误");
		echo json_encode($returndata);
		return;
	}
	$load=Loader::getInstance();
	$load->model('Pashu_model');

	$pashu_config=$load->pashu_model->getCurrentConfig();
	if($pashu_config['status']==1){
		$returndata=array('code'=>3,'msg'=>"游戏还没有开始");
		echo json_encode($returndata);
		return;
	}

	if($pashu_config['status']==3){
		$returndata=array('code'=>2,'msg'=>"游戏已经结束");
		echo json_encode($returndata);
		return;
	}
	$returndata=$load->pashu_model->addscore($openid,$updatingscore);
	$data=array('code'=>$returndata['code'],'message'=>$returndata['msg'],'data'=>$returndata['data']);
	echo json_encode($data);
	return;
}
function getstatus(){
	// echo 1;
	$load=Loader::getInstance();
	$load->model('Pashu_model');
	
	$pashu_config=$load->pashu_model->getCurrentConfig();
	// echo var_export($pashu_config);
	if(!$pashu_config){
		$returndata=array('code'=>-1,'msg'=>"数据错误");
		echo json_encode($returndata);
		return;
	}
	if($pashu_config['currentshow']==1){
		$returndata=array('code'=>-2,'msg'=>"请刷新页面获取最新的活动");
		echo json_encode($returndata);
		return;
	}

	if($pashu_config['status']==1){
		$returndata=array('code'=>1,'status'=>1,'msg'=>"活动还未开始请耐心等待");
		echo json_encode($returndata);
		return;
	}

	if($pashu_config['status']==3){
		$returndata=array('code'=>1,'status'=>3,'msg'=>"本轮活动已经结束");
		echo json_encode($returndata);
		return;
	}

	if($pashu_config['status']==2){
		$returndata=array('code'=>1,'status'=>2,'msg'=>"游戏进行中");
		echo json_encode($returndata);
		return;
	}
}

// function pashuconfig($id){
// 	$pashu_config_m=new M('pashu_config');
// 	$pashu_config=$pashu_config_m->find('id='.$id);
// 	return $pashu_config;
// }