<?php
session_start();
include_once '../../common.php';
include_once '../../conn.php';
if ($dailiid == "admin") {
    echo $dailiid = "";
}
if (!isset($_SESSION['username'])) {
    echo "<script>alert('身份已失效请重新登录!')</script><script>location.href='index.php'</script>";
    exit;
}
function messagetogame($info, $port)
{

    $socket = @socket_create(AF_INET, SOCK_STREAM, SOL_TCP) or die;
    $result = @socket_connect($socket, '127.0.0.1', $port) or die;
    $in = md5($info . 'oncientwebonline12345678901234567890') . $info . "\n";
    @socket_write($socket, $in, strlen($in)) or die;
    $out = @trim(socket_read($socket, 1024));
    @socket_close($socket);
    return $out;
}
$username = ubo($_SESSION['username']);
if ($_POST) {
    $qu = ubo(iconv("UTF-8", "GB2312", $_POST['qu']));
    $name = ubo(iconv("UTF-8", "GB2312", $_POST['name']));
    $money = ubo(iconv("UTF-8", "GB2312", $_POST['money']));
    date_default_timezone_set('PRC');
    if ($name == 'admin') {
        $msg = iconv("GB2312", "UTF-8", "这个账户不允许添加");
        $json_data = array('zt' => "0", 'msg' => $msg);
        echo json_encode($json_data);
    } elseif ($name == null) {
        $msg = iconv("GB2312", "UTF-8", "用户名不能为空");
        $json_data = array('zt' => "0", 'msg' => $msg);
        echo json_encode($json_data);
    } elseif ($money == null) {
        $msg = iconv("GB2312", "UTF-8", "金额不能为空");
        $json_data = array('zt' => "0", 'msg' => $msg);
        echo json_encode($json_data);
    } elseif ($qu == null) {
        $msg = iconv("GB2312", "UTF-8", "区服不能为空");
        $json_data = array('zt' => "0", 'msg' => $msg);
        echo json_encode($json_data);
    } else {

        $date = time();
        $jin = $money;
        $pay_orderid = date("YmdHis") . rand(100000, 999999);
        $type = "(`user`,`uid`,`jin`,`shifoulingqu`,`shijian`,`dd`,`orderid`) VALUES ('buchang','$name','$jin','$qu','$pay_orderid','1','$date')";
        $server = $servers[$qu];

        $msg = messagetogame('chongzhi ' . $name . ' ' . $money, $server);

        if (substr($msg,0,5)!='error') {
            dbinsert(chongzhi, $type);
            echo json_encode(array('zt' => "1"));
        } else {
            $msg = iconv("GB2312", "UTF-8", "补偿失败，该玩家可能不存在或不在线");
            $json_data = array('zt' => "0", 'msg' => $msg);
            echo json_encode($json_data);
        }


    }
} else {
    $msg = iconv("GB2312", "UTF-8", "参数错误");
    $json_data = array('zt' => "0", 'msg' => $msg);
    echo json_encode($json_data);
}
?>
