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

import com.red.circle.framework.core.asserts.ResponseAssert;
import com.red.circle.other.app.dto.cmd.game.GameLudoStartCmd;
import com.red.circle.other.infra.database.mongo.entity.game.ludo.GameLudoRun;
import com.red.circle.other.infra.database.mongo.entity.game.ludo.GameStatus;
import com.red.circle.other.infra.database.mongo.entity.game.ludo.GameStatusEvent;
import com.red.circle.other.infra.database.mongo.service.game.GameLudoRunService;
import com.red.circle.other.inner.asserts.GameErrorCode;
import java.util.Objects;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 开始ludo游戏.
 *
 * @author pengliang on 2021/7/27
 */
@Component
@RequiredArgsConstructor
public class GameLudoStartCmdExe {

  private final GameLudoRunService gameLudoRunService;

  public void execute(GameLudoStartCmd cmd) {
    GameLudoRun gameLudoRun = gameLudoRunService.get(cmd.getId());
    // 8004 游戏不存在
    ResponseAssert.notNull(GameErrorCode.GAME_NOT_EXIST, gameLudoRun);
    // 8011 游戏人数不足
    ResponseAssert
        .isTrue(GameErrorCode.INSUFFICIENT_PLAYERS, gameLudoRun.playersSize() >= 2);
    // 8025 没有权限
    ResponseAssert
        .isTrue(GameErrorCode.PERMISSION_DENIED,
            Objects.equals(gameLudoRun.getUserId(), cmd.getHomeownerUserId()));

    gameLudoRunService.updateGameStatus(gameLudoRun.getId(), GameStatus.PROGRESS,
        GameStatusEvent.NONE);
  }

}
