385 lines
11 KiB
PHP
385 lines
11 KiB
PHP
<?php
|
|
/**
|
|
* Created by PhpStorm.
|
|
* User: zn
|
|
* Date: 2017/5/17
|
|
* Time: 18:52
|
|
*/
|
|
|
|
namespace common\services;
|
|
|
|
|
|
use common\models\ConfigExpendItem;
|
|
use common\utils\CommUtil;
|
|
use Yii;
|
|
use yii\base\Model;
|
|
|
|
/**
|
|
* 游戏配置文件
|
|
* Class GameConfig
|
|
* @package common\models
|
|
*/
|
|
class GameConfigServices extends Model
|
|
{
|
|
const CACHE_GAME_CONFIG_KEY = 'gameConfig'; //游戏配置缓存key
|
|
const CACHE_GAME_CONFIG_MTIME_KEY = 'gameConfigMtime'; //游戏配置文件修改时间缓存key
|
|
|
|
private static $_runtimeGameConfig = null;
|
|
|
|
const COMMON_DRY_ID = 1;//干旱减产比例
|
|
const COMMON_BUG_ID = 2;//虫害减产比例
|
|
const COMMON_GRASS_ID = 3;//杂草减产比例
|
|
const COMMON_MIN_OUTPUT_ID = 4;//每块土地保底产量比例
|
|
const COMMON_STEAL_NUM = 5;//每块土地每次被偷取数量
|
|
const COMMON_DISEASE_RAND_TIME = 6;//干虫草每日出现间隔时间
|
|
const COMMON_DRY_RAND_NUM = 7; //干旱每次出现概率
|
|
const COMMON_BUG_RAND_NUM = 8;//虫害每次出现概率
|
|
const COMMON_GRASS_RAND_NUM = 9;//杂草每次出现概率
|
|
const COMMON_STEAL_CATCH = 10;//偷东西被抓时扣多少金币
|
|
const COMMON_MYSTERY_SEED = 11;//神秘种子
|
|
const COMMON_REBATE_1 = 12;//1级分销返点比例
|
|
const COMMON_REBATE_2 = 13;//2级分销返点比例
|
|
const COMMON_REBATE_3 = 14;//3级分销返点比例
|
|
const COMMON_REBATE_4 = 29;//4级分销返点比例
|
|
const COMMON_REBATE_5 = 30;//5级分销返点比例
|
|
const COMMON_REBATE_6 = 31;//6级分销返点比例
|
|
const COMMON_REBATE_7 = 32;//7级分销返点比例
|
|
const COMMON_REBATE_8 = 33;//8级分销返点比例
|
|
const COMMON_LOTTERY_MONEY = 15;//抽奖扣钱基数
|
|
const COMMON_DRAW_PERCENTAGE = 16;//交易提成
|
|
const COMMON_GOLD_EXCHANGE_GEM_RATE = 17;//金币兑换钻石比例
|
|
const COMMON_GOLD_EXCHANGE_PET_EXP_RATE = 18;//金币兑换宠物经验比例
|
|
const COMMON_PLEASURE_GROUND_GOLD = 19;//游乐场进场扣除多少金币
|
|
const COMMON_ENTER_ARENA_COUNT = 20;//每天可以挑战竞技场次数
|
|
const COMMON_ARENA_TIME_DIS = 21;//每次挑战竞技场时间间隔(秒)
|
|
const COMMON_FEED_EXPEND_POINT = 22;//看家护院喂养消耗点数
|
|
const COMMON_FEED_EXPEND_PERIOD = 23;//看家护院喂养点时间计算周期(秒)
|
|
const COMMON_GEM_EXCHANGE_GOLD_RATE = 24;//钻石兑换金币比例
|
|
const COMMON_BROADCAST_MESSAGE_EXPEND = 25;//激活账号费用
|
|
|
|
const COMMON_REBATE_LIST = [
|
|
1 => self::COMMON_REBATE_1,
|
|
2 => self::COMMON_REBATE_2,
|
|
3 => self::COMMON_REBATE_3,
|
|
4 => self::COMMON_REBATE_4,
|
|
5 => self::COMMON_REBATE_5,
|
|
6 => self::COMMON_REBATE_6,
|
|
7 => self::COMMON_REBATE_7,
|
|
8 => self::COMMON_REBATE_8,
|
|
];
|
|
|
|
/**
|
|
* 获取游戏配置
|
|
*/
|
|
public static function getGameConfig()
|
|
{
|
|
if (self::$_runtimeGameConfig !== null) {
|
|
return self::$_runtimeGameConfig;
|
|
}
|
|
|
|
$gameConfigFile = Yii::$app->params['game_config_file'];
|
|
$fileMtime = file_exists($gameConfigFile) ? filemtime($gameConfigFile) : false;
|
|
|
|
if ($fileMtime !== false) {
|
|
$cacheMtime = Yii::$app->redis->get(self::CACHE_GAME_CONFIG_MTIME_KEY);
|
|
if ((string)$cacheMtime === (string)$fileMtime) {
|
|
$gameConfigContent = Yii::$app->redis->get(self::CACHE_GAME_CONFIG_KEY);
|
|
if (!empty($gameConfigContent)) {
|
|
$gameConfig = json_decode($gameConfigContent, true);
|
|
if (is_array($gameConfig)) {
|
|
self::$_runtimeGameConfig = $gameConfig;
|
|
return self::$_runtimeGameConfig;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if (self::reloadGameConfig()) {
|
|
return self::$_runtimeGameConfig;
|
|
}
|
|
|
|
$gameConfigContent = Yii::$app->redis->get(self::CACHE_GAME_CONFIG_KEY);
|
|
if (!empty($gameConfigContent)) {
|
|
$gameConfig = json_decode($gameConfigContent, true);
|
|
if (is_array($gameConfig)) {
|
|
self::$_runtimeGameConfig = $gameConfig;
|
|
return self::$_runtimeGameConfig;
|
|
}
|
|
}
|
|
|
|
return [];
|
|
}
|
|
|
|
/**
|
|
* 重新读取配置文件
|
|
*/
|
|
public static function reloadGameConfig()
|
|
{
|
|
//从文件读取
|
|
$gameConfigFile = Yii::$app->params['game_config_file'];
|
|
if (file_exists($gameConfigFile)) {
|
|
$gameConfigContent = file_get_contents($gameConfigFile);
|
|
$gameConfig = json_decode($gameConfigContent, true);
|
|
if (!is_array($gameConfig)) {
|
|
return false;
|
|
}
|
|
|
|
//缓存配置文件
|
|
Yii::$app->redis->set(self::CACHE_GAME_CONFIG_KEY, $gameConfigContent);
|
|
Yii::$app->redis->set(self::CACHE_GAME_CONFIG_MTIME_KEY, filemtime($gameConfigFile));
|
|
|
|
self::$_runtimeGameConfig = $gameConfig;
|
|
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* 保存配置表
|
|
*/
|
|
public static function saveGameConfig($config)
|
|
{
|
|
$gameConfigFile = Yii::$app->params['game_config_file'];
|
|
$configJsonStr = json_encode($config);
|
|
if (!file_put_contents($gameConfigFile, $configJsonStr))
|
|
return false;
|
|
|
|
//更新缓存
|
|
self::reloadGameConfig();
|
|
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* 获取参数
|
|
* @param $path
|
|
* @param null $defaultValue
|
|
* @return null
|
|
*/
|
|
public static function getParamByPath($path, $defaultValue = null)
|
|
{
|
|
return CommUtil::getArrayParamByPath(self::getGameConfig(), $path, $defaultValue);
|
|
}
|
|
|
|
/**
|
|
* 玩家最大等级
|
|
* @return int
|
|
*/
|
|
public static function getMaxPlayerLevel()
|
|
{
|
|
$list = self::getParamByPath('player_level');
|
|
$maxLevel = 0;
|
|
foreach ($list as $item) {
|
|
if ($item['id'] > $maxLevel)
|
|
$maxLevel = $item['id'];
|
|
}
|
|
|
|
return $maxLevel;
|
|
}
|
|
|
|
public static function getMaxVipLevel()
|
|
{
|
|
$list = self::getParamByPath('vip_list');
|
|
$maxLevel = 0;
|
|
foreach ($list as $item) {
|
|
if ($item['id'] > $maxLevel)
|
|
$maxLevel = $item['id'];
|
|
}
|
|
|
|
return $maxLevel;
|
|
}
|
|
|
|
|
|
/**
|
|
* 根据等级和索引获取农田升级配置
|
|
* @param $level
|
|
* @param $position
|
|
* @return array
|
|
*/
|
|
public static function getLandUpgradeByLevelAndPosition($level, $position)
|
|
{
|
|
$list = self::getParamByPath('land_upgrade');
|
|
|
|
$position++;
|
|
foreach ($list as $item) {
|
|
if ($item['level'] == $level && $item['position'] == $position)
|
|
return $item;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* 获取通用配置表中的数值
|
|
* @param $id
|
|
* @return int
|
|
*/
|
|
public static function getCommonNumByID($id)
|
|
{
|
|
$config = self::getParamByPath('common.' . $id);
|
|
|
|
if (!$config) return 0;
|
|
|
|
// $type = $config['type'];
|
|
// if ($type == 1 || $type == 2 || $type == 4) {
|
|
// return $config['min'];
|
|
// }
|
|
//
|
|
// if ($type == 3) {
|
|
// return rand($config['min'], $config['max']);
|
|
// }
|
|
|
|
return rand($config['min'], $config['max']);
|
|
}
|
|
|
|
/**
|
|
* 根据type2获取物品ItemID
|
|
* @param $type2
|
|
* @return bool
|
|
*/
|
|
public static function getItemIDByType2($type2)
|
|
{
|
|
$list = self::getParamByPath('item_list');
|
|
foreach ($list as $item) {
|
|
if ($item['type2'] == $type2) return $item['id'];
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* 创建物品类型对应的物品名称列表
|
|
*/
|
|
public static function getItemIDToNameList()
|
|
{
|
|
$configList = self::getParamByPath('item_list');
|
|
$returnList = [];
|
|
|
|
foreach ($configList as $key => $item) {
|
|
$returnList[$key] = $item['name'];
|
|
}
|
|
|
|
return $returnList;
|
|
}
|
|
|
|
/**
|
|
* 随机一个抽奖数据
|
|
*/
|
|
public static function getLotteryOddsItem()
|
|
{
|
|
$itemList = self::getParamByPath('lottery');
|
|
$totalOdds = 0;
|
|
|
|
$list = [];
|
|
foreach ($itemList as $key => $item) {
|
|
|
|
$odds = intval($item['odds']);
|
|
if ($odds == 0) continue;
|
|
|
|
$item['minOdds'] = $totalOdds;
|
|
$item['maxOdds'] = $odds + $totalOdds;
|
|
array_push($list, $item);
|
|
|
|
$totalOdds += $odds;
|
|
}
|
|
|
|
$odds = mt_rand(0, $totalOdds);
|
|
foreach ($list as $item) {
|
|
if ($item['minOdds'] <= $odds && $odds < $item['maxOdds'])
|
|
return $item;
|
|
}
|
|
|
|
if (count($list) == 0)
|
|
throw new StatusException(ResultStatusServices::RS_PARAM_ERROR);
|
|
|
|
return $list[0];
|
|
}
|
|
|
|
/**
|
|
*解析升级购买消耗字段
|
|
* @param $field
|
|
* @return ConfigExpendItem[]
|
|
* @throws StatusException
|
|
*/
|
|
public static function parseExpendList($field)
|
|
{
|
|
if (empty($field)) return [];
|
|
|
|
$list1 = explode(',', $field);
|
|
if (count($list1) == 0) return [];
|
|
|
|
$returnList = [];
|
|
foreach ($list1 as $item1) {
|
|
$list2 = explode(':', $item1);
|
|
if (count($list2) != 2) throw new StatusException(ResultStatusServices::RS_CONFIG_ERROR, '配置数据错误:' . $item1);
|
|
|
|
$expendItem = new ConfigExpendItem();
|
|
$expendItem->itemID = $list2[0];
|
|
$expendItem->num = intval($list2[1]);
|
|
|
|
$itemConfig = self::getParamByPath('item_list.' . $expendItem->itemID);
|
|
if (empty($itemConfig)) throw new StatusException(ResultStatusServices::RS_CONFIG_ERROR, '配置数据错误:' . $item1);
|
|
|
|
$expendItem->name = $itemConfig['name'];
|
|
|
|
array_push($returnList, $expendItem);
|
|
}
|
|
|
|
return $returnList;
|
|
}
|
|
|
|
/**
|
|
* 根据果实ID查找种子ID
|
|
* @param $cropID
|
|
* @return mixed
|
|
* @throws StatusException
|
|
*/
|
|
public static function getSeedItemIDByCropID($cropID)
|
|
{
|
|
$list = self::getParamByPath('item_list');
|
|
foreach ($list as $item) {
|
|
if ($item['type2'] == ItemServices::ITEM_TYPE2_SEEDS && $item['effect_id'] == $cropID)
|
|
return $item['id'];
|
|
}
|
|
|
|
throw new StatusException(ResultStatusServices::RS_PARAM_ERROR);
|
|
}
|
|
|
|
public static function getMaxFactoryLevel()
|
|
{
|
|
$list = self::getParamByPath('factory_upgrade');
|
|
$maxLevel = 0;
|
|
foreach ($list as $item) {
|
|
if ($item['id'] > $maxLevel)
|
|
$maxLevel = $item['id'];
|
|
}
|
|
|
|
return $maxLevel;
|
|
}
|
|
|
|
public static function getMaxComposeLevel()
|
|
{
|
|
$list = self::getParamByPath('land_extend');
|
|
$maxLevel = 0;
|
|
foreach ($list as $item) {
|
|
if ($item['id'] > $maxLevel)
|
|
$maxLevel = $item['id'];
|
|
}
|
|
|
|
return $maxLevel;
|
|
}
|
|
|
|
public static function getBattleConfig($type)
|
|
{
|
|
$list = self::getParamByPath('battle');
|
|
foreach ($list as $item) {
|
|
if (intval($item['outcome_type']) == $type) return $item;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
|
|
}
|