diff --git a/webapp/controller/SupplyBoxController.class.php b/webapp/controller/SupplyBoxController.class.php new file mode 100644 index 0000000..ab0ef1c --- /dev/null +++ b/webapp/controller/SupplyBoxController.class.php @@ -0,0 +1,261 @@ + $mysql_conf['host'], + 'port' => $mysql_conf['port'], + 'user' => $mysql_conf['user'], + 'passwd' => $mysql_conf['passwd'], + 'dbname' => 'gamedb2001_' . $mysql_conf['instance_id'] + )); + return $conn; + } + + protected function getSupplyBox($box_id) + { + $g_conf_supply_cluster = require('../config/game2001api.supplybox.cluster.php'); + $supply_conf = getSupplyConfig($g_conf_supply_cluster, $box_id); + $s = array( + 'id' => $supply_conf['id'], + 'drop' => $supply_conf['drop'], + 'num' => $supply_conf['num'], + 'drop_free' => $supply_conf['drop_free'], + 'num_free' => $supply_conf['num_free'], + 'price' => $supply_conf['price'], + 'parameter' => $supply_conf['parameter'], + ); + return $s; + } + + protected function getDrop($drop_id) + { + $g_conf_drop_cluster = require('../config/game2001api.drop.cluster.php'); + $drop_conf = getDropConfig($g_conf_drop_cluster, $drop_id); + $d = array( + 'drop_id' => $drop_conf['drop_id'], + 'item_id' => $drop_conf['item_id'], + 'num' => $drop_conf['num'], + 'weight' => $drop_conf['weight'], + 'type' => $drop_conf['type'] + ); + return $d; + } + + protected function getExplode($string) + { + $delim = "|"; + $drop_multiply = explode($delim, $string); + $delim1 = ":"; + $arr = array(); + for ($i = 0; $i < count($drop_multiply); $i++) { + $mul = explode($delim1, $drop_multiply[$i]); + array_push($arr, $mul); + } + return $arr; + } + + public function supplyBoxInfo() + { + $account_id = $_REQUEST['account_id']; + $conn = $this->getMysql($account_id); + $box_list = array(); + if (!$conn) { + phpcommon\sendError(ERR_USER_BASE + 1, '没有这个玩家'); + return; + } + $rowCount = $conn->execQueryRowCount('SELECT * FROM supplybox WHERE accountid = :account_id', + array( + ':account_id' => $account_id + )); + if ($rowCount != 0) { + $rows = $conn->execQuery('SELECT * FROM supplybox WHERE accountid = :account_id', + array( + ':account_id' => $account_id + )); + foreach ($rows as $row) { + array_push($box_list, array( + 'box_id' => $row['box_id'], + 'box_num' => $row['box_num'], + 'buy_times' => $row['buy_times'] + )); + } + } + echo json_encode(array( + 'errcode' => 0, + 'errmsg' => '', + 'supplybox_list' => $box_list + )); + } + + + public function openSupplyBox() + { + $account_id = $_REQUEST['account_id']; + $conn = $this->getMysql($account_id); + if (!$conn) { + phpcommon\sendError(ERR_USER_BASE + 1, '没有这个玩家'); + return; + } + $box_id = $_REQUEST['box_id']; + $free_open = $_REQUEST['free_open']; + $s = $this->getSupplyBox($box_id); + if (!$s) { + phpcommon\sendError(ERR_USER_BASE + 2, '没有这个宝箱'); + return; + } + $drop_id = 0; + if ($free_open == 0) { + $drop_id = $s['drop']; + } else { + $drop_id = $s['drop_free']; + } + $d = $this->getDrop($drop_id); + if (!$d) { + phpcommon\sendError(ERR_USER_BASE + 3, '没有这个奖励'); + return; + } + + $item_list = array(); + $row = $conn->execQueryOne('SELECT * FROM supplybox WHERE accountid=:accountid AND box_id=:box_id', + array( + ':accountid' => $account_id, + ':box_id' => $box_id + )); + if (!$row) { + phpcommon\sendError(ERR_USER_BASE + 2, '没有这个宝箱'); + return; + } + if ($row['box_num'] <= 0) { + phpcommon\sendError(ERR_USER_BASE + 3, '宝箱数量不足'); + return; + } + $box_num = $row['box_num'] - 1; + $ret = $conn->execScript('UPDATE supplybox SET box_num=:box_num ' . + ' WHERE accountid=:accountid AND box_id=:box_id;', + array( + ':accountid' => $account_id, + ':box_id' => $box_id, + ':box_num' => $box_num + )); + if (!$ret) { + die(); + return; + } + //确定奖励倍数 + $sum = 0; + $mul = 0; + $array = $this->getExplode($s['num']); + for ($i = 0; $i < count($array); $i++) { + $sum += $array[$i][1]; + } + $rand = Rand(0, $sum); + $multiply = 0; + for ($i = 0; $i < count($array); $i++) { + $multiply += $array[$i][1]; + if ($multiply > $rand) { + $mul = $array[$i][0]; + break; + } + } + //发送奖励 + $weight_sum = 0; + $weight_array = $this->getExplode($d['weight']); + for ($i = 0; $i < count($weight_array); $i++) { + $weight_sum += $weight_array[$i][0]; + } + $random = Rand(0, $weight_sum); + $weight = 0; + for ($i = 0; $i < count($weight_array); $i++) { + $weight += $weight_array[$i][0]; + if ($weight > $random) { + $key = $i; + break; + } + } + $item_id_array = $this->getExplode($d['item_id']); + $num_array = $this->getExplode($d['num']); + $item_id = $item_id_array[$key][0]; + $item_num = $num_array[$key][0] * $mul; + array_push($item_list, array( + 'item_id' => $item_id, + 'item_num' => $item_num, + )); + echo json_encode(array( + 'errcode' => 0, + 'errmsg' => '', + 'item_list' => $item_list + )); + } + + public function buySupplyBox() + { + $account_id = $_REQUEST['account_id']; + $conn = $this->getMysql($account_id); + if (!$conn) { + phpcommon\sendError(ERR_USER_BASE + 1, '没有这个玩家'); + return; + } + $box_id = $_REQUEST['box_id']; + $coin_num = $_REQUEST['coin_num']; + $s = $this->getSupplyBox($box_id); + if (!$s) { + phpcommon\sendError(ERR_USER_BASE + 2, '没有这个宝箱'); + return; + } + if ($coin_num < 5) { + phpcommon\sendError(ERR_USER_BASE + 3, '金币不足'); + return; + } + $row = $conn->execQueryOne('SELECT * FROM supplybox WHERE accountid=:account_id AND box_id=:box_id', + array( + ':account_id' => $account_id, + ':box_id' => $box_id + )); + if (!$row) { + if ($coin_num < $s['price']) { + phpcommon\sendError(ERR_USER_BASE + 3, '金币不足'); + return; + } + $ret = $conn->execScript('INSERT INTO supplybox(accountid, box_id, box_num, buy_times) ' . + ' VALUES(:accountid, :box_id, :box_num, :buy_times);', + array( + ':accountid' => $account_id, + ':box_id' => $box_id, + ':box_num' => 1, + ':buy_times' => 1 + )); + if (!$ret) { + die(); + return; + } + } else { + if ($coin_num < $s['price'] * pow($s['parameter'], ($row['box_num'] - 1))) { + phpcommon\sendError(ERR_USER_BASE + 3, '金币不足'); + return; + } + $box_num = $row['box_num'] + 1; + $buy_times = $row['buy_times'] + 1; + $ret = $conn->execScript('UPDATE supplybox SET box_num=:box_num, buy_times=:buy_times ' . + ' WHERE accountid=:accountid AND box_id=:box_id;', + array( + ':accountid' => $account_id, + ':box_id' => $box_id, + ':box_num' => $box_num, + ':buy_times' => $buy_times + )); + if (!$ret) { + die(); + return; + } + } + echo json_encode(array( + 'errcode' => 0, + 'errmsg' => '', + )); + } +} +?>