mirror of
https://github.com/2OOP/pism.git
synced 2026-02-04 19:04:49 +00:00
Merge remote-tracking branch 'origin/UI' into 185-networkingeventlistener
This commit is contained in:
@@ -79,6 +79,7 @@ public final class App extends Application {
|
|||||||
public static void quit() {
|
public static void quit() {
|
||||||
ViewStack.cleanup();
|
ViewStack.cleanup();
|
||||||
stage.close();
|
stage.close();
|
||||||
|
System.exit(0); // TODO: This is like dropping a nuke
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void reload() {
|
public static void reload() {
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package org.toop.app;
|
package org.toop.app;
|
||||||
|
|
||||||
|
import com.google.common.util.concurrent.AbstractScheduledService;
|
||||||
import org.toop.app.game.ReversiGame;
|
import org.toop.app.game.ReversiGame;
|
||||||
import org.toop.app.game.TicTacToeGame;
|
import org.toop.app.game.TicTacToeGame;
|
||||||
import org.toop.app.view.ViewStack;
|
import org.toop.app.view.ViewStack;
|
||||||
@@ -9,19 +10,26 @@ import org.toop.app.view.views.OnlineView;
|
|||||||
import org.toop.app.view.views.SendChallengeView;
|
import org.toop.app.view.views.SendChallengeView;
|
||||||
import org.toop.app.view.views.ServerView;
|
import org.toop.app.view.views.ServerView;
|
||||||
import org.toop.framework.eventbus.EventFlow;
|
import org.toop.framework.eventbus.EventFlow;
|
||||||
|
import org.toop.framework.networking.clients.TournamentNetworkingClient;
|
||||||
import org.toop.framework.networking.events.NetworkEvents;
|
import org.toop.framework.networking.events.NetworkEvents;
|
||||||
|
import org.toop.framework.networking.interfaces.NetworkingClient;
|
||||||
|
import org.toop.framework.networking.types.NetworkingConnector;
|
||||||
import org.toop.local.AppContext;
|
import org.toop.local.AppContext;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.CopyOnWriteArrayList;
|
import java.util.concurrent.CopyOnWriteArrayList;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
public final class Server {
|
public final class Server {
|
||||||
private String user = "";
|
private String user = "";
|
||||||
|
|
||||||
private long clientId = -1;
|
private long clientId = -1;
|
||||||
private List<String> onlinePlayers = new CopyOnWriteArrayList<String>();
|
private List<String> onlinePlayers = new CopyOnWriteArrayList<String>();
|
||||||
|
private List<String> gameList = new CopyOnWriteArrayList<>();
|
||||||
|
|
||||||
private ServerView view;
|
private ServerView view;
|
||||||
|
|
||||||
@@ -58,8 +66,12 @@ public final class Server {
|
|||||||
}
|
}
|
||||||
|
|
||||||
new EventFlow()
|
new EventFlow()
|
||||||
.addPostEvent(NetworkEvents.StartClient.class, ip, parsedPort)
|
.addPostEvent(NetworkEvents.StartClient.class,
|
||||||
|
new TournamentNetworkingClient(),
|
||||||
|
new NetworkingConnector(ip, parsedPort, 10, 1, TimeUnit.SECONDS)
|
||||||
|
)
|
||||||
.onResponse(NetworkEvents.StartClientResponse.class, e -> {
|
.onResponse(NetworkEvents.StartClientResponse.class, e -> {
|
||||||
|
// TODO add if unsuccessful
|
||||||
this.user = user;
|
this.user = user;
|
||||||
clientId = e.clientId();
|
clientId = e.clientId();
|
||||||
|
|
||||||
@@ -68,30 +80,23 @@ public final class Server {
|
|||||||
view = new ServerView(user, this::sendChallenge, this::disconnect);
|
view = new ServerView(user, this::sendChallenge, this::disconnect);
|
||||||
ViewStack.push(view);
|
ViewStack.push(view);
|
||||||
|
|
||||||
startPopulateThread();
|
startPopulateScheduler();
|
||||||
|
|
||||||
|
populateGameList();
|
||||||
|
|
||||||
}).postEvent();
|
}).postEvent();
|
||||||
|
|
||||||
new EventFlow().listen(this::handleReceivedChallenge);
|
new EventFlow().listen(this::handleReceivedChallenge);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void populatePlayerList() {
|
private void populatePlayerList(ScheduledExecutorService scheduler, Runnable populatingTask) {
|
||||||
new EventFlow().listen(NetworkEvents.PlayerlistResponse.class, e -> {
|
|
||||||
if (e.clientId() == clientId) {
|
|
||||||
onlinePlayers = new ArrayList<String>(List.of(e.playerlist()));
|
|
||||||
onlinePlayers.removeIf(name -> name.equalsIgnoreCase(user));
|
|
||||||
|
|
||||||
view.update(onlinePlayers);
|
final long DELAY = 5;
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
final EventFlow sendGetPlayerList = new EventFlow().addPostEvent(new NetworkEvents.SendGetPlayerlist(clientId));
|
if (!isPolling) scheduler.shutdown();
|
||||||
|
else {
|
||||||
while (isPolling) {
|
populatingTask.run();
|
||||||
sendGetPlayerList.postEvent();
|
scheduler.schedule(() -> populatePlayerList(scheduler, populatingTask), DELAY, TimeUnit.SECONDS);
|
||||||
|
|
||||||
try {
|
|
||||||
Thread.sleep(5000);
|
|
||||||
} catch (InterruptedException _) {}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -173,6 +178,7 @@ public final class Server {
|
|||||||
|
|
||||||
private void disconnect() {
|
private void disconnect() {
|
||||||
new EventFlow().addPostEvent(new NetworkEvents.CloseClient(clientId)).postEvent();
|
new EventFlow().addPostEvent(new NetworkEvents.CloseClient(clientId)).postEvent();
|
||||||
|
isPolling = false;
|
||||||
ViewStack.push(new OnlineView());
|
ViewStack.push(new OnlineView());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -184,27 +190,39 @@ public final class Server {
|
|||||||
forfeitGame();
|
forfeitGame();
|
||||||
|
|
||||||
ViewStack.push(view);
|
ViewStack.push(view);
|
||||||
startPopulateThread();
|
startPopulateScheduler();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void startPopulateThread() {
|
private void startPopulateScheduler() {
|
||||||
isPolling = true;
|
isPolling = true;
|
||||||
|
|
||||||
final Thread populateThread = new Thread(this::populatePlayerList);
|
EventFlow getPlayerlistFlow = new EventFlow()
|
||||||
populateThread.setDaemon(false);
|
.addPostEvent(new NetworkEvents.SendGetPlayerlist(clientId))
|
||||||
populateThread.start();
|
.listen(NetworkEvents.PlayerlistResponse.class, e -> {
|
||||||
|
if (e.clientId() == clientId) {
|
||||||
|
onlinePlayers = new ArrayList<>(List.of(e.playerlist()));
|
||||||
|
onlinePlayers.removeIf(name -> name.equalsIgnoreCase(user));
|
||||||
|
|
||||||
|
view.update(onlinePlayers);
|
||||||
|
}
|
||||||
|
}, false);
|
||||||
|
|
||||||
|
final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
|
||||||
|
scheduler.schedule(() -> populatePlayerList(scheduler, getPlayerlistFlow::postEvent), 0, TimeUnit.MILLISECONDS);
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<String> getGamesList() {
|
private void gamesListFromServerHandler(NetworkEvents.GamelistResponse event) {
|
||||||
final List<String> list = new ArrayList<String>();
|
gameList.addAll(List.of(event.gamelist()));
|
||||||
list.add("tic-tac-toe"); // Todo: get games list from server and check if the game is supported
|
}
|
||||||
list.add("reversi");
|
|
||||||
|
|
||||||
|
public void populateGameList() {
|
||||||
new EventFlow().addPostEvent(new NetworkEvents.SendGetGamelist(clientId))
|
new EventFlow().addPostEvent(new NetworkEvents.SendGetGamelist(clientId))
|
||||||
.listen(NetworkEvents.GamelistResponse.class, e -> {
|
.listen(NetworkEvents.GamelistResponse.class,
|
||||||
System.out.println(Arrays.toString(e.gamelist()));
|
this::gamesListFromServerHandler, true
|
||||||
}).postEvent();
|
).postEvent();
|
||||||
|
|
||||||
return list;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<String> getGameList() {
|
||||||
|
return gameList;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -46,7 +46,7 @@ public final class SendChallengeView extends View {
|
|||||||
gameText.setText(AppContext.getString("to-a-game-of"));
|
gameText.setText(AppContext.getString("to-a-game-of"));
|
||||||
|
|
||||||
final ComboBox<String> gamesCombobox = combobox();
|
final ComboBox<String> gamesCombobox = combobox();
|
||||||
gamesCombobox.getItems().addAll(server.getGamesList());
|
gamesCombobox.getItems().addAll(server.getGameList());
|
||||||
gamesCombobox.setValue(gamesCombobox.getItems().getFirst());
|
gamesCombobox.setValue(gamesCombobox.getItems().getFirst());
|
||||||
|
|
||||||
final Button sendButton = button();
|
final Button sendButton = button();
|
||||||
|
|||||||
Reference in New Issue
Block a user