game2003api/webapp/controller/TankController.class.php
aozhiwei ee07f2f6fe 1
2019-10-17 16:39:33 +08:00

771 lines
31 KiB
PHP

<?php
class TankController{
protected function getMysql($account_id)
{
$mysql_conf = getMysqlConfig(crc32($account_id));
$conn = new phpcommon\Mysql(array(
'host' => $mysql_conf['host'],
'port' => $mysql_conf['port'],
'user' => $mysql_conf['user'],
'passwd' => $mysql_conf['passwd'],
'dbname' => 'gamedb2003_' . $mysql_conf['instance_id']
));
return $conn;
}
protected function getTank($tank_id)
{
$tank_meta_table = require('../res/tank_economy@tank_economy.php');
$tank_meta = getTankConfig($tank_meta_table, $tank_id);
$t = array(
'id' => $tank_meta['tank_id'],
'level' => $tank_meta['level'],
'coin_origin' => $tank_meta['coin_origin'],
'coin_times' => $tank_meta['coin_times'],
'coin_sell' => $tank_meta['coin_sell'],
'diamond' => $tank_meta['diamond'],
'coin_produce' => $tank_meta['coin_produce'],
'coin_lv' => $tank_meta['coin_lv'],
'diamond_lv' => $tank_meta['diamond_lv'],
'drop_level' => $tank_meta['drop_level'],
'drop_number' => $tank_meta['drop_number'],
'ad_level' => $tank_meta['ad_level'],
'buy_range1' => $tank_meta['buy_range1'],
'buy_times1' => $tank_meta['buy_times1'],
'free_lv1' => $tank_meta['free_lv1'],
'buy_range2' => $tank_meta['buy_range2'],
'buy_times2' => $tank_meta['buy_times2'],
'free_lv2' => $tank_meta['free_lv2'],
);
return $t;
}
public function sendInfo()
{
$account_id = $_REQUEST['account_id'];
//登录校验
$login = loginVerify($account_id, $_REQUEST['session_id']);
if (!$login) {
phpcommon\sendError(ERR_USER_BASE + 1, 'session无效');
return;
}
$conn = $this->getMysql($account_id);
if (!$conn) {
phpcommon\sendError(ERR_USER_BASE + 1,'没有这个玩家');
return;
}
$tankInfo = json_decode(file_get_contents('php://input'), true);
//error_log(file_get_contents('php://input'));
//error_log(json_encode($tankInfo));
if (isset($tankInfo['tank_list'])) {
$tank_data = $tankInfo['tank_list'];
$ret = $conn->execScript('UPDATE user SET tank_data=:tank_data, modify_time=:modify_time ' .
' WHERE accountid=:account_id;',
array(
':account_id' => $account_id,
':tank_data' => json_encode($tank_data),
':modify_time' => time()
));
if (!$ret) {
die();
return;
}
}
if (isset($tankInfo['coin_num'])) {
$coin_num = $tankInfo['coin_num'];
$ret = $conn->execScript('UPDATE user SET coin_num=:coin_num, modify_time=:modify_time ' .
' WHERE accountid=:account_id;',
array(
':account_id' => $account_id,
':coin_num' => $coin_num,
':modify_time' => time()
));
if (!$ret) {
die();
return;
}
}
if (isset($tankInfo['buy_list'])) {
$buy_data = $tankInfo['buy_list'];
$ret = $conn->execScript('UPDATE user SET buy_data=:buy_data, modify_time=:modify_time ' .
' WHERE accountid=:account_id;',
array(
':account_id' => $account_id,
':buy_data' => json_encode($buy_data),
':modify_time' => time()
));
if (!$ret) {
die();
return;
}
}
if (isset($tankInfo['pass'])) {
$pass = $tankInfo['pass'];
$ret = $conn->execScript('UPDATE user SET pass=:pass, modify_time=:modify_time ' .
' WHERE accountid=:account_id;',
array(
':account_id' => $account_id,
':pass' => $pass,
':modify_time' => time()
));
if (!$ret) {
die();
return;
}
}
if (isset($tankInfo['energy'])) {
$energy = $tankInfo['energy'];
$ret = $conn->execScript('UPDATE user SET energy=:energy, modify_time=:modify_time ' .
' WHERE accountid=:account_id;',
array(
':account_id' => $account_id,
':energy' => $energy,
':modify_time' => time()
));
if (!$ret) {
die();
return;
}
}
if (isset($tankInfo['diamond_num'])) {
$diamond_num = $tankInfo['diamond_num'];
$ret = $conn->execScript('UPDATE user SET diamond_num=:diamond_num, modify_time=:modify_time ' .
' WHERE accountid=:account_id;',
array(
':account_id' => $account_id,
':diamond_num' => $diamond_num,
':modify_time' => time()
));
if (!$ret) {
die();
return;
}
}
if (isset($tankInfo['cumul_coin'])) {
$cumul_coin = $tankInfo['cumul_coin'];
$ret = $conn->execScript('UPDATE user SET cumul_coin=:cumul_coin, modify_time=:modify_time ' .
' WHERE accountid=:account_id;',
array(
':account_id' => $account_id,
':cumul_coin' => $cumul_coin,
':modify_time' => time()
));
if (!$ret) {
die();
return;
}
}
echo json_encode(array(
'errcode' => 0,
'errmsg' => '',
));
}
/* protected function getTankuuid($accountid)
{
$mysql_conf = getMysqlConfig(crc32($accountid));
$conn = $this->getMysql($accountid);
$ret = $conn->execScript("INSERT INTO id_pool(createtime) VALUES(:createtime);",
array(
'createtime' => time()
));
if (!$ret) {
die();
}
$row = $conn->execQueryOne('SELECT LAST_INSERT_ID();', array());
if (empty($row)) {
die();
}
$orderid = $this->getIdPool($mysql_conf['instance_id'] , $row[0]);
return $orderid;
}
protected function getIdPool($db_instance_id, $idx)
{
if ($db_instance_id >= 100 || $db_instance_id <= 0) {
phpcommon\sendError(ERR_USER_BASE + 1, 'instance_id无效');
die();
}
if ($idx >= 4294967295 || $idx <= 0) {
phpcommon\sendError(ERR_USER_BASE + 1, 'idx无效');
die();
}
$instance_id = sprintf('%02d', $db_instance_id);
$id = sprintf('%010d',$idx);
$tank_uuid = strftime('%y%m%d%H%M%S') . $instance_id . $id;
return $tank_uuid;
}
public function tankInfo()
{
$account_id = $_REQUEST['account_id'];
//登录校验
$login = loginVerify($account_id, $_REQUEST['session_id']);
if (!$login) {
phpcommon\sendError(ERR_USER_BASE + 1, 'session无效');
return;
}
$conn = $this->getMysql($account_id);
if(!$conn){
phpcommon\sendError(ERR_USER_BASE + 1,'没有这个玩家');
return;
}
//购买次数信息
$buy_list = array();
$row_times = $conn->execQueryOne('SELECT times FROM buy_times ' .
' WHERE accountid=:accountid AND level=:level;',
array(
':accountid' => $account_id,
':level' => $level,
));
if ($row_times) {
foreach ($row_times as $row_times) {
array_push($buy_list, array(
'level' => $row_times['level'],
'times' => $row_times['times'],
));
}
}
//坦克列表
$tank_list = array();
//$curr_tank_uuid = '';
$rows_tank = $conn->execQuery('SELECT tankInfo FROM tankInfo WHERE accountid=:account_id;',
array(
':account_id' => $account_id,
));
//新玩家默认赠送一级坦克
if ($rows_tank) {
foreach ($rows_tank as $row_tank) {
if ($row_tank['tank_level'] == 0) {
continue;
}
array_push($tank_list, array(
'tank_uuid' => $row_tank['tank_uuid'],
'level' => $row_tank['tank_level'],
));
}
} else {
$curr_tank_uuid = $this->getTankuuid($account_id);
$ret = $conn->execScript('INSERT INTO tank(accountid, tank_uuid, tank_level, create_time, modify_time) ' .
' VALUES(:account_id, :tank_uuid, 1, :create_time, :modify_time)',
array(
':account_id' => $account_id,
':tank_uuid' => $curr_tank_uuid,
':create_time' => time(),
':modify_time' => time()
));
if (!$ret) {
die();
return;
}
//更新默认上阵坦克信息
$ret = $conn->execScript('UPDATE user SET curr_tank_uuid=:tank_uuid, modify_time=:modify_time ' .
' WHERE accountid=:account_id;',
array(
':account_id' => $account_id,
':tank_uuid' => $curr_tank_uuid,
':modify_time' => time()
));
if (!$ret) {
die();
return;
}
array_push($tank_list, array(
'tankInfo' => $row_tank['tankInfo'],
));
}
$row = $conn->execQueryOne('SELECT coin_num FROM user WHERE accountid=:account_id;',
array(
':account_id' => $account_id,
));
if (!$row) {
phpcommon\sendError(ERR_USER_BASE + 1,'没有这个玩家');
return;
}
$coin_num = $row['coin_num'];
echo json_encode(array(
'errcode' => 0,
'errmsg' => '',
'buy_list' => $buy_list,
'coin_num' => $coin_num,
'tank_list' => $tank_list
));
}
public function tankCompose()
{
$account_id = $_REQUEST['account_id'];
//登录校验
$login = loginVerify($account_id, $_REQUEST['session_id']);
if (!$login) {
phpcommon\sendError(ERR_USER_BASE + 1, 'session无效');
return;
}
$conn = $this->getMysql($account_id);
if (!$conn) {
phpcommon\sendError(ERR_USER_BASE + 1,'没有这个玩家');
return;
}
$new_uuid = $_REQUEST['newtank_uuid'];
$del_uuid = $_REQUEST['deltank_uuid'];
//判断是否存在足够多的坦克
$row_new = $this->selectTank($account_id, $new_uuid);
$row_del = $this->selectTank($account_id, $del_uuid);
//坦克合成
$compose_level = $row_new['tank_level'] + 1;
$this->updateTankLevel($account_id, $new_uuid, $compose_level);
$this->updateTankLevel($account_id, $del_uuid, 0);
$compose_status = 0;
//判断是否合成新坦克
$row = $conn->execQueryOne('SELECT max_level FROM user WHERE accountid=:accountid;',
array(
':accountid' => $account_id,
));
if ($compose_level > $row['max_level']) {
$compose_status = 1;
//更新新坦克上阵,更新坦克最大等级
$ret = $conn->execScript('UPDATE user SET max_level=:level, curr_tank_uuid=:curr_tank_uuid, modify_time=:modify_time ' .
' WHERE accountid=:account_id;',
array(
':account_id' => $account_id,
':level' => $compose_level,
':curr_tank_uuid' => $new_uuid,
':modify_time' => time()
));
if (!$ret) {
die();
return;
}
}
echo json_encode(array(
'errcode' => 0,
'errmsg' => '',
'status' => $compose_status,
'level' => $compose_level
));
}
protected function selectTank($account_id, $tank_uuid)
{
$conn = $this->getMysql($account_id);
$row = $conn->execQueryOne('SELECT tank_uuid, tank_level FROM tank ' .
' WHERE accountid=:account_id AND tank_uuid=:tank_uuid;',
array(
':account_id' => $account_id,
':tank_uuid' => $tank_uuid
));
if (!$row) {
phpcommon\sendError(ERR_USER_BASE + 2,'坦克不存在');
die();
}
return $row;
}
protected function updateTankLevel($account_id, $tank_uuid, $level)
{
$conn = $this->getMysql($account_id);
$ret = $conn->execScript('UPDATE tank SET tank_level=:level, modify_time=:modify_time ' .
' WHERE accountid=:account_id AND tank_uuid=:tank_uuid;',
array(
':account_id' => $account_id,
':tank_uuid' => $tank_uuid,
':level' => $level,
':modify_time' => time()
));
if (!$ret) {
die();
}
}
*/
public function buyTank()
{
$account_id = $_REQUEST['account_id'];
//登录校验
$login = loginVerify($account_id, $_REQUEST['session_id']);
if (!$login) {
phpcommon\sendError(ERR_USER_BASE + 1, 'session无效');
return;
}
$conn = $this->getMysql($account_id);
if (!$conn) {
phpcommon\sendError(ERR_USER_BASE + 1,'没有这个玩家');
return;
}
$level = $_REQUEST['level'];
$row = $conn->execQueryOne('SELECT coin_num, diamond_num FROM user WHERE accountid=:accountid;',
array(
':accountid' => $account_id,
));
if (!$row) {
phpcommon\sendError(ERR_USER_BASE + 1,'没有这个玩家');
return;
}
//$max_tank = $this->getTank(10000 + $row['max_level']);
$t = $this->getTank(10000 + $level);
$num = 0;
$times = 0;
//$row_times = $conn->execQueryOne('SELECT times FROM buy_times ' .
// ' WHERE accountid=:accountid AND level=:level;',
// array(
// ':accountid' => $account_id,
// ':level' => $level,
// ));
//$this->buyLevelLimit($level, $max_tank['diamond_lv']);
if ($row['diamond_num'] < $t['diamond']) {
phpcommon\sendError(ERR_USER_BASE + 3,'钻石不足');
return;
}
$cur_num = $row['diamond_num'] - $t['diamond'];
$this->updateBuyInfo($account_id, $cur_num, 0, 0);
/*switch ($_REQUEST['type'])
{
case 0:
//金币购买
$this->buyLevelLimit($level, $max_tank['coin_lv']);
if (!$row_times) {
$times = 1;
$this->insertBuyInfo($account_id, $level);
} else {
$times = $row_times['times'] + 1;
}
$num = $this->test($t['coin_origin'], $times, $t['coin_times'] * 1000);
if ($row['coin_num'] < $num) {
phpcommon\sendError(ERR_USER_BASE + 3,'金币不足');
return;
}
$cur_num = phpcommon\bnSub_s($row['coin_num'], $num);
$this->updateBuyInfo($account_id, $cur_num, $level, $times);
break;
case 1:
//钻石购买
$this->buyLevelLimit($level, $max_tank['diamond_lv']);
if ($row['diamond_num'] < $t['diamond']) {
phpcommon\sendError(ERR_USER_BASE + 3,'钻石不足');
return;
}
$cur_num = $row['diamond_num'] - $t['diamond'];
$this->updateBuyInfo($account_id, $cur_num, 0, 0);
break;
case 2:
//金币加视频购买
$this->buyLevelLimit($level, $max_tank['buy_range2']);
if (!$row_times) {
$times = 1;
$this->insertBuyInfo($account_id, $level);
} else {
$times = $row_times['times'] + 1;
}
$num = $this->test($t['coin_origin'], $times, $t['coin_times'] * 1000);
if ($row['coin_num'] < $num) {
phpcommon\sendError(ERR_USER_BASE + 3,'金币不足');
return;
}
$cur_num = phpcommon\bnSub_s($row['coin_num'], $num);
$this->updateBuyInfo($account_id, $cur_num, $level, $times);
$level = $level + 1;
break;
default:
break;
}
//插入到坦克列表中
$curr_tank_uuid = $this->getTankuuid($account_id);
$ret = $conn->execScript('INSERT INTO tank(accountid, tank_uuid, tank_level, create_time, modify_time) ' .
' VALUES(:account_id, :tank_uuid, :tank_level, :create_time, :modify_time)',
array(
':account_id' => $account_id,
':tank_uuid' => $curr_tank_uuid,
':tank_level' => $level,
':create_time' => time(),
':modify_time' => time()
));
if (!$ret) {
die();
return;
}*/
echo json_encode(array(
'errcode' => 0,
'errmsg' => '',
'diamond_num' => $cur_num
));
}
protected function buyLevelLimit($level, $t_level)
{
if ($level > $t_level) {
phpcommon\sendError(ERR_USER_BASE + 2, '购买等级大于当前等级');
die();
}
}
/* protected function insertBuyInfo($account_id, $level)
{
$conn = $this->getMysql($account_id);
$ret = $conn->execScript('INSERT INTO buy_times(accountid, level, times, create_time, modify_time) ' .
' VALUES(:account_id, :level, 0, :create_time, :modify_time)',
array(
':account_id' => $account_id,
':level' => $level,
':create_time' => time(),
':modify_time' => time()
));
if (!$ret) {
die();
}
}
*/
protected function updateBuyInfo($account_id, $num, $level, $times)
{
$conn = $this->getMysql($account_id);
if ($times != 0) {
$ret = $conn->execScript('UPDATE user SET coin_num=:num, modify_time=:modify_time ' .
' WHERE accountid=:account_id;',
array(
':account_id' => $account_id,
':num' => $num,
':modify_time' => time()
));
if (!$ret) {
die();
}
$ret = $conn->execScript('UPDATE buy_times SET times=:times, modify_time=:modify_time ' .
' WHERE accountid=:account_id AND level=:level;',
array(
':account_id' => $account_id,
':times' => $times,
':level' => $level,
':modify_time' => time()
));
if (!$ret) {
die();
}
} else {
$ret = $conn->execScript('UPDATE user SET diamond_num=:num, modify_time=:modify_time ' .
' WHERE accountid=:account_id;',
array(
':account_id' => $account_id,
':num' => $num,
':modify_time' => time()
));
if (!$ret) {
die();
}
}
}
/*
public function tankListInfo()
{
$account_id = $_REQUEST['account_id'];
//登录校验
$login = loginVerify($account_id, $_REQUEST['session_id']);
if (!$login) {
phpcommon\sendError(ERR_USER_BASE + 1, 'session无效');
return;
}
$conn = $this->getMysql($account_id);
if (!$conn) {
phpcommon\sendError(ERR_USER_BASE + 1,'没有这个玩家');
return;
}
$tank_list = $_REQUEST['tank_list'];
$ret = $conn->execScript('UPDATE user SET tank_slot_list=:tank_slot_list, modify_time=:modify_time ' .
' WHERE accountid=:account_id;',
array(
':account_id' => $account_id,
':tank_slot_list' => $tank_list,
':modify_time' => time()
));
if (!$ret) {
die();
return;
}
echo json_encode(array(
'errcode' => 0,
'errmsg' => '',
));
}
public function currentTank()
{
$account_id = $_REQUEST['account_id'];
//登录校验
$login = loginVerify($account_id, $_REQUEST['session_id']);
if (!$login) {
phpcommon\sendError(ERR_USER_BASE + 1, 'session无效');
return;
}
$conn = $this->getMysql($account_id);
if (!$conn) {
phpcommon\sendError(ERR_USER_BASE + 1,'没有这个玩家');
return;
}
$tank_uuid = $_REQUEST['tank_uuid'];
$ret = $conn->execScript('UPDATE user SET curr_tank_uuid=:curr_tank_uuid, modify_time=:modify_time ' .
' WHERE accountid=:account_id;',
array(
':account_id' => $account_id,
':curr_tank_uuid' => $tank_uuid,
':modify_time' => time()
));
if (!$ret) {
die();
return;
}
echo json_encode(array(
'errcode' => 0,
'errmsg' => '',
));
}
public function tankRecover()
{
$account_id = $_REQUEST['account_id'];
//登录校验
$login = loginVerify($account_id, $_REQUEST['session_id']);
if (!$login) {
phpcommon\sendError(ERR_USER_BASE + 1, 'session无效');
return;
}
$conn = $this->getMysql($account_id);
if (!$conn) {
phpcommon\sendError(ERR_USER_BASE + 1,'没有这个玩家');
return;
}
$tank_uuid = $_REQUEST['tank_uuid'];
$row = $conn->execQueryOne('SELECT coin_num FROM user WHERE accountid=:accountid;',
array(
':accountid' => $account_id,
));
if (!$row) {
phpcommon\sendError(ERR_USER_BASE + 1,'没有这个玩家');
return;
}
//删除坦克
$row_tank = $this->selectTank($account_id, $tank_uuid);
$this->updateTankLevel($account_id, $tank_uuid, 0);
//增加金钱
$t = $this->getTank(10000 + $row_tank['tank_level']);
$num = phpcommon\bnAdd_s($row['coin_num'], $t['coin_sell']);
$ret = $conn->execScript('UPDATE user SET coin_num=:num, modify_time=:modify_time ' .
' WHERE accountid=:account_id;',
array(
':account_id' => $account_id,
':num' => $num,
':modify_time' => time()
));
if (!$ret) {
die();
return;
}
echo json_encode(array(
'errcode' => 0,
'errmsg' => '',
));
}
public function sumComposeTank()
{
$account_id = $_REQUEST['account_id'];
//登录校验
$login = loginVerify($account_id, $_REQUEST['session_id']);
if (!$login) {
phpcommon\sendError(ERR_USER_BASE + 1, 'session无效');
return;
}
$conn = $this->getMysql($account_id);
if (!$conn) {
phpcommon\sendError(ERR_USER_BASE + 1,'没有这个玩家');
return;
}
$rows = $conn->execQuery('SELECT tank_uuid, tank_level FROM tank WHERE accountid=:accountid;',
array(
':accountid' => $account_id,
));
if (!$rows) {
phpcommon\sendError(ERR_USER_BASE + 2,'没有这个坦克');
return;
}
$tank_array = array();
foreach ($rows as $row) {
$key = $row['tank_level'];
if (!array_key_exists($key, $tank_array)) {
$tank_array[$key] = array();
}
array_push($tank_array[$key], array(
'tank_uuid' => $row['tank_uuid'],
));
}
$level = 1;
$max_level = 1;
$curr_tank_uuid = '';
$row_user = $conn->execQueryOne('SELECT max_level FROM user WHERE accountid=:accountid;',
array(
':accountid' => $account_id,
));
if (!$row_user) {
phpcommon\sendError(ERR_USER_BASE + 1,'没有这个玩家');
return;
}
//一键合成
for (; $level <= $row_user['max_level']; $level++) {
if (!array_key_exists($level, $tank_array) || count($tank_array[$level]) < 2) {
continue;
}
$tank = $tank_array[$level];
$len = count($tank_array[$level]);
$new_num = floor($len / 2);
for ($i = 0; $i < $len; $i = $i + 2) {
$this->updateTankLevel($account_id, $tank[$i]['tank_uuid'], $level + 1);
$this->updateTankLevel($account_id, $tank[$i + 1]['tank_uuid'], 0);
}
$max_level = $level + 1;
$curr_tank_uuid = $tank[0]['tank_uuid'];
}
$compose_status = 0;
if ($max_level > $row_user['max_level']) {
$compose_status = 1;
//更新新坦克上阵,更新坦克最大等级
$ret = $conn->execScript('UPDATE user SET max_level=:level, curr_tank_uuid=:curr_tank_uuid, modify_time=:modify_time ' .
' WHERE accountid=:account_id;',
array(
':account_id' => $account_id,
':level' => $max_level,
':curr_tank_uuid' => $curr_tank_uuid,
':modify_time' => time()
));
if (!$ret) {
die();
return;
}
}
echo json_encode(array(
'errcode' => 0,
'errmsg' => '',
'status' => $compose_status,
));
}
public function test1()
{
$this->test(310, 1, 1175);
}
public function test($base_value, $times, $multiple)
{
$v = (int)$multiple;
$value = gmp_init($base_value);
$cfg_value = gmp_init($v);
$fada_exp = gmp_init('1000');
$exp = gmp_pow($cfg_value, $times);
$exp2 = gmp_pow($fada_exp, $times);
$result = gmp_mul($value, $exp);
$real_result = gmp_div($result, $exp2);
return gmp_strval($real_result);
}*/
}
?>