Aggiunta classifica match + globale. Versione funzionante terminata
This commit is contained in:
parent
f7c9d0ff99
commit
429eb1cb02
40 changed files with 2152 additions and 1108 deletions
|
|
@ -1,9 +1,9 @@
|
|||
package com.texttwist.client;
|
||||
|
||||
import com.texttwist.client.pages.AuthService;
|
||||
import com.texttwist.client.services.AuthService;
|
||||
import com.texttwist.client.pages.Home;
|
||||
import com.texttwist.client.pages.MatchService;
|
||||
import com.texttwist.client.pages.SessionService;
|
||||
import com.texttwist.client.services.MatchModel;
|
||||
import com.texttwist.client.services.SessionService;
|
||||
import com.texttwist.client.services.NotificationClient;
|
||||
import constants.Config;
|
||||
import interfaces.INotificationClient;
|
||||
|
|
@ -11,7 +11,6 @@ import interfaces.INotificationServer;
|
|||
import utilities.Logger;
|
||||
|
||||
import javax.swing.*;
|
||||
import javax.swing.text.Position;
|
||||
import java.awt.*;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
|
@ -28,7 +27,7 @@ public class App extends JFrame {
|
|||
|
||||
public static AuthService authService;
|
||||
public static SessionService sessionService;
|
||||
public static MatchService matchService;
|
||||
public static MatchModel match;
|
||||
public static JFrame app;
|
||||
|
||||
public App() throws IOException {
|
||||
|
|
@ -62,7 +61,7 @@ public class App extends JFrame {
|
|||
//Init services
|
||||
authService = new AuthService();
|
||||
sessionService = new SessionService();
|
||||
matchService = new MatchService();
|
||||
match = new MatchModel();
|
||||
app = this;
|
||||
Home home = new Home(this);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,48 @@
|
|||
package com.texttwist.client.controllers;
|
||||
import com.texttwist.client.App;
|
||||
import com.texttwist.client.pages.Game;
|
||||
import com.texttwist.client.tasks.SendWords;
|
||||
import com.texttwist.client.tasks.StartGame;
|
||||
import com.texttwist.client.tasks.WaitForPlayers;
|
||||
import com.texttwist.client.tasks.WaitForScore;
|
||||
import javax.swing.*;
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
/**
|
||||
* Game Controller
|
||||
*/
|
||||
public class GameController {
|
||||
|
||||
private Game game;
|
||||
private Timer timer;
|
||||
|
||||
public GameController(Game game){
|
||||
this.game = game;
|
||||
this.timer = game.timer;
|
||||
|
||||
}
|
||||
|
||||
public DefaultListModel<String> getLetters(){
|
||||
return App.match.letters;
|
||||
}
|
||||
|
||||
public DefaultListModel<String> getWords() {
|
||||
return App.match.words;
|
||||
}
|
||||
|
||||
public SwingWorker waitForPlayers(SwingWorker callback) {
|
||||
return new WaitForPlayers(callback);
|
||||
}
|
||||
|
||||
private SwingWorker waitForScore(SwingWorker callback){
|
||||
return new WaitForScore(callback);
|
||||
}
|
||||
|
||||
public SwingWorker sendWords(SwingWorker callback){
|
||||
return new SendWords(App.match.words, waitForScore(callback));
|
||||
}
|
||||
|
||||
public SwingWorker startGame() {
|
||||
return new StartGame(App.match.letters, game);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
package com.texttwist.client.controllers;
|
||||
import com.texttwist.client.services.HighscoresService;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
/**
|
||||
* Created by loke on 28/06/2017.
|
||||
*/
|
||||
public class HighscoresController {
|
||||
|
||||
HighscoresService highscoresService = new HighscoresService();
|
||||
|
||||
public HighscoresController(){
|
||||
|
||||
}
|
||||
|
||||
|
||||
public void fetchHighscores(JFrame window){
|
||||
highscoresService.fetchHighscores(new Callable<String>() {
|
||||
@Override
|
||||
public String call() throws Exception {
|
||||
window.revalidate();
|
||||
window.repaint();
|
||||
System.out.println("ASDDD");
|
||||
return "";
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,24 +1,24 @@
|
|||
package com.texttwist.client.pages;
|
||||
import com.texttwist.client.App;
|
||||
import models.Response;
|
||||
|
||||
import java.net.MalformedURLException;
|
||||
import java.rmi.NotBoundException;
|
||||
import java.rmi.RemoteException;
|
||||
|
||||
/**
|
||||
* Created by loke on 15/06/2017.
|
||||
*/
|
||||
public class HomeController {
|
||||
|
||||
public HomeController(){
|
||||
}
|
||||
|
||||
public Response login(String userName, String password) throws RemoteException, NotBoundException, MalformedURLException {
|
||||
Response res = App.authService.login(userName,password);
|
||||
if (res.code == 200){
|
||||
App.sessionService.create(userName, res.data.get("token").toString());
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
package com.texttwist.client.controllers;
|
||||
import com.texttwist.client.App;
|
||||
import models.Response;
|
||||
|
||||
import java.net.MalformedURLException;
|
||||
import java.rmi.NotBoundException;
|
||||
import java.rmi.RemoteException;
|
||||
|
||||
/**
|
||||
* Created by loke on 15/06/2017.
|
||||
*/
|
||||
public class HomeController {
|
||||
|
||||
public HomeController(){
|
||||
}
|
||||
|
||||
public Response login(String userName, String password) throws RemoteException, NotBoundException, MalformedURLException {
|
||||
Response res = App.authService.login(userName,password);
|
||||
if (res.code == 200){
|
||||
App.sessionService.create(userName, res.data.get("token").toString());
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,27 +1,27 @@
|
|||
package com.texttwist.client.pages;
|
||||
|
||||
import com.texttwist.client.App;
|
||||
import models.Response;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.io.IOException;
|
||||
import java.net.MalformedURLException;
|
||||
import java.rmi.NotBoundException;
|
||||
import java.rmi.RemoteException;
|
||||
|
||||
/**
|
||||
* Created by loke on 18/06/2017.
|
||||
*/
|
||||
public class MatchSetupController {
|
||||
|
||||
public MatchSetupController(){}
|
||||
public Object play(DefaultListModel<String> userNames) {
|
||||
try {
|
||||
return App.matchService.play(userNames);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
package com.texttwist.client.controllers;
|
||||
|
||||
import com.texttwist.client.App;
|
||||
import models.Response;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.io.IOException;
|
||||
import java.net.MalformedURLException;
|
||||
import java.rmi.NotBoundException;
|
||||
import java.rmi.RemoteException;
|
||||
|
||||
/**
|
||||
* Created by loke on 18/06/2017.
|
||||
*/
|
||||
public class MatchSetupController {
|
||||
|
||||
public MatchSetupController(){}
|
||||
public Object play(DefaultListModel<String> userNames) {
|
||||
try {
|
||||
return App.match.play(userNames);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,25 +1,25 @@
|
|||
package com.texttwist.client.pages;
|
||||
|
||||
import com.texttwist.client.App;
|
||||
import models.Response;
|
||||
|
||||
import java.net.MalformedURLException;
|
||||
import java.rmi.NotBoundException;
|
||||
import java.rmi.RemoteException;
|
||||
|
||||
/**
|
||||
* Created by loke on 17/06/2017.
|
||||
*/
|
||||
public class MenuController {
|
||||
|
||||
public MenuController(){
|
||||
}
|
||||
|
||||
public Response logout(String userName) throws RemoteException, NotBoundException, MalformedURLException {
|
||||
Response res = App.authService.logout(userName);
|
||||
if (res.code == 200){
|
||||
App.sessionService.remove();
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
package com.texttwist.client.controllers;
|
||||
|
||||
import com.texttwist.client.App;
|
||||
import models.Response;
|
||||
|
||||
import java.net.MalformedURLException;
|
||||
import java.rmi.NotBoundException;
|
||||
import java.rmi.RemoteException;
|
||||
|
||||
/**
|
||||
* Created by loke on 17/06/2017.
|
||||
*/
|
||||
public class MenuController {
|
||||
|
||||
public MenuController(){
|
||||
}
|
||||
|
||||
public Response logout(String userName) throws RemoteException, NotBoundException, MalformedURLException {
|
||||
Response res = App.authService.logout(userName);
|
||||
if (res.code == 200){
|
||||
App.sessionService.remove();
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,21 +1,21 @@
|
|||
package com.texttwist.client.pages;
|
||||
|
||||
import com.texttwist.client.App;
|
||||
import models.Response;
|
||||
|
||||
import java.net.MalformedURLException;
|
||||
import java.rmi.NotBoundException;
|
||||
import java.rmi.RemoteException;
|
||||
|
||||
/**
|
||||
* Created by loke on 17/06/2017.
|
||||
*/
|
||||
public class RegisterController {
|
||||
|
||||
public RegisterController(){
|
||||
}
|
||||
|
||||
public Response register(String userName, String password) throws RemoteException, NotBoundException, MalformedURLException {
|
||||
return App.authService.register(userName,password);
|
||||
}
|
||||
}
|
||||
package com.texttwist.client.controllers;
|
||||
|
||||
import com.texttwist.client.App;
|
||||
import models.Response;
|
||||
|
||||
import java.net.MalformedURLException;
|
||||
import java.rmi.NotBoundException;
|
||||
import java.rmi.RemoteException;
|
||||
|
||||
/**
|
||||
* Created by loke on 17/06/2017.
|
||||
*/
|
||||
public class RegisterController {
|
||||
|
||||
public RegisterController(){
|
||||
}
|
||||
|
||||
public Response register(String userName, String password) throws RemoteException, NotBoundException, MalformedURLException {
|
||||
return App.authService.register(userName,password);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,62 +1,66 @@
|
|||
package com.texttwist.client.pages;
|
||||
|
||||
import com.texttwist.client.tasks.StartGame;
|
||||
import com.texttwist.client.controllers.GameController;
|
||||
import constants.Config;
|
||||
import constants.Palette;
|
||||
import com.texttwist.client.ui.*;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.*;
|
||||
|
||||
/**
|
||||
* Created by loke on 14/06/2017.
|
||||
* Game Page
|
||||
*/
|
||||
public class Game extends Page {
|
||||
|
||||
private TTContainer gameContainer;
|
||||
public GameController gameController;
|
||||
public TTGameBox gameBox;
|
||||
public DefaultListModel<String> words = new DefaultListModel<String>();
|
||||
public DefaultListModel<Point> letterSpawningPoint = new DefaultListModel<Point>();
|
||||
private TTGameBox gameBox;
|
||||
private GameController gameController;
|
||||
public Timer timer;
|
||||
|
||||
public Timer timer = null;
|
||||
/*Spawnig points, fixed and not modifiable*/
|
||||
private final DefaultListModel<Point> letterSpawningPoints = setLetterSpawningPoint();
|
||||
|
||||
public Point getRandomPosition(){
|
||||
if(letterSpawningPoint.size()>1) {
|
||||
int index = ThreadLocalRandom.current().nextInt(0, letterSpawningPoint.size() - 1);
|
||||
Point placeholder = letterSpawningPoint.get(index);
|
||||
letterSpawningPoint.remove(index);
|
||||
/*Available spawning points*/
|
||||
private DefaultListModel<Point> availableLetterSpawningPoint = new DefaultListModel<>();
|
||||
|
||||
public Game(JFrame window) throws IOException {
|
||||
|
||||
super(window);
|
||||
gameController = new GameController(this);
|
||||
availableLetterSpawningPoint.clear();
|
||||
availableLetterSpawningPoint = letterSpawningPoints;
|
||||
gameController.waitForPlayers(gameController.startGame()).execute();
|
||||
createUIComponents();
|
||||
window.setVisible(true);
|
||||
}
|
||||
|
||||
private Point occupyRandomPosition(){
|
||||
|
||||
if(availableLetterSpawningPoint.size() > 1) {
|
||||
int index = ThreadLocalRandom.current().nextInt(0, letterSpawningPoints.size() - 1);
|
||||
Point placeholder = letterSpawningPoints.get(index);
|
||||
letterSpawningPoints.remove(index);
|
||||
return placeholder;
|
||||
}
|
||||
|
||||
return new Point(0,0);
|
||||
}
|
||||
|
||||
|
||||
public void showLetters(){
|
||||
for(int i = 0; i< this.gameController.letters.size(); i++){
|
||||
TTLetter letter = new TTLetter(
|
||||
getRandomPosition(),
|
||||
this.gameController.letters.get(i),
|
||||
gameContainer);
|
||||
}
|
||||
|
||||
window.repaint();
|
||||
window.revalidate();
|
||||
}
|
||||
|
||||
public Game(JFrame window) throws IOException {
|
||||
super(window);
|
||||
createUIComponents();
|
||||
gameController = new GameController();
|
||||
letterSpawningPoint = setLetterSpawningPoint();
|
||||
this.gameController.waitForPlayers();
|
||||
this.gameController.startGame(this);
|
||||
window.setVisible(true);
|
||||
private SwingWorker timeIsOver() {
|
||||
return gameController.sendWords(new SwingWorker() {
|
||||
@Override
|
||||
protected Object doInBackground() throws Exception {
|
||||
new Highscores(window,true);
|
||||
return null;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private DefaultListModel<Point> setLetterSpawningPoint(){
|
||||
DefaultListModel l = new DefaultListModel<Point>();
|
||||
|
||||
DefaultListModel<Point> l = new DefaultListModel<>();
|
||||
|
||||
//FirstRow
|
||||
l.addElement(new Point(100,30));
|
||||
|
|
@ -78,49 +82,55 @@ 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;
|
||||
}
|
||||
|
||||
public void showLetters(){
|
||||
|
||||
/* Place letters in a available random spawning point */
|
||||
for(int i = 0; i < gameController.getLetters().size(); i++){
|
||||
new TTLetter(
|
||||
occupyRandomPosition(),
|
||||
gameController.getLetters().get(i),
|
||||
gameContainer
|
||||
);
|
||||
}
|
||||
|
||||
window.repaint();
|
||||
window.revalidate();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void createUIComponents() throws IOException {
|
||||
|
||||
addLogo(root);
|
||||
|
||||
gameContainer = new TTContainer(
|
||||
null,
|
||||
new Dimension(1150,220),
|
||||
Palette.root_backgroundColor,
|
||||
-1,
|
||||
root);
|
||||
null,
|
||||
new Dimension(1150,220),
|
||||
Palette.root_backgroundColor,
|
||||
-1,
|
||||
root
|
||||
);
|
||||
|
||||
gameBox = new TTGameBox(
|
||||
new Point(150, 90),
|
||||
new Dimension(250, 40),
|
||||
"Insert word and Press ENTER!",
|
||||
words,
|
||||
gameContainer);
|
||||
gameController.getWords(),
|
||||
gameContainer
|
||||
);
|
||||
|
||||
addFooter(root);
|
||||
addNext(footer,
|
||||
new Font(Palette.inputBox_font.getFontName(), Font.BOLD, 70),
|
||||
null,
|
||||
"Done!",
|
||||
new Callable<Object>() {
|
||||
@Override
|
||||
public Object call() throws Exception {
|
||||
return null;
|
||||
}
|
||||
});
|
||||
|
||||
timer = addTimer(footer,
|
||||
new Font(Palette.inputBox_font.getFontName(), Font.BOLD, 40),
|
||||
null,
|
||||
"00:00",
|
||||
new Callable<Object>() {
|
||||
@Override
|
||||
public Object call() throws Exception {
|
||||
gameController.sendWords(words);
|
||||
return null;
|
||||
}},
|
||||
15);
|
||||
timer = addTimer(
|
||||
footer,
|
||||
new Font(Palette.inputBox_font.getFontName(), Font.BOLD, 40),
|
||||
null,
|
||||
"00:00",
|
||||
timeIsOver(),
|
||||
Config.timeoutGame
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,85 +0,0 @@
|
|||
package com.texttwist.client.pages;
|
||||
|
||||
import com.texttwist.client.App;
|
||||
import com.texttwist.client.tasks.StartGame;
|
||||
import com.texttwist.client.tasks.WaitForPlayers;
|
||||
import com.texttwist.client.tasks.WaitForScore;
|
||||
import com.texttwist.client.ui.TTDialog;
|
||||
import com.texttwist.client.ui.TTLetter;
|
||||
import constants.Config;
|
||||
import models.Message;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.*;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
|
||||
/**
|
||||
* Created by loke on 25/06/2017.
|
||||
*/
|
||||
public class GameController {
|
||||
|
||||
|
||||
public DefaultListModel<String> letters = new DefaultListModel<String>();
|
||||
public DefaultListModel<String> words = new DefaultListModel<String>();
|
||||
|
||||
public GameController(){
|
||||
}
|
||||
|
||||
public DefaultListModel<String> waitForPlayers() {
|
||||
return App.matchService.waitForPlayers();
|
||||
}
|
||||
|
||||
|
||||
public void waitForScore(){
|
||||
SwingWorker worker = new WaitForScore();
|
||||
worker.execute();
|
||||
|
||||
}
|
||||
|
||||
public boolean addWordToWordsList(String word) {
|
||||
words.addElement(word);
|
||||
return true;
|
||||
}
|
||||
|
||||
public Callable<Object> sendWords(DefaultListModel<String> words){
|
||||
System.out.println("SENDDDD" + words);
|
||||
|
||||
DatagramSocket clientSocket = null;
|
||||
try {
|
||||
clientSocket = new DatagramSocket();
|
||||
|
||||
InetAddress IPAddress = InetAddress.getByName(Config.WordsReceiverServerURI);
|
||||
byte[] sendData = new byte[1024];
|
||||
byte[] receiveData = new byte[1024];
|
||||
Message msg = new Message("WORDS",App.sessionService.account.userName,"",words);
|
||||
String sentence = msg.toString();
|
||||
sendData = sentence.getBytes();
|
||||
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, Config.WordsReceiverServerPort);
|
||||
clientSocket.send(sendPacket);
|
||||
clientSocket.close();
|
||||
waitForScore();
|
||||
|
||||
} catch (SocketException e) {
|
||||
e.printStackTrace();
|
||||
} catch (UnknownHostException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
public Thread startGame(Game game){
|
||||
Thread t = new Thread(new StartGame(game));
|
||||
t.start();
|
||||
return t;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -1,7 +1,10 @@
|
|||
package com.texttwist.client.pages;
|
||||
|
||||
import com.texttwist.client.App;
|
||||
import com.texttwist.client.controllers.HighscoresController;
|
||||
import constants.Palette;
|
||||
import com.texttwist.client.ui.*;
|
||||
import javafx.util.Pair;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
|
|
@ -14,33 +17,33 @@ import java.util.concurrent.Callable;
|
|||
public class Highscores extends Page{
|
||||
|
||||
public TTContainer highscoreContainer;
|
||||
Highscores(JFrame window) throws IOException {
|
||||
public Boolean isPartialScore;
|
||||
public HighscoresController highscoreController;
|
||||
public DefaultListModel<Pair<String, Integer>> ranks = new DefaultListModel<>();
|
||||
|
||||
public Highscores(JFrame window, Boolean isPartialScore) throws IOException {
|
||||
super(window);
|
||||
this.isPartialScore = isPartialScore;
|
||||
|
||||
highscoreController = new HighscoresController();
|
||||
System.out.println("SHSHSHSHs");
|
||||
System.out.println(App.match.ranks);
|
||||
System.out.println(App.match.globalRanks);
|
||||
|
||||
System.out.println(ranks);
|
||||
System.out.println("SHSHSHSssssssHs");
|
||||
|
||||
if(this.isPartialScore){
|
||||
this.ranks = App.match.ranks;
|
||||
} else {
|
||||
this.highscoreController.fetchHighscores(window);
|
||||
this.ranks = App.match.globalRanks;
|
||||
}
|
||||
createUIComponents();
|
||||
|
||||
window.setVisible(true);
|
||||
}
|
||||
|
||||
private DefaultListModel fetchHighscores(){
|
||||
DefaultListModel<String> highscoreList = new DefaultListModel<String>();
|
||||
highscoreList.addElement("Pippo 41");
|
||||
highscoreList.addElement("Paperino 37");
|
||||
highscoreList.addElement("Gaia 34");
|
||||
highscoreList.addElement("Luigi 32");
|
||||
highscoreList.addElement("Marco 31");
|
||||
highscoreList.addElement("Minnie 30");
|
||||
highscoreList.addElement("Franco 30");
|
||||
highscoreList.addElement("Qua 29");
|
||||
highscoreList.addElement("Luca 27");
|
||||
highscoreList.addElement("Qui 26");
|
||||
highscoreList.addElement("Jorge 25");
|
||||
highscoreList.addElement("David 22");
|
||||
highscoreList.addElement("Quo 21");
|
||||
highscoreList.addElement("Raphael 21");
|
||||
highscoreList.addElement("Miguel 16");
|
||||
highscoreList.addElement("Carmen 14");
|
||||
highscoreList.addElement("Beatriz 12");
|
||||
return highscoreList;
|
||||
}
|
||||
@Override
|
||||
public void createUIComponents() throws IOException {
|
||||
addLogo(root);
|
||||
|
|
@ -54,7 +57,7 @@ public class Highscores extends Page{
|
|||
TTLabel title = new TTLabel(
|
||||
new Point(200,0),
|
||||
new Dimension(350,50),
|
||||
"Highscores",
|
||||
this.isPartialScore ? "Scores of the match" : "Highscores",
|
||||
new Font(Palette.inputBox_font.getFontName(), Font.ITALIC, 38),
|
||||
null,
|
||||
highscoreContainer);
|
||||
|
|
@ -62,7 +65,7 @@ public class Highscores extends Page{
|
|||
TTScrollList highscoreList = new TTScrollList(
|
||||
new Point(20, 60),
|
||||
new Dimension(515, 142),
|
||||
fetchHighscores(),
|
||||
this.ranks,
|
||||
highscoreContainer);
|
||||
addFooter(root);
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
package com.texttwist.client.pages;
|
||||
import com.texttwist.client.controllers.HomeController;
|
||||
import constants.Palette;
|
||||
import com.texttwist.client.ui.*;
|
||||
import com.texttwist.client.ui.TTDialog;
|
||||
|
|
|
|||
|
|
@ -45,7 +45,7 @@ public class MatchRequests extends Page{
|
|||
TTScrollList pendingMatches = new TTScrollList(
|
||||
new Point(20, 60),
|
||||
new Dimension(520, 142),
|
||||
App.matchService.pendingList,
|
||||
App.match.pendingList,
|
||||
matchsContainer);
|
||||
|
||||
pendingMatches.addMouseListener(new MouseAdapter() {
|
||||
|
|
@ -56,7 +56,7 @@ public class MatchRequests extends Page{
|
|||
if (evt.getClickCount() == 2) {
|
||||
// Double-click detected
|
||||
int index = thisList.locationToIndex(evt.getPoint());
|
||||
App.matchService.joinMatch(App.matchService.pendingList.get(index));
|
||||
App.match.joinMatch(App.match.pendingList.get(index));
|
||||
}
|
||||
}
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,8 +1,8 @@
|
|||
package com.texttwist.client.pages;
|
||||
|
||||
import com.texttwist.client.controllers.MatchSetupController;
|
||||
import constants.Palette;
|
||||
import com.texttwist.client.ui.*;
|
||||
import models.Response;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
package com.texttwist.client.pages;
|
||||
import com.texttwist.client.App;
|
||||
import com.texttwist.client.controllers.MenuController;
|
||||
import constants.Palette;
|
||||
import com.texttwist.client.ui.*;
|
||||
|
||||
|
|
@ -80,7 +81,7 @@ public class Menu extends Page{
|
|||
new Callable<Object>() {
|
||||
@Override
|
||||
public Object call() throws Exception {
|
||||
return new Highscores(Page.window);
|
||||
return new Highscores(Page.window, false);
|
||||
}
|
||||
},
|
||||
menuBar);
|
||||
|
|
|
|||
|
|
@ -93,7 +93,7 @@ public class Page {
|
|||
parent);
|
||||
}
|
||||
|
||||
public Timer addTimer(TTContainer parent, Font font, Color fontColor, String caption, Callable<Object> timerEndHandler, Integer value) {
|
||||
public Timer addTimer(TTContainer parent, Font font, Color fontColor, String caption, SwingWorker timerEndHandler, Integer value) {
|
||||
TTLabel lblTimer = new TTLabel(
|
||||
new Point(0, 0),
|
||||
new Dimension(150, 50),
|
||||
|
|
@ -109,7 +109,7 @@ public class Page {
|
|||
if (count <= 0) {
|
||||
lblTimer.setText("00:00");
|
||||
try {
|
||||
timerEndHandler.call();
|
||||
timerEndHandler.execute();
|
||||
} catch (Exception e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
package com.texttwist.client.pages;
|
||||
|
||||
import com.texttwist.client.controllers.RegisterController;
|
||||
import constants.Palette;
|
||||
import com.texttwist.client.ui.*;
|
||||
import models.Response;
|
||||
|
|
|
|||
|
|
@ -1,37 +1,32 @@
|
|||
package com.texttwist.client.pages;
|
||||
|
||||
import com.texttwist.client.App;
|
||||
import constants.Config;
|
||||
import interfaces.IAuth;
|
||||
import models.Response;
|
||||
|
||||
import java.net.MalformedURLException;
|
||||
import java.rmi.Naming;
|
||||
import java.rmi.NotBoundException;
|
||||
import java.rmi.RemoteException;
|
||||
|
||||
/**
|
||||
* Created by loke on 17/06/2017.
|
||||
*/
|
||||
public class AuthService {
|
||||
|
||||
protected String baseUrl = Config.getAuthServerURI().concat("/auth");
|
||||
|
||||
public AuthService(){
|
||||
}
|
||||
|
||||
public Response login(String userName, String password) throws RemoteException, NotBoundException, MalformedURLException {
|
||||
IAuth auth = (IAuth) Naming.lookup(baseUrl);
|
||||
return auth.login(userName, password);
|
||||
}
|
||||
|
||||
public Response register(String userName, String password) throws RemoteException, NotBoundException, MalformedURLException {
|
||||
IAuth auth = (IAuth) Naming.lookup(baseUrl);
|
||||
return auth.register(userName, password);
|
||||
}
|
||||
|
||||
public Response logout(String userName) throws RemoteException, NotBoundException, MalformedURLException {
|
||||
IAuth auth = (IAuth) Naming.lookup(baseUrl);
|
||||
return auth.logout(userName, App.sessionService.account.token);
|
||||
}
|
||||
}
|
||||
package com.texttwist.client.services;
|
||||
import com.texttwist.client.App;
|
||||
import constants.Config;
|
||||
import interfaces.IAuth;
|
||||
import models.Response;
|
||||
import java.net.MalformedURLException;
|
||||
import java.rmi.Naming;
|
||||
import java.rmi.NotBoundException;
|
||||
import java.rmi.RemoteException;
|
||||
|
||||
/**
|
||||
* Authentication Service
|
||||
*/
|
||||
public class AuthService {
|
||||
|
||||
private String baseUrl = Config.getAuthServerURI().concat("/auth");
|
||||
|
||||
public Response login(String userName, String password) throws RemoteException, NotBoundException, MalformedURLException {
|
||||
IAuth auth = (IAuth) Naming.lookup(baseUrl);
|
||||
return auth.login(userName, password);
|
||||
}
|
||||
|
||||
public Response register(String userName, String password) throws RemoteException, NotBoundException, MalformedURLException {
|
||||
IAuth auth = (IAuth) Naming.lookup(baseUrl);
|
||||
return auth.register(userName, password);
|
||||
}
|
||||
|
||||
public Response logout(String userName) throws RemoteException, NotBoundException, MalformedURLException {
|
||||
IAuth auth = (IAuth) Naming.lookup(baseUrl);
|
||||
return auth.logout(userName, App.sessionService.account.token);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,44 @@
|
|||
package com.texttwist.client.services;
|
||||
|
||||
import com.texttwist.client.App;
|
||||
import com.texttwist.client.tasks.FetchHighscore;
|
||||
import com.texttwist.client.tasks.WaitForScore;
|
||||
import constants.Config;
|
||||
import javafx.util.Pair;
|
||||
import models.Message;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.io.IOException;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.SocketChannel;
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
/**
|
||||
* Created by loke on 28/06/2017.
|
||||
*/
|
||||
public class HighscoresService {
|
||||
|
||||
ByteBuffer buffer = ByteBuffer.allocate(1024);
|
||||
public DefaultListModel<String> ranks = new DefaultListModel<>();
|
||||
|
||||
SocketChannel clientSocket = null;
|
||||
|
||||
public HighscoresService(){
|
||||
InetSocketAddress socketAddress = new InetSocketAddress(Config.GameServerURI, Config.GameServerPort);
|
||||
try {
|
||||
clientSocket = SocketChannel.open(socketAddress);
|
||||
clientSocket.configureBlocking(false);
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void fetchHighscores(Callable<String> callback){
|
||||
SwingWorker worker = new FetchHighscore(callback, clientSocket);
|
||||
worker.execute();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -1,113 +1,118 @@
|
|||
package com.texttwist.client.pages;
|
||||
|
||||
import com.texttwist.client.App;
|
||||
import com.texttwist.client.tasks.InvitePlayers;
|
||||
import com.texttwist.client.tasks.WaitForPlayers;
|
||||
import com.texttwist.client.ui.TTDialog;
|
||||
import constants.Config;
|
||||
import models.Message;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.io.*;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.SocketChannel;
|
||||
import java.util.concurrent.*;
|
||||
|
||||
/**
|
||||
* Created by loke on 18/06/2017.
|
||||
*/
|
||||
public class MatchService {
|
||||
|
||||
public Integer multicastId = 0 ;
|
||||
public DefaultListModel<String> pendingList = new DefaultListModel<String>();
|
||||
ByteBuffer buffer = ByteBuffer.allocate(1024);
|
||||
public DefaultListModel<String> words = new DefaultListModel<String>();
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void newMatch(String userName) {
|
||||
//Aggiungi alla lista di inviti
|
||||
try {
|
||||
this.addToPendingList(userName);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
//Visualizza popup
|
||||
new TTDialog("success", "New invitation from: " + userName + "!",
|
||||
new Callable() {
|
||||
@Override
|
||||
public Object call() throws Exception {
|
||||
App.matchService.joinMatch(userName);
|
||||
return null;
|
||||
}
|
||||
},
|
||||
new Callable() {
|
||||
@Override
|
||||
public Object call() throws Exception {
|
||||
return new Menu(Page.window);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
public DefaultListModel<String> waitForPlayers(){
|
||||
DefaultListModel<String> words = new DefaultListModel<>();
|
||||
SwingWorker worker = new WaitForPlayers(clientSocket, words);
|
||||
worker.execute();
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
public void setWords(DefaultListModel<String> words){
|
||||
this.words = words;
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public Object play(DefaultListModel<String> userNames) throws IOException {
|
||||
SwingWorker worker = new InvitePlayers(userNames,clientSocket);
|
||||
worker.execute();
|
||||
return null;
|
||||
}
|
||||
|
||||
public void setMulticastId(Integer multicastId){
|
||||
this.multicastId = multicastId;
|
||||
}
|
||||
|
||||
public void addToPendingList(String username) throws IOException {
|
||||
pendingList.addElement(username);
|
||||
}
|
||||
}
|
||||
package com.texttwist.client.services;
|
||||
|
||||
import com.texttwist.client.App;
|
||||
import com.texttwist.client.pages.Game;
|
||||
import com.texttwist.client.pages.Menu;
|
||||
import com.texttwist.client.pages.Page;
|
||||
import com.texttwist.client.tasks.InvitePlayers;
|
||||
import com.texttwist.client.tasks.WaitForPlayers;
|
||||
import com.texttwist.client.ui.TTDialog;
|
||||
import constants.Config;
|
||||
import javafx.util.Pair;
|
||||
import models.Message;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.io.*;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.SocketChannel;
|
||||
import java.util.concurrent.*;
|
||||
|
||||
/**
|
||||
* Created by loke on 18/06/2017.
|
||||
*/
|
||||
public class MatchModel {
|
||||
|
||||
public Integer multicastId = 0 ;
|
||||
public DefaultListModel<String> pendingList = new DefaultListModel<String>();
|
||||
ByteBuffer buffer = ByteBuffer.allocate(1024);
|
||||
public DefaultListModel<String> words = new DefaultListModel<String>();
|
||||
public DefaultListModel<String> letters = new DefaultListModel<String>();
|
||||
public DefaultListModel<Pair<String,Integer>> ranks = new DefaultListModel<>();
|
||||
public DefaultListModel<Pair<String,Integer>> globalRanks = new DefaultListModel<>();
|
||||
|
||||
public SocketChannel clientSocket = null;
|
||||
|
||||
public MatchModel(){
|
||||
InetSocketAddress socketAddress = new InetSocketAddress(Config.GameServerURI, Config.GameServerPort);
|
||||
try {
|
||||
clientSocket = SocketChannel.open(socketAddress);
|
||||
clientSocket.configureBlocking(false);
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void newMatch(String userName) {
|
||||
//Aggiungi alla lista di inviti
|
||||
try {
|
||||
this.addToPendingList(userName);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
//Visualizza popup
|
||||
new TTDialog("success", "New invitation from: " + userName + "!",
|
||||
new Callable() {
|
||||
@Override
|
||||
public Object call() throws Exception {
|
||||
App.match.joinMatch(userName);
|
||||
return null;
|
||||
}
|
||||
},
|
||||
new Callable() {
|
||||
@Override
|
||||
public Object call() throws Exception {
|
||||
return new Menu(Page.window);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
public void setWords(DefaultListModel<String> words){
|
||||
this.words = words;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void setLetters(DefaultListModel<String> letters){
|
||||
this.letters = letters;
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public Object play(DefaultListModel<String> userNames) throws IOException {
|
||||
SwingWorker worker = new InvitePlayers(userNames,clientSocket);
|
||||
worker.execute();
|
||||
return null;
|
||||
}
|
||||
|
||||
public void setMulticastId(Integer multicastId){
|
||||
this.multicastId = multicastId;
|
||||
}
|
||||
|
||||
public void addToPendingList(String username) throws IOException {
|
||||
pendingList.addElement(username);
|
||||
}
|
||||
}
|
||||
|
|
@ -29,7 +29,7 @@ public class NotificationClient implements INotificationClient {
|
|||
|
||||
if(users.contains(App.sessionService.account.userName)){
|
||||
Logger.write(userName+" ti ha sfidato!");
|
||||
App.matchService.newMatch(userName);
|
||||
App.match.newMatch(userName);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,20 +1,20 @@
|
|||
package com.texttwist.client.pages;
|
||||
|
||||
import models.Session;
|
||||
|
||||
/**
|
||||
* Created by loke on 17/06/2017.
|
||||
*/
|
||||
public class SessionService {
|
||||
|
||||
public Session account;
|
||||
public SessionService(){}
|
||||
|
||||
public void create(String userName, String token) {
|
||||
account = new Session(userName, token);
|
||||
}
|
||||
|
||||
public void remove(){
|
||||
account = null;
|
||||
}
|
||||
}
|
||||
package com.texttwist.client.services;
|
||||
|
||||
import models.Session;
|
||||
|
||||
/**
|
||||
* Created by loke on 17/06/2017.
|
||||
*/
|
||||
public class SessionService {
|
||||
|
||||
public Session account;
|
||||
public SessionService(){}
|
||||
|
||||
public void create(String userName, String token) {
|
||||
account = new Session(userName, token);
|
||||
}
|
||||
|
||||
public void remove(){
|
||||
account = null;
|
||||
}
|
||||
}
|
||||
99
Client/src/com/texttwist/client/tasks/FetchHighscore.java
Normal file
99
Client/src/com/texttwist/client/tasks/FetchHighscore.java
Normal file
|
|
@ -0,0 +1,99 @@
|
|||
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;
|
||||
import java.net.InetAddress;
|
||||
import java.net.MulticastSocket;
|
||||
import java.net.UnknownHostException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.SocketChannel;
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
/**
|
||||
* Created by loke on 28/06/2017.
|
||||
*/
|
||||
public class FetchHighscore extends SwingWorker<Void,Void> {
|
||||
|
||||
DefaultListModel<Pair<String,Integer>> globalRanks = new DefaultListModel<Pair<String,Integer>>();
|
||||
Callable<String> callback;
|
||||
SocketChannel socketChannel;
|
||||
ByteBuffer buffer = ByteBuffer.allocate(1024);
|
||||
|
||||
//TODO PASSARE LA CALLBACK ALLO SWING WORKER ED ESEGUIRLA AL DONE
|
||||
public FetchHighscore(Callable<String> callback, SocketChannel socketChannel){
|
||||
this.socketChannel = socketChannel;
|
||||
this.callback = callback;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void doInBackground() {
|
||||
Message message = new Message("FETCH_HIGHSCORES", App.sessionService.account.userName, App.sessionService.account.token, new DefaultListModel<>());
|
||||
buffer.flip();
|
||||
buffer.clear();
|
||||
byte[] byteMessage = new String(message.toString()).getBytes();
|
||||
buffer = ByteBuffer.wrap(byteMessage);
|
||||
try {
|
||||
socketChannel.write(buffer);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
try {
|
||||
buffer = ByteBuffer.allocate(1024);
|
||||
|
||||
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);
|
||||
System.out.println(line);
|
||||
if (msg.message.equals("HIGHSCORES")) {
|
||||
|
||||
for(int i = 0; i< msg.data.size()-1; i++){
|
||||
String[] splitted = msg.data.get(i).split(":");
|
||||
System.out.println(splitted.toString());
|
||||
globalRanks.addElement(new Pair<String, Integer>(splitted[0],new Integer(splitted[1])));
|
||||
}
|
||||
|
||||
App.match.globalRanks = globalRanks;
|
||||
|
||||
System.out.println(globalRanks);
|
||||
/*new TTDialog("alert", "Users not online!",
|
||||
new Callable() {
|
||||
@Override
|
||||
public Object call() throws Exception {
|
||||
return null;
|
||||
}
|
||||
}, null);*/
|
||||
return null;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void done(){
|
||||
System.out.println("Done");
|
||||
App.match.globalRanks = globalRanks;
|
||||
try {
|
||||
this.callback.call();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -83,7 +83,7 @@ public class InvitePlayers extends SwingWorker<Boolean,Void> {
|
|||
|
||||
@Override
|
||||
public void done() {
|
||||
System.out.println("Done");
|
||||
System.out.println("Done invite players");
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
70
Client/src/com/texttwist/client/tasks/SendWords.java
Normal file
70
Client/src/com/texttwist/client/tasks/SendWords.java
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
package com.texttwist.client.tasks;
|
||||
|
||||
import com.texttwist.client.App;
|
||||
import com.texttwist.client.pages.Highscores;
|
||||
import constants.Config;
|
||||
import javafx.util.Pair;
|
||||
import models.Message;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.io.IOException;
|
||||
import java.net.*;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.RunnableFuture;
|
||||
|
||||
import static com.texttwist.client.pages.Page.window;
|
||||
|
||||
/**
|
||||
* Created by loke on 29/06/2017.
|
||||
*/
|
||||
public class SendWords extends SwingWorker<Void,Void> {
|
||||
|
||||
DefaultListModel<Pair<String,Integer>> ranks = new DefaultListModel<Pair<String,Integer>>();
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void doInBackground() {
|
||||
DatagramSocket clientSocket = null;
|
||||
try {
|
||||
clientSocket = new DatagramSocket();
|
||||
|
||||
InetAddress IPAddress = InetAddress.getByName(Config.WordsReceiverServerURI);
|
||||
byte[] sendData = new byte[1024];
|
||||
byte[] receiveData = new byte[1024];
|
||||
Message msg = new Message("WORDS", App.sessionService.account.userName, "", words);
|
||||
String sentence = msg.toString();
|
||||
sendData = sentence.getBytes();
|
||||
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, Config.WordsReceiverServerPort);
|
||||
clientSocket.send(sendPacket);
|
||||
clientSocket.close();
|
||||
|
||||
return null;
|
||||
} catch (UnknownHostException e) {
|
||||
e.printStackTrace();
|
||||
} catch (SocketException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void done(){
|
||||
System.out.println("Done");
|
||||
try {
|
||||
this.callback.execute();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -2,10 +2,7 @@ package com.texttwist.client.tasks;
|
|||
|
||||
import com.texttwist.client.App;
|
||||
import com.texttwist.client.pages.Game;
|
||||
import com.texttwist.client.pages.GameController;
|
||||
import com.texttwist.client.pages.Page;
|
||||
import com.texttwist.client.ui.TTDialog;
|
||||
import oracle.jrockit.jfr.JFR;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.util.concurrent.Callable;
|
||||
|
|
@ -13,21 +10,20 @@ import java.util.concurrent.Callable;
|
|||
/**
|
||||
* Created by loke on 25/06/2017.
|
||||
*/
|
||||
public class StartGame implements Runnable {
|
||||
public class StartGame extends SwingWorker<Void,Void> {
|
||||
|
||||
public Game game;
|
||||
public StartGame(Game game){
|
||||
|
||||
/*Words inserted from user*/
|
||||
private DefaultListModel<String> letters = new DefaultListModel<>();
|
||||
private Game game;
|
||||
|
||||
public StartGame(DefaultListModel<String> letters, Game game){
|
||||
this.letters = letters;
|
||||
this.game = game;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(){
|
||||
|
||||
//Letters are ready? Polling
|
||||
while(!(this.game.gameController.letters.size() > 0)) {
|
||||
this.game.gameController.letters = App.matchService.words;
|
||||
}
|
||||
public Void doInBackground(){
|
||||
|
||||
//Mostra pannello di conferma che le lettere sono tutte arrivate
|
||||
new TTDialog("success", "Game is ready. Press OK to start!",
|
||||
|
|
@ -35,9 +31,16 @@ public class StartGame implements Runnable {
|
|||
@Override
|
||||
public Object call() throws Exception {
|
||||
game.showLetters();
|
||||
System.out.println(letters);
|
||||
game.timer.start();
|
||||
return null;
|
||||
}
|
||||
}, null);
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void done(){
|
||||
System.out.println("Done start game");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,6 +14,7 @@ import java.nio.ByteBuffer;
|
|||
import java.nio.channels.SocketChannel;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.RunnableFuture;
|
||||
|
||||
/**
|
||||
* Created by loke on 25/06/2017.
|
||||
|
|
@ -22,11 +23,15 @@ public class WaitForPlayers extends SwingWorker<DefaultListModel<String>,Default
|
|||
|
||||
public SocketChannel socketChannel;
|
||||
public DefaultListModel<String> words;
|
||||
ByteBuffer buffer = ByteBuffer.allocate(1024);
|
||||
public DefaultListModel<String> letters;
|
||||
|
||||
public WaitForPlayers(SocketChannel socketChannel, DefaultListModel<String> words) {
|
||||
this.socketChannel = socketChannel;
|
||||
ByteBuffer buffer = ByteBuffer.allocate(1024);
|
||||
SwingWorker callback;
|
||||
|
||||
public WaitForPlayers(SwingWorker callback) {
|
||||
this.callback = callback;
|
||||
this.words = words;
|
||||
this.socketChannel = App.match.clientSocket;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -65,11 +70,10 @@ public class WaitForPlayers extends SwingWorker<DefaultListModel<String>,Default
|
|||
System.out.println(msg.data);
|
||||
Integer multicastId = Integer.valueOf(data.remove(data.size()-2));
|
||||
System.out.println(multicastId);
|
||||
App.matchService.setMulticastId(multicastId);
|
||||
words = msg.data;
|
||||
App.match.setMulticastId(multicastId);
|
||||
letters = msg.data;
|
||||
|
||||
//socketChannel.close();
|
||||
App.matchService.setWords(words);
|
||||
return words;
|
||||
}
|
||||
}
|
||||
|
|
@ -80,7 +84,16 @@ public class WaitForPlayers extends SwingWorker<DefaultListModel<String>,Default
|
|||
return new DefaultListModel<String>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void done(){
|
||||
System.out.println("Done");
|
||||
System.out.println("Done wait for players");
|
||||
try {
|
||||
System.out.println(letters);
|
||||
App.match.setLetters(letters);
|
||||
System.out.println("PAROLE IN INVIO");
|
||||
this.callback.execute();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,10 @@
|
|||
package com.texttwist.client.tasks;
|
||||
|
||||
import com.sun.org.apache.xpath.internal.operations.Bool;
|
||||
import com.texttwist.client.App;
|
||||
import constants.Config;
|
||||
import javafx.util.Pair;
|
||||
import models.Message;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.io.IOException;
|
||||
|
|
@ -9,19 +12,29 @@ import java.net.DatagramPacket;
|
|||
import java.net.InetAddress;
|
||||
import java.net.MulticastSocket;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.RunnableFuture;
|
||||
|
||||
/**
|
||||
* Created by loke on 27/06/2017.
|
||||
*/
|
||||
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
|
||||
public WaitForScore(SwingWorker callback){
|
||||
this.callback = callback;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void doInBackground() {
|
||||
InetAddress group = null;
|
||||
try {
|
||||
|
||||
MulticastSocket ms = new MulticastSocket(App.matchService.multicastId);
|
||||
MulticastSocket ms = new MulticastSocket(App.match.multicastId);
|
||||
InetAddress ia = InetAddress.getByName(Config.ScoreMulticastServerURI);
|
||||
ms.joinGroup(ia);
|
||||
System.out.println("Join multicast group");
|
||||
|
|
@ -30,7 +43,21 @@ public class WaitForScore extends SwingWorker<Void,Void> {
|
|||
DatagramPacket recv = new DatagramPacket(buf, buf.length);
|
||||
ms.receive(recv);
|
||||
String s = new String(recv.getData());
|
||||
System.out.println("HSHSHSHS");
|
||||
System.out.println(s);
|
||||
Message msg = Message.toMessage(s);
|
||||
System.out.println(msg.data);
|
||||
|
||||
for(int i = 0; i< msg.data.size()-1; i++){
|
||||
String[] splitted = msg.data.get(i).split(":");
|
||||
System.out.println(splitted.toString());
|
||||
ranks.addElement(new Pair<String, Integer>(splitted[0],new Integer(splitted[1])));
|
||||
}
|
||||
|
||||
App.match.ranks = ranks;
|
||||
System.out.println(App.match.ranks);
|
||||
System.out.println("ENDDDDd");
|
||||
|
||||
} catch (UnknownHostException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
|
|
@ -39,8 +66,15 @@ public class WaitForScore extends SwingWorker<Void,Void> {
|
|||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void done(){
|
||||
System.out.println("Done");
|
||||
App.match.ranks = ranks;
|
||||
try {
|
||||
this.callback.execute();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ public class TTGameBox extends TTInputField{
|
|||
public TTGameBox(Point position,
|
||||
Dimension dimension,
|
||||
String placeholer,
|
||||
DefaultListModel listModel,
|
||||
DefaultListModel<String> list,
|
||||
TTContainer parent){
|
||||
|
||||
super(position, dimension, placeholer, parent);
|
||||
|
|
@ -31,8 +31,7 @@ public class TTGameBox extends TTInputField{
|
|||
super.keyPressed(e);
|
||||
if(e.getKeyCode() == 10){
|
||||
try {
|
||||
System.out.println(getText());
|
||||
listModel.addElement(getText());
|
||||
list.addElement(getText());
|
||||
setText("");
|
||||
} catch (Exception e1) {
|
||||
e1.printStackTrace();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue