<?php
header("Content-type:text/html;charset=utf-8");
//报名
if (isset($_REQUEST['name']) && isset($_REQUEST['key']) && isset($_REQUEST['chess']) ) {
$tmp['name']=strip_tags(iconv('gb2312','utf-8',$_REQUEST['name']));
$tmp['key']=strip_tags(iconv('gb2312','utf-8',$_REQUEST['key']));
$tmp['chess']= $_REQUEST['chess'];
$tmp['x']=-1;
$tmp['y']=-1;
$tmp['time']=date("y-m-d h:i:s",time());
insertName($tmp);
}
//刷新下棋位置
if (isset($_REQUEST['name']) && isset($_REQUEST['x']) && isset($_REQUEST['y'])) {
$tmp['name']=strip_tags(iconv('gb2312','utf-8',$_REQUEST['name']));
$tmp['x']=$_REQUEST['x'];
$tmp['y']=$_REQUEST['y'];
updateName($tmp);
}
//退出游戏
if (isset($_REQUEST['name']) && isset($_REQUEST['del']) ) {
$tmp['name']=$_REQUEST['name'];
deleteName($tmp);
}
//显示所有玩家信息
selectName();
function insertName($tmp){
$kv = new SaeKV();
$ret = $kv->init();
$ret = $kv->set(strip_tags(md5($tmp['name'])), $tmp);
}
function deleteName($tmp){
$kv = new SaeKV();
$kv->init();
$ret = $kv->delete(strip_tags(md5($tmp['name'])));
}
function updateName($tmp){
$kv = new SaeKV();
$kv->init();
$ret = $kv->set(strip_tags(md5($tmp['name'])), $tmp);
}
function selectName(){
$kv = new SaeKV();
$kv->init();
$ret = $kv->pkrget('', 100);
$arr;
while (true) {
$arr = $ret;
end($ret);
$start_key = key($ret);
$i = count($ret);
if ($i < 100)
break;
$ret = $kv->pkrget('', 100, $start_key);
}
$_return;
foreach ($arr as $key => $value) {
$_return[] = $value;
$_pre=strtotime($value['time']);
$_now=strtotime(date('y-m-d h:i:s',time()));
$_delta = $_now - $_pre;
//如果超过1小时 自动删除记录
if ($_delta>3600) {
//echo $value['name']."的时间差是".$_delta;
deleteName($value);
}
}
echo json_encode($_return,JSON_UNESCAPED_UNICODE);
}
?>
网友评论