$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' => 'gamedb2004_' . $mysql_conf['instance_id'] )); return $conn; } protected function getItem($item_id) { $g_conf_item_cluster = require('../res/item@item.php'); $item_conf = getItemConfig($g_conf_item_cluster, $item_id); $it = array( 'id' => $item_conf['id'], 'price' => $item_conf['price'], 'dprice' => $item_conf['dprice'], ); return $it; } protected function getShop($shop_id) { $g_conf_shop_cluster = require('../res/shop@shop.php'); $shop_conf = getShopConfig($g_conf_shop_cluster, $shop_id); $s = array( 'shop_id' => $shop_conf['shop_id'], 'item_id' => $shop_conf['item_id'], 'num' => $shop_conf['num'], 'item_weight' => $shop_conf['item_weight'], 'discount' => $shop_conf['discount'], 'discount_weight' => $shop_conf['discount_weight'], 'tip' => $shop_conf['tip'], 'type' => $shop_conf['shop_type'] ); return $s; } protected function getParameter($para_id) { $g_conf_para_cluster = require('../res/parameter@parameter.php'); $para_conf = getParameterConfig($g_conf_para_cluster, $para_id); $p = array( 'id' => $para_conf['id'], 'param_name' => $para_conf['param_name'], 'param_value' => $para_conf['param_value'], ); return $p; } 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; } protected function getShopInfo($shop_uuid, $type) { $r = $this->getRedis($shop_uuid); if (!$r) { die(); return; } $shop_list = array(); $user_db_str = $r->get($shop_uuid); if (empty($user_db_str)) { $shop_list = $this->randomShop($type); $shop_db = array( 'shop_uuid' => $shop_uuid, 'shop_list' => $shop_list, ); $r -> set($shop_uuid, json_encode($shop_db)); $r -> pexpire($shop_uuid, 1000 * 3600 * 24); } else { $user_db = json_decode($user_db_str, true); if (empty($user_db)) { phpcommon\sendError(ERR_USER_BASE + 1,'session失效'); return; } foreach ($user_db['shop_list'] as $shop) { array_push($shop_list, array( 'shop_id' => $shop['shop_id'], 'item_id' => $shop['item_id'], 'item_num' => $shop['item_num'], 'discount' => $shop['discount'], 'price' => $shop['price'], 'tip' => $shop['tip'], 'status' => $shop['status'], )); } } return $shop_list; } public function shopInfo() { $account_id = $_REQUEST['account_id']; //登录校验 $login = loginVerify($account_id, $_REQUEST['session_id']); if (!$login) { phpcommon\sendError(ERR_USER_BASE + 1, 'session无效'); return; } $shop_uuid = 'game2004api_shop_uuid: ' . md5($_REQUEST['account_id']); $diamond_shop_uuid = 'game2004api_diamond_shop_uuid:' . md5($_REQUEST['account_id']); //钻石商店信息 $diamond_shop_list = $this->getShopInfo($diamond_shop_uuid, 2); //商店信息 $shop_list = $this->getShopInfo($shop_uuid, 1); $conn = $this->getMysql($account_id); if (!$conn) { phpcommon\sendError(ERR_USER_BASE + 2, '没有这个玩家'); return; } $row = $conn->execQueryOne('SELECT shop_flush_times, diamond_shop_flush_times FROM user WHERE accountid=:accountid;', array( ':accountid' => $account_id )); if (!$row) { phpcommon\sendError(ERR_USER_BASE + 2, '没有这个玩家'); return; } echo json_encode(array( 'errcode' => 0, 'errmsg'=> '', 'shop_uuid' => $shop_uuid, 'shop_list' => $shop_list, 'shop_flush_times' => $row['shop_flush_times'], 'diamond_shop_uuid' => $diamond_shop_uuid, 'diamond_shop_list' => $diamond_shop_list, 'diamond_shop_flush_times' => $row['diamond_shop_flush_times'], )); } public function buyItem() { $account_id = $_REQUEST['account_id']; //登录校验 $login = loginVerify($account_id, $_REQUEST['session_id']); if (!$login) { phpcommon\sendError(ERR_USER_BASE + 1, 'session无效'); return; } $shop_id = $_REQUEST['shop_id']; if (!isset($_REQUEST['shop_uuid'])) { return; } $shop_uuid = $_REQUEST['shop_uuid']; $item_id = 0; $item_num = 0; $price = 0; $status = 0; $flag = 0; $tip = 0; $r = $this->getRedis($shop_uuid); $user_db_str = $r->get($shop_uuid); if (empty($user_db_str)) { 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; } foreach ($user_db['shop_list'] as $shop) { if ($shop['shop_id'] == $shop_id) { $item_id = $shop['item_id']; $item_num = $shop['item_num']; $price = $shop['price']; $status = $shop['status']; $tip = $shop['tip']; $flag = 1; break; } } if ($flag == 0) { phpcommon\sendError(ERR_USER_BASE + 3, '没有这个商品'); return; } if ($status == 1) { phpcommon\sendError(ERR_USER_BASE + 4, '商品已购买'); return; } //扣除货币 $this->SubCoin($price, $account_id, $tip); //修改购买状态 foreach ($user_db['shop_list'] as &$shop) { if ($shop['shop_id'] == $shop_id) { $shop['status'] = 1; } } $r->set($shop_uuid, json_encode($user_db)); $r -> pexpire($shop_uuid, 1000 * 3600 * 24); //增加奖励 $addreward = new classes\AddReward(); $addreward->addReward($item_id, $item_num, $account_id); $coin_num = $addreward->getCoinNum($account_id); $diamond_num = $addreward->getDiamondNum($account_id); $item_list = array(); array_push($item_list,array( 'item_id' => $item_id, 'item_num' => $item_num, )); echo json_encode(array( 'errcode' => 0, 'errmsg'=> '', 'coin_nums' => $coin_num, 'diamond_nums' => $diamond_num, 'item_list' => $item_list )); } public function buyDoubleItem() { $account_id = $_REQUEST['account_id']; //登录校验 $login = loginVerify($account_id, $_REQUEST['session_id']); if (!$login) { phpcommon\sendError(ERR_USER_BASE + 1, 'session无效'); return; } if (!isset($_REQUEST['shop_uuid'])) { return; } $shop_id = $_REQUEST['shop_id']; $shop_uuid = $_REQUEST['shop_uuid']; $item_id = 0; $item_num = 0; $price = 0; $status = 0; $flag = 0; $r = $this->getRedis($shop_uuid); $user_db_str = $r->get($shop_uuid); if (empty($user_db_str)) { 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; } foreach ($user_db['shop_list'] as $shop) { if ($shop['shop_id'] == $shop_id) { $item_id = $shop['item_id']; $item_num = $shop['item_num']; $price = $shop['price']; $status = $shop['status']; $flag = 1; break; } } if ($flag == 0) { phpcommon\sendError(ERR_USER_BASE + 3, '没有这个商品'); return; } //增加奖励 $p = $this->getParameter(REWARD_TIMES); $times = $p['param_value'] - 1; $addreward = new classes\AddReward(); $addreward->addReward($item_id, $item_num * $times, $account_id); $coin_num = $addreward->getCoinNum($account_id); echo json_encode(array( 'errcode' => 0, 'errmsg'=> '', 'coin_nums' => $coin_num, )); } public function flushShop() { $account_id = $_REQUEST['account_id']; //登录校验 $login = loginVerify($account_id, $_REQUEST['session_id']); if (!$login) { phpcommon\sendError(ERR_USER_BASE + 1, 'session无效'); return; } if (!isset($_REQUEST['shop_uuid'])) { return; } $shop_uuid = $_REQUEST['shop_uuid']; $shop_type = $_REQUEST['shop_type']; $shop_list = array(); $flush_times = 0; if ($_REQUEST['type'] == 3) { $p = $this->getParameter(RAND_SHOP_GOLD); if ($shop_type == 2) { $p = $this->getParameter(RAND_DIAMONDSHOP_GOLD); } $this->SubCoin($p['param_value'], $account_id, $_REQUEST['type']); } $conn = $this->getMysql($account_id); if (!$conn) { phpcommon\sendError(ERR_USER_BASE + 2, '没有这个玩家'); return; } $row = $conn->execQueryOne('SELECT shop_flush_times, diamond_shop_flush_times FROM user WHERE accountid=:accountid;', array( ':accountid' => $account_id )); if (!$row) { phpcommon\sendError(ERR_USER_BASE + 2, '没有这个玩家'); return; } if ($shop_type == 1) { $p_flush = $this->getParameter(MAX_SHOP_REFRESH); if ($p_flush['param_value'] <= $row['shop_flush_times']) { phpcommon\sendError(ERR_USER_BASE + 3, '今日刷新次数已满'); return; } $ret = $conn->execScript('UPDATE user SET shop_flush_times=:shop_flush_times, modify_time=:modify_time ' . ' WHERE accountid=:accountid;', array( ':accountid' => $account_id, ':shop_flush_times' => $row['shop_flush_times'] + 1, ':modify_time' => time() )); if (!$ret) { die(); return; } $flush_times = $row['shop_flush_times'] + 1; } if ($shop_type == 2) { $p_flush = $this->getParameter(RAND_DIAMONDSHOP_TIME); if ($p_flush['param_value'] <= $row['diamond_shop_flush_times']) { phpcommon\sendError(ERR_USER_BASE + 3, '今日刷新次数已满'); return; } $ret = $conn->execScript('UPDATE user SET diamond_shop_flush_times=:diamond_shop_flush_times, modify_time=:modify_time ' . ' WHERE accountid=:accountid;', array( ':accountid' => $account_id, ':diamond_shop_flush_times' => $row['diamond_shop_flush_times'] + 1, ':modify_time' => time() )); if (!$ret) { die(); return; } $flush_times = $row['diamond_shop_flush_times'] + 1; } $r = $this->getRedis($shop_uuid); $user_db_str = $r->get($shop_uuid); if (empty($user_db_str)) { 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; } unset($user_db['shop_list']); $shop_list = $this->randomShop($shop_type); $user_db['shop_list'] = $shop_list; $r -> set($shop_uuid, json_encode($user_db)); $r -> pexpire($shop_uuid, 1000 * 3600 * 24); $addreward = new classes\AddReward(); $coin_num = $addreward->getCoinNum($account_id); $diamond_num = $addreward->getDiamondNum($account_id); echo json_encode(array( 'errcode' => 0, 'errmsg'=> '', 'shop_uuid' => $shop_uuid, 'shop_list' => $shop_list, 'flush_times' => $flush_times, 'coin_nums' => $coin_num, 'diamond_nums' => $diamond_num )); } protected function randomShop($type) { $shop_list = array(); $g_conf_shop_cluster = require('../res/shop@shop.php'); $coin_len = 0; $diamond_len = 0; $len = 0; $id = 0; for ($i = 1; $i <= count($g_conf_shop_cluster); $i++) { $s = $this->getShop($i); if ($s['type'] == 1) { $coin_len++; } if ($s['type'] == 2) { $diamond_len++; } } if ($type == 1) { $id = 1; $len = $coin_len; } if ($type == 2) { $id = $coin_len + 1; $len = $diamond_len + $coin_len; } for ($i = $id; $i <= $len; $i++) { $item_id = 0; $item_num = 0; $discount = 0; $price = 0; $key = 0; $s = $this->getShop($i); //确定商品id和数量 $weight_sum = 0; $weight_array = $this->getExplode($s['item_weight']); for ($ii = 0; $ii < count($weight_array); $ii++) { $weight_sum += $weight_array[$ii][0]; } $random = Rand(0, $weight_sum); $weight = 0; for ($ii = 0; $ii < count($weight_array); $ii++) { $weight += $weight_array[$ii][0]; if ($weight > $random) { $key = $ii; break; } } $item_id_array = $this->getExplode($s['item_id']); $num_array = $this->getExplode($s['num']); $item_id = $item_id_array[$key][0]; $item_num = $num_array[$key][0]; //确定折扣和价格 $it = $this->getItem($item_id); if ($s['tip'] == 1) { $discount = 0; $price = 0; } else { $keys = 0; $weight_sum = 0; $weight_array = $this->getExplode($s['discount_weight']); for ($ii = 0; $ii < count($weight_array); $ii++) { $weight_sum += $weight_array[$ii][0]; } $random = Rand(0, $weight_sum); $weight = 0; for ($ii = 0; $ii < count($weight_array); $ii++) { $weight += $weight_array[$ii][0]; if ($weight > $random) { $keys = $ii; break; } } $discount_array = $this->getExplode($s['discount']); $discount = $discount_array[$keys][0]; $price = round($it['price'] * $item_num * ($discount * 1.0 / 10)); if ($s['tip'] == 4) { $price = round($it['dprice'] * $item_num * ($discount * 1.0 / 10)); } } array_push($shop_list, array( 'shop_id' => $s['shop_id'], 'item_id' => $item_id, 'item_num' => $item_num, 'discount' => $discount, 'price' => $price, 'tip' => $s['tip'], 'status' => 0, )); } return $shop_list; } protected function subCoin($coin_num, $account_id, $tips) { $conn = $this->getMysql($account_id); if (!$conn) { phpcommon\sendError(ERR_USER_BASE + 2, '没有这个玩家'); return; } $rowCoin = $conn->execQueryOne('SELECT coin_num, diamond_num FROM user WHERE accountid=:accountid;', array( ':accountid' => $account_id )); if ($tips == 3) { //扣除货币 if ($rowCoin['coin_num'] < $coin_num) { phpcommon\sendError(ERR_USER_BASE + 5, '金币不足'); die(); } $ret = $conn->execScript('UPDATE user SET coin_num=:coin_num, modify_time=:modify_time ' . ' WHERE accountid=:accountid;', array( ':accountid' => $account_id, ':coin_num' => $rowCoin['coin_num'] - $coin_num, ':modify_time' => time() )); if (!$ret) { die(); } } if ($tips == 4) { //扣除货币 if ($rowCoin['diamond_num'] < $coin_num) { phpcommon\sendError(ERR_USER_BASE + 5, '钻石不足'); die(); } $ret = $conn->execScript('UPDATE user SET diamond_num=:diamond_num, modify_time=:modify_time ' . ' WHERE accountid=:accountid;', array( ':accountid' => $account_id, ':diamond_num' => $rowCoin['diamond_num'] - $coin_num, ':modify_time' => time() )); if (!$ret) { die(); } } } } ?>