From 1470627b8280a04fa7269cc6c19ff77a67372d26 Mon Sep 17 00:00:00 2001 From: 119wjw <119wjw@naver.com> Date: Tue, 12 Apr 2022 15:08:14 +0900 Subject: [PATCH] =?UTF-8?q?feat=20:=20=EA=B2=8C=EC=9E=84=20=EC=8B=9C?= =?UTF-8?q?=EC=9E=91=20=ED=9B=84=20=EC=B9=B4=EB=93=9C=20=EB=B6=84=EB=B0=B0?= =?UTF-8?q?=ED=95=98=EB=8A=94=20=EB=A1=9C=EC=A7=81=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Issue : #15 --- .../fourtrashes/pokerface/core/game/Card.java | 9 ++ .../pokerface/core/game/GameController.java | 28 ++++- .../pokerface/core/game/GameManager.java | 12 +- .../pokerface/core/game/GameManagerImpl.java | 108 ------------------ .../pokerface/core/game/Player.java | 29 +++++ .../pokerface/core/game/PokerGameManager.java | 66 +++++++++++ .../pokerface/core/game/UserStatus.java | 19 --- .../fourtrashes/pokerface/domain/Room.java | 6 +- .../pokerface/dto/PlayerStateDTO.java | 21 ++++ .../pokerface/service/RoomServiceImpl.java | 4 +- 10 files changed, 155 insertions(+), 147 deletions(-) create mode 100644 src/main/java/com/fourtrashes/pokerface/core/game/Card.java delete mode 100644 src/main/java/com/fourtrashes/pokerface/core/game/GameManagerImpl.java create mode 100644 src/main/java/com/fourtrashes/pokerface/core/game/Player.java create mode 100644 src/main/java/com/fourtrashes/pokerface/core/game/PokerGameManager.java delete mode 100644 src/main/java/com/fourtrashes/pokerface/core/game/UserStatus.java create mode 100644 src/main/java/com/fourtrashes/pokerface/dto/PlayerStateDTO.java diff --git a/src/main/java/com/fourtrashes/pokerface/core/game/Card.java b/src/main/java/com/fourtrashes/pokerface/core/game/Card.java new file mode 100644 index 0000000..31533b2 --- /dev/null +++ b/src/main/java/com/fourtrashes/pokerface/core/game/Card.java @@ -0,0 +1,9 @@ +package com.fourtrashes.pokerface.core.game; + +import lombok.AllArgsConstructor; + +@AllArgsConstructor +public class Card { + private int card; + private boolean isOpen; +} diff --git a/src/main/java/com/fourtrashes/pokerface/core/game/GameController.java b/src/main/java/com/fourtrashes/pokerface/core/game/GameController.java index 287d1ee..be9b385 100644 --- a/src/main/java/com/fourtrashes/pokerface/core/game/GameController.java +++ b/src/main/java/com/fourtrashes/pokerface/core/game/GameController.java @@ -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 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 users = roomList.get(roomId).getUserList(); + PokerGameManager gameManager = roomList.get(roomId).getGameManager(); + if (isAllPlayerReady()) { + gameManager.startGame(users); + gameManager.firstDeal(); + } + ArrayList players = gameManager.getPlayers(); + for(Player player : players){ + // destination에 무엇이 들어가야 하는지 결정 필요 + simpMessagingTemplate.convertAndSendToUser(player.getSessionId(),"", new PlayerStateDTO(player)); + } } } diff --git a/src/main/java/com/fourtrashes/pokerface/core/game/GameManager.java b/src/main/java/com/fourtrashes/pokerface/core/game/GameManager.java index dbe66cd..a202bed 100644 --- a/src/main/java/com/fourtrashes/pokerface/core/game/GameManager.java +++ b/src/main/java/com/fourtrashes/pokerface/core/game/GameManager.java @@ -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 users); void settle(); - void getUserTurn(); - void setUser(String sessionId); } diff --git a/src/main/java/com/fourtrashes/pokerface/core/game/GameManagerImpl.java b/src/main/java/com/fourtrashes/pokerface/core/game/GameManagerImpl.java deleted file mode 100644 index 3fbba46..0000000 --- a/src/main/java/com/fourtrashes/pokerface/core/game/GameManagerImpl.java +++ /dev/null @@ -1,108 +0,0 @@ -package com.fourtrashes.pokerface.core.game; - -import java.util.Iterator; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ConcurrentMap; - -public class GameManagerImpl implements GameManager { - - ConcurrentMap players = new ConcurrentHashMap<>(); - CardDeck cardDeck = new CardDeckImpl(); - - @Override - public void startGame() { - - cardDeck.shuffle(); - - // 카드 4장씩 나누어주고 한장 버리는 로직 - firstDeal(); - - // 배팅 - bet(); - - // 4번째 카드 - deal(); - - // 배팅 - bet(); - - // 5번째 카드 - deal(); - - // 배팅 - bet(); - - // 6번째 카드 - deal(); - - // 배팅 - bet(); - - // 히든 카드 - lastDeal(); - - // 배팅 - bet(); - - endGame(); - } - - @Override - public void ready(String sessionId) { - players.get(sessionId).setReady(true); - if(isAllPlayerReady()){ - startGame(); - } - } - - private boolean isAllPlayerReady(){ - Iterator sessionIds = players.keySet().iterator(); - while (sessionIds.hasNext()) { - String sessionId = sessionIds.next(); - if(!players.get(sessionId).isReady()){ - return false; - } - } - return true; - } - - @Override - public void firstDeal() { - - } - - @Override - public void lastDeal(){ - - } - - @Override - public void endGame() { - settle(); - } - - @Override - public void bet() { - - } - - @Override - public void deal() { - - } - - @Override - public void settle() { - - } - - @Override - public void getUserTurn(){ - - } - - @Override - public void setUser(String sessionId){ - players.put(sessionId, new UserStatus()); - } -} diff --git a/src/main/java/com/fourtrashes/pokerface/core/game/Player.java b/src/main/java/com/fourtrashes/pokerface/core/game/Player.java new file mode 100644 index 0000000..6f85aae --- /dev/null +++ b/src/main/java/com/fourtrashes/pokerface/core/game/Player.java @@ -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 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)); + } +} diff --git a/src/main/java/com/fourtrashes/pokerface/core/game/PokerGameManager.java b/src/main/java/com/fourtrashes/pokerface/core/game/PokerGameManager.java new file mode 100644 index 0000000..16efd48 --- /dev/null +++ b/src/main/java/com/fourtrashes/pokerface/core/game/PokerGameManager.java @@ -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 players; + private CardDeck cardDeck = new CardDeckImpl(); + + // 로직변경 : 모든 유저가 레디상태가 되면 GameManager의 startGame(유저 목록)을 불러주세요. + @Override + public void startGame(ConcurrentMap users) { + cardDeck.shuffle(); + + Iterator 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 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; diff --git a/src/main/java/com/fourtrashes/pokerface/dto/PlayerStateDTO.java b/src/main/java/com/fourtrashes/pokerface/dto/PlayerStateDTO.java new file mode 100644 index 0000000..f1898af --- /dev/null +++ b/src/main/java/com/fourtrashes/pokerface/dto/PlayerStateDTO.java @@ -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 cardList; + private int turn; + private boolean isDead; + + public PlayerStateDTO(Player player){ + this.cardList = player.getCardList(); + this.turn = player.getTurn(); + this.isDead = player.isDead(); + } + +} diff --git a/src/main/java/com/fourtrashes/pokerface/service/RoomServiceImpl.java b/src/main/java/com/fourtrashes/pokerface/service/RoomServiceImpl.java index 80b0eeb..7889255 100644 --- a/src/main/java/com/fourtrashes/pokerface/service/RoomServiceImpl.java +++ b/src/main/java/com/fourtrashes/pokerface/service/RoomServiceImpl.java @@ -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; @@ -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;