Skip to content

Commit

Permalink
feat : 게임 시작 후 카드 분배하는 로직 구현
Browse files Browse the repository at this point in the history
Issue : #15
  • Loading branch information
wiizii committed Apr 12, 2022
1 parent e86a5a1 commit 1470627
Show file tree
Hide file tree
Showing 10 changed files with 155 additions and 147 deletions.
9 changes: 9 additions & 0 deletions src/main/java/com/fourtrashes/pokerface/core/game/Card.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.fourtrashes.pokerface.core.game;

import lombok.AllArgsConstructor;

@AllArgsConstructor
public class Card {
private int card;
private boolean isOpen;
}
Original file line number Diff line number Diff line change
@@ -1,29 +1,45 @@
package com.fourtrashes.pokerface.core.game;

import com.fourtrashes.pokerface.domain.Room;
import com.fourtrashes.pokerface.dto.PlayerStateDTO;
import lombok.RequiredArgsConstructor;
import org.springframework.messaging.handler.annotation.DestinationVariable;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.simp.SimpMessageHeaderAccessor;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Controller;

import java.util.ArrayList;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

@Controller
@RequiredArgsConstructor
public class GameController {

private final SimpMessagingTemplate simpMessagingTemplate;
private final ConcurrentMap<Integer, Room> roomList;

// 추후 구현사항 room객체에 들어가야 할듯
boolean isAllPlayerReady(){
return true;
}

@MessageMapping(value = "/game/ready/{roomId}")
public void ready(@DestinationVariable("roomId") Integer roomId, SimpMessageHeaderAccessor header){
Room room = roomList.get(roomId);
GameManager gameManager = room.getGameManager();
gameManager.ready(header.getSessionId());
// 전달할 메세지 타입
// 1. 모두 준비가 되었을 때 방장이 시작하면 준비된 플레이어들 전달
// 2. 모든사람이 ready일때 자동 시작하려면 메세지 없어도 무방
// 구현 내용에 따라 시작하는 함수 달라짐
// 모든사람이 ready일때 자동 시작
ConcurrentHashMap<String, Object> users = roomList.get(roomId).getUserList();
PokerGameManager gameManager = roomList.get(roomId).getGameManager();
if (isAllPlayerReady()) {
gameManager.startGame(users);
gameManager.firstDeal();
}
ArrayList<Player> players = gameManager.getPlayers();
for(Player player : players){
// destination에 무엇이 들어가야 하는지 결정 필요
simpMessagingTemplate.convertAndSendToUser(player.getSessionId(),"", new PlayerStateDTO(player));
}
}

}
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
package com.fourtrashes.pokerface.core.game;

import java.util.concurrent.ConcurrentMap;

public interface GameManager {
void startGame();
void endGame();
void ready(String sessionId);
void bet();
void firstDeal();
void deal();
void lastDeal();
void startGame(ConcurrentMap<String, Object> users);
void settle();
void getUserTurn();
void setUser(String sessionId);
}
108 changes: 0 additions & 108 deletions src/main/java/com/fourtrashes/pokerface/core/game/GameManagerImpl.java

This file was deleted.

29 changes: 29 additions & 0 deletions src/main/java/com/fourtrashes/pokerface/core/game/Player.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.fourtrashes.pokerface.core.game;

import lombok.Getter;
import lombok.Setter;

import java.util.ArrayList;

@Getter
@Setter
public class Player {

private String sessionId;
private int turn;
private boolean isDead;
private boolean isReady;
private ArrayList<Card> cardList;

public Player(String sessionId){
this.sessionId = sessionId;
this.turn = -1;
this.isDead = false;
this.isReady = false;
this.cardList = new ArrayList<>();
}

public void putInCard(int card, boolean isOpen){
cardList.add(new Card(card, isOpen));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.fourtrashes.pokerface.core.game;

import lombok.Getter;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.concurrent.ConcurrentMap;

@Getter
public class PokerGameManager implements GameManager {

public ArrayList<Player> players;
private CardDeck cardDeck = new CardDeckImpl();

// 로직변경 : 모든 유저가 레디상태가 되면 GameManager의 startGame(유저 목록)을 불러주세요.
@Override
public void startGame(ConcurrentMap<String, Object> users) {
cardDeck.shuffle();

Iterator<String> sessionIds = users.keySet().iterator();
int turn = 0;
while (sessionIds.hasNext()) {
String sessionId = sessionIds.next();
Player player = new Player(sessionId);
player.setTurn(turn++);
players.add(player);
}

}

public void firstDeal() {
int turn = getUserTurn();
// 카드 4장 배분
for(int i=0;i<players.size() * 4;i++){
int card = cardDeck.getTop();
players.get(turn).putInCard(card, false);
turn = (turn + 1) % players.size();
}

}

public void lastDeal(){

}

public void bet() {

}

public void deal() {

}

@Override
public void settle() {

}

private int getUserTurn(){
/*
4명의 플레이어중에 가장 높은 패를 들고있는 플레이어의 index를 return 해야함.
자리 순서대로 카드가 분배될 것이므로 인원수만큼 반복 돌리면 됌.
*/
return 0;
}
}
19 changes: 0 additions & 19 deletions src/main/java/com/fourtrashes/pokerface/core/game/UserStatus.java

This file was deleted.

6 changes: 3 additions & 3 deletions src/main/java/com/fourtrashes/pokerface/domain/Room.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.fourtrashes.pokerface.domain;

import com.fourtrashes.pokerface.core.game.GameManager;
import com.fourtrashes.pokerface.core.game.PokerGameManager;
import lombok.Data;

import java.util.concurrent.ConcurrentHashMap;
Expand All @@ -17,9 +17,9 @@ public class Room extends BaseDomain {
private String url;
private Integer capacity;
private ConcurrentHashMap<String, Object> userList;
private GameManager gameManager;
private PokerGameManager gameManager;

public Room(Integer id, String url, Integer capacity, GameManager gameManager) {
public Room(Integer id, String url, Integer capacity, PokerGameManager gameManager) {
this.id = id;
this.url = url;
this.capacity = capacity;
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/com/fourtrashes/pokerface/dto/PlayerStateDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.fourtrashes.pokerface.dto;

import com.fourtrashes.pokerface.core.game.Card;
import com.fourtrashes.pokerface.core.game.Player;
import lombok.Data;

import java.util.ArrayList;

@Data
public class PlayerStateDTO {
private ArrayList<Card> cardList;
private int turn;
private boolean isDead;

public PlayerStateDTO(Player player){
this.cardList = player.getCardList();
this.turn = player.getTurn();
this.isDead = player.isDead();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.fourtrashes.pokerface.constants.ExceptionType;
import com.fourtrashes.pokerface.domain.Room;
import com.fourtrashes.pokerface.core.game.GameManagerImpl;
import com.fourtrashes.pokerface.core.game.PokerGameManager;
import com.fourtrashes.pokerface.exception.WebSocketException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
Expand All @@ -22,7 +22,7 @@ public class RoomServiceImpl implements RoomService{
public Room createRoom() {
Integer roomId = idGenerator.getAndIncrement();
String roomUrl = UUID.randomUUID().toString();
Room newRoom = new Room(roomId, roomUrl, ROOM_LIMIT, new GameManagerImpl());
Room newRoom = new Room(roomId, roomUrl, ROOM_LIMIT, new PokerGameManager());
roomList.put(roomId, newRoom);

return newRoom;
Expand Down

0 comments on commit 1470627

Please sign in to comment.