<?php
error_reporting ( E_ALL );
require_once(dirname(__FILE__) . '/../common/db.class.php');
require_once(dirname(__FILE__) . '/../common/function.php');
require_once(dirname(__FILE__) .DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'models'.DIRECTORY_SEPARATOR.'shuqian_model.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=isset($_POST['updatingscore'])?intval($_POST['updatingscore']):0;
	// echo $updatingscore;
	if ($id==0) {
		$returndata=array('code'=>-3,'msg'=>"数据错误");
		echo json_encode($returndata);
		return;
	}
	if ($openid=='') {
		$returndata=array('code'=>-2,'msg'=>"用户数据错误");
		echo json_encode($returndata);
		return;
	}

	$shuqian_model=new Shuqian_model();
	$shuqian_config=$shuqian_model->getCurrentConfig();

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

	if($shuqian_config['status']==3){
		$returndata=array('code'=>2,'msg'=>"游戏已经结束");
		echo json_encode($returndata);
		return;
	}
	$result=$shuqian_model->addScore($openid,$updatingscore);
	// echo var_export($result);
	$returndata=array();
	$returndata['code']=$result['code'];
	$returndata['msg']='';
	$returndata['data']=$result['data'];
	echo json_encode($returndata);
	return;
}
function getstatus(){
	$shuqian_model=new Shuqian_model();
	$shuqian_config=$shuqian_model->getCurrentConfig();
	if(!$shuqian_config){
		$returndata=array('code'=>-1,'msg'=>"数据错误");
		echo json_encode($returndata);
		return;
	}
	if($shuqian_config['currentshow']==1){
		$returndata=array('code'=>-2,'msg'=>"请刷新页面获取最新的活动");
		echo json_encode($returndata);
		return;
	}

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

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

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

// function shuqianconfig($id){
// 	$shuqian_config_m=new M('shuqian_config');
// 	$shuqian_config=$shuqian_config_m->find('id='.$id);
// 	return $shuqian_config;
// }