<?php
 
include_once './include/common.inc.php';
sajax_init();
sajax_export("doSub","listfso","checkserv","doView");
sajax_handle_client_request();
if(!$_userid) header('location:/member/login.php?forward='.urlencode($PHP_URL));
$today = date('Y-m-d',$PHP_TIME);
if (!$MOD['enablelottery']) showmessage("对不起，管理员未开启抽奖功能！");
$lr = $db->get_one("SELECT * FROM ".TABLE_LOTTERY." WHERE userid='$_userid'");
if (!$lr) $db->query("INSERT INTO ".TABLE_LOTTERY." (`userid`,`adddate`,`nums`) VALUES ('$_userid','$today','0')");
if($lr['adddate'] <$today) $db->query("UPDATE ".TABLE_LOTTERY." SET nums=0,adddate='$today' WHERE userid='$_userid'");
if($lr['adddate'] >$today) showmessage("您的抽奖记录出现异常，请主动与管理员联系！");
if($lr['adddate'] = $today &&$lr['nums'] >= $MOD['lotterynums']) showmessage("您今天的抽奖次数已用完了，请明天再来吧！");
$inums = $MOD['lotterynums']-$lr['nums'];
function doSub($id,$idfso)
{
global $db,$_userid,$_username,$MOD,$PHP_TIME;
if(!$id) return "<span class=font06>请选择服务器</span>";
if(!$idfso) return "<span class=font06>请选择角色</span>";
if (!check_con($id)) return "<span class=font06>当前分区连接失败，请与管理员联系</span>";
$sqldb = real_con($id);
if(is_logonline($id,$idfso)) return "<span class=font06>请确保您已正常退出游戏！</span>";
$sqldb->select_db(game_fso($id));
$fsoinfo = $sqldb->get_one("SELECT AccountID,Level,OnlineTime FROM user WHERE ID='$idfso' AND deleted=0");
if(!$fsoinfo) return "<span class=font06>未查询到角色数据！</span>";
if($fsoinfo['Level'] <$MOD['lotterylevel']) "<span class=font06>您的等级未达到抽奖需要的等级！</span>";
$giveonlinetime = $MOD['ilottery']*60;
$OnlineTime = floor($fsoinfo['OnlineTime']/60);
if($OnlineTime <$MOD['ilottery']) "<span class=font06>您的在线时间已不足以进行一次抽奖！</span>";
$sqldb->select_db(game_acc($id));
$accinfo = $sqldb->get_one("SELECT username FROM user WHERE idx='$fsoinfo[AccountID]'");
if(!$accinfo) return "<span class=font06>错误的请求！</span>";
$totalnums = $db->get_one("SELECT * FROM ".TABLE_LOTTERY." WHERE userid='$_userid'");
$todaynums = $MOD['lotterynums']-$totalnums['nums']-1;
if($todaynums <=0)
{
$todaynumshow = "很遗憾，您今天的抽奖机会已用完了！";
return "<span class=font06>很遗憾，您今天的抽奖机会已用完了！</span>";
}else{
$todaynumshow = "您还有 <font color=darkgreen>".$todaynums."</font> 次抽奖机会，是否继续？";
}
if($accinfo['username'] != "$_username") return "<span class=font06>非法操作，你的操作已被记录！</span>";
$lotteryitems = explode(',',$MOD['lotteryitems']);
$itemnums = count($lotteryitems);
$mitem = mt_rand(1,$itemnums);
$itemcolor = mt_rand(1,3);
$aitem = $mitem-1;
$itemid = $lotteryitems[$aitem];
$db->query("UPDATE ".TABLE_LOTTERY." SET nums=nums+1 WHERE userid='$_userid'");
$sqldb->select_db(game_fso($id));
$sqldb->query("UPDATE user SET OnlineTime=OnlineTime-$giveonlinetime WHERE ID='$idfso'");
if($itemid == '0') return '<TABLE class=mw_tabA cellSpacing=0 cellPadding=0 width="90%" align=center border=0><TBODY><TR><TD height="30" colspan="2">你真是倒霉到了极点！</TD></TR><TD height=30 style="text-align:center;color:#f00;font-weight:bold">你什么也没抽到</TD></TR><TR><TD height=30 align="center">'.$todaynumshow.'</TD></TR><TR><TD class=pat10 align="center"><input type="button" class="hsbtn" value="确认" onclick="doView();CloseLoad()"></TD></TR></TBODY></TABLE>';
$rsitem = $db->get_one("SELECT itemname FROM ".TABLE_ITEMS." WHERE itemid='$itemid'");
if(!$rsitem) 
{
$db->query("UPDATE ".TABLE_LOTTERY." SET nums=nums-1 WHERE userid='$_userid'");
$sqldb->query("UPDATE user SET OnlineTime=OnlineTime+$giveonlinetime WHERE ID='$idfso'");
return "<span class=font06>错误的道具ID:".$itemid."，请向管理员报告此错误！此次抽奖不计算次数和时间。</span>";
}
$nowtime = date("Y-m-d H:i:s",$PHP_TIME);
$sqldb->query("INSERT INTO t_gift (`charID`,`level`,`type`,`attribute`,`addTime`,`limitTime`,`msg`) VALUES ('$idfso',0,1,'ver=1;count=1;|id=$itemid;color=$itemcolor;lev=0;bind=0;|','$nowtime','2010-10-10 20:20:20','网站抽奖道具')");
return '<TABLE class=mw_tabA cellSpacing=0 cellPadding=0 width="90%" align=center border=0><TBODY><TR><TD height="30" colspan="2">恭喜你获得：</TD></TR><TD height=30 style="text-align:center;color:#900;font-weight:bold">'.$rsitem[itemname].'</TD></TR><TR><TD height=30 align="center">'.$todaynumshow.' </TD></TR><TR><TD class=pat10 align="center"><input type="button" class="hsbtn" value="确认" onclick="doView();CloseLoad();"></TD></TR></TBODY></TABLE>';
}
function doView($id,$value)
{
global $db,$_username,$MOD;
if(!$id) return '<TABLE class=regform cellSpacing=0 cellPadding=0 width="80%" align=center border=0><TBODY><TR><TH></TH><TD style="color:#f00;font-size:14px;font-weight:bold;padding:20px 0px;text-align:left;">游戏分区读取错误！</TD></TR></TBODY></TABLE>';
if(!$value) return '<TABLE class=regform cellSpacing=0 cellPadding=0 width="80%" align=center border=0><TBODY><TR><TH></TH><TD style="color:#f00;font-size:14px;font-weight:bold;padding:20px 0px;text-align:left;">请选择角色！</TD></TR></TBODY></TABLE>';
if (!check_con($id)) return '<TABLE class=regform cellSpacing=0 cellPadding=0 width="80%" align=center border=0><TBODY><TR><TH></TH><TD style="color:#f00;font-size:14px;font-weight:bold;padding:20px 0px;text-align:left;">当前分区连接失败，请与管理员联系</TD></TR></TBODY></TABLE>';
if (is_logonline($id,$value)) return '<TABLE class=regform cellSpacing=0 cellPadding=0 width="80%" align=center border=0><TBODY><TR><TH></TH><TD style="color:#f00;font-size:14px;font-weight:bold;padding:20px 0px;text-align:left;">请确保您已正常退出游戏！</TD></TR></TBODY></TABLE>';
$sqldb = real_con($id);
$sqldb->select_db(game_fso($id));
$fsoinfo = $sqldb->get_one("SELECT AccountID,Level,OnlineTime FROM user WHERE ID='$value'");
if($fsoinfo)
{
$sqldb->select_db(game_acc($id));
$accinfo = $sqldb->get_one("SELECT username FROM user WHERE idx='$fsoinfo[AccountID]'");
if(!$accinfo)
{
return '<TABLE class=regform cellSpacing=0 cellPadding=0 width="80%" align=center border=0><TBODY><TR><TH></TH><TD style="color:#f00;font-size:14px;font-weight:bold;padding:20px 0px;text-align:left;">非法请求！</TD></TR></TBODY></TABLE>';
}else{
if($accinfo['username'] != "$_username") return '<TABLE class=regform cellSpacing=0 cellPadding=0 width="80%" align=center border=0><TBODY><TR><TH></TH><TD style="color:#f00;font-size:14px;font-weight:bold;padding:20px 0px;text-align:left;">非法操作，你的操作已被记录！</TD></TR></TBODY></TABLE>';
}
$Level = $fsoinfo['Level'];
$OnlineTime = floor($fsoinfo['OnlineTime']/60);
if($OnlineTime <$MOD['ilottery']) return '<TABLE class=regform cellSpacing=0 cellPadding=0 width="80%" align=center border=0><TBODY><TR><TH></TH><TD style="color:#f00;font-size:14px;font-weight:bold;padding:20px 0px;text-align:left;">抱歉，该角色没有足够的在线时间进行抽奖！</TD></TR></TBODY></TABLE>';
if($Level <$MOD['lotterylevel']) return '<TABLE class=regform cellSpacing=0 cellPadding=0 width="80%" align=center border=0><TBODY><TR><TH></TH><TD style="color:#f00;font-size:14px;font-weight:bold;padding:20px 0px;text-align:left;">抱歉，该角色等级未达到抽奖等级需求！</TD></TR></TBODY></TABLE>';
if($Level &&$OnlineTime)
{
return '<TABLE class=regform cellSpacing=0 cellPadding=0 width="80%" align=center border=0><TBODY><TR><TH style="font-weight:bold;font-size:14px">角色等级： </TH><TD colSpan=2 style="color:#555;font-size:14px;font-weight:bold"><em style="font-weight:bold;color:#f00">'.$Level.'</em> 级</TD></TR><TR><TH style="font-weight:bold;font-size:14px">在线时间： </TH><TD colSpan=2 style="color:#555;font-size:14px;font-weight:bold"><em style="font-weight:bold;color:#f00">'.$OnlineTime.'</em> 分钟</TD></TR><TR><TH></TH><TD colSpan=2><input id="subok" type="submit" onClick="doSub();" class="shsbtn" value="给我抽"></TD></TR></TBODY></TABLE>';
}else{
return '<TABLE class=regform cellSpacing=0 cellPadding=0 width="80%" align=center border=0><TBODY><TR><TH></TH><TD style="color:#f00;font-size:14px;font-weight:bold;padding:20px 0px;text-align:left;">角色信息读取失败!</TD></TR></TBODY></TABLE>';
}
}else{
return '<TABLE class=regform cellSpacing=0 cellPadding=0 width="80%" align=center border=0><TBODY><TR><TH></TH><TD style="color:#f00;font-size:14px;font-weight:bold;padding:20px 0px;text-align:left;">未读取到角色信息!</TD></TR></TBODY></TABLE>';
}
}
function listfso($id)
{
global $db,$_userid,$_username;
if(!$id) return "游戏分区读取错误";
if (!check_con($id)) return "<span class=font06>当前分区连接失败，请与管理员联系</span>";
$sqldb = real_con($id);
$info = $sqldb->get_one("SELECT idx FROM user WHERE username='$_username'");
if($info)
{
$sqldb->select_db(game_fso($id));
$users = array();
$rs = $sqldb->query("SELECT ID,Name FROM user WHERE AccountID='".$info['idx']."' AND deleted = 0");
if(!$rs)
{
return "该分区没有角色！";
}else{
while($r = $sqldb->fetch_array($rs))
{
$users[] = $r;
}
foreach($users as $value)
{
$bstr .= '<option value='.$value[ID].'>'.$value[Name].'</option>';
}
return '<select id="idfso" onchange="doView()"><option>-- 请选择角色 --</option>'.$bstr.'</select>';
}
}else{
return "您没有激活该分区";
}
}
function checkserv($id)
{
global $db,$_userid,$_username;
if(!$id) return "<span class=font06>请选择分区</span>";
if (!check_con($id)) return "<span class=font06>当前分区连接失败，请与管理员联系</span>";
$sqldb = real_con($id);
$info = $sqldb->get_one("SELECT idx FROM user WHERE username='$_username'");
if($info)
{
$sqldb->select_db(game_fso($id));
$infou = $sqldb->get_one("SELECT ID FROM user WHERE AccountID='".$info['idx']."' AND deleted = 0");
if(!$infou)
{
return "<span class=font06>该分区没有角色！</span>";
}else{
return "true";
}
}else{
return "<span class=font06>您没有激活该分区</span>";
}
return "true";
}
$head['title'] = '在线时间抽奖';
$head['keywords'] = $MOD['seo_keywords'];
$head['description'] = $MOD['seo_description'];
$infos = $db->get_one("SELECT area FROM ".TABLE_MEMBER_INFO." WHERE userid='$_userid'");
if(!$infos['area']) showmessage("请选激活游戏分区再进行此操作！","/game/actgame.php");
$gamelists = array();
$rs = $db->query("SELECT * FROM ".TABLE_GAMESERVER." WHERE id IN ($infos[area]) ORDER BY id");
while($r = $db->fetch_array($rs))
{
$gamelists[] = $r;
}
include template($mod,'lottery');
?>