| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- <?php
- namespace App\Module\Game\Dtos;
- use UCore\Dto\BaseDto;
- /**
- * 房屋变更临时数据传输对象
- *
- * 用于存储和传输房屋变更的临时数据
- */
- class HouseChangeTempDto extends BaseDto
- {
- /**
- * 旧等级
- *
- * @var int
- */
- public int $oldLevel;
- /**
- * 新等级
- *
- * @var int
- */
- public int $newLevel;
- /**
- * 是否为升级(true为升级,false为降级)
- *
- * @var bool
- */
- public bool $isUpgrade;
- /**
- * 产出加成
- *
- * @var float
- */
- public float $outputBonus;
- /**
- * 特殊土地上限
- *
- * @var int
- */
- public int $specialLandLimit;
- /**
- * 更新时间戳
- *
- * @var int
- */
- public int $updatedAt;
- /**
- * 从缓存数据创建DTO对象
- *
- * @param mixed $cachedData 缓存数据
- * @return array 包含DTO对象的数组
- */
- public static function fromCache($cachedData): array
- {
- // 为了保持与原有逻辑兼容,我们将创建一个新的方法来处理单个对象的情况
- $singleDto = self::fromCacheSingle($cachedData);
- if ($singleDto === null) {
- return [];
- }
- return [0 => $singleDto]; // 使用0作为默认键
- }
- /**
- * 从缓存数据创建单个DTO对象
- *
- * 这是一个辅助方法,保留原有的逻辑,但不覆盖父类的fromCache方法
- *
- * @param mixed $cachedData 缓存数据
- * @return HouseChangeTempDto|null 包含DTO对象或null
- */
- public static function fromCacheSingle($cachedData): ?HouseChangeTempDto
- {
- if (empty($cachedData)) {
- return null;
- }
- if ($cachedData instanceof self) {
- return $cachedData;
- }
- if (!is_array($cachedData)) {
- return null;
- }
- $dto = new self();
- $dto->oldLevel = $cachedData['old_level'] ?? 0;
- $dto->newLevel = $cachedData['new_level'] ?? 0;
- $dto->isUpgrade = $cachedData['is_upgrade'] ?? true;
- $dto->outputBonus = $cachedData['output_bonus'] ?? 0.0;
- $dto->specialLandLimit = $cachedData['special_land_limit'] ?? 0;
- $dto->updatedAt = $cachedData['updated_at'] ?? time();
- return $dto;
- }
- /**
- * 转换为数组
- *
- * @return array
- */
- public function toArray(): array
- {
- return [
- 'old_level' => $this->oldLevel,
- 'new_level' => $this->newLevel,
- 'is_upgrade' => $this->isUpgrade,
- 'output_bonus' => $this->outputBonus,
- 'special_land_limit' => $this->specialLandLimit,
- 'updated_at' => $this->updatedAt,
- ];
- }
- }
|