| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- <?php
- namespace App\Module\AppGame\Proto;
- use Uraus\Kku\Common\LandDisaster;
- /**
- * 灾害转换辅助类
- *
- * 负责将Farm模块的灾害数组转换为Protobuf的LandDisaster对象数组
- */
- class DisasterConverter
- {
- /**
- * 将灾害数组转换为LandDisaster对象数组
- *
- * @param array $disasters 灾害数组
- * @return LandDisaster[] LandDisaster对象数组
- */
- public static function convertToLandDisasters(array $disasters): array
- {
- $landDisasters = [];
-
- foreach ($disasters as $disaster) {
- if (!isset($disaster['type'])) {
- continue;
- }
-
- $landDisaster = new LandDisaster();
- $landDisaster->setType($disaster['type']);
-
- // 设置灾害是否活跃,默认为活跃状态
- $isActive = ($disaster['status'] ?? 'active') === 'active';
- $landDisaster->setActive($isActive);
-
- $landDisasters[] = $landDisaster;
- }
-
- return $landDisasters;
- }
- /**
- * 设置DataLand的向后兼容灾害标志
- *
- * 为了保持向后兼容性,根据灾害类型设置对应的布尔值标志
- *
- * @param \Uraus\Kku\Common\DataLand $dataLand DataLand对象
- * @param array $disasters 灾害数组
- * @return void
- */
- public static function setCompatibilityFlags(\Uraus\Kku\Common\DataLand $dataLand, array $disasters): void
- {
- foreach ($disasters as $disaster) {
- if (isset($disaster['type']) && ($disaster['status'] ?? 'active') === 'active') {
- switch ($disaster['type']) {
- case 3: // 杂草 (WEED)
- $dataLand->setNeedWeed(true);
- break;
- case 2: // 虫害 (PEST)
- $dataLand->setNeedPestControl(true);
- break;
- case 1: // 干旱 (DROUGHT)
- $dataLand->setNeedWatering(true);
- break;
- }
- }
- }
- }
- /**
- * 处理DataLand的灾害信息
- *
- * 统一处理灾害信息的设置,包括新的disasters属性和向后兼容的布尔值标志
- *
- * @param \Uraus\Kku\Common\DataLand $dataLand DataLand对象
- * @param array $disasters 灾害数组
- * @return void
- */
- public static function processDisasters(\Uraus\Kku\Common\DataLand $dataLand, array $disasters): void
- {
- if (!empty($disasters)) {
- // 设置新的disasters属性
- $landDisasters = self::convertToLandDisasters($disasters);
- $dataLand->setDisasters($landDisasters);
-
- // 设置向后兼容的布尔值标志
- self::setCompatibilityFlags($dataLand, $disasters);
- } else {
- // 没有灾害时设置空数组
- $dataLand->setDisasters([]);
- }
- }
- }
|