Skip to content

Commit f181c10

Browse files
committed
start golf4j
1 parent 5ab2b46 commit f181c10

20 files changed

+1751
-14584
lines changed

MODULE.bazel

+2
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ maven.install(
113113
"io.sentry:sentry-logback:7.14.0",
114114
"junit:junit:4.13.2",
115115
"org.assertj:assertj-core:3.26.3",
116+
"org.eclipse.jetty:jetty-server:11.0.23",
117+
"org.eclipse.jetty.websocket:websocket-jetty-server:11.0.23",
116118
"org.scala-lang:scala3-library_3:jar:3.3.1",
117119
"org.scala-lang:scala3-compiler_3:3.3.1",
118120
"org.slf4j:slf4j-api:2.0.16",

MODULE.bazel.lock

+1,546-14,569
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cpp/cards/golf/game_state.cc

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ unordered_set<int> GameState::winners() const {
3131
int minScore = 40; // max score is 9 10 Q K == 39
3232
int playerIndex = 0;
3333
for (auto& p : players) {
34-
int playerScore = p.score();
34+
const int playerScore = p.score();
3535
if (playerScore < minScore) {
3636
minScore = playerScore;
3737
winningPlayers.clear();
@@ -42,7 +42,7 @@ unordered_set<int> GameState::winners() const {
4242

4343
playerIndex++;
4444
}
45-
if (winningPlayers.find(whoKnocked) != winningPlayers.end()) {
45+
if (winningPlayers.contains(whoKnocked)) {
4646
winningPlayers.clear();
4747
winningPlayers.insert(whoKnocked);
4848
}
@@ -89,7 +89,7 @@ StatusOr<GameState> GameState::swapDrawForDiscardPile(int player) const {
8989
const deque<Card> discardPileForNewGameState = std::move(updatedDiscardPile);
9090

9191
// update whose turn it is
92-
int newWhoseTurn = (whoseTurn + 1) % players.size();
92+
const int newWhoseTurn = (whoseTurn + 1) % players.size();
9393

9494
return GameState{drawPileForNewGameState,
9595
discardPileForNewGameState,

jvm/src/main/java/com/muchq/cards/castle/BUILD.bazel

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ java_library(
99
],
1010
visibility = ["//visibility:public"],
1111
deps = [
12-
"//jvm/src/main/java/com/muchq/cards",
12+
"//jvm/src/main/java/com/muchq/cards/model",
1313
],
1414
)

jvm/src/main/java/com/muchq/cards/castle/GameState.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.muchq.cards.castle;
22

3-
import com.muchq.cards.Card;
3+
import com.muchq.cards.model.Card;
44

55
import java.util.Deque;
66
import java.util.List;

jvm/src/main/java/com/muchq/cards/castle/Player.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.muchq.cards.castle;
22

3-
import com.muchq.cards.Card;
3+
import com.muchq.cards.model.Card;
44

55
import java.util.List;
66

jvm/src/main/java/com/muchq/cards/castle/ThreeDown.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.muchq.cards.castle;
22

3-
import com.muchq.cards.Card;
3+
import com.muchq.cards.model.Card;
44

55
import java.util.Optional;
66

jvm/src/main/java/com/muchq/cards/castle/ThreeUp.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.muchq.cards.castle;
22

3-
import com.muchq.cards.Card;
3+
import com.muchq.cards.model.Card;
44

55
import java.util.Optional;
66

Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.muchq.cards.castle;
22

3-
import com.muchq.cards.Rank;
3+
import com.muchq.cards.model.Rank;
44

55
public record Turn(Rank rank, int howMany) {
66
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
java_binary(
2+
name = "golf_ws",
3+
srcs = [
4+
"Game.java",
5+
"GameHandler.java",
6+
"Main.java",
7+
"Request.java",
8+
"Response.java",
9+
],
10+
main_class = "com.muchq.cards.golf_ws.Main",
11+
visibility = ["//visibility:public"],
12+
deps = [
13+
"//jvm/src/main/java/com/muchq/json",
14+
"@maven//:org_eclipse_jetty_jetty_server",
15+
"@maven//:org_eclipse_jetty_jetty_servlet",
16+
"@maven//:org_eclipse_jetty_websocket_websocket_jetty_api",
17+
"@maven//:org_eclipse_jetty_websocket_websocket_jetty_server",
18+
"@maven//:org_slf4j_slf4j_api",
19+
],
20+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.muchq.cards.golf_ws;
2+
3+
import org.eclipse.jetty.websocket.api.Session;
4+
5+
public class Game {
6+
public Game(String gameId) {}
7+
8+
public void addPlayer(Session session) {}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package com.muchq.cards.golf_ws;
2+
3+
import com.muchq.json.JsonUtils;
4+
import java.io.IOException;
5+
import java.util.Collections;
6+
import java.util.HashMap;
7+
import java.util.HashSet;
8+
import java.util.Map;
9+
import java.util.Set;
10+
import org.eclipse.jetty.websocket.api.Session;
11+
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketClose;
12+
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketConnect;
13+
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage;
14+
import org.eclipse.jetty.websocket.api.annotations.WebSocket;
15+
16+
@WebSocket
17+
public class GameHandler {
18+
private static final Set<Session> sessions = Collections.synchronizedSet(new HashSet<>());
19+
private static final Map<String, Game> games = new HashMap<>();
20+
21+
@OnWebSocketConnect
22+
public void onConnect(Session session) {
23+
sessions.add(session);
24+
}
25+
26+
@OnWebSocketClose
27+
public void onClose(Session session, int statusCode, String reason) {
28+
sessions.remove(session);
29+
}
30+
31+
@OnWebSocketMessage
32+
public void onMessage(Session session, String message) {
33+
Request request = JsonUtils.readAs(message, Request.class);
34+
String action = request.getAction();
35+
switch (action) {
36+
case "start-new-game":
37+
startNewGame(session, request);
38+
break;
39+
case "join-existing-game":
40+
joinExistingGame(session, request);
41+
break;
42+
case "peek":
43+
peek(session, request);
44+
break;
45+
case "swap":
46+
swap(session, request);
47+
break;
48+
case "knock":
49+
knock(session, request);
50+
break;
51+
default:
52+
sendError(session, "Invalid action");
53+
}
54+
}
55+
56+
private void startNewGame(Session session, Request request) {
57+
String gameId = request.getGameId();
58+
Game game = new Game(gameId);
59+
games.put(gameId, game);
60+
sendResponse(session, "Game started with ID: " + gameId);
61+
}
62+
63+
private void joinExistingGame(Session session, Request request) {
64+
String gameId = request.getGameId();
65+
Game game = games.get(gameId);
66+
if (game != null) {
67+
game.addPlayer(session);
68+
sendResponse(session, "Joined game with ID: " + gameId);
69+
} else {
70+
sendError(session, "Game not found");
71+
}
72+
}
73+
74+
private void peek(Session session, Request request) {
75+
// Implement peek logic
76+
sendResponse(session, "Peek action executed");
77+
}
78+
79+
private void swap(Session session, Request request) {
80+
// Implement swap logic
81+
sendResponse(session, "Swap action executed");
82+
}
83+
84+
private void knock(Session session, Request request) {
85+
// Implement knock logic
86+
sendResponse(session, "Knock action executed");
87+
}
88+
89+
private void sendResponse(Session session, String message) {
90+
try {
91+
Response response = Response.success(message);
92+
session.getRemote().sendString(JsonUtils.writeAsString(response));
93+
} catch (IOException e) {
94+
e.printStackTrace();
95+
}
96+
}
97+
98+
private void sendError(Session session, String error) {
99+
try {
100+
Response response = Response.error(error);
101+
session.getRemote().sendString(JsonUtils.writeAsString(response));
102+
} catch (IOException e) {
103+
e.printStackTrace();
104+
}
105+
}
106+
}
107+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.muchq.cards.golf_ws;
2+
3+
import org.eclipse.jetty.server.Server;
4+
import org.eclipse.jetty.servlet.ServletContextHandler;
5+
import org.eclipse.jetty.servlet.ServletHolder;
6+
import org.eclipse.jetty.websocket.server.config.JettyWebSocketServletContainerInitializer;
7+
8+
public class Main {
9+
public static void main(String[] args) throws Exception {
10+
Server server = new Server(8080);
11+
12+
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
13+
context.setContextPath("/");
14+
server.setHandler(context);
15+
16+
JettyWebSocketServletContainerInitializer.configure(
17+
context,
18+
(servletContext, wsContainer) -> {
19+
wsContainer.addMapping("", (req, res) -> new GameHandler());
20+
});
21+
22+
ServletHolder holder = new ServletHolder(new org.eclipse.jetty.servlet.DefaultServlet());
23+
context.addServlet(holder, "/");
24+
25+
server.start();
26+
server.join();
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.muchq.cards.golf_ws;
2+
3+
public class Request {
4+
public String getAction() {
5+
return "";
6+
}
7+
8+
public String getGameId() {
9+
return "";
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.muchq.cards.golf_ws;
2+
3+
public class Response {
4+
public Response() {}
5+
6+
public static Response success(String message) {
7+
return new Response();
8+
}
9+
10+
public static Response error(String message) {
11+
return new Response();
12+
}
13+
}

jvm/src/main/java/com/muchq/cards/BUILD.bazel renamed to jvm/src/main/java/com/muchq/cards/model/BUILD.bazel

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
java_library(
2-
name = "cards",
2+
name = "model",
33
srcs = [
44
"Card.java",
55
"Deck.java",

jvm/src/main/java/com/muchq/cards/Card.java renamed to jvm/src/main/java/com/muchq/cards/model/Card.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.muchq.cards;
1+
package com.muchq.cards.model;
22

33
import java.util.List;
44

jvm/src/main/java/com/muchq/cards/Deck.java renamed to jvm/src/main/java/com/muchq/cards/model/Deck.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.muchq.cards;
1+
package com.muchq.cards.model;
22

33
import java.util.ArrayDeque;
44
import java.util.ArrayList;
@@ -7,7 +7,7 @@
77
import java.util.List;
88

99
public record Deck(Deque<Card> cards) {
10-
10+
1111
public boolean hasNext() {
1212
return !cards.isEmpty();
1313
}

jvm/src/main/java/com/muchq/cards/Rank.java renamed to jvm/src/main/java/com/muchq/cards/model/Rank.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.muchq.cards;
1+
package com.muchq.cards.model;
22

33
public enum Rank {
44
TWO,

jvm/src/main/java/com/muchq/cards/Suit.java renamed to jvm/src/main/java/com/muchq/cards/model/Suit.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.muchq.cards;
1+
package com.muchq.cards.model;
22

33
public enum Suit {
44
CLUBS,

0 commit comments

Comments
 (0)