Skip to content

Commit

Permalink
[feat] #19 유저 게임 입장 구현
Browse files Browse the repository at this point in the history
- 유저의 게임 입장과 알림을 구현했습니다.
  • Loading branch information
9JaHyun committed Feb 5, 2023
1 parent 789895d commit 8d4884b
Show file tree
Hide file tree
Showing 11 changed files with 190 additions and 46 deletions.
1 change: 1 addition & 0 deletions backend/poker-service/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ build/
!gradle/wrapper/gradle-wrapper.jar
!**/src/main/**/build/
!**/src/test/**/build/
/src/main/resources/static/

### STS ###
.apt_generated
Expand Down
6 changes: 6 additions & 0 deletions backend/poker-service/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-websocket'
testImplementation 'org.springframework.boot:spring-boot-starter-test'

implementation 'org.webjars:webjars-locator-core'
implementation "org.webjars:sockjs-client:1.0.2"
implementation "org.webjars:stomp-websocket:2.3.3"
implementation 'org.webjars:bootstrap:3.4.0'
implementation 'org.webjars:jquery:3.6.2'
}

tasks.named('test') {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.pokerservice.controller.rest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class GameInfoController {

private static final Logger logger = LoggerFactory.getLogger(GameInfoController.class);

@GetMapping("/gameInfo/{gameId}")
public void getGameInfo(@PathVariable long gameId) {

}

@GetMapping("/gameInfo/players/{gameId}")
public void getPlayers(@PathVariable long gameId) {

}

@GetMapping("/gameInfo/ping/{gameId}")
public void ping(@PathVariable long gameId) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.pokerservice.controller.rest;

import com.pokerservice.core.domain.GameType;
import com.pokerservice.core.port.GameMakeUseCase;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class GameManageController {

private final GameMakeUseCase gameMakeUseCase;

public GameManageController(GameMakeUseCase gameMakeUseCase) {
this.gameMakeUseCase = gameMakeUseCase;
}


@PostMapping("/game")
public ResponseEntity<Long> makeGame(GameType gameType) {
return ResponseEntity.ok(gameMakeUseCase.makeGame(gameType));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,37 +35,31 @@ public void enter(@Payload GameMessage gameMessage, SimpMessageHeaderAccessor he

gameEnterUsecase.enterGame(new User(userId, sessionId, gameMessage.getSender()),
gameMessage.getGameId());
headerAccessor.getSessionAttributes()
.put("ws-session", headerAccessor.getUser().getName());
headerAccessor.getSessionAttributes().put("ws-session", headerAccessor.getUser().getName());
} catch (Exception e) {
sendingOperations.convertAndSend("/sub/public/" + gameMessage.getGameId(),
new GameMessage(MessageType.ERROR, null, null, 0, 0));
}

sendingOperations.convertAndSend("/sub/public/" + gameMessage.getGameId(), gameMessage);
}


// @MessageMapping("/action")
// public void gameAction(@Payload GameMessage gameMessage,
// SimpMessageHeaderAccessor headerAccessor) {
// MessageType packet = gameMessage.getType();
//
// switch (packet) {
// case GAME_START -> {
// if (gameUseCase.gameStart(gameMessage.getGameId())) {
// sendingOperations.convertAndSend("/sub/public/" + gameMessage.getGameId(),
// gameMessage);
// }
// }
//
// case BET -> {
// gameUseCase.betting(gameMessage.getGameId(),
// gameMessage.getContent().get("betAmount"));
// sendingOperations.convertAndSend("/sub/public/" + gameMessage.getGameId(),
// gameMessage);
// }
// }
//
// }
@MessageMapping("/action")
public void gameAction(@Payload GameMessage gameMessage,
SimpMessageHeaderAccessor headerAccessor) {
MessageType packet = gameMessage.getType();

switch (packet) {
case BET -> {
gameUseCase.betting(gameMessage.getGameId(),
gameMessage.getContent().get("betAmount"));
sendingOperations.convertAndSend("/sub/public/" + gameMessage.getGameId(),
gameMessage);
}



}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.pokerservice.controller.ws;

import com.pokerservice.controller.ws.GameMessage.MessageType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.messaging.simp.SimpMessageHeaderAccessor;
import org.springframework.messaging.simp.SimpMessageSendingOperations;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

private static final Logger logger = LoggerFactory.getLogger(TestController.class);
private final SimpMessageSendingOperations sendingOperations;

public TestController(SimpMessageSendingOperations sendingOperations) {
this.sendingOperations = sendingOperations;
}

@MessageMapping("/test")
public void test(@Payload GameMessage gameMessage, SimpMessageHeaderAccessor headerAccessor) {
try {
headerAccessor.getSessionAttributes()
.put("ws-session", headerAccessor.getUser().getName());
} catch (Exception e) {
sendingOperations.convertAndSend("/sub/public/" + gameMessage.getGameId(),
new GameMessage(MessageType.ERROR, null, null, 0, 0));
}

sendingOperations.convertAndSend("/sub/public/" + gameMessage.getGameId(), gameMessage);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
import java.util.Random;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Logger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Game {

private final Logger logger = Logger.getLogger("Game");
private final Logger log = LoggerFactory.getLogger(Game.class);
private static final String MESSAGE_ENDPOINT = "/topic/public/";

private long id;
private final int MIN_CARD_VALUE = 1;
Expand Down Expand Up @@ -43,7 +45,13 @@ public static Game create4PGame() {
}

public void enterGame(Player player) {
if (isFull()) {
throw new AssertionError("game room already Full! current: " + players.size());
}

players.add(player);
log.info("enter the game / gameId: {} players: {}/{}", id, players.size(),
gameType.getPlayerCount());
}

public void exitGame(Player player) {
Expand Down Expand Up @@ -71,36 +79,47 @@ public void drawCard() {
while (++gameCard[pickCard] >= 1) {
player.drawCard(pickCard);

GameMessage sendMyCard = GameMessage.createDrawMessage(player.getSeatNo(), pickCard);
sendMyCard.send(player, "/topic/public/" + id);
GameMessage sendMyCard = GameMessage.createDrawMessage(player.getSeatNo(),
pickCard);
sendMyCard.send(player, MESSAGE_ENDPOINT + id);
}
}
}

public void gameStart() {
public void gameStartOrNot() {
if (isFull()) {
log.info("====== game start!");
gameState = GameState.START;

totalBetAmount = 0;
GameMessage gameMessage = GameMessage.createStartMessage();
gameMessage.sendAll(players, "/topic/public/" + id);
gameMessage.sendAll(players, MESSAGE_ENDPOINT + id);
}
}

public void stageStart() {
log.info("====== game start!");
gameState = GameState.START;

totalBetAmount = 0;
GameMessage gameMessage = GameMessage.createStartMessage();
gameMessage.sendAll(players, MESSAGE_ENDPOINT + id);
}

public void betting() {
for (Player player : players) {
player.updateChip(-betAmount);

GameMessage message = GameMessage.createBettingMessage(betAmount,
player.getChip(), player.getSeatNo());
totalBetAmount += betAmount;
message.sendAll(players, "/topic/public/" + id);
message.sendAll(players, MESSAGE_ENDPOINT + id);
}
}

public void focus(int focusSeat) {
GameMessage message = GameMessage.createFocusMessage(focusSeat);
message.sendAll(players, "/topic/public/" + id);
message.sendAll(players, MESSAGE_ENDPOINT + id);
}

public void requestAction() {
Expand All @@ -109,16 +128,16 @@ public void requestAction() {

for (Player player : players) {
idx += 1;
GameMessage message = new GameMessage(MessageType.BATTLE, null, null,0, 0);
message.send(player, "/topic/public/" + id);
GameMessage message = new GameMessage(MessageType.BATTLE, null, null, 0, 0);
message.send(player, MESSAGE_ENDPOINT + id);

focus(player.getSeatNo());
}
}

public void turn(int turn) {
this.currentTurn = turn;
logger.info("========= turn: " + turn + " =========");
log.info("========= turn: " + turn + " =========");

if (currentTurn >= 1 && currentTurn <= 3) {
requestAction();
Expand All @@ -133,8 +152,8 @@ public boolean isFull() {
return gameType.getPlayerCount() == players.size();
}

public Logger getLogger() {
return logger;
public Logger getLog() {
return log;
}

public int getMIN_CARD_VALUE() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,5 @@

public interface GameUseCase {

boolean gameStart(long gameId);

void betting(long gameId, int bettingAmount);
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,12 @@ public long makeGame(GameType gameType) {
@Override
public void enterGame(User user, long gameId) {
Game game = storagePort.findGameById(gameId);
if (game != null) {
log.info("game find! gameInfo = {}", game);
}
Player player = Player.create(user.userId(), user.sessionId(), user.nickname());
game.enterGame(player);
}

@Override
public boolean gameStart(long gameId) {
Game game = storagePort.findGameById(gameId);
return true;
game.gameStartOrNot();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.pokerservice.core.service;

import com.pokerservice.controller.ws.GameMessage;

public record SessionMessage(
GameMessage gameMessage,
String session,
String name
) {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.pokerservice;

import com.pokerservice.controller.ws.GameMessage.MessageType;
import java.util.Random;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class RandomTest {

@Test
void randomTest(){
int min = 1;
int max = 10;
Random random = new Random();
for (int i = 0; i < 100; i++) {
System.out.println(random.nextInt((max - min) + min) + min);
}
}

@Test
void enumToStringTest(){
// given
MessageType join = MessageType.valueOf("JOIN");
MessageType draw = MessageType.valueOf("DRAW");

// when
Assertions.assertTrue(join == MessageType.JOIN);
Assertions.assertFalse(join == MessageType.DRAW);

// then
}
}

0 comments on commit 8d4884b

Please sign in to comment.