package com.red.circle.other.app.command.game.roompk;


import com.alibaba.fastjson.JSON;
import com.red.circle.common.business.dto.cmd.RoomIdCmd;
import com.red.circle.framework.core.asserts.ResponseAssert;
import com.red.circle.other.app.dto.clientobject.game.GameRoomPkUserCO;
import com.red.circle.other.infra.database.cache.service.other.GameRoomPkCacheService;
import com.red.circle.other.infra.database.rds.entity.game.GameRoomPkRecord;
import com.red.circle.other.infra.database.rds.service.game.GameRoomPkRecordService;
import com.red.circle.other.inner.asserts.GameErrorCode;
import java.util.Objects;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 房间PK - 获得房间中未结束的pk双方人员.
 *
 * @author pengshigang on 2021/9/25
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class GameRoomPkUserCmdExe {

  private final GameRoomPkCacheService gameRoomPkCacheService;
  private final GameRoomPkRecordService gameRoomPkRecordService;

  public GameRoomPkUserCO execute(RoomIdCmd cmd) {

    GameRoomPkRecord pkRecord = gameRoomPkRecordService.getEfficientPkByRoomId(cmd.getRoomId());
    if (Objects.isNull(pkRecord)) {
      return null;
    }

    return getRoomPkCache(pkRecord.getId());
  }

  private GameRoomPkUserCO getRoomPkCache(Long id) {
    GameRoomPkUserCO pkRecord = gameRoomPkCacheService.getRoomPk(id, GameRoomPkUserCO.class);
    ResponseAssert.notNull(GameErrorCode.GAME_OVER, pkRecord);
    return pkRecord;
  }


}
