up funzionante perfetto

This commit is contained in:
Lorenzo Iovino 2017-07-10 21:34:37 +02:00
parent cb63402b9f
commit 6b915932ae
20 changed files with 2541 additions and 576 deletions

View file

@ -71,7 +71,7 @@ public class App extends JFrame {
HomePage home = new HomePage(this);
/* app.addWindowListener(new WindowAdapter()
/*app.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{

View file

@ -6,6 +6,7 @@ import com.texttwist.client.tasks.StartGame;
import com.texttwist.client.tasks.WaitForPlayers;
import com.texttwist.client.tasks.WaitForScore;
import javax.swing.*;
import java.nio.ByteBuffer;
/**
* GamePage Controller

View file

@ -42,23 +42,25 @@ public class FetchHighscore extends SwingWorker<Void,Void> {
while (socketChannel.read(buffer) != -1) {
buffer.clear();
String line = new String(buffer.array(), buffer.position(), buffer.remaining());
if (line.startsWith("MESSAGE")) {
Message msg = Message.toMessage(line);
//MODIFICARE QUI. IL BUG SI VERIFICA ANCHE CON 2 CLIENT, INVIANDO IL GIOCO A UN CLIENT CHE STA SULLA PAGNA DI HIGHSCORES
if (msg.message.equals("HIGHSCORES") && msg.data != null) {
for(int i = 0; i< msg.data.size()-1; i++){
String[] splitted = msg.data.get(i).split(":");
globalRanks.addElement(new Pair<>(splitted[0],new Integer(splitted[1])));
}
buffer.clear();
return null;
}
}
buffer.clear();
}
} catch (IOException e) {
e.printStackTrace();

View file

@ -28,6 +28,7 @@ public class InvitePlayers extends SwingWorker<Boolean,Void> {
}
@Override
public Boolean doInBackground() {
buffer = ByteBuffer.allocate(1024);
Message message = new Message("START_GAME", App.session.account.userName, App.session.token, userNames);
byte[] byteMessage = new String(message.toString()).getBytes();
@ -40,7 +41,6 @@ public class InvitePlayers extends SwingWorker<Boolean,Void> {
try {
while (socketChannel.read(buffer) != -1) {
buffer.clear();
String line = new String(buffer.array(), buffer.position(), buffer.remaining());
@ -51,9 +51,11 @@ public class InvitePlayers extends SwingWorker<Boolean,Void> {
new Callable() {
@Override
public Object call() throws Exception {
buffer.clear();
return null;
}
}, null);
buffer.clear();
return null;
}
@ -66,13 +68,16 @@ public class InvitePlayers extends SwingWorker<Boolean,Void> {
public Object call() throws Exception {
//In attesa dei giocatori
new GamePage(Page.window);
buffer.clear();
return null;
}
}, null);
buffer.clear();
return null;
}
}
buffer.clear();
}
} catch (IOException e) {
e.printStackTrace();

View file

@ -37,20 +37,29 @@ public class WaitForPlayers extends SwingWorker<DefaultListModel<String>,Default
@Override
public DefaultListModel<String> doInBackground() {
try {
buffer = ByteBuffer.allocate(1024);
String line1 = new String(buffer.array(), buffer.position(), buffer.remaining());
System.out.println("Questo è il buffer prima: " + line1);
TTDialog loading = new TTDialog("alert", "Waiting for users joins",null,null);
buffer.flip();
while (this.socketChannel.read(this.buffer) != -1) {
String line = new String(this.buffer.array(), this.buffer.position(), this.buffer.remaining());
while (this.socketChannel.read(buffer) != -1) {
String line = new String(buffer.array(), buffer.position(), buffer.remaining());
// String line = new String()
buffer.clear();
if (line.startsWith("MESSAGE")) {
buffer.clear();
System.out.println("Mi arriva questo dal server " + line);
Message msg = Message.toMessage(line);
System.out.println(msg.message);
if (msg.message.equals("JOIN_TIMEOUT")) {
loading.dispose();
joinTimeout = true;
System.out.println("JOIN TIMEOUT ENTERED");
new TTDialog("alert", "TIMEOUT!",
new Callable() {
@Override
@ -65,6 +74,8 @@ public class WaitForPlayers extends SwingWorker<DefaultListModel<String>,Default
if (msg.message.equals("MATCH_NOT_AVAILABLE")) {
loading.dispose();
System.out.println("MATCH NOT AVAILABLE ENTERED");
joinTimeout = true;
new TTDialog("alert", "THE GAME IS NOT MORE AVAILABLE!",
new Callable() {
@ -80,6 +91,7 @@ public class WaitForPlayers extends SwingWorker<DefaultListModel<String>,Default
if (msg.message.equals("GAME_STARTED")) {
loading.dispose();
System.out.println("GAME STARTED ENTERED");
DefaultListModel<String> data;
if(msg.data !=null ) {
@ -96,9 +108,15 @@ public class WaitForPlayers extends SwingWorker<DefaultListModel<String>,Default
//socketChannel.close();
return words;
} else {
System.out.println("USCITO CON");
System.out.println(line);
return new DefaultListModel<>();
}
}
buffer = ByteBuffer.allocate(1024);
}
}
} catch (IOException e) {