refactoring WaitForScore

This commit is contained in:
Lorenzo Iovino 2017-07-12 17:07:12 +02:00
parent eac6e43420
commit e2070cf597
17 changed files with 757 additions and 410 deletions

View file

@ -32,7 +32,7 @@ public class App extends JFrame {
public static Session session;
public static Game game;
public static JFrame app;
public static Logger logger;
public App() throws IOException {
setPreferredSize( new Dimension( 640, 480 ));
setSize(new Dimension(640,480));
@ -40,11 +40,9 @@ public class App extends JFrame {
setResizable( false );
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Definitions of registry for auth
long id = Thread.currentThread().getId();
Logger logger = new Logger(new File("./client_"+id+".log"), "Client"+id);
Logger.write("Client starting ...");
logger = new Logger(new File("./client_"+id+".log"), "Client"+id, true);
logger.write("Client starting ...");
//Init models
game = new Game();
@ -54,28 +52,10 @@ public class App extends JFrame {
app = this;
HomePage home = new HomePage(this);
/*app.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
try {
authService.logout(App.session.account.userName);
System.out.println("LOGOUT BECAUSE WINDOW CLOSED");
} catch (RemoteException e1) {
e1.printStackTrace();
} catch (NotBoundException e1) {
e1.printStackTrace();
} catch (MalformedURLException e1) {
e1.printStackTrace();
}
}
});*/
new HomePage(this);
}
public static Point getWindowsPosition(){
return new Point(app.getX(), app.getY());
}
}

View file

@ -11,12 +11,12 @@ public class Main {
//Load fonts
try {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
ge.registerFont(Font.createFont(Font.TRUETYPE_FONT, new File(
GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
graphicsEnvironment.registerFont(Font.createFont(Font.TRUETYPE_FONT, new File(
new File("./Client/resources/fonts/DK Trained Monkey.otf").getCanonicalPath())));
} catch (IOException|FontFormatException e) {
System.out.println("Font non caricato correttamente");
System.out.println("ERROR: Font not found!");
}
App entrypoint = new App();

View file

@ -33,4 +33,5 @@ public class GameController {
public SwingWorker startGame() {
return new StartGame(App.game.letters, game);
}
}

View file

@ -39,8 +39,8 @@ public class Game {
public MulticastSocket multicastSocket;
public SocketChannel clientSocket;
public INotificationServer notificationServer;
public Boolean gameIsStarted = false;
private ByteBuffer buffer = ByteBuffer.allocate(1024);
public Game(){
@ -111,7 +111,7 @@ public class Game {
if(!gameIsStarted) {
this.pendingList.clear();
try {
DefaultListModel<String> matchNames = new DefaultListModel<String>();
DefaultListModel<String> matchNames = new DefaultListModel<>();
matchNames.addElement(matchName);
Message message = new Message("JOIN_GAME", App.session.account.userName, App.session.token, matchNames);
@ -132,6 +132,16 @@ public class Game {
return null;
}
public Void start(){
App.game.gameIsStarted = true;
return null;
}
public Void stop(){
App.game.gameIsStarted = false;
return null;
}
public void setMulticastId(Integer multicastId){
this.multicastId = multicastId;
}

View file

@ -18,14 +18,14 @@ public class NotificationClient implements INotificationClient {
@Override
public Response sendInvite(String userName, DefaultListModel<String> users) throws RemoteException {
Logger.write("Invoked invitation with username =" + userName + "|" + users.toString() );
App.logger.write("Invoked invitation with username =" + userName + "|" + users.toString() );
if(App.session != null) {
if (users.contains(App.session.account.userName)) {
Logger.write(userName + " send a invitation!");
App.logger.write(userName + " send a invitation!");
App.game.newMatch(userName);
} else {
Logger.write("User " + userName + " is slogged");
App.logger.write("User " + userName + " is slogged");
}
}
return null;

View file

@ -21,7 +21,6 @@ public class SendWords extends SwingWorker<Void,Void> {
SwingWorker callback;
DefaultListModel<String> words = new DefaultListModel<>();
//TODO PASSARE LA CALLBACK ALLO SWING WORKER ED ESEGUIRLA AL DONE
public SendWords(DefaultListModel<String> words, SwingWorker callback){
this.callback = callback;
this.words = words;
@ -30,6 +29,7 @@ public class SendWords extends SwingWorker<Void,Void> {
@Override
public Void doInBackground() {
try {
System.out.println(words);
InetSocketAddress myAddress = new InetSocketAddress(Config.WordsReceiverServerURI, Config.WordsReceiverServerPort);
DatagramChannel datagramChannel = DatagramChannel.open();
datagramChannel.bind(null);

View file

@ -12,7 +12,6 @@ import java.util.concurrent.Callable;
*/
public class StartGame extends SwingWorker<Void,Void> {
/*Words inserted from user*/
private DefaultListModel<String> letters = new DefaultListModel<>();
private GamePage gamePage;
@ -20,12 +19,11 @@ public class StartGame extends SwingWorker<Void,Void> {
public StartGame(DefaultListModel<String> letters, GamePage game){
this.letters = letters;
this.gamePage = game;
}
@Override
public Void doInBackground(){
App.game.gameIsStarted =true;
App.game.start();
//Mostra pannello di conferma che le lettere sono tutte arrivate
new TTDialog("success", "Game is ready. Press OK to start!",
new Callable() {

View file

@ -2,9 +2,7 @@ package com.texttwist.client.tasks;
import com.texttwist.client.App;
import constants.Config;
import javafx.util.Pair;
import models.Message;
import javax.swing.*;
import java.io.IOException;
import java.net.DatagramPacket;
@ -12,68 +10,61 @@ import java.net.InetAddress;
import java.net.UnknownHostException;
/**
* Created by loke on 27/06/2017.
* Author: Lorenzo Iovino on 27/06/2017.
* Description: This job will waits for the score of the match sent by server, at end it will execute a callback
* function that show the highscore pages.
*/
public class WaitForScore extends SwingWorker<Void,Void> {
DefaultListModel<Pair<String,Integer>> ranks = new DefaultListModel<Pair<String,Integer>>();
SwingWorker callback;
//TODO PASSARE LA CALLBACK ALLO SWING WORKER ED ESEGUIRLA AL DONE
private SwingWorker callback;
public WaitForScore(SwingWorker callback){
this.callback = callback;
}
@Override
public Void doInBackground() {
InetAddress group = null;
try {
Message msg;
try {
//Wait for the final scores of the match
while(true) {
byte[] buf = new byte[1024];
DatagramPacket recv = new DatagramPacket(buf, buf.length);
App.game.multicastSocket.receive(recv);
DatagramPacket receivedDatagram = new DatagramPacket(buf, buf.length);
App.game.multicastSocket.receive(receivedDatagram);
String s = new String(recv.getData());
msg = Message.toMessage(s);
String s = new String(receivedDatagram.getData());
Message msg = Message.toMessage(s);
//When arrive a message with message=FINALSCORE popolate ranks
if(msg.message.equals("FINALSCORE")){
if(msg.data != null) {
App.game.ranks.clear();
App.game.ranks = utilities.Parse.score(msg.data);
}
break;
}
}
if(msg.data != null) {
for (int i = 0; i < msg.data.size() - 1; i++) {
String[] splitted = msg.data.get(i).split(":");
ranks.addElement(new Pair<String, Integer>(splitted[0], new Integer(splitted[1])));
}
}
App.game.ranks = ranks;
} catch (UnknownHostException e) {
e.printStackTrace();
App.logger.write("WAIT FOR SCORE: Host unknown");
} catch (IOException e) {
e.printStackTrace();
App.logger.write("WAIT FOR SCORE: Can't read from multicast Socket");
}
return null;
}
@Override
public void done(){
App.game.ranks = ranks;
try {
//Leave group and close multicast socket
App.game.multicastSocket.leaveGroup(InetAddress.getByName(Config.ScoreMulticastServerURI));
App.game.multicastSocket.close();
//Stop game
App.game.stop();
//Call callback
this.callback.execute();
} catch (IOException e) {
e.printStackTrace();
}
App.game.multicastSocket.close();
App.game.gameIsStarted =false;
try {
this.callback.execute();
} catch (Exception e) {
e.printStackTrace();
}
}
}

View file

@ -24,6 +24,7 @@ public class TTGameBox extends TTInputField{
setBounds(position.x, position.y, dimension.width, dimension.height);
setPreferredSize(dimension);
setForeground(Palette.fontColor);
list.clear();
addKeyListener(new KeyAdapter() {
@Override