diff --git a/doc/GameOver.py b/doc/GameOver.py index ad17630..b2a5330 100644 --- a/doc/GameOver.py +++ b/doc/GameOver.py @@ -7,51 +7,59 @@ class GameOver(object): def __init__(self): self.apis = [ { - 'desc': 'firstRewardInfo', + 'desc': '首战奖励信息firstRewardInfo', 'group': 'GameOver', 'url': 'webapp/index.php?c=GameOver&a=firstRewardInfo', 'params': [ _common.ReqHead(), + ['rank', 0, '排名'], + ['type', 0, '类型'], + ['coin', 0, '金币'], + ['kills', 0, '击杀数'], ], 'response': [ _common.RspHead(), + ['first_uuid', '', '唯一id'], + ['!item_list', _common.RewardItemInfo, '奖励信息列表'], + ['coin', 0, '金币'], + ['score', 0, '积分'], ] }, { - 'desc': 'getFirstReward', + 'desc': '获取首战奖励getFirstReward', 'group': 'GameOver', 'url': 'webapp/index.php?c=GameOver&a=getFirstReward', 'params': [ _common.ReqHead(), + ['type', 0, '类型'], + ['first_uuid', '', '唯一id'], ], 'response': [ _common.RspHead(), + ['!item_list', _common.DropItemInfo, '掉落道具列表'], + ['coin_num', 0, '当前金币数'], + ['rmb_num', 0, '当前点券数量'], + ['!all_item_list', _common.RewardItemInfo, '实际奖励的道具列表'], ] }, { - 'desc': 'getFuckBoxReward', + 'desc': '获取Fuck宝箱?getFuckBoxReward', 'group': 'GameOver', 'url': 'webapp/index.php?c=GameOver&a=getFuckBoxReward', 'params': [ _common.ReqHead(), + ['id', 0, 'id'], ], 'response': [ _common.RspHead(), + ['!item_list', _common.DropItemInfo, '掉落道具列表'], + ['coin_num', 0, '当前金币数'], + ['rmb_num', 0, '当前点券数量'], + ['!all_item_list', _common.RewardItemInfo, '实际奖励的道具列表'], ] }, { - 'desc': 'getDiamond', - 'group': 'GameOver', - 'url': 'webapp/index.php?c=GameOver&a=getDiamond', - 'params': [ - _common.ReqHead(), - ], - 'response': [ - _common.RspHead(), - ] - }, - { - 'desc': 'updateDiamond', + 'desc': '待删除updateDiamond', 'group': 'GameOver', 'url': 'webapp/index.php?c=GameOver&a=updateDiamond', 'params': [ @@ -62,18 +70,23 @@ class GameOver(object): ] }, { - 'desc': 'addSpoilsItems', + 'desc': '添加战利品addSpoilsItems', 'group': 'GameOver', 'url': 'webapp/index.php?c=GameOver&a=addSpoilsItems', 'params': [ _common.ReqHead(), + ['data', '', '数据json格式[{"values":[item_id, item_num]}]'], ], 'response': [ _common.RspHead(), + ['!item_list', _common.DropItemInfo, '掉落道具列表'], + ['coin_num', 0, '当前金币数'], + ['rmb_num', 0, '当前点券数量'], + ['!all_item_list', _common.RewardItemInfo, '实际奖励的道具列表'], ] }, { - 'desc': 'fightRevive', + 'desc': '待删除fightRevive', 'group': 'GameOver', 'url': 'webapp/index.php?c=GameOver&a=fightRevive', 'params': [ @@ -84,7 +97,7 @@ class GameOver(object): ] }, { - 'desc': 'addGameTimes', + 'desc': '添加游戏次数addGameTimes', 'group': 'GameOver', 'url': 'webapp/index.php?c=GameOver&a=addGameTimes', 'params': [ diff --git a/webapp/controller/BaseAuthedController.class.php b/webapp/controller/BaseAuthedController.class.php index 3812302..543397f 100644 --- a/webapp/controller/BaseAuthedController.class.php +++ b/webapp/controller/BaseAuthedController.class.php @@ -605,4 +605,23 @@ class BaseAuthedController extends BaseController { ); return $row ? $row['using_id'] : 0; } + + protected function redisSetAndExpire($pk, $key, $val, $time) + { + $r = $this->getRedis($pk); + $r->set($key, $val); + $r->pexpire($key, $time); + } + + protected function redisGetJson($pk, $key) + { + $r = $this->getRedis($pk); + $dataStr = $r->get($key); + $result = null; + if (!empty($dataStr)) { + $result = json_decode($dataStr, true); + } + return $result; + } + } diff --git a/webapp/controller/GameOverController.class.php b/webapp/controller/GameOverController.class.php index c473da6..52e9d37 100644 --- a/webapp/controller/GameOverController.class.php +++ b/webapp/controller/GameOverController.class.php @@ -2,54 +2,24 @@ require 'classes/Quest.php'; require 'classes/AddReward.php'; -require_once 'metatable/parameter.php'; require_once('mt/Parameter.php'); require_once('mt/Drop.php'); require_once('mt/RankReward.php'); require_once('mt/Equip.php'); +require_once('mt/Box.php'); class GameOverController extends BaseAuthedController { - protected function getBox($box_id) - { - $box_meta_table = require('../res/box@box.php'); - $box_meta = getBoxConfig($box_meta_table, $box_id); - $b = array( - 'box_id' => $box_meta['box_type'], - 'item_id' => $box_meta['item_id'], - 'num' => $box_meta['num'], - 'weight' => $box_meta['weight'], - 'type' => $box_meta['type'], - 'time' => $box_meta['time'], - 'count' => $box_meta['count'], - ); - return $b; - } - public function firstRewardInfo() { - $account_id = $_REQUEST['account_id']; - - $first_uuid = 'game2005api_first_uuid:' . $_REQUEST['account_id']; - $r = $this->getRedis($first_uuid); - if (!$r) { - die(); - return; - } + $first_uuid = 'game2005api_first_uuid:' . $this->getAccountId(); $rank = $_REQUEST['rank']; - $type = 0; + $type = isset($_REQUEST['type']) ? $_REQUEST['type'] : 0; $coin = 0; $score = 0; - $kills = 0; - $normal_coin = 0; $num = 1; - if (isset($_REQUEST['type'])) { - $type = $_REQUEST['type']; - } - $p = mt\Parameter::getOldParam(EQUIPREWARD_PARAMETER); - $par = $p['param_value']; - + $par = mt\Parameter::getVal('equipreward_parameter', 0); if ($type == 1) { $ar = mt\RankReward::getOldRankReward($rank); $coin = $ar['zbmode_param']; @@ -58,25 +28,21 @@ class GameOverController extends BaseAuthedController { $score = $_REQUEST['kills'] * 10; } } else if ($type == 0) { - if (isset($_REQUEST['coin'])) { - $num = ceil($_REQUEST['coin'] / $par); - } + $num = isset($_REQUEST['coin']) ? ceil($_REQUEST['coin'] / $par) : $num; } - //道具物品 $first_list = array(); array_push($first_list, array( 'item_id' => 10010, 'item_num' => $num, 'time' => 0, )); - $first_list = $this->randomBox($first_list, 8); - $first_list = $this->randomBox($first_list, 9); + mt\Box::randomBox($firstList, 8); + mt\Box::randomBox($firstList, 9); $first_db = array( 'first_uuid' => $first_uuid, 'first_list' => $first_list, ); - $r -> set($first_uuid, json_encode($first_db)); - $r -> pexpire($first_uuid, 1000 * 3600 * 2); + $this->redisSetAndExpire($first_uuid, $first_uuid, json_encode($first_db), 1000 * 3600 * 2); echo json_encode(array( 'errcode' => 0, @@ -90,33 +56,27 @@ class GameOverController extends BaseAuthedController { public function getFirstReward() { - $account_id = $_REQUEST['account_id']; $type = $_REQUEST['type']; $first_uuid = $_REQUEST['first_uuid']; - $item_id = 0; - $item_num = 0; - $flag = 0; $addreward = new classes\AddReward(); $item_list = array(); $all_item_list = array(); if ($type == 0) { - $item_list = $this->fixReward($item_list, 5); + mt\Box::randomBoxNN(5, $itemList); } else { - $r = $this->getRedis($first_uuid); - $user_db_str = $r->get($first_uuid); - if (empty($user_db_str)) { + $info = $this->redisGetJson($first_uuid, $first_uuid); + if (empty($info)) { phpcommon\sendError(ERR_USER_BASE + 1,'session失效'); return; } - $user_db = json_decode($user_db_str, true); - if (empty($user_db)) { - phpcommon\sendError(ERR_USER_BASE + 1,'session失效'); - return; - } - $item_list = $user_db['first_list']; + $item_list = $info['first_list']; } foreach($item_list as $it) { - $items = $addreward->addReward($it['item_id'], $it['item_num'], $account_id, $it['time'], 0); + $items = $addreward->addReward($it['item_id'], + $it['item_num'], + $this->getAccountId(), + $it['time'], + 0); foreach($items as $j) { array_push($all_item_list, array( 'item_id' => $j['item_id'], @@ -125,159 +85,33 @@ class GameOverController extends BaseAuthedController { )); } } - $coin_num = $addreward->getCoinNum($account_id); - $rmb_num = $addreward->getRmbNum($account_id); + $userInfo = $this->getUserInfo(array( + 'coin_num', + 'rmb_num', + )); echo json_encode(array( 'errcode' => 0, 'errmsg'=> '', - 'coin_nums' => $coin_num, - 'rmb_nums' => $rmb_num, + 'coin_nums' => $userInfo['coin_num'], + 'rmb_nums' => $userInfo['rmb_num'], 'item_list' => $item_list, 'all_item_list' => $all_item_list )); } - protected function fixReward($item_list, $type) - { - $b1 = $this->getbox($type); - if (!$b1) { - phpcommon\sendError(ERR_USER_BASE + 3, '没有这个奖励'); - die(); - } - $fixed_id = $this->getExplode($b1['item_id']); - $fixed_time = $this->getExplode($b1['time']); - $fixed_num = $this->getExplode($b1['num']); - $fixed_array = $this->getExplode($b1['weight']); - - for($i = 0; $i < count($fixed_id); $i++) { - $random = Rand(0, 10000); - if ($fixed_array[$i][0] >= $random) { - array_push($item_list, array( - 'item_id' => $fixed_id[$i][0], - 'item_num' => $fixed_num[$i][0], - 'time' => $fixed_time[$i][0] - )); - } - } - return $item_list; - } - - protected function randomBox($item_list, $type) { - $b1 = $this->getbox($type); - if (!$b1) { - phpcommon\sendError(ERR_USER_BASE + 3, '没有这个奖励'); - die(); - } - $fixed_id = $this->getExplode($b1['item_id']); - $fixed_time = $this->getExplode($b1['time']); - $fixed_num = $this->getExplode($b1['num']); - $fixed_array = $this->getExplode($b1['weight']); - $sum = 0; - for($i = 0; $i < count($fixed_array); $i++) { - $sum = $fixed_array[$i][0] + $sum; - } - - $rm = Rand(0, $sum); - $key = 0; - for($j = 0; $j < count($fixed_array); $j++) { - $key = $fixed_array[$j][0] + $key; - if ($key >= $rm) { - array_push($item_list, array( - 'item_id' => $fixed_id[$j][0], - 'item_num' => $fixed_num[$j][0], - 'time' => $fixed_time[$j][0] - )); - if ($type == 8) { - array_push($item_list, array( - 'item_id' => $fixed_id[$j][0] + 2000, - 'item_num' => $fixed_num[$j][0], - 'time' => $fixed_time[$j][0] - )); - - } - break; - } - } - return $item_list; - } - protected function randomReward($rank,$type) - { - //随机奖励 - $r = mt\RankReward::getOldRankReward($rank); - $b = mt\Drop::getOldDrop($r['ad_drop']); - $count = $r['ad_num']; - if ($type == 1) { - $b = mt\Drop::getOldDrop($r['js_drop']); - $count = $r['js_num']; - } - $item_list = array(); - $item_id_array = $this->getExplode($b['item_id']); - $item_num_array = $this->getExplode($b['num']); - $weight_array = $this->getExplode($b['weight']); - $reward_array = array(); - for ($c = 0; $c < $count; $c++) { - $weight_sum = 0; - $keys = 0; - for ($ii = 0; $ii < count($weight_array); $ii++) { - $flag = $this->removal($reward_array, $item_id_array[$ii][0]); - if ($flag == 1) { - continue; - } - $weight_sum += $weight_array[$ii][0]; - } - $random = Rand(0, $weight_sum); - $weight = 0; - for ($ii = 0; $ii < count($weight_array); $ii++) { - $flag = $this->removal($reward_array, $item_id_array[$ii][0]); - if ($flag == 1) { - continue; - } - $weight += $weight_array[$ii][0]; - if ($weight >= $random) { - $keys = $ii; - break; - } - } - $item_id = $item_id_array[$keys][0]; - $item_num = $item_num_array[$keys][0]; - $time = 0; - array_push($reward_array, array( - 'id' => $item_id - )); - array_push($item_list, array( - 'item_id' => $item_id, - 'item_num' => $item_num, - 'time' => $time - )); - } - return $item_list; - } - - protected function removal($reward, $id) - { - $flag = 0; - foreach ($reward as $r) { - if ($id == $r['id']) { - $flag = 1; - break; - } - } - return $flag; - } - public function getFuckBoxReward() { - $account_id = $_REQUEST['account_id']; - $id = 10; - if (isset($_REQUEST['id'])) { - $id = $_REQUEST['id']; - } + $id = isset($_REQUEST['id']) ? $_REQUEST['id'] : 10; $item_list = array(); $all_item_list = array(); - $item_list = $this->randomBoxNew($id); + mt\Box::randomBoxNew($id, $itemList); $addreward = new classes\AddReward(); foreach($item_list as $it) { - $items = $addreward->addReward($it['item_id'], $it['item_num'], $account_id, $it['time'], 0); + $items = $addreward->addReward($it['item_id'], + $it['item_num'], + $this->getAccountId(), + $it['time'], + 0); foreach($items as $j) { array_push($all_item_list, array( 'item_id' => $j['item_id'], @@ -286,117 +120,54 @@ class GameOverController extends BaseAuthedController { )); } } - $coin_num = $addreward->getCoinNum($account_id); - $rmb_num = $addreward->getRmbNum($account_id); + $userInfo = $this->getUserInfo(array( + 'coin_num', + 'rmb_num' + )); echo json_encode(array( 'errcode' => 0, 'errmsg'=> '', - 'coin_nums' => $coin_num, - 'rmb_nums' => $rmb_num, + 'coin_nums' => $userInfo['coin_num'], + 'rmb_nums' => $userInfo['rmb_num'], 'item_list' => $item_list, 'all_item_list' => $all_item_list )); } - protected function randomBoxNew($type) - { - //随机奖励 - $b = $this->getBox($type); - $count = $b['count']; - $item_list = array(); - $item_id_array = $this->getExplode($b['item_id']); - $item_num_array = $this->getExplode($b['num']); - $item_time_array = $this->getExplode($b['time']); - $weight_array = $this->getExplode($b['weight']); - $reward_array = array(); - for ($c = 0; $c < $count; $c++) { - $weight_sum = 0; - $keys = 0; - for ($ii = 0; $ii < count($weight_array); $ii++) { - $flag = $this->removal($reward_array, $item_id_array[$ii][0]); - if ($flag == 1) { - continue; - } - $weight_sum += $weight_array[$ii][0]; - } - $random = Rand(0, $weight_sum); - $weight = 0; - for ($ii = 0; $ii < count($weight_array); $ii++) { - $flag = $this->removal($reward_array, $item_id_array[$ii][0]); - if ($flag == 1) { - continue; - } - $weight += $weight_array[$ii][0]; - if ($weight >= $random) { - $keys = $ii; - break; - } - } - $item_id = $item_id_array[$keys][0]; - $item_num = $item_num_array[$keys][0]; - $time = $item_time_array[$keys][0]; - array_push($reward_array, array( - 'id' => $item_id - )); - array_push($item_list, array( - 'item_id' => $item_id, - 'item_num' => $item_num, - 'time' => $time - )); - } - return $item_list; - } - - public function getDiamond() - { - $account_id = $_REQUEST['account_id']; - $addreward = new classes\AddReward(); - $p = mt\Parameter::getOldParam(CLAN_VIDEO_DIAMOND); - $par = $p['param_value']; - $items = $addreward->addReward(10003, $par, $account_id, 0, 0); - $rmb_num = $addreward->getRmbNum($account_id); - echo json_encode(array( - 'errcode' => 0, - 'errmsg'=> '', - 'rmb_nums' => $rmb_num, - )); - } - public function updateDiamond() { - $account_id = $_REQUEST['account_id']; - $conn = $this->getMysql($account_id); - $rowUser = $conn->execQueryOne('SELECT rmb_num FROM user WHERE accountid=:accountid;', - array( - ':accountid' => $account_id, - )); - if (!$rowUser) { - phpcommon\sendError(ERR_USER_BASE + 2, '没有这个玩家'); - return; - } - $p = mt\Parameter::getOldParam(CLAN_DIAMOND); - $par = $p['param_value']; - if ($rowUser['rmb_num'] < $par) { + $userInfo = $this->getUserInfo(array( + 'rmb_num' + )); + $par = mt\Parameter::getVal('clan_diamond', 0); + if ($userInfo['rmb_num'] < $par) { $par = 0; } $addreward = new classes\AddReward(); $items = $addreward->addReward(10003, -$par, $account_id, 0, 0); - $rmb_num = $addreward->getRmbNum($account_id); + $newUserInfo = $this->getUserInfo(array( + 'rmb_num' + )); echo json_encode(array( 'errcode' => 0, 'errmsg'=> '', - 'rmb_nums' => $rmb_num, + 'rmb_nums' => $userInfo['rmb_num'], )); } public function addSpoilsItems() { - $account_id = $_REQUEST['account_id']; - $conn = $this->getMysql($account_id); - $arr = json_decode($_REQUEST['data'], true); + /* + data: [ + { + "values":[item_id, item_num] + } + ] + */ $addreward = new classes\AddReward(); $item_list = array(); $all_item_list = array(); + $arr = json_decode($_REQUEST['data'], true); if (!$arr) { phpcommon\sendError(ERR_USER_BASE + 3, '没有这个道具'); return; @@ -417,13 +188,15 @@ class GameOverController extends BaseAuthedController { )); } } - $coin_num = $addreward->getCoinNum($account_id); - $diamond_num = $addreward->getDiamondNum($account_id); + $userInfo = $this->getUserInfo(array( + 'coin_num', + 'rmb_num' + )); echo json_encode(array( 'errcode' => 0, 'errmsg'=> '', - 'coin_nums' => $coin_num, - 'diamond_nums' => $diamond_num, + 'coin_nums' => $userInfo['coin_num'], + 'diamond_nums' => $userInfo['rmb_num'], 'item_list' => $item_list, 'all_item_list' => $all_item_list )); @@ -431,31 +204,19 @@ class GameOverController extends BaseAuthedController { public function fightRevive() { - $account_id = $_REQUEST['account_id']; - $conn = $this->getMysql($account_id); - $rowUser = $conn->execQueryOne('SELECT rmb_num FROM user WHERE accountid=:accountid;', - array( - ':accountid' => $account_id, - )); - if (!$rowUser) { - phpcommon\sendError(ERR_USER_BASE + 2, '没有这个玩家'); - return; - } - $cost = metatable\getParameterByName('revive_money'); - if ($cost > $rowUser['rmb_num']) { + $userInfo = $this->getUserInfo(array( + 'rmb_num' + )); + $cost = mt\Parameter::getVal('revive_money', 0); + if ($cost > $userInfo['rmb_num']) { phpcommon\sendError(ERR_USER_BASE + 3, '点券不足'); return; } - $ret = $conn->execScript('UPDATE user SET rmb_num=:rmb_num ' . - 'WHERE accountid=:accountid;', - array( - ':accountid' => $account_id, - ':rmb_num' => $rowUser['rmb_num'] - $cost, - )); - if (!$ret) { - die(); - return; - } + $this->updateUserInfo(array( + 'rmb_num' => function () { + return "rmb_num - ${cost}"; + } + )); echo json_encode(array( 'errcode' => 0, 'errmsg'=> '', @@ -464,40 +225,20 @@ class GameOverController extends BaseAuthedController { public function addGameTimes() { - $row = phpcommon\SqlHelper::selectOne - ($this->getSelfMysql(), - 'user', - array( - 'game_times', - ), - array( - 'accountid' => $this->getAccountId() - ) - ); - if (!$row) { - phpcommon\sendError(ERR_USER_BASE + 1, '没有这个玩家'); - return; - } - - $ret = phpcommon\SqlHelper::update - ($this->getSelfMysql(), - 'user', - array( - 'accountid' => $this->getAccountId() - ), - array( - 'game_times' => $row['game_times'] + 1, - 'modify_time' => $this->getNowTime() - ) - ); - if (!$ret) { - die(); - return; - } + $this->updateUserInfo(array( + 'game_times' => function () { + return 'game_times + 1'; + }, + 'modify_time' => $this->getNowTime() + )); + $userInfo = $this->getUserInfo(array( + 'game_times' + )); echo json_encode(array( 'errcode' => 0, 'errmsg'=> '', - 'game_times' => $row['game_times'] + 1, + 'game_times' => $userInfo['game_times'], )); } + } diff --git a/webapp/mt/Box.php b/webapp/mt/Box.php new file mode 100644 index 0000000..406f949 --- /dev/null +++ b/webapp/mt/Box.php @@ -0,0 +1,136 @@ + $$meta['box_type'], + 'item_id' => $$meta['item_id'], + 'num' => $$meta['num'], + 'weight' => $$meta['weight'], + 'type' => $$meta['type'], + 'time' => $$meta['time'], + 'count' => $$meta['count'], + ); + } + + public static function randomBox($id, &$itemList, $cb = null) + { + $meta = self::get($id); + if ($meta) { + $ids = splitStr1($meta['item_id']); + $times = splitStr1($meta['time']); + $nums = splitStr1($meta['num']); + $weights = splitStr1($meta['weight']); + $totalWeights = 0; + foreach ($weights as $weight) { + $totalWeights += $weight; + } + if (count($ids) == count($times) && + count($ids) == count($nums) && + count($ids) == count($weights) && + count($ids) > 0 && + $totalWeights > 0 + ) { + $rnd = rand(0, $totalWeights); + $currWeight = 0; + $i = 0; + foreach ($weights as $weight) { + $currWeight += $weight; + if ($currWeight > $rnd) { + if ($cb && !$cb($ids[i])){ + continue; + } + array_push($itemList, array( + 'item_id' => $ids[$i], + 'item_num' => $nums[$i], + 'time' => $times[$i] + )); + if ($id == 8) { //这是为啥??? + array_push($itemList, array( + 'item_id' => $ids[$i]+ 2000, + 'item_num' => $nums[$i], + 'time' => $times[$i] + )); + + } + break; + }//end if + ++$i; + } + } + } + } + + public static function randomBoxNN($id, &$itemList) + { + $meta = self::get($id); + if ($meta) { + $ids = splitStr1($meta['item_id']); + $times = splitStr1($meta['time']); + $nums = splitStr1($meta['num']); + $weights = splitStr1($meta['weight']); + if (count($ids) == count($times) && + count($ids) == count($nums) && + count($ids) == count($weights) && + count($ids) > 0 + ) { + $i = 0; + foreach ($weights as $weight) { + $rnd = rand(0, 10000); + if ($weight >= $rnd) { + array_push($itemList, array( + 'item_id' => $ids[$i], + 'item_num' => $nums[$i], + 'time' => $times[$i] + )); + }//end if + ++$i; + } + } + } + } + + public static function randomBoxNew($id, &$itemList) + { + $meta = self::get($id); + if ($meta) { + $cb = function ($itemId) use(&$itemList) { + foreach ($itemList as $item) { + if ($item['item_id'] == $itemId) { + return false; + } + } + return true; + }; + for ($i = 0; $i < $meta['count']; ++$i) { + self::randomBox($id, $itemList, $cb); + } + } + } + + protected static function getMetaList() + { + if (!self::$metaList) { + self::$metaList = getMetaTable('box@box.php'); + } + return self::$metaList; + } + + protected static $metaList; + +}