| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- <?php
- namespace App\Module\Farm\Casts;
- use App\Module\Farm\Enums\DISASTER_TYPE;
- use UCore\Model\CastsAttributes;
- /**
- * 灾害抵抗属性类型转换器
- *
- * 用于将数据库中存储的JSON格式的灾害抵抗属性转换为PHP对象,以及将PHP对象转换回JSON格式。
- * 灾害抵抗属性包含对不同类型灾害的抵抗率,如干旱、虫害、杂草等。
- */
- class DisasterResistanceCast extends CastsAttributes
- {
- /**
- * 干旱抵抗率(百分比格式,1=1%,100=100%)
- *
- * @var float $drought
- */
- public float $drought = 0.0;
- /**
- * 虫害抵抗率(百分比格式,1=1%,100=100%)
- *
- * @var float $pest
- */
- public float $pest = 0.0;
- /**
- * 杂草抵抗率(百分比格式,1=1%,100=100%)
- *
- * @var float $weed
- */
- public float $weed = 0.0;
- /**
- * 获取指定灾害类型的抵抗率
- *
- * @param DISASTER_TYPE|int $type 灾害类型
- * @return float 抵抗率(百分比格式,1=1%,需要除以100转换为小数)
- */
- public function getResistance($type): float
- {
- if ($type instanceof DISASTER_TYPE) {
- $type = $type->value;
- }
- return match ($type) {
- DISASTER_TYPE::DROUGHT->value => $this->drought,
- DISASTER_TYPE::PEST->value => $this->pest,
- DISASTER_TYPE::WEED->value => $this->weed,
- default => 0.0,
- };
- }
- /**
- * 设置指定灾害类型的抵抗率
- *
- * @param DISASTER_TYPE|int $type 灾害类型
- * @param float $value 抵抗率
- * @return self
- */
- public function setResistance($type, float $value): self
- {
- if ($type instanceof DISASTER_TYPE) {
- $type = $type->value;
- }
- match ($type) {
- DISASTER_TYPE::DROUGHT->value => $this->drought = $value,
- DISASTER_TYPE::PEST->value => $this->pest = $value,
- DISASTER_TYPE::WEED->value => $this->weed = $value,
- default => null,
- };
- return $this;
- }
- }
|