CropInfoDto.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. namespace App\Module\AppGame\Proto;
  3. use Uraus\Kku\Common\DataLand;
  4. /**
  5. * 作物信息DTO转换为Protobuf数据对象
  6. */
  7. class CropInfoDto
  8. {
  9. /**
  10. * 将Farm模块的CropInfoDto填充到Protobuf的DataLand对象中
  11. *
  12. * 注意:此方法不创建新的DataLand对象,而是填充现有对象的作物相关字段
  13. *
  14. * @param \App\Module\Farm\Dtos\CropInfoDto $cropInfoDto
  15. * @param DataLand $dataLand 要填充的DataLand对象
  16. * @return DataLand
  17. */
  18. public static function fillDataLand(\App\Module\Farm\Dtos\CropInfoDto $cropInfoDto, DataLand $dataLand): DataLand
  19. {
  20. // 设置作物ID和种子ID
  21. $dataLand->setPlantId($cropInfoDto->id);
  22. $dataLand->setSeedId($cropInfoDto->seedId);
  23. // 设置种植时间
  24. if (!empty($cropInfoDto->plantTime)) {
  25. // 如果plantTime是字符串格式的日期时间,转换为时间戳
  26. if (is_string($cropInfoDto->plantTime)) {
  27. $plantTime = strtotime($cropInfoDto->plantTime);
  28. $dataLand->setSeedPlantingTimes($plantTime);
  29. }
  30. // 如果已经是时间戳,直接使用
  31. else if (is_numeric($cropInfoDto->plantTime)) {
  32. $dataLand->setSeedPlantingTimes($cropInfoDto->plantTime);
  33. }
  34. }
  35. // 设置种子状态,这里需要将Farm模块的生长阶段映射到Proto的种子状态
  36. // 假设我们有一个映射关系,这里简单地直接使用生长阶段值
  37. $dataLand->setSeedStatus($cropInfoDto->growthStage);
  38. // 检查是否有灾害
  39. if (!empty($cropInfoDto->disasters)) {
  40. foreach ($cropInfoDto->disasters as $disaster) {
  41. // 根据灾害类型设置相应的标志
  42. if (isset($disaster['type'])) {
  43. switch ($disaster['type']) {
  44. case 1: // 假设1表示杂草
  45. $dataLand->setNeedWeed(true);
  46. break;
  47. case 2: // 假设2表示虫害
  48. $dataLand->setNeedPestControl(true);
  49. break;
  50. case 3: // 假设3表示缺水
  51. $dataLand->setNeedWatering(true);
  52. break;
  53. }
  54. }
  55. }
  56. }
  57. // 检查是否可以施肥
  58. $dataLand->setCanFertilization(!$cropInfoDto->fertilized);
  59. return $dataLand;
  60. }
  61. }