Synchronization for joins the game

This commit is contained in:
Lorenzo Iovino 2017-06-23 19:34:43 +02:00
parent 124b672aeb
commit 9cc0297a5b
16 changed files with 1462 additions and 443 deletions

View file

@ -1,5 +1,6 @@
package com.texttwist.client.pages;
import com.texttwist.client.App;
import constants.Palette;
import com.texttwist.client.ui.*;
@ -24,7 +25,11 @@ public class Game extends Page {
createUIComponents();
letters = fetchLetters();
letterSpawningPoint = setLetterSpawningPoint();
showLetters();
if(letters.size() > 0) {
showLetters();
} else {
joiningPhase();
}
window.setVisible(true);
}
@ -51,12 +56,11 @@ public class Game extends Page {
l.addElement(new Point(350,145));
l.addElement(new Point(450,140));
l.addElement(new Point(550,130));
return l;
return l;
}
private DefaultListModel<String> fetchLetters(){
DefaultListModel l = new DefaultListModel<String>();
DefaultListModel l = new DefaultListModel<String>();/*
l.addElement("C");
l.addElement("A");
l.addElement("E");
@ -64,7 +68,7 @@ public class Game extends Page {
l.addElement("C");
l.addElement("I");
l.addElement("L");
l.addElement("S");
l.addElement("S");*/
return l;
}
@ -99,6 +103,13 @@ public class Game extends Page {
window.revalidate();
}
public void joiningPhase(){
//Visualizza popup
new TTDialog("success", "Waiting for users joins",null,null);
window.repaint();
window.revalidate();
}
@Override
public void createUIComponents() throws IOException {
addLogo(root);
@ -109,7 +120,7 @@ public class Game extends Page {
-1,
root);
TTGameBox searchUserBar = new TTGameBox(
TTGameBox gameBox = new TTGameBox(
new Point(150, 90),
new Dimension(250, 40),
"Word!",

View file

@ -21,7 +21,17 @@ public class MatchService {
public DefaultListModel<String> pendingList = new DefaultListModel<String>();
ByteBuffer buffer = ByteBuffer.allocate(1024);
SocketChannel clientSocket = null;
public MatchService(){
InetSocketAddress socketAddress = new InetSocketAddress(Config.GameServerURI, Config.GameServerPort);
try {
clientSocket = SocketChannel.open(socketAddress);
clientSocket.configureBlocking(false);
} catch (IOException e) {
e.printStackTrace();
}
}
@ -50,12 +60,20 @@ public class MatchService {
}
public void joinMatch(String userName) {
public void joinMatch(String matchName) {
//Svuota la lista dei match pendenti e joina il match selezionato
this.pendingList.clear();
try {
//Invia tcp req a server per dirgli che sto joinando
DefaultListModel<String> matchNames = new DefaultListModel<String>();
matchNames.addElement(matchName);
Message message = new Message("JOIN_GAME", App.sessionService.account.userName, App.sessionService.account.token, matchNames);
byte[] byteMessage = new String(message.toString()).getBytes();
buffer = ByteBuffer.wrap(byteMessage);
clientSocket.write(buffer);
new Game(Page.window);
} catch (IOException e) {
e.printStackTrace();
}
@ -64,10 +82,6 @@ public class MatchService {
public Object play(DefaultListModel<String> userNames) throws IOException {
InetSocketAddress socketAddress = new InetSocketAddress(Config.GameServerURI, Config.GameServerPort);
SocketChannel clientSocket = SocketChannel.open(socketAddress);
clientSocket.configureBlocking(false);
Message message = new Message("START_GAME", App.sessionService.account.userName, App.sessionService.account.token, userNames);
byte[] byteMessage = new String(message.toString()).getBytes();
@ -79,12 +93,10 @@ public class MatchService {
buffer.clear();
String line = new String(buffer.array(), buffer.position(), buffer.remaining());
System.out.println(line);
if (line.startsWith("MESSAGE")) {
Message msg = Message.toMessage(line);
Message msg = Message.toMessage(line);
if (msg.message.equals("USER_NOT_ONLINE")) {
clientSocket.close();
new TTDialog("alert", "Users not online!",
new Callable() {
@Override
@ -96,7 +108,7 @@ public class MatchService {
}
if (msg.message.equals("INVITES_ALL_SENDED")) {
clientSocket.close();
// clientSocket.close();
new TTDialog("success", "Invite all sended!",
new Callable() {
@Override
@ -108,6 +120,19 @@ public class MatchService {
return null;
}
if (msg.message.equals("TIMEOUT")) {
clientSocket.close();
new TTDialog("alert", "TIMEOUT!",
new Callable() {
@Override
public Object call() throws Exception {
return new Menu(Page.window);
}
}, null);
return null;
}
}
}

View file

@ -23,29 +23,6 @@ public class MatchSetup extends Page{
window.setVisible(true);
}
//TODO Spostare i metodi di fetches nella classe MatchSetupService per separare MVC
private DefaultListModel fetchUsers(){
DefaultListModel<String> usersList = new DefaultListModel<String>();
usersList.addElement("Pippo");
usersList.addElement("Paperino");
usersList.addElement("Gaia");
usersList.addElement("Luigi");
usersList.addElement("Marco");
usersList.addElement("Minnie");
usersList.addElement("Franco");
usersList.addElement("Qua");
usersList.addElement("Luca");
usersList.addElement("Qui");
usersList.addElement("Jorge");
usersList.addElement("David");
usersList.addElement("Quo");
usersList.addElement("Raphael");
usersList.addElement("Miguel");
usersList.addElement("Carmen");
usersList.addElement("Beatriz");
return usersList;
}
@Override
public void createUIComponents() throws Exception {
addLogo(root);