129 lines
5.1 KiB
PHP
129 lines
5.1 KiB
PHP
<?php
|
|
/**
|
|
* Created by PhpStorm.
|
|
* User: zn
|
|
* Date: 2017/5/18
|
|
* Time: 19:51
|
|
*/
|
|
|
|
namespace app\controllers;
|
|
|
|
|
|
use common\models\User;
|
|
use common\modules\achievement\AchievementUtil;
|
|
use common\modules\dailytask\DailyTaskUtil;
|
|
use common\modules\safe\SafeUtil;
|
|
use common\modules\systemlog\SystemLogUtil;
|
|
use common\services\ClientMessageServices;
|
|
use common\services\GameConfigServices;
|
|
use common\services\ItemServices;
|
|
use common\services\ResultStatusServices;
|
|
use common\services\StatusException;
|
|
use common\utils\MoneyUtil;
|
|
use common\utils\RedisKeyUtil;
|
|
use Yii;
|
|
use yii\rest\Controller;
|
|
|
|
/**
|
|
* 加工厂
|
|
* Class StoreController
|
|
* @package app\controllers
|
|
*/
|
|
class FactoryController extends Controller
|
|
{
|
|
/**
|
|
* 加工
|
|
* @return array
|
|
*/
|
|
public function actionDo()
|
|
{
|
|
$userID = Yii::$app->request->get('user_id');
|
|
$factoryID = (int)Yii::$app->request->get('factory_id');//加工id
|
|
$count = (int)Yii::$app->request->get('count');
|
|
|
|
if (!$userID) return ClientMessageServices::sendMessage(ResultStatusServices::RS_USER_NOT_LOGIN);//验证登录
|
|
|
|
//验证
|
|
if (empty($factoryID) || $count <= 0) return ClientMessageServices::sendMessage(ResultStatusServices::RS_PARAM_ERROR);
|
|
|
|
SafeUtil::dbOperate(function ($userID, $factoryID, $count) {
|
|
//获取物品配置数据
|
|
$factoryConfig = GameConfigServices::getParamByPath('factory.' . $factoryID);
|
|
if (empty($factoryConfig)) throw new StatusException(ResultStatusServices::RS_NOT_FIND_IN_CONFIG);
|
|
|
|
//判断等级是否足够
|
|
$user = User::findById($userID);
|
|
if ($factoryConfig['level'] > $user->factory_level) throw new StatusException(ResultStatusServices::RS_LEVEL_LIMIT);
|
|
|
|
for ($i = 1; $i <= 4; $i++) {
|
|
if (!isset($factoryConfig['fruit_id' . $i])) continue;
|
|
MoneyUtil::parseItem($factoryConfig['fruit_id' . $i], $count, false, $userID, SystemLogUtil::FROM_TYPE_FACTORY_EXPEND, ['factory_config' => $factoryConfig]);
|
|
}
|
|
|
|
//产出
|
|
MoneyUtil::parseItem($factoryConfig['goods_id'], $count, true, $userID, SystemLogUtil::FROM_TYPE_FACTORY_OUTPUT, ['factory_config' => $factoryConfig]);
|
|
|
|
list($itemID, $num) = explode(':', $factoryConfig['goods_id']);
|
|
$itemID = (int)$itemID;
|
|
$num = (int)$num;
|
|
$num *= $count;
|
|
|
|
//任务记录
|
|
DailyTaskUtil::addTaskCount($userID, DailyTaskUtil::TYPE_MAKE, $itemID, 1);
|
|
|
|
AchievementUtil::addCount($userID, AchievementUtil::ID_MAKE, 1);
|
|
|
|
//添加角色经验
|
|
return MoneyUtil::addUserExp($userID, (int)$factoryConfig['experience']);
|
|
|
|
}, [$userID, $factoryID, $count]);
|
|
|
|
return ClientMessageServices::sendMessage(ResultStatusServices::RS_OPERATION_SUCCESS);
|
|
}
|
|
|
|
/**
|
|
* 升级
|
|
* @return array
|
|
*/
|
|
public function actionUpgrade()
|
|
{
|
|
$userID = Yii::$app->request->get('user_id');
|
|
|
|
if (!$userID) return ClientMessageServices::sendMessage(ResultStatusServices::RS_USER_NOT_LOGIN);//验证登录
|
|
|
|
SafeUtil::dbOperate(function ($userID) {
|
|
SafeUtil::redisLockOperate(function ($userID) {
|
|
$user = User::findById($userID);
|
|
$level = $user->factory_level + 1;
|
|
$upgradeConfig = GameConfigServices::getParamByPath('factory_upgrade.' . $level);
|
|
if (empty($upgradeConfig)) throw new StatusException(ResultStatusServices::RS_FACTORY_MAX_LEVEL);
|
|
|
|
$exp = (int)$upgradeConfig['effect_plus_experience'];
|
|
if ($exp <= 0) throw new StatusException(ResultStatusServices::RS_CONFIG_ERROR);
|
|
|
|
for ($i = 1; $i <= 5; $i++) {
|
|
if (!isset($upgradeConfig['fruit_id' . $i])) continue;
|
|
MoneyUtil::parseItem($upgradeConfig['fruit_id' . $i], 1, false, $userID, SystemLogUtil::FROM_TYPE_FACTORY_UPGRADE, ['upgrade_config' => $upgradeConfig]);
|
|
}
|
|
|
|
//gold
|
|
$gold = (int)$upgradeConfig['gold'];
|
|
$log = SystemLogUtil::get($userID, SystemLogUtil::TYPE_GOLD, -$gold, 0, SystemLogUtil::FROM_TYPE_FACTORY_UPGRADE, ['upgrade_config' => $upgradeConfig]);
|
|
MoneyUtil::checkAndModifyRes($userID, false, -$gold, ItemServices::ITEM_TYPE2_GOLD, $log);
|
|
|
|
//增加经验
|
|
$user->factory_exp += $exp;
|
|
if ($user->factory_exp >= $upgradeConfig['experience']) {
|
|
$user->factory_level++;
|
|
$user->factory_exp = 0;
|
|
|
|
//任务记录
|
|
DailyTaskUtil::addTaskCount($userID, DailyTaskUtil::TYPE_UPGRADE_FACTORY, null, 1);
|
|
}
|
|
|
|
if (!$user->save(true, ['factory_level', 'factory_exp', 'updated_at'])) throw new StatusException(ResultStatusServices::RS_SAVE_DB_ERROR);
|
|
}, [$userID], RedisKeyUtil::lockFactoryExp($userID));
|
|
}, [$userID]);
|
|
return ClientMessageServices::sendMessage(ResultStatusServices::RS_OPERATION_SUCCESS);
|
|
}
|
|
} |