| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- <?php
- namespace App\Module\AppGame\Proto;
- use App\Module\Farm\Dtos\FarmInfoDto;
- use App\Module\Farm\Dtos\GodBuffDto;
- use App\Module\Farm\Dtos\LandInfoDto;
- use App\Module\GameItems\Dtos\ItemDto;
- use App\Module\Pet\Dtos\PetDataDto;
- use Uraus\Kku\Common\DataCoin;
- use Uraus\Kku\Common\DataGod;
- use Uraus\Kku\Common\DataHourse;
- use Uraus\Kku\Common\DataItem;
- use Uraus\Kku\Common\DataLand;
- use Uraus\Kku\Common\DataPet;
- use Uraus\Kku\Common\LastData;
- /**
- * LastData辅助类,用于将多个DTO转换为Protobuf的LastData对象
- */
- class LastDataHelper
- {
- /**
- * 将Farm模块的LandInfoDto列表转换为Protobuf的DataLand列表并添加到LastData中
- *
- * @param LastData $lastData
- * @param LandInfoDto[] $landInfoDtos
- * @return LastData
- */
- public static function addLands(LastData $lastData, array $landInfoDtos): LastData
- {
- $dataLands = [];
- foreach ($landInfoDtos as $landInfoDto) {
- $dataLands[] = LandInfoDto::toDataLand($landInfoDto);
- }
- $lastData->setLands($dataLands);
-
- return $lastData;
- }
-
- /**
- * 将Farm模块的FarmInfoDto转换为Protobuf的DataHourse并添加到LastData中
- *
- * @param LastData $lastData
- * @param FarmInfoDto $farmInfoDto
- * @return LastData
- */
- public static function addHourse(LastData $lastData, FarmInfoDto $farmInfoDto): LastData
- {
- $dataHourse = FarmInfoDto::toDataHourse($farmInfoDto);
- $lastData->setHourse($dataHourse);
-
- return $lastData;
- }
-
- /**
- * 将Farm模块的GodBuffDto列表转换为Protobuf的DataGod列表并添加到LastData中
- *
- * @param LastData $lastData
- * @param GodBuffDto[] $godBuffDtos
- * @return LastData
- */
- public static function addGods(LastData $lastData, array $godBuffDtos): LastData
- {
- $dataGods = [];
- foreach ($godBuffDtos as $godBuffDto) {
- $dataGods[] = GodBuffDto::toDataGod($godBuffDto);
- }
- $lastData->setGods($dataGods);
-
- return $lastData;
- }
-
- /**
- * 将GameItems模块的ItemDto列表转换为Protobuf的DataItem列表并添加到LastData中
- *
- * @param LastData $lastData
- * @param array $items 物品数据数组
- * @return LastData
- */
- public static function addItems(LastData $lastData, array $items): LastData
- {
- $dataItems = [];
- foreach ($items as $item) {
- $dataItem = new DataItem();
- $dataItem->setItemId($item['item_id'] ?? 0);
- if (isset($item['instance_id'])) {
- $dataItem->setInstanceId($item['instance_id']);
- }
- $dataItem->setQuantity($item['quantity'] ?? 0);
- if (isset($item['expire_time'])) {
- $dataItem->setExpireTime(is_numeric($item['expire_time']) ? $item['expire_time'] : strtotime($item['expire_time']));
- }
- $dataItems[] = $dataItem;
- }
- $lastData->setItems($dataItems);
-
- return $lastData;
- }
-
- /**
- * 将货币数据添加到LastData中
- *
- * @param LastData $lastData
- * @param array $coins 货币数据数组
- * @return LastData
- */
- public static function addCoins(LastData $lastData, array $coins): LastData
- {
- $dataCoins = [];
- foreach ($coins as $coin) {
- $dataCoin = new DataCoin();
- $dataCoin->setType($coin['fund_id'] ?? $coin['type'] ?? 0);
- $dataCoin->setQuantity($coin['balance'] ?? $coin['quantity'] ?? 0);
- $dataCoins[] = $dataCoin;
- }
- $lastData->setCoins($dataCoins);
-
- return $lastData;
- }
-
- /**
- * 将Pet模块的PetDataDto列表转换为Protobuf的DataPet列表并添加到LastData中
- *
- * @param LastData $lastData
- * @param PetDataDto[] $petDataDtos
- * @return LastData
- */
- public static function addPets(LastData $lastData, array $petDataDtos): LastData
- {
- // 这里需要实现PetDataDto到DataPet的转换
- // 由于没有提供PetDataDto的完整结构,这里只是一个示例
- $dataPets = [];
- foreach ($petDataDtos as $petDataDto) {
- $dataPet = new DataPet();
- $dataPet->setId($petDataDto->id ?? 0);
- $dataPet->setTypeId($petDataDto->typeId ?? 0);
- $dataPet->setName($petDataDto->name ?? '');
- $dataPet->setLevel($petDataDto->level ?? 0);
- $dataPet->setExp($petDataDto->exp ?? 0);
- $dataPet->setPower($petDataDto->power ?? 0);
- $dataPet->setMaxpower($petDataDto->maxpower ?? 0);
- $dataPet->setScore($petDataDto->score ?? 0);
- $dataPet->setFightingCapacity($petDataDto->fightingCapacity ?? 0);
- $dataPet->setGrade($petDataDto->grade ?? 0);
- $dataPet->setStatus($petDataDto->status ?? 0);
- $dataPets[] = $dataPet;
- }
- $lastData->setPets($dataPets);
-
- return $lastData;
- }
- }
|