game2004api/webapp/controller/SoloController.class.php
2025-03-16 17:17:46 +08:00

682 lines
23 KiB
PHP

<?php
require 'classes/AddReward.php';
require_once 'metatable/item.php';
require_once 'metatable/parameter.php';
require_once 'metatable/drop.php';
class SoloController
{
protected function getRedis($key)
{
$redis_conf = getRedisConfig(crc32($key));
$r = new phpcommon\Redis(array(
'host' => $redis_conf['host'],
'port' => $redis_conf['port'],
'passwd' => $redis_conf['passwd']
));
return $r;
}
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' => DBNAME_PREFIX . $mysql_conf['instance_id']
));
return $conn;
}
protected function getGameLevelInfo($level)
{
$conf = require('../res/gamelevel@gamelevel.php');
return array_key_exists($level, $conf) ? $conf[$level] : null;
}
protected function getPlayerLevelInfo($level)
{
$conf = require('../res/playerlevel@playerlevel.php');
return array_key_exists($level, $conf) ? $conf[$level] : null;
}
public function soloInfo()
{
$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;
}
$solorow = $conn->execQueryOne(
'SELECT * FROM solo WHERE accountid=:accountid;',
array(
':accountid' => $account_id
)
);
$medals = 0;
$privilege = new classes\Privilege();
$medalsgrowlimit = metatable\getParameterById(MEDAL_LIMIT) + $privilege->getMedalLimitPlus($account_id);
$nowtime = time();
$lastredeem = $nowtime;
$level = 1;
$exp = 0;
$gamelevel = array(
'curlevel' => 1,
'curwave' => 0,
'levelinfo' => array(),
);
$offlinehours = 0;
if (!$solorow) {
$medals = $medalsgrowlimit;
$ret = $conn->execScript(
'INSERT INTO solo(accountid, level, exp, medals, lastredeem, lastoffline, create_time, modify_time) ' .
' VALUES(:account_id, 1, 0, :medals, :lastredeem, :lastoffline, :create_time, :modify_time) ' .
' ON DUPLICATE KEY UPDATE accountid=:account_id, level=:level, exp=:exp, medals=:medals, lastredeem=:lastredeem, lastoffline=:lastoffline, modify_time=:modify_time;',
array(
':account_id' => $account_id,
':medals' => $medals,
':lastredeem' => $lastredeem,
':lastoffline' => $nowtime,
':create_time' => $nowtime,
':modify_time' => $nowtime
)
);
} else {
$medalsecs = intval(metatable\getParameterById(MEDAL_SECONDS));
$lastredeem = $solorow['lastredeem'];
$medals = $solorow['medals'];
$lastoffline = $solorow['lastoffline'];
$updatedb = false;
$offlinehours = $solorow['offline'];
if ($solorow['medals'] < $medalsgrowlimit && $medalsecs + $solorow['lastredeem'] <= $nowtime) { //结算体力自然增长
$addmedals = ($nowtime - $solorow['lastredeem']) / $medalsecs;
$lastredeem = $solorow['lastredeem'] + $addmedals * $medalsecs;
$medals = $addmedals + $solorow['medals'];
$updatedb = true;
} else {
$medals = $solorow['medals'];
$lastredeem = $solorow['lastredeem'];
}
if ($lastoffline + 3600 <= $nowtime) {
$offlinehours += ($nowtime - $lastoffline) / 3600;
$offlinelimit = metatable\getParameterById(OFFLINE_LIMIT) + $privilege->getOfflineLimitPlus($account_id);
if ($offlinehours > $offlinelimit) {
$offlinehours = $offlinelimit;
}
$updatedb = true;
} else if ($lastoffline + 1800 >= $nowtime) {
$updatedb = true;
}
if ($updatedb) {
$ret = $conn->execScript(
'UPDATE solo SET medals=:medals, lastredeem=:lastredeem, lastoffline=:lastoffline, offline=:offline, modify_time=:modify_time' .
' WHERE accountid=:accountid;',
array(
':accountid' => $account_id,
':medals' => $medals,
':lastredeem' => $lastredeem,
':lastoffline' => $nowtime,
':offline' => $offlinehours,
':modify_time' => $nowtime
)
);
}
$level = $solorow['level'];
$exp = $solorow['exp'];
if (!is_null($solorow['gamelevel']) && !empty($solorow['gamelevel'])) {
$gamelevel = json_decode($solorow['gamelevel']);
}
}
$upexp = 0;
$lvinfo = $this->getPlayerLevelInfo($level);
if ($lvinfo) {
$upexp = $lvinfo['experience'];
}
echo json_encode(array(
'errcode' => 0,
'errmsg' => '',
'level' => $level,
'exp' => $exp,
'upexp' => $upexp,
'medals' => $medals,
'medal_limit' => $medalsgrowlimit,
'lastredeem' => $lastredeem,
'offlinehours' => $offlinehours,
'gamelevel' => $gamelevel
));
}
public function offlineAward()
{
$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;
}
$solorow = $conn->execQueryOne(
'SELECT offline, gamelevel FROM solo WHERE accountid=:accountid;',
array(
':accountid' => $account_id
)
);
if (!$solorow || $solorow['offline'] < 1 || is_null($solorow['gamelevel']) || $solorow['gamelevel'] == '') {
phpcommon\sendError(ERR_USER_BASE + 1, '没有这个玩家 2');
return;
}
$gamelevelinfo = json_decode($solorow['gamelevel'], true);
if ($gamelevelinfo['curlevel'] < 1) {
phpcommon\sendError(ERR_USER_BASE + 1, '没有这个玩家 3');
return;
}
$levelcfg = $this->getGameLevelInfo($gamelevelinfo['curlevel']);
if (!$levelcfg) {
phpcommon\sendError(ERR_USER_BASE + 1, '没有这个玩家 4');
return;
}
$offlinehours = $solorow['offline'];
$fixawards = explode($levelcfg['offlineitem_id'], '|');
$item_list = array();
$all_item_list = array();
foreach ($fixawards as $fixitemstr) {
$itemstrs = explode($fixitemstr, ':');
if (count($itemstrs) < 2) {
continue;
}
$item_list[] = array(
'item_id' => $itemstrs[0],
'item_num' => $itemstrs[1] * $offlinehours,
'time' => 0
);
}
$droplist = explode($levelcfg['offlineitem_id2'], '|');
$dropawards = array();
for ($i = 0; $i < $offlinehours; $i++) {
foreach ($droplist as $dropid) {
$dropitems = array();
$dropitems = metatable\getDropAllListById($dropid, $dropitems);
foreach ($dropitems as $dropitem) {
$itemid = $dropitem['item_id'];
$itemnum = $dropitem['item_num'];
if ($itemid == 0 || $itemnum == 0) {
continue;
}
$dropawards[$itemid] += $itemnum;
}
}
}
foreach ($dropawards as $itemid => $itemnum) {
$item_list[] = array(
'item_id' => $itemid,
'item_num' => $itemnum,
'time' => 0
);
}
$addreward = new classes\AddReward();
foreach ($item_list as $itemaward) {
$items = $addreward->addReward($itemaward['item_id'], $itemaward['item_num'], $account_id, 0, 0);
foreach ($items as $i) {
array_push($all_item_list, array(
'item_id' => $i['item_id'],
'item_num' => $i['item_num'],
'time' => 0,
));
}
}
$coin_num = $addreward->getCoinNum($account_id);
$diamond_num = $addreward->getDiamondNum($account_id);
$adfree = $addreward->getAdfree($account_id);
$medals = $addreward->getMedals($account_id);
echo json_encode(array(
'errcode' => 0,
'errmsg' => '',
'offlinehours' => $offlinehours,
'coin_nums' => $coin_num,
'diamond_nums' => $diamond_num,
'adfree' => $adfree,
'medals' => $medals,
'item_list' => $item_list,
'all_item_list' => $all_item_list,
));
}
// 战斗结算
public function settle()
{
$this->levelaward(true);
}
// 领取关卡宝箱
public function complete()
{
$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;
}
$solorow = $conn->execQueryOne(
'SELECT * FROM solo WHERE accountid=:accountid;',
array(
':accountid' => $account_id
)
);
if (!$solorow) {
phpcommon\sendError(ERR_USER_BASE + 1, '没有这个玩家 1');
return;
}
$gamelevel = array(
'curlevel' => 1,
'curwave' => 0,
'levelinfo' => array(),
);
if (!is_null($solorow['gamelevel']) && !empty($solorow['gamelevel'])) {
$gamelevel = json_decode($solorow['gamelevel'], true);
}
$level = $_REQUEST['level'];
$wave = $_REQUEST['wave'];
if (!array_key_exists($level, $gamelevel['levelinfo'])) {
phpcommon\sendError(ERR_USER_BASE + 1, '没有这个奖励');
return;
}
$foundwave = false;
foreach ($gamelevel['levelinfo'][$level] as $key => $waveitem) {
if ($wave == $waveitem) {
$foundwave = true;
unset($gamelevel['levelinfo'][$level][$key]);
if (count($gamelevel['levelinfo'][$level]) == 0) {
unset($gamelevel['levelinfo'][$level]);
}
break;
}
}
if (!$foundwave) {
phpcommon\sendError(ERR_USER_BASE + 1, '没有这个奖励 1');
return;
}
$levelcfg = $this->getGameLevelInfo($level);
if (!$levelcfg) {
phpcommon\sendError(ERR_USER_BASE + 1, '没有这个奖励 2');
return;
}
$wavearr = explode($levelcfg['completionwave'], '|');
$foundwave = false;
$item_list = array();
foreach ($wavearr as $key => $waveitem) {
if ($wave != $waveitem) {
continue;
}
$waveawardarr = explode($levelcfg['completionreward'], '|');
if (!array_key_exists($key, $waveawardarr)) {
break;
}
$awardarr = explode($waveawardarr[$key], ';');
foreach($awardarr as $awarditem) {
$strs = explode($awarditem, ':');
if (count($strs) < 2) {
continue;
}
$item_list[]=array(
'item_id' => $strs[0],
'item_num' => $strs[1],
"time" => 0,
);
}
$foundwave = true;
}
if (!$foundwave) {
phpcommon\sendError(ERR_USER_BASE + 1, '没有这个奖励 3');
return;
}
$ret = $conn->execScript(
'UPDATE solo SET gamelevel=:gamelevel, modify_time=:modify_time' .
' WHERE accountid=:accountid;',
array(
':accountid' => $account_id,
':gamelevel' => json_encode($gamelevel),
':modify_time' => time()
)
);
if (!$ret) {
phpcommon\sendError(ERR_RETRY + 1, '系统繁忙');
return;
}
$addreward = new classes\AddReward();
$all_item_list = array();
foreach ($item_list as $itemaward) {
$items = $addreward->addReward($itemaward['item_id'], $itemaward['item_num'], $account_id, 0, 0);
foreach ($items as $i) {
array_push($all_item_list, array(
'item_id' => $i['item_id'],
'item_num' => $i['item_num'],
'time' => 0,
));
}
}
$coin_num = $addreward->getCoinNum($account_id);
$diamond_num = $addreward->getDiamondNum($account_id);
$adfree = $addreward->getAdfree($account_id);
$medals = $addreward->getMedals($account_id);
$response = array(
'errcode' => 0,
'errmsg' => '',
'level' => $level,
'wave' => $wave,
'coin_nums' => $coin_num,
'diamond_nums' => $diamond_num,
'adfree' => $adfree,
'medals' => $medals,
'item_list' => $item_list,
'all_item_list' => $all_item_list,
);
echo json_encode($response);
}
// 扫荡结算
public function sweep()
{
$this->levelaward(false);
}
protected function levelaward($isbattle)
{
$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;
}
$solorow = $conn->execQueryOne(
'SELECT * FROM solo WHERE accountid=:accountid;',
array(
':accountid' => $account_id
)
);
if (!$solorow) {
phpcommon\sendError(ERR_USER_BASE + 1, '没有这个玩家 1');
return;
}
$gamelevel = array(
'curlevel' => 1,
'curwave' => 0,
'levelinfo' => array(),
);
if (!is_null($solorow['gamelevel']) && !empty($solorow['gamelevel'])) {
$gamelevel = json_encode($solorow['gamelevel'], true);
}
$curlevelcfg = $this->getGameLevelInfo($gamelevel['curlevel']);
if (!$curlevelcfg) {
phpcommon\sendError(ERR_USER_BASE + 1, '关卡未通过');
return;
}
$curmonsterwaves = explode($curlevelcfg['monsterlist_id'], '|');
$curfinished = $gamelevel['curwave'] > 0 && $gamelevel['curwave'] == count($curmonsterwaves);
$level = $_REQUEST['level'];
if ($isbattle) {
if ((!$curfinished && $level > $gamelevel['curlevel']) ||
($curfinished && $level > $gamelevel['curlevel'] + 1)
) {
phpcommon\sendError(ERR_USER_BASE + 1, '关卡未通过 1');
return;
}
} else {
if ((!$curfinished && $level >= $gamelevel['curlevel']) ||
($curfinished && $level > $gamelevel['curlevel'])
) {
phpcommon\sendError(ERR_USER_BASE + 1, '关卡未通过 2');
return;
}
}
$reqlevelcfg = $this->getGameLevelInfo($level);
if (!$reqlevelcfg) {
phpcommon\sendError(ERR_USER_BASE + 1, '关卡未通过 3');
return;
}
if ($reqlevelcfg['medal_cost'] > $solorow['medals']) {
phpcommon\sendError(ERR_USER_BASE + 2, '体力不足');
return;
}
$reqmonsterwaves = explode($reqlevelcfg['monsterlist_id'], '|');
$reqfinished = $isbattle ? ($_REQUEST['wave'] > 0 && $_REQUEST['wave'] == count($reqmonsterwaves)) : true;
$item_list = array();
// fixed award
$fixedawards = explode($reqlevelcfg['fixedreward'], '|');
foreach ($fixedawards as $fixitem) {
$itemstrs = explode($fixitem, '|');
if (count($itemstrs) < 2) {
continue;
}
$item_list[] = array(
"item_id" => $itemstrs[0],
"item_num" => $itemstrs[1],
"time" => 0,
);
}
// level finished award
if ($reqfinished) {
$finishedawards = explode($reqlevelcfg['winreward'], '|');
foreach ($finishedawards as $finishitem) {
$itemstrs = explode($finishitem, '|');
if (count($itemstrs) < 2) {
continue;
}
$item_list[] = array(
"item_id" => $itemstrs[0],
"item_num" => $itemstrs[1],
"time" => 0,
);
}
}
if ($isbattle) {
// level box award
$wave = $_REQUEST['wave'];
if ($curfinished && $level == $gamelevel['curlevel'] + 1) {
$newwaves = array();
$completewaves = explode($reqlevelcfg['completionwave'], '|');
foreach ($completewaves as $waveitem) {
if ($wave >= $waveitem) {
$newwaves[] = $waveitem;
}
}
$gamelevel['levelinfo'][$level] = $newwaves;
$gamelevel['curlevel'] = $level;
$gamelevel['curwave'] = $wave;
} else if (!$curfinished && $level == $gamelevel['curlevel'] && $wave > $gamelevel['curwave']) {
if ($gamelevel['curwave'] == 0) {
$gamelevel['levelinfo'][$level] = array();
}
$completewaves = explode($reqlevelcfg['completionwave'], '|');
foreach ($completewaves as $waveitem) {
if ($wave >= $waveitem && $waveitem > $gamelevel['curwave']) {
$gamelevel['levelinfo'][$level][] = $waveitem;
}
}
$gamelevel['curwave'] = $wave;
}
}
// cost medals, add exp
$curexp = $solorow['exp'] + $reqlevelcfg['medal_cost'];
$curlv = $solorow['level'];
$curlvcfg = $this->getPlayerLevelInfo($curlv);
$uplv_item_list = array();
while ($curlvcfg && $curlvcfg['experience'] > 0 && $curexp >= $curlvcfg['experience']) {
$curexp -= $curlvcfg['experience'];
$curlv++;
$curlvcfg = $this->getPlayerLevelInfo($curlv);
if ($curlvcfg) {
$uplvawards = explode($curlvcfg['level_reward'], '|');
foreach ($uplvawards as $uplvitem) {
$itemstrs = explode($uplvitem, '|');
if (count($itemstrs) < 2) {
continue;
}
$uplv_item_list[] = array(
"item_id" => $itemstrs[0],
"item_num" => $itemstrs[1],
"time" => 0,
);
}
}
}
$curmedals = $solorow['medals'] - $reqlevelcfg['medal_cost'];
$lastredeem = $solorow['lastredeem'];
$privilege = new classes\Privilege();
$medalsgrowlimit = metatable\getParameterById(MEDAL_LIMIT) + $privilege->getMedalLimitPlus($account_id);
if ($solorow['medals'] >= $medalsgrowlimit && $curmedals < $medalsgrowlimit) {
$lastredeem = time();
}
$ret = $conn->execScript(
'UPDATE solo SET medals=:medals, lastredeem=:lastredeem, level=:level, exp=:exp, gamelevel=:gamelevel, modify_time=:modify_time' .
' WHERE accountid=:accountid;',
array(
':accountid' => $account_id,
':medals' => $curmedals,
':lastredeem' => $lastredeem,
':level' => $curlv,
':exp' => $curexp,
':gamelevel' => json_encode($gamelevel),
':modify_time' => time()
)
);
if (!$ret) {
phpcommon\sendError(ERR_RETRY + 1, '系统繁忙');
return;
}
$addreward = new classes\AddReward();
$all_item_list = array();
foreach ($item_list as $itemaward) {
$items = $addreward->addReward($itemaward['item_id'], $itemaward['item_num'], $account_id, 0, 0);
foreach ($items as $i) {
array_push($all_item_list, array(
'item_id' => $i['item_id'],
'item_num' => $i['item_num'],
'time' => 0,
));
}
}
$uplv_all_item_list = array();
foreach ($uplv_item_list as $itemaward) {
$items = $addreward->addReward($itemaward['item_id'], $itemaward['item_num'], $account_id, 0, 0);
foreach ($items as $i) {
array_push($uplv_all_item_list, array(
'item_id' => $i['item_id'],
'item_num' => $i['item_num'],
'time' => 0,
));
}
}
$coin_num = $addreward->getCoinNum($account_id);
$diamond_num = $addreward->getDiamondNum($account_id);
$adfree = $addreward->getAdfree($account_id);
$medals = $addreward->getMedals($account_id);
$response = array(
'errcode' => 0,
'errmsg' => '',
'level' => $level,
'coin_nums' => $coin_num,
'diamond_nums' => $diamond_num,
'adfree' => $adfree,
'medals' => $medals,
'item_list' => $item_list,
'all_item_list' => $all_item_list,
'playerlvup' => array(
'item_list' => $uplv_item_list,
'all_item_list' => $uplv_all_item_list,
)
);
if ($isbattle) {
$response['wave'] = $_REQUEST['wave'];
}
echo json_encode($response);
}
}