-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from BlazingTwist/15-define-backend-communication
Implement Backend/Frontend communication, refactoring and tests.
- Loading branch information
Showing
145 changed files
with
5,620 additions
and
919 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -78,3 +78,4 @@ fabric.properties | |
|
||
target | ||
/logs/ | ||
/src/main/jchess-web/api-docs |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package example.undertow; | ||
|
||
import io.undertow.Handlers; | ||
import io.undertow.Undertow; | ||
import io.undertow.server.HttpHandler; | ||
import io.undertow.server.handlers.PathHandler; | ||
import io.undertow.server.handlers.resource.ClassPathResourceManager; | ||
import io.undertow.servlet.Servlets; | ||
import io.undertow.servlet.api.DeploymentInfo; | ||
import io.undertow.servlet.api.DeploymentManager; | ||
import io.undertow.websockets.WebSocketConnectionCallback; | ||
import io.undertow.websockets.core.AbstractReceiveListener; | ||
import io.undertow.websockets.core.BufferedTextMessage; | ||
import io.undertow.websockets.core.WebSocketChannel; | ||
import io.undertow.websockets.core.WebSockets; | ||
import io.undertow.websockets.spi.WebSocketHttpExchange; | ||
import jakarta.servlet.ServletException; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
|
||
public class UndertowWebsocket { | ||
private static final Logger logger = LoggerFactory.getLogger(UndertowWebsocket.class); | ||
|
||
public static void main(String[] args) throws ServletException, IOException { | ||
DeploymentInfo deployment = Servlets.deployment() | ||
.setClassLoader(UndertowWebsocket.class.getClassLoader()) | ||
.setContextPath("") | ||
.setDeploymentName("Example_UndertowWebsocket") | ||
.setResourceManager(new ClassPathResourceManager(UndertowWebsocket.class.getClassLoader())); | ||
|
||
DeploymentManager manager = Servlets.defaultContainer().addDeployment(deployment); | ||
manager.deploy(); | ||
HttpHandler handler = manager.start(); | ||
PathHandler pathHandler = Handlers.path(handler) | ||
.addPrefixPath("/websocket", Handlers.websocket(new SocketHandler())); | ||
|
||
Undertow server = Undertow.builder() | ||
.addHttpListener(8880, "localhost") | ||
.setHandler(pathHandler) | ||
.build(); | ||
server.start(); | ||
logger.info("Server started"); | ||
} | ||
|
||
public static class SocketHandler extends AbstractReceiveListener implements WebSocketConnectionCallback { | ||
private final List<WebSocketChannel> channels = new ArrayList<>(); | ||
|
||
@Override | ||
public void onConnect(WebSocketHttpExchange exchange, WebSocketChannel channel) { | ||
logger.info("onConnect"); | ||
this.channels.add(channel); | ||
|
||
channel.getReceiveSetter().set(this); | ||
channel.resumeReceives(); | ||
} | ||
|
||
@Override | ||
protected void onFullTextMessage(WebSocketChannel channel, BufferedTextMessage message) { | ||
String msgText = message.getData(); | ||
logger.info("Received message {}", msgText); | ||
notifyChannels(msgText); | ||
} | ||
|
||
public void notifyChannels(String message) { | ||
int numChannelsNotified = 0; | ||
int numChannelsDropped = 0; | ||
Iterator<WebSocketChannel> iterator = channels.iterator(); | ||
while (iterator.hasNext()) { | ||
WebSocketChannel channel = iterator.next(); | ||
if (channel.getCloseCode() >= 0) { | ||
iterator.remove(); | ||
numChannelsDropped++; | ||
continue; | ||
} | ||
|
||
WebSockets.sendText(message, channel, null); | ||
numChannelsNotified++; | ||
} | ||
logger.info("Notified {} channels, dropped {} channels", numChannelsNotified, numChannelsDropped); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package jchess.ecs; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class EcsEventManager { | ||
private final Map<Class<?>, EcsEvent<?>> events = new HashMap<>(); | ||
|
||
public <T extends EcsEvent<?>> void registerEvent(T event) { | ||
events.put(event.getClass(), event); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public <T extends EcsEvent<?>> T getEvent(Class<? extends EcsEvent<?>> eventClass) { | ||
return (T) events.get(eventClass); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package jchess.game.common; | ||
|
||
import jchess.ecs.EcsEventManager; | ||
import jchess.ecs.Entity; | ||
import jchess.ecs.EntityManager; | ||
import jchess.game.common.events.BoardClickedEvent; | ||
import jchess.game.common.events.PieceMoveEvent; | ||
import jchess.game.common.events.RenderEvent; | ||
import jchess.game.common.components.MarkerComponent; | ||
import jchess.game.common.components.MarkerType; | ||
import jchess.game.common.theme.IIconKey; | ||
|
||
public abstract class BaseChessGame implements IChessGame { | ||
protected final EntityManager entityManager; | ||
protected final EcsEventManager eventManager; | ||
protected final int numPlayers; | ||
|
||
protected int activePlayerId = 0; | ||
|
||
public BaseChessGame(int numPlayers) { | ||
this.entityManager = new EntityManager(); | ||
this.eventManager = new EcsEventManager(); | ||
this.numPlayers = numPlayers; | ||
|
||
eventManager.registerEvent(new RenderEvent()); | ||
eventManager.registerEvent(new PieceMoveEvent()); | ||
|
||
BoardClickedEvent boardClickedEvent = new BoardClickedEvent(); | ||
eventManager.registerEvent(boardClickedEvent); | ||
boardClickedEvent.addPostEventListener(vector -> onBoardClicked(vector.getX(), vector.getY())); | ||
} | ||
|
||
@Override | ||
public abstract void start(); | ||
|
||
protected abstract Entity getEntityAtPosition(int x, int y); | ||
|
||
protected abstract IIconKey getMarkerIcon(MarkerType markerType); | ||
|
||
protected void onBoardClicked(int x, int y) { | ||
Entity clickedEntity = getEntityAtPosition(x, y); | ||
if (clickedEntity == null) { | ||
return; | ||
} | ||
|
||
MarkerComponent clickedMarker = clickedEntity.marker; | ||
// delete all markers | ||
for (Entity entity : entityManager.getEntities()) { | ||
entity.marker = null; | ||
} | ||
|
||
if (markerShouldConsumeClick(clickedMarker)) { | ||
if (clickedMarker.onMarkerClicked != null) { | ||
clickedMarker.onMarkerClicked.run(); | ||
} | ||
} else if (clickedEntity.piece != null) { | ||
// show the tiles this piece can move to | ||
boolean isActivePiece = clickedEntity.piece.identifier.ownerId() == activePlayerId; | ||
clickedEntity.findValidMoves().forEach(validMove -> createMoveMarker(clickedEntity, validMove, isActivePiece)); | ||
createSelectionMarker(clickedEntity); | ||
} else if (clickedEntity.tile != null) { | ||
// show which pieces can move to the selected tile | ||
entityManager.getEntities().stream() | ||
.filter(entity -> entity.piece != null | ||
&& entity.tile != null | ||
&& entity.findValidMoves().anyMatch(move -> move == clickedEntity)) | ||
.forEach(attacker -> { | ||
createMoveMarker(clickedEntity, attacker, false); | ||
}); | ||
createSelectionMarker(clickedEntity); | ||
} | ||
|
||
eventManager.getEvent(RenderEvent.class).fire(null); | ||
} | ||
|
||
protected boolean markerShouldConsumeClick(MarkerComponent marker) { | ||
if (marker == null) return false; | ||
if (marker.onMarkerClicked != null) return true; | ||
if (marker.markerType == MarkerType.Selection) return true; // click consumed to hide all markers | ||
return false; | ||
} | ||
|
||
protected void createSelectionMarker(Entity selectedTile) { | ||
MarkerComponent marker = new MarkerComponent(this::getMarkerIcon); | ||
marker.onMarkerClicked = null; | ||
marker.markerType = MarkerType.Selection; | ||
selectedTile.marker = marker; | ||
} | ||
|
||
protected void createMoveMarker(Entity fromTile, Entity toTile, boolean isActivePiece) { | ||
MarkerComponent marker = new MarkerComponent(this::getMarkerIcon); | ||
marker.onMarkerClicked = isActivePiece ? () -> movePiece(fromTile, toTile) : null; | ||
marker.markerType = isActivePiece ? MarkerType.YesAction : MarkerType.NoAction; | ||
toTile.marker = marker; | ||
} | ||
|
||
protected void movePiece(Entity fromTile, Entity toTile) { | ||
toTile.piece = fromTile.piece; | ||
fromTile.piece = null; | ||
|
||
// end turn | ||
activePlayerId = (activePlayerId + 1) % numPlayers; | ||
} | ||
|
||
@Override | ||
public EntityManager getEntityManager() { | ||
return entityManager; | ||
} | ||
|
||
@Override | ||
public EcsEventManager getEventManager() { | ||
return eventManager; | ||
} | ||
|
||
@Override | ||
public int getActivePlayerId() { | ||
return activePlayerId; | ||
} | ||
} |
Oops, something went wrong.