accountId = $_REQUEST['account_id']; $this->sessionId = $_REQUEST['session_id']; if ($_REQUEST['c'] == 'Role' && $_REQUEST['a'] == 'battleReport') { return; } if (!phpcommon\isValidSessionId($this->accountId, $this->sessionId)) { phpcommon\sendError(500, '无效的session_id'); die(); } } public function getAccountId() { return $this->accountId; } public function getChannel() { return phpcommon\extractChannel($this->getAccountId()); } public function getSessionId() { return $this->sessionId; } public function getRegisterTime() { $registertime = phpcommon\extractRegisterTimeFromSessionId($this->sessionId); return $registertime; } public function getMysql($data) { $mysql_conf = getMysqlConfig(crc32($data)); $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; } public function getSelfMysql() { if (!$this->mysqlConn) { $this->mysqlConn = $this->getMysql($this->getAccountId()); } return $this->mysqlConn; } public function getRedis($data) { $redis_conf = getRedisConfig(crc32($data)); $r = new phpcommon\Redis(array( 'host' => $redis_conf['host'], 'port' => $redis_conf['port'], 'passwd' => $redis_conf['passwd'] )); return $r; } public function isValidSex($sex) { return in_array($sex, array(0, 1, 2)); } public function getUserInfo($fields) { $row = SqlHelper::selectOne ($this->getSelfMysql(), 't_user', $fields, array( 'accountid' => $this->getAccountId() ) ); if (empty($row)) { phpcommon\sendError(500, '服务器内部错误'); error_log('getUserInfo error '. $this->getAccountId()); die(); } return $row; } public function getOrmUserInfo() { $row = SqlHelper::ormSelectOne ($this->getSelfMysql(), 't_user', array( 'accountid' => $this->getAccountId() ) ); if (empty($row)) { phpcommon\sendError(500, '服务器内部错误'); error_log('getUserInfo error '. $this->getAccountId()); die(); } return $row; } public function safeGetOrmUserInfo() { $row = SqlHelper::ormSelectOne ($this->getSelfMysql(), 't_user', array( 'accountid' => $this->getAccountId() ) ); return $row ? $row : null; } public function updateUserInfo($fieldsKv) { SqlHelper::update ($this->getSelfMysql(), 'user', array( 'accountid' => $this->getAccountId() ), $fieldsKv ); } public function sendDataToClient($_code,$_msg,$_data) { echo json_encode(array( 'errcode' => $_code, 'errmsg' => $_msg, 'data' => $_data, )); } public function getItem($itemId) { $row = SqlHelper::selectOne ($this->getSelfMysql(), 'bag', array( 'id', 'num' ), array( 'id' => $itemId, 'accountid' => $this->getAccountId() )); if (empty($row)) { return null; } return array( 'item_id' => $row['id'], 'item_num' => $row['num'] ); } public function getItemCount($itemId, $userInfo) { switch ($itemId) { case V_ITEM_GOLD: { return $userInfo['coin_num']; } break; case V_ITEM_DIAMOND: { return $userInfo['diamond_num']; } break; case V_ITEM_LOTTERY: { return $userInfo['rmb_num']; } break; default: { $row = SqlHelper::selectOne ($this->getSelfMysql(), 'bag', array( 'id', 'num' ), array( 'id' => $itemId, 'accountid' => $this->getAccountId() )); return $row ? $row['num'] : 0; } break; } } public function isVirtualItem($itemId) { $isVirtualItem = false; if($itemId == $this->goldID) { $isVirtualItem = true; } else if($itemId == $this->lotteryID) { $isVirtualItem = true; } return $isVirtualItem; } public function addItem($items) { foreach ($items as $item) { if ($this->isVirtualItem($item['item_id'])) { if ($item['item_id'] == $this->goldID) { $this->addGold($item['item_num']); } else if ($item['item_id'] == $this->lotteryID){ $this->addLottery($item['item_num']); } } else { SqlHelper::upsert ($this->getSelfMysql(), 't_bag', array( 'accountid' => $this->getAccountId(), 'item_id' => $item['item_id'] ), array( 'item_num' => function () use($item) { return "item_num + {$item['item_num']}";}, 'modifytime' => $this->getNowTime() ), array( 'accountid' => $this->getAccountId(), 'item_id' => $item['item_id'], 'color_id' => 0, 'status' => 1, 'active_time' => 0, 'item_num' => $item['item_num'], 'createtime' => $this->getNowTime(), 'modifytime' => $this->getNowTime() ) ); } } } public function decItems($items) { $this->decItem($items); } public function decItem($items) { foreach ($items as $item) { if ($this->isVirtualItem($item['item_id'])) { // if ($item['item_id'] == VIRTUAL_ITEM_GOLD_ID) { // $this->decGold($item['item_num']); // } } else { SqlHelper::update ($this->getSelfMysql(), 'bag', array( 'accountid' => $this->getAccountId(), 'id' => $item['item_id'] ), array( 'num' => function () use($item) { return "CASE WHEN num < {$item['item_num']} THEN 0 ELSE num - {$item['item_num']} END"; }, 'modify_time' => $this->getNowTime() ) ); } } } public function deleteItem($item) { $tmpItemID = $item["item_id"]; $beDeleteNum = $item['item_num']; $accountID = $this->getAccountId(); $conn = $this->getMysql($accountID); $sqlStr = "SELECT id,num FROM bag WHERE accountid=:accountid AND id=:itemId"; $row = $conn->execQuery($sqlStr,array(':accountid' => $accountID,':itemId' =>$tmpItemID)); $code = 100; if($row) { $num1 = $row[0]['num']; if($num1 < $beDeleteNum) { $code = 98;//道具不足 } } else { $code = 99;//道具不存在 } return $code; } public function decGold($decGold) { SqlHelper::update ( $this->getSelfMysql(), 'user', array( 'accountid' => $this->getAccountId() ), array( 'coin_num' => function () use($decGold) { return "CASE coin_num WHEN coin_num < {$decGold} THEN 0 ELSE coin_num - {$decGold} END"; } ) ); // $this->incV(TN_CONSUME_GOLD, 0, $decGold); // $this->incV(TN_CONSUME_GOLD, $this->getNowDaySeconds(), $decGold); } public function addGold($addGold) { SqlHelper::update ( $this->getSelfMysql(), 'user', array( 'accountid' => $this->getAccountId() ), array( 'coin_num' => function () use($addGold) { return "coin_num + {$addGold}"; } ) ); } public function getTalentLv($skillId, $skillTypeId) { $row = SqlHelper::selectOne ( $this->getSelfMysql(), 'gun_intensify', array( 'gun_type_id', 'skill_id', 'skill_lv' ), array( 'accountid' => $this->getAccountId(), 'skill_id' => $skillId, 'gun_type_id' => $skillTypeId ) ); return $row ? $row['skill_iv'] : 0; } public function hasEnoughItemsEx($items, &$lackItem) { $userInfo = $this->getUserInfo(array( 'coin_num', 'diamond_num', 'rmb_num' )); foreach ($items as $item) { $inventory = $this->getItemCount($item['item_id'], $userInfo); if ($inventory < $item['item_num']) { $lackItem = array( 'item_id' => $item['item_id'], 'item_num' => $item['item_num'], 'inventory' => $inventory ); return false; } } return true; } public function getUsingEquipId() { $row = SqlHelper::selectOne ($this->getSelfMysql(), 'equip', array( 'using_id', ), array( 'accountid' => $this->getAccountId(), ) ); return $row ? $row['using_id'] : 0; } public function redisSetAndExpire($pk, $key, $val, $time) { $r = $this->getRedis($pk); $r->set($key, $val); $r->pexpire($key, $time); } public function redisGetJson($pk, $key) { $r = $this->getRedis($pk); $dataStr = $r->get($key); $result = null; if (!empty($dataStr)) { $result = json_decode($dataStr, true); } return $result; } }