Skip to content

Golf4j #270

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions bazel/java.MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ maven.install(
"io.sentry:sentry-logback:8.1.0",
"junit:junit:4.13.2",
"org.assertj:assertj-core:3.27.3",
"org.eclipse.jetty:jetty-server:11.0.23",
"org.eclipse.jetty.websocket:websocket-jetty-server:11.0.23",
"org.scala-lang:scala3-library_3:jar:3.3.1",
"org.scala-lang:scala3-compiler_3:3.6.3",
"org.slf4j:slf4j-api:2.0.16",
Expand Down
6 changes: 3 additions & 3 deletions cpp/cards/golf/game_state.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ unordered_set<int> GameState::winners() const {
int minScore = 40; // max score is 9 10 Q K == 39
int playerIndex = 0;
for (auto& p : players) {
int playerScore = p.score();
const int playerScore = p.score();
if (playerScore < minScore) {
minScore = playerScore;
winningPlayers.clear();
Expand All @@ -42,7 +42,7 @@ unordered_set<int> GameState::winners() const {

playerIndex++;
}
if (winningPlayers.find(whoKnocked) != winningPlayers.end()) {
if (winningPlayers.contains(whoKnocked)) {
winningPlayers.clear();
winningPlayers.insert(whoKnocked);
}
Expand Down Expand Up @@ -89,7 +89,7 @@ StatusOr<GameState> GameState::swapDrawForDiscardPile(int player) const {
const deque<Card> discardPileForNewGameState = std::move(updatedDiscardPile);

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

return GameState{drawPileForNewGameState,
discardPileForNewGameState,
Expand Down
2 changes: 1 addition & 1 deletion jvm/src/main/java/com/muchq/cards/castle/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ java_library(
],
visibility = ["//visibility:public"],
deps = [
"//jvm/src/main/java/com/muchq/cards",
"//jvm/src/main/java/com/muchq/cards/model",
],
)
2 changes: 1 addition & 1 deletion jvm/src/main/java/com/muchq/cards/castle/GameState.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.muchq.cards.castle;

import com.muchq.cards.Card;
import com.muchq.cards.model.Card;

import java.util.Deque;
import java.util.List;
Expand Down
2 changes: 1 addition & 1 deletion jvm/src/main/java/com/muchq/cards/castle/Player.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.muchq.cards.castle;

import com.muchq.cards.Card;
import com.muchq.cards.model.Card;

import java.util.List;

Expand Down
2 changes: 1 addition & 1 deletion jvm/src/main/java/com/muchq/cards/castle/ThreeDown.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.muchq.cards.castle;

import com.muchq.cards.Card;
import com.muchq.cards.model.Card;

import java.util.Optional;

Expand Down
2 changes: 1 addition & 1 deletion jvm/src/main/java/com/muchq/cards/castle/ThreeUp.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.muchq.cards.castle;

import com.muchq.cards.Card;
import com.muchq.cards.model.Card;

import java.util.Optional;

Expand Down
2 changes: 1 addition & 1 deletion jvm/src/main/java/com/muchq/cards/castle/Turn.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.muchq.cards.castle;

import com.muchq.cards.Rank;
import com.muchq.cards.model.Rank;

public record Turn(Rank rank, int howMany) {
}
22 changes: 22 additions & 0 deletions jvm/src/main/java/com/muchq/cards/golf_ws/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
load("@rules_java//java:java_binary.bzl", "java_binary")

java_binary(
name = "golf_ws",
srcs = [
"Game.java",
"GameHandler.java",
"Main.java",
"Request.java",
"Response.java",
],
main_class = "com.muchq.cards.golf_ws.Main",
visibility = ["//visibility:public"],
deps = [
"//jvm/src/main/java/com/muchq/json",
"@maven//:org_eclipse_jetty_jetty_server",
"@maven//:org_eclipse_jetty_jetty_servlet",
"@maven//:org_eclipse_jetty_websocket_websocket_jetty_api",
"@maven//:org_eclipse_jetty_websocket_websocket_jetty_server",
"@maven//:org_slf4j_slf4j_api",
],
)
9 changes: 9 additions & 0 deletions jvm/src/main/java/com/muchq/cards/golf_ws/Game.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.muchq.cards.golf_ws;

import org.eclipse.jetty.websocket.api.Session;

public class Game {
public Game(String gameId) {}

public void addPlayer(Session session) {}
}
107 changes: 107 additions & 0 deletions jvm/src/main/java/com/muchq/cards/golf_ws/GameHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package com.muchq.cards.golf_ws;

import com.muchq.json.JsonUtils;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketClose;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketConnect;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage;
import org.eclipse.jetty.websocket.api.annotations.WebSocket;

@WebSocket
public class GameHandler {
private static final Set<Session> sessions = Collections.synchronizedSet(new HashSet<>());
private static final Map<String, Game> games = new HashMap<>();

@OnWebSocketConnect
public void onConnect(Session session) {
sessions.add(session);
}

@OnWebSocketClose
public void onClose(Session session, int statusCode, String reason) {
sessions.remove(session);
}

@OnWebSocketMessage
public void onMessage(Session session, String message) {
Request request = JsonUtils.readAs(message, Request.class);
String action = request.getAction();
switch (action) {
case "start-new-game":
startNewGame(session, request);
break;
case "join-existing-game":
joinExistingGame(session, request);
break;
case "peek":
peek(session, request);
break;
case "swap":
swap(session, request);
break;
case "knock":
knock(session, request);
break;
default:
sendError(session, "Invalid action");
}
}

private void startNewGame(Session session, Request request) {
String gameId = request.getGameId();
Game game = new Game(gameId);
games.put(gameId, game);
sendResponse(session, "Game started with ID: " + gameId);
}

private void joinExistingGame(Session session, Request request) {
String gameId = request.getGameId();
Game game = games.get(gameId);
if (game != null) {
game.addPlayer(session);
sendResponse(session, "Joined game with ID: " + gameId);
} else {
sendError(session, "Game not found");
}
}

private void peek(Session session, Request request) {
// Implement peek logic
sendResponse(session, "Peek action executed");
}

private void swap(Session session, Request request) {
// Implement swap logic
sendResponse(session, "Swap action executed");
}

private void knock(Session session, Request request) {
// Implement knock logic
sendResponse(session, "Knock action executed");
}

private void sendResponse(Session session, String message) {
try {
Response response = Response.success(message);
session.getRemote().sendString(JsonUtils.writeAsString(response));
} catch (IOException e) {
e.printStackTrace();
}
}

private void sendError(Session session, String error) {
try {
Response response = Response.error(error);
session.getRemote().sendString(JsonUtils.writeAsString(response));
} catch (IOException e) {
e.printStackTrace();
}
}
}

28 changes: 28 additions & 0 deletions jvm/src/main/java/com/muchq/cards/golf_ws/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.muchq.cards.golf_ws;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.websocket.server.config.JettyWebSocketServletContainerInitializer;

public class Main {
public static void main(String[] args) throws Exception {
Server server = new Server(8080);

ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/");
server.setHandler(context);

JettyWebSocketServletContainerInitializer.configure(
context,
(servletContext, wsContainer) -> {
wsContainer.addMapping("", (req, res) -> new GameHandler());
});

ServletHolder holder = new ServletHolder(new org.eclipse.jetty.servlet.DefaultServlet());
context.addServlet(holder, "/");

server.start();
server.join();
}
}
11 changes: 11 additions & 0 deletions jvm/src/main/java/com/muchq/cards/golf_ws/Request.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.muchq.cards.golf_ws;

public class Request {
public String getAction() {
return "";
}

public String getGameId() {
return "";
}
}
13 changes: 13 additions & 0 deletions jvm/src/main/java/com/muchq/cards/golf_ws/Response.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.muchq.cards.golf_ws;

public class Response {
public Response() {}

public static Response success(String message) {
return new Response();
}

public static Response error(String message) {
return new Response();
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
load("@rules_java//java:java_library.bzl", "java_library")

java_library(
name = "cards",
name = "model",
srcs = [
"Card.java",
"Deck.java",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.muchq.cards;
package com.muchq.cards.model;

import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.muchq.cards;
package com.muchq.cards.model;

import java.util.ArrayDeque;
import java.util.ArrayList;
Expand All @@ -7,7 +7,7 @@
import java.util.List;

public record Deck(Deque<Card> cards) {

public boolean hasNext() {
return !cards.isEmpty();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.muchq.cards;
package com.muchq.cards.model;

public enum Rank {
TWO,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.muchq.cards;
package com.muchq.cards.model;

public enum Suit {
CLUBS,
Expand Down
Loading
Loading