Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
jberclaz committed Jul 20, 2024
1 parent a0f7276 commit cdf3bdb
Show file tree
Hide file tree
Showing 11 changed files with 37 additions and 411 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
<artifactId>maven-surefire-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<argLine>-XX:+EnableDynamicAgentLoading</argLine>
<argLine>@{argLine} -XX:+EnableDynamicAgentLoading</argLine>
</configuration>
</plugin>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,16 @@
import java.util.logging.Logger;
import java.util.stream.Collectors;

public class JassPlayer extends AbstractRemotePlayer {
private final static Logger LOGGER = Logger.getLogger(JassPlayer.class.getName());
public class InteractivePlayer extends AbstractRemotePlayer {
private final static Logger LOGGER = Logger.getLogger(InteractivePlayer.class.getName());
private final IJassUi ui;
private final Map<Integer, Integer> playersPositions = new HashMap<>();
private final Map<Integer, BasePlayer> players = new HashMap<>();
private Plie plie;
private Card playedCard;

//private IController controller = null;
//private Thread controllerThread = null;
//private IClientNetwork network = null;
private boolean hasStoeck;

public JassPlayer(IJassUi ui, int playerId, String name, int gameId) {
public InteractivePlayer(IJassUi ui, int playerId, String name, int gameId) {
super(playerId);
players.put(playerId, new ClientPlayer(playerId, name));
playersPositions.put(playerId, 0);
Expand Down Expand Up @@ -67,7 +63,6 @@ public void prepareTeamDrawing(boolean firstAttempt) {

@Override
public int drawCard() {
// var lock = controller.getLock();
var lock = new ReentrantLock();
lock.lock();
var condition = lock.newCondition();
Expand Down Expand Up @@ -291,51 +286,7 @@ public void setHandScore(int ourScore, int theirScore, Team match) {
ui.displayStatusMessage(String.format("Résultat de la manche: nous %d, eux %d", ourScore, theirScore));
}
}
/*
@Override
public int connect(String name, String host, int gameId) {
network = networkFactory.getClientNetwork();
var connectionInfo = network.connect(host, gameId, name);
if (connectionInfo.error != ConnectionError.CONNECTION_SUCCESSFUL) {
network = null;
return connectionInfo.error;
}
id = connectionInfo.playerId;
playersPositions.put(id, 0);
controller = new RemoteController(this, network);
controllerThread = new Thread(controller, "controller-thread");
controllerThread.start();
try {
ui.setPlayer(new ClientPlayer(id, name), 0);
} catch (Exception e) {
LOGGER.log(Level.WARNING, "Error while setting player info", e);
}
return connectionInfo.gameId;
}

@Override
public boolean disconnect() {
if (controller != null) {
controller.terminate();
}
network.disconnect();
if (controllerThread != null) {
try {
controllerThread.join(500);
} catch (InterruptedException e) {
LOGGER.log(Level.WARNING, "Error while waiting for controller thread to die", e);
}
}
controller = null;
controllerThread = null;
return true;
}
@Override
public boolean isConnected() {
return network != null && network.isConnected();
}
*/
private int getInitialRelativePosition(BasePlayer player) {
return (player.getId() - id + 4) % 4;
}
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/com/leflat/jass/client/JassClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
public class JassClient {
private static void localGame(IJassUi ui, ConnectionInfo dialogInfo) {
ui.setPlayer(new ClientPlayer(0, dialogInfo.name), 0);
var player = new JassPlayer(ui, 0, dialogInfo.name, -1);
var player = new InteractivePlayer(ui, 0, dialogInfo.name, -1);
var gameController = new GameController(0);
try {
gameController.addPlayer(player);
Expand Down Expand Up @@ -54,7 +54,7 @@ private static void networkGame(IJassUi ui, ConnectionInfo dialogInfo) {
}
} else {
ui.setPlayer(new ClientPlayer(connectionInfo.playerId, dialogInfo.name), 0);
var player = new JassPlayer(ui, connectionInfo.playerId, dialogInfo.name, connectionInfo.gameId);
var player = new InteractivePlayer(ui, connectionInfo.playerId, dialogInfo.name, connectionInfo.gameId);

var gameController = new RemoteController(player, network);
var controllerThread = new Thread(gameController, "controller-thread");
Expand Down Expand Up @@ -114,10 +114,8 @@ public static void main(String[] args) {
dialogInfo = ui.showConnectDialog();
if (dialogInfo.ok) {
if (dialogInfo.local) {
// local game
localGame(ui, dialogInfo);
} else {
// network game
networkGame(ui, dialogInfo);
}
}
Expand Down
67 changes: 2 additions & 65 deletions src/main/java/com/leflat/jass/client/ModernUi.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ public class ModernUi extends JFrame implements IJassUi, MouseListener {
private static final String APP_TITLE = "Jass by FLAT®";
private static final Logger LOGGER = Logger.getLogger(OriginalUi.class.getName());

//private final IConnectable myself;
private ModernGamePanel gamePanel;
private Lock lock;
private Condition condition;
Expand All @@ -29,7 +28,6 @@ public class ModernUi extends JFrame implements IJassUi, MouseListener {


public ModernUi() {
//this.myself = player;
initComponents();
loadLogos();
}
Expand Down Expand Up @@ -73,8 +71,6 @@ public void showUi(boolean enable) {
setSize(630, 530 + insets.top);
setMinimumSize(new Dimension(630, 530 + insets.top));
setResizable(true);

//connectDialog();
}

public ConnectionInfo showConnectDialog() {
Expand All @@ -92,58 +88,14 @@ public ConnectionInfo showConnectDialog() {
this.dispose();
return new ConnectionInfo(false);
}
myName = dc.name;
serverHost = dc.host;
if (dc.local) {
return new ConnectionInfo(dc.name);
}
return new ConnectionInfo(dc.name, dc.host, dc.gameId >= 0, dc.gameId, dc.aiPlayers);
}

/*
private void connectDialog() {
int returnCode;
do {
DialogConnection dc;
if (myName != null && serverHost != null) {
dc = new DialogConnection(this, myName, serverHost);
} else {
dc = new DialogConnection(this);
}
dc.pack();
dc.setLocationRelativeTo(this);
dc.setVisible(true);
if (!dc.ok) {
this.setVisible(false);
this.dispose();
return;
}
myName = dc.name;
serverHost = dc.host;
returnCode = myself.connect(dc.name, dc.host, dc.gameId);
if (returnCode >= 0) {
setGameId(returnCode);
if (dc.gameId < 0 && dc.aiPlayers > 0) {
for (int i=0; i<dc.aiPlayers; ++i) {
var aiName = String.format("iBerte %d", i+1);
var aiPlayer = new RemoteArtificialPlayer(new ClientNetworkFactory());
aiPlayer.connect(aiName, dc.host, returnCode);
}
}
} else {
switch (returnCode) {
case ConnectionError.SERVER_UNREACHABLE:
JOptionPane.showMessageDialog(this, "La connection a échoué.", "Erreur", JOptionPane.ERROR_MESSAGE);
break;
case ConnectionError.GAME_FULL:
JOptionPane.showMessageDialog(this, "Ce jeu est déja complet.", "Erreur", JOptionPane.ERROR_MESSAGE);
break;
case ConnectionError.UNKNOWN_GAME:
JOptionPane.showMessageDialog(this, "Le jeu " + dc.gameId + " n'existe pas.", "Erreur", JOptionPane.ERROR_MESSAGE);
break;
}
}
} while (returnCode < 0);
}
*/
void exitUi() {
/*
if (myself.isConnected()) {
Expand Down Expand Up @@ -407,19 +359,4 @@ public void setGameId(int gameId) {
public void showMessage(String title, String message, int type) {
JOptionPane.showMessageDialog(this, message, title, type);
}

/*
private void disconnect() {
for (int i = 0; i < 4; i++) {
gamePanel.clearPlayer(intToPlayerPosition(i));
}
gamePanel.clearCards();
gamePanel.hideAtout();
gamePanel.setMode(ModernGamePanel.GameMode.IDLE);
setScore(0, 0);
setGameId(-1);
connectDialog();
}
*/
}
20 changes: 8 additions & 12 deletions src/main/java/com/leflat/jass/server/GameController.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,10 @@ public void run() {

playAnotherGame = getPlayerById(0).getNewGame();

for (Team team : teams) {
team.resetScore();
}
Arrays.stream(teams).forEach(Team::resetScore);
} while (playAnotherGame);

LOGGER.info("No longer playing");

} catch (PlayerLeftExpection e) {
LOGGER.log(Level.WARNING, "Player " + e.playerId + " left the game", e);
disconnectedPlayer = getPlayerById(e.getPlayerId());
Expand All @@ -102,22 +99,22 @@ public void run() {

void playOneGame() throws PlayerLeftExpection, BrokenRuleException {
int firstToPlay = drawCards();
Plie plie;
boolean gameOver;
do {
plie = playOneHand(firstToPlay);
gameOver = playOneHand(firstToPlay);

setTeamsScoreAsync();

/* waits a few seconds so that the players can see the last
* cards and the score */
waitSec(2);

if (plie != null) {
if (!gameOver) {
firstToPlay = (firstToPlay + 1) % 4;
drawCards();
}
// répète jusqu'à ce qu'on gagne
} while (plie != null);
} while (!gameOver);

// Sends the winner to all player
var winners = teams[0].hasWon() ? teams[0] : teams[1];
Expand All @@ -127,7 +124,7 @@ void playOneGame() throws PlayerLeftExpection, BrokenRuleException {
waitSec(4);
}

Plie playOneHand(int firstToPlay) throws PlayerLeftExpection, BrokenRuleException {
boolean playOneHand(int firstToPlay) throws PlayerLeftExpection, BrokenRuleException {
int nextPlayer = firstToPlay;
Plie plie = null;
int[] handScores = new int[2];
Expand All @@ -138,6 +135,7 @@ Plie playOneHand(int firstToPlay) throws PlayerLeftExpection, BrokenRuleExceptio
for (int i = 0; i < 9; i++) {
plie = playPlie(nextPlayer);
if (plie == null) {
// game over
break;
}
nextPlayer = getPlayerPosition(plie.getOwner());
Expand Down Expand Up @@ -165,7 +163,7 @@ Plie playOneHand(int firstToPlay) throws PlayerLeftExpection, BrokenRuleExceptio
waitSec(2);
}

return plie;
return plie == null;
}

Plie playPlie(int startingPlayer) throws PlayerLeftExpection, BrokenRuleException {
Expand Down Expand Up @@ -497,6 +495,4 @@ void setPlayedCardAsync(BasePlayer player, Card card) throws PlayerLeftExpection
throw new RuntimeException(ex.getCause());
}
}


}
40 changes: 0 additions & 40 deletions src/main/java/com/leflat/jass/test/TestArtificialPlayer.java

This file was deleted.

35 changes: 0 additions & 35 deletions src/main/java/com/leflat/jass/test/TestComputerPlayer.java

This file was deleted.

Loading

0 comments on commit cdf3bdb

Please sign in to comment.