557 lines
20 KiB
PHP
557 lines
20 KiB
PHP
<?php
|
|
|
|
require_once('models/User.php');
|
|
require_once('models/Hero.php');
|
|
require_once('models/Season.php');
|
|
require_once('models/Nft.php');
|
|
|
|
require_once('mt/Parameter.php');
|
|
require_once('mt/Drop.php');
|
|
require_once('mt/Season.php');
|
|
require_once('mt/Hero.php');
|
|
require_once('mt/Rank.php');
|
|
require_once('mt/Item.php');
|
|
|
|
require_once('services/AwardService.php');
|
|
require_once('services/PropertyChgService.php');
|
|
require_once('services/SeasonService.php');
|
|
require_once('services/NameService.php');
|
|
|
|
use phpcommon\SqlHelper;
|
|
use models\User;
|
|
use models\Hero;
|
|
use models\Season;
|
|
use models\Nft;
|
|
|
|
class UserController extends BaseAuthedController {
|
|
|
|
public function login()
|
|
{
|
|
$this->updateSession(myself()->_getAccountId(),
|
|
myself()->_getSessionId());
|
|
//$user_name = $_REQUEST['name'];
|
|
//$avatar_url = $_REQUEST['avatar_url'];
|
|
|
|
$userName = 'guest';
|
|
$avatarUrl = '18003';
|
|
|
|
$userInfo = $this->_safeGetOrmUserInfo();
|
|
if (!$userInfo) {
|
|
$this->createNewUser($userName, $avatarUrl);
|
|
|
|
$this->addChip();
|
|
|
|
$userInfo = $this->_getOrmUserInfo();
|
|
}
|
|
if (!$this->loginCheck($userInfo)) {
|
|
$userInfo = $this->_getOrmUserInfo();
|
|
}
|
|
$this->_rspData(array(
|
|
'info' => User::show($userInfo)
|
|
));
|
|
$this->_incDailyV(TN_DAILY_LOGINS, 0, 1);
|
|
}
|
|
|
|
private function addChip(){
|
|
$list1 = [130001,130002,130003,130004,130005,130006,130007,130008,130009,130010];
|
|
$list2 = [130011,130012,130013,130014,130015,130016];
|
|
for ($i=1;$i<=15;$i++){
|
|
$itemId1 = $list1[rand(0,9)];
|
|
$itemMeta = mt\Item::get($itemId1);
|
|
if ($itemMeta) {
|
|
$tokenType = Nft::getTokenType($itemMeta);
|
|
if ($tokenType == Nft::NONE_TYPE) {
|
|
// myself()->_rspErr(1, 'param item_id error');
|
|
return;
|
|
} else {
|
|
SqlHelper::insert(
|
|
myself()->_getMarketMysql(),
|
|
't_nft',
|
|
array(
|
|
'token_id' => myself()->_getNowTime()+$i+5,
|
|
'token_type' => $tokenType,
|
|
'game_id' => 2006,
|
|
'item_id' => $itemId1,
|
|
'owner_address' => myself()->_getOpenId(),
|
|
'createtime' => myself()->_getNowTime(),
|
|
'modifytime' => myself()->_getNowTime(),
|
|
)
|
|
);
|
|
}
|
|
}
|
|
}
|
|
for ($i=16;$i<=30;$i++){
|
|
$itemId = $list2[rand(0,7)];
|
|
$itemMeta = mt\Item::get($itemId);
|
|
if ($itemMeta) {
|
|
$tokenType = Nft::getTokenType($itemMeta);
|
|
if ($tokenType == Nft::NONE_TYPE) {
|
|
// myself()->_rspErr(1, 'param item_id error');
|
|
return;
|
|
} else {
|
|
SqlHelper::insert(
|
|
myself()->_getMarketMysql(),
|
|
't_nft',
|
|
array(
|
|
'token_id' => myself()->_getNowTime()+$i+5,
|
|
'token_type' => $tokenType,
|
|
'game_id' => 2006,
|
|
'item_id' => $itemId,
|
|
'owner_address' => myself()->_getOpenId(),
|
|
'createtime' => myself()->_getNowTime(),
|
|
'modifytime' => myself()->_getNowTime(),
|
|
)
|
|
);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private function loginCheck($userInfo)
|
|
{
|
|
$seasonService = new services\SeasonService();
|
|
return $seasonService->checkSeason($userInfo);
|
|
}
|
|
|
|
private function createNewUser($userName, $avatarUrl)
|
|
{
|
|
$initRankMeta = mt\Rank::getInitRank();
|
|
$currSeasonMeta = mt\Season::getCurrentSeason();
|
|
$gold = 0;
|
|
$diamond = 0;
|
|
$heroList = array();
|
|
$haveHeadHeroList = array();
|
|
$headList = array();
|
|
$headFrameList = array();
|
|
$addItems = array();
|
|
{
|
|
foreach (mt\Parameter::getListValue('creator_hero_id') as $heroId) {
|
|
$heroMeta = mt\Item::get($heroId);
|
|
if ($heroMeta) {
|
|
Hero::addHero($heroMeta);
|
|
array_push($heroList, $heroMeta);
|
|
if ($heroMeta['hero_head']) {
|
|
array_push($haveHeadHeroList, $heroMeta);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
{
|
|
foreach (mt\Parameter::getListValue('creator_present_items') as $itemsStr) {
|
|
list($itemId, $itemNum) = explode(':', $itemsStr);
|
|
if ($itemNum > 0) {
|
|
switch ($itemId) {
|
|
case V_ITEM_GOLD:
|
|
{
|
|
$gold += $itemNum;
|
|
}
|
|
break;
|
|
case V_ITEM_DIAMOND:
|
|
{
|
|
$diamond += $itemNum;
|
|
}
|
|
break;
|
|
default:
|
|
{
|
|
$itemMeta = mt\Item::get($itemId);
|
|
switch ($itemMeta['type']) {
|
|
case mt\Item::HEAD_TYPE:
|
|
{
|
|
array_push($headList, $itemId);
|
|
}
|
|
break;
|
|
case mt\Item::HEAD_FRAME_TYPE:
|
|
{
|
|
array_push($headFrameList, $itemId);
|
|
}
|
|
break;
|
|
default:
|
|
{
|
|
array_push($addItems,
|
|
array(
|
|
'item_id' => $itemId,
|
|
'item_num' => $itemNum
|
|
));
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
$headId = !empty($headList) ? $headList[rand() % count($headList)] : 0;
|
|
$headFrame = !empty($headFrameList) ? $headFrameList[rand() % count($headFrameList)] : 0;
|
|
$heroUniId = 0;
|
|
$heroId = !empty($heroList) ? $heroList[rand() % count($heroList)]['id'] : 0;
|
|
if ($headId == 0 || count($headList) <= 1) {
|
|
$headId = !empty($haveHeadHeroList) ?
|
|
$haveHeadHeroList[rand() % count($haveHeadHeroList)]['hero_head'] : 0;
|
|
}
|
|
//Hero::randHero($heroUniId, $heroId);
|
|
SqlHelper::upsert
|
|
($this->_getSelfMysql(),
|
|
't_user',
|
|
array(
|
|
'account_id' => $this->_getAccountId()
|
|
),
|
|
array(
|
|
),
|
|
array(
|
|
'account_id' => $this->_getAccountId(),
|
|
'channel' => $this->_getChannel(),
|
|
'name' => $userName,
|
|
'sex' => rand() % 2,
|
|
#'avatar_url' => $avatar_url,
|
|
'gold' => $gold,
|
|
'diamond' => $diamond,
|
|
'head_frame' => $headFrame,
|
|
'level' => 1,
|
|
'exp' => 0,
|
|
'rank' => $initRankMeta ? $initRankMeta['rank'] : 1,
|
|
'history_best_rank' => $initRankMeta ? $initRankMeta['rank'] : 1,
|
|
'score' => $initRankMeta ? $initRankMeta['min_score'] : 0,
|
|
'head_id' => $headId,
|
|
'hero_id' => $heroId,
|
|
'last_season_id' => $currSeasonMeta ? $currSeasonMeta['id'] : 0,
|
|
'head_list' => json_encode($headList),
|
|
'head_frame_list' => json_encode($headFrameList),
|
|
'score_modifytime' => $this->_getNowTime(),
|
|
'best_rank_modifytime' => $this->_getNowTime(),
|
|
'createtime' => $this->_getNowTime(),
|
|
'modifytime' => $this->_getNowTime(),
|
|
)
|
|
);
|
|
$awardService = new services\AwardService();
|
|
$propertyChgService = new services\PropertyChgService();
|
|
$this->_addItems($addItems, $awardService, $propertyChgService);
|
|
}
|
|
|
|
public function active()
|
|
{
|
|
$name = getReqVal('name', '');
|
|
$nameSign = getReqVal('name_sign', '');
|
|
$sex = getReqVal('sex', 0);
|
|
$headId = getReqVal('head_id', 0);
|
|
$headFrame = getReqVal('head_frame', 0);
|
|
$heroId = getReqVal('hero_id', 0);
|
|
|
|
$userInfo = $this->_getOrmUserInfo();
|
|
if ($userInfo['activated']) {
|
|
$this->_rspErr(10, 'Activated');
|
|
return;
|
|
}
|
|
$nameService = new services\NameService();
|
|
if (!$nameService->verifyNameSign($name, $nameSign)){
|
|
$this->_rspErr(1, 'Signature verification failed');
|
|
return;
|
|
}
|
|
if ($nameService->nameUsed($name)){
|
|
$this->_rspErr(2, 'Name already occupied');
|
|
return;
|
|
}
|
|
if (!isValidSex($sex)) {
|
|
$this->_rspErr(1, 'Parameter error, illegal sex');
|
|
return;
|
|
}
|
|
if (!User::isValidHeadId($userInfo, $headId)) {
|
|
$this->_rspErr(1, 'Parameter error, illegal head_id');
|
|
return;
|
|
}
|
|
if (!User::isValidHeadFrame($userInfo, $headFrame)) {
|
|
$this->_rspErr(1, 'Parameter error, illegal head_frame');
|
|
return;
|
|
}
|
|
$heroMeta = mt\Hero::get($heroId);
|
|
if (!$heroMeta) {
|
|
$this->_rspErr(1, 'Parameter error, illegal hero_id');
|
|
return;
|
|
}
|
|
$heroCfg = null;
|
|
{
|
|
$list = mt\Parameter::parseList('creator_present_player', array('|', ':'));
|
|
foreach ($list as $item) {
|
|
if ($item[0] == $heroId) {
|
|
$heroCfg = $item;
|
|
break;
|
|
}
|
|
}
|
|
if (!$heroCfg || $heroCfg[1] <= 0) {
|
|
$this->_rspErr(1, 'Parameter error, illegal hero_id');
|
|
return;
|
|
}
|
|
}
|
|
if (!$nameService->useName($name)) {
|
|
$this->_rspErr(2, 'Name already occupied');
|
|
return;
|
|
}
|
|
$this->_updateUserInfo(array(
|
|
'name' => $name,
|
|
'sex' => $sex,
|
|
'head_id' => $headId,
|
|
'head_frame' => $headFrame,
|
|
'activated' => 1,
|
|
'rename_count' => function () {
|
|
return 'rename_count + 1';
|
|
}
|
|
));
|
|
Hero::addTryHero($heroMeta, $heroCfg[1]);
|
|
$propertyChgService = new services\PropertyChgService();
|
|
$propertyChgService->addUserChg();
|
|
$this->_rspData(array(
|
|
'property_chg' => $propertyChgService->toDto()
|
|
));
|
|
}
|
|
|
|
public function update()
|
|
{
|
|
$userInfo = $this->_getOrmUserInfo();
|
|
$validFields = array(
|
|
'sex' => array(
|
|
'field_name' => 'sex',
|
|
'val_func' => function ($val) {
|
|
return $val;
|
|
},
|
|
'valid_func' => function ($val, &$errCode, &$errMsg) use($userInfo) {
|
|
if (isValidSex($val)) {
|
|
return true;
|
|
} else {
|
|
$errCode = 1;
|
|
$errMsg = 'sex parameter error';
|
|
return false;
|
|
}
|
|
}
|
|
),
|
|
'head_id' => array(
|
|
'field_name' => 'head_id',
|
|
'val_func' => function ($val) {
|
|
return $val;
|
|
},
|
|
'valid_func' => function ($val, &$errCode, &$errMsg) use($userInfo) {
|
|
if (User::isValidHeadId($userInfo, $val)) {
|
|
return true;
|
|
} else {
|
|
$errCode = 1;
|
|
$errMsg = 'head_id parameter error';
|
|
return false;
|
|
}
|
|
}
|
|
),
|
|
'head_frame' => array(
|
|
'field_name' => 'head_frame',
|
|
'val_func' => function ($val) {
|
|
return $val;
|
|
},
|
|
'valid_func' => function ($val, &$errCode, &$errMsg) use($userInfo) {
|
|
if (User::isValidHeadFrame($userInfo, $val)) {
|
|
return true;
|
|
} else {
|
|
$errCode = 1;
|
|
$errMsg = 'head_id parameter error';
|
|
return false;
|
|
}
|
|
}
|
|
),
|
|
'hero_id' => array(
|
|
'field_name' => 'hero_id',
|
|
'val_func' => function ($val) {
|
|
return $val;
|
|
},
|
|
'valid_func' => function ($val, &$errCode, &$errMsg) use (&$heroId) {
|
|
if (Hero::getValidHero($val)) {
|
|
return true;
|
|
} else {
|
|
$errCode = 1;
|
|
$errMsg = 'hero does not exist';
|
|
return false;
|
|
}
|
|
}
|
|
),
|
|
'first_fight' => array(
|
|
'field_name' => 'first_fight',
|
|
'val_func' => function ($val) {
|
|
return empty($val) ? 0 : 1;
|
|
},
|
|
'valid_func' => function ($val, &$errCode, &$errMsg) {
|
|
return true;
|
|
}
|
|
),
|
|
'already_guide' => array(
|
|
'field_name' => 'already_guide',
|
|
'val_func' => function ($val) {
|
|
return empty($val) ? 0 : 1;
|
|
},
|
|
'valid_func' => function ($val, &$errCode, &$errMsg) {
|
|
return true;
|
|
}
|
|
)
|
|
);
|
|
$fieldsKv = array();
|
|
$errCod = 0;
|
|
$errMsg = '';
|
|
foreach ($validFields as $key => $field) {
|
|
$reqVal = getReqVal($key, '');
|
|
if (!empty($reqVal)) {
|
|
if (isset($field['valid_func'])) {
|
|
if (!$field['valid_func']($reqVal, $errCode, $errMsg)) {
|
|
$this->_rspErr($errCode, $errMsg);
|
|
return;
|
|
}
|
|
$fieldsKv[$field['field_name']] = $field['val_func']($reqVal);
|
|
}
|
|
}
|
|
}
|
|
|
|
if (count($fieldsKv) > 0) {
|
|
$this->_updateUserInfo($fieldsKv);
|
|
}
|
|
$propertyChgService = new services\PropertyChgService();
|
|
$propertyChgService->addUserChg();
|
|
$this->_rspData(array(
|
|
'property_chg' => $propertyChgService->toDto()
|
|
));
|
|
}
|
|
|
|
public function info()
|
|
{
|
|
$targetId = getReqVal('target_id', '');
|
|
$userDb = SqlHelper::ormSelectOne
|
|
($this->_getMysql($targetId),
|
|
't_user',
|
|
array(
|
|
'account_id' => $targetId
|
|
)
|
|
);
|
|
if (!$userDb) {
|
|
$this->_rspErr(1, 'Account does not exist');
|
|
return;
|
|
}
|
|
$this->_rspData(array(
|
|
'info' => User::info($userDb)
|
|
));
|
|
}
|
|
|
|
public function query()
|
|
{
|
|
$name = getReqVal('name', '');
|
|
$filter = array(
|
|
'name' => $name,
|
|
);
|
|
if (SERVER_ENV == _ONLINE) {
|
|
$filter['channel'] = BC_CHANNEL;
|
|
}
|
|
$userDb = SqlHelper::ormSelectOne
|
|
($this->_getMysql($name),
|
|
't_user',
|
|
$filter
|
|
);
|
|
if (!$userDb) {
|
|
$this->_rspErr(1, 'not found');
|
|
return;
|
|
}
|
|
$channel = phpcommon\extractChannel($userDb['account_id']);
|
|
if ($channel != BC_CHANNEL) {
|
|
if (SERVER_ENV == _ONLINE) {
|
|
$this->_rspErr(1, 'not found');
|
|
return;
|
|
}
|
|
}
|
|
$this->_rspData(array(
|
|
'info' => User::info($userDb)
|
|
));
|
|
}
|
|
|
|
public function detailInfo()
|
|
{
|
|
$targetId = getReqVal('target_id', '');
|
|
$userDb = User::find($targetId);
|
|
if (!$userDb) {
|
|
$this->_rspErr(1, 'Account does not exist');
|
|
return;
|
|
}
|
|
$userDto = User::info($userDb);
|
|
$userDto['current_rank'] = $userDb['rank'];
|
|
$userDto['history_seasons'] = array();
|
|
$seasonDbs = Season::getHistorySeasons($targetId);
|
|
foreach ($seasonDbs as $seasonDb) {
|
|
$battleData = json_decode($seasonDb['battle_data'], true);
|
|
$seasonBattleData = isset($battleData) ? getXVal($battleData, 'season_data', array()) : array();
|
|
$gameTimes = getXVal($seasonBattleData, 'total_battle_times', 0);
|
|
$winTimes = getXVal($seasonBattleData, 'total_win_times', 0);
|
|
$winRate = $gameTimes > 0 ? intval($winTimes / $gameTimes * 100) : 0;
|
|
$totalKills = getXVal($seasonBattleData, 'total_kills_times', 0);
|
|
$totalDamage = getXVal($seasonBattleData, 'total_damage_out', 0);
|
|
$totalAlive = getXVal($seasonBattleData, 'total_alive_time', 0);
|
|
$totalRecoverHp = getXVal($seasonBattleData, 'total_recover_hp', 0);
|
|
$avgDamage = $gameTimes > 0 ? intval($totalDamage / $gameTimes) : 0;
|
|
$avgKills = $gameTimes > 0 ? intval($totalKills / $gameTimes) : 0;
|
|
$starKills = $gameTimes > 0 ? intval($totalKills / $gameTimes / 10 *100) : 0;
|
|
$starDamage = $gameTimes > 0 ? intval($totalDamage / $gameTimes / 1500 * 100) : 0;
|
|
$starAlive = $gameTimes > 0 ? intval($totalAlive / $gameTimes / 300 * 0.1) : 0;
|
|
$starRecover = $gameTimes > 0 ? intval($totalRecoverHp / $gameTimes / 300 * 100) : 0;
|
|
$starWin = $gameTimes > 0 ? intval($winTimes / $gameTimes / 0.5 * 100) : 0;
|
|
array_push($userDto['history_seasons'],
|
|
array(
|
|
'season_id' => $seasonDb['season_id'],
|
|
'total_kills' => $totalKills,
|
|
'game_times' => $gameTimes,
|
|
'win_times' => $winTimes,
|
|
'win_rate' => $winRate,
|
|
'max_kills' => getXVal($seasonBattleData, 'max_kills_times', 0),
|
|
'avg_kills' => $avgKills,
|
|
'max_damage_out' => getXVal($seasonBattleData, 'max_damage_out', 0),
|
|
'avg_damage_out' => $avgDamage,
|
|
'star_kills' => min(100, $starKills),
|
|
'star_damage' => min(100, $starDamage),
|
|
'star_alive' => min(100, $starAlive),
|
|
'star_recover' => min(100, $starRecover),
|
|
'star_win' => min(100, $starWin),
|
|
));
|
|
}
|
|
if (count($userDto['history_seasons']) <= 0) {
|
|
$currSeasonMeta = mt\Season::getCurrentSeason();
|
|
array_push($userDto['history_seasons'],
|
|
array(
|
|
'season_id' => $currSeasonMeta['id'],
|
|
'total_kills' => 0,
|
|
'game_times' => 0,
|
|
'win_times' => 0,
|
|
'win_rate' => 0,
|
|
'max_kills' => 0,
|
|
'avg_kills' => 0,
|
|
'max_damage_out' => 0,
|
|
'avg_damage_out' => 0,
|
|
'star_kills' => min(100, 0),
|
|
'star_damage' => min(100, 0),
|
|
'star_alive' => min(100, 0),
|
|
'star_recover' => min(100, 0),
|
|
'star_win' => min(100, 0),
|
|
));
|
|
}
|
|
$this->_rspData(array(
|
|
'info' => $userDto
|
|
));
|
|
}
|
|
|
|
public function like(){
|
|
$account_id = getReqVal('target_id', '');
|
|
if (! $account_id){
|
|
$this->_rspErr(1, 'target_id param error');
|
|
return;
|
|
}
|
|
if ($account_id == $this->_getAccountId()){
|
|
$this->_rspErr(1, 'target_id param error');
|
|
return;
|
|
}
|
|
$userDb = User::find($account_id);
|
|
if (!$userDb){
|
|
$this->_rspErr(1, 'target_id param error');
|
|
return;
|
|
}
|
|
User::updateLikeCount($account_id);
|
|
$this->_rspOk();
|
|
}
|
|
|
|
}
|