implementazione persistenza
This commit is contained in:
parent
406a5647b8
commit
eac6e43420
33 changed files with 1876 additions and 736 deletions
|
|
@ -6,7 +6,6 @@ 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
|
||||
|
|
@ -19,14 +18,6 @@ public class GameController {
|
|||
this.game = game;
|
||||
}
|
||||
|
||||
public DefaultListModel<String> getLetters(){
|
||||
return App.game.letters;
|
||||
}
|
||||
|
||||
public DefaultListModel<String> getWords() {
|
||||
return App.game.words;
|
||||
}
|
||||
|
||||
public SwingWorker waitForPlayers(SwingWorker callback) {
|
||||
return new WaitForPlayers(callback);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ import java.rmi.RemoteException;
|
|||
public class MenuController {
|
||||
|
||||
public Response logout(String userName, INotificationClient stub) throws RemoteException, NotBoundException, MalformedURLException {
|
||||
Response res = App.authService.logout(userName, stub);
|
||||
Response res = App.authService.logout(userName, stub);
|
||||
if (res.code == 200){
|
||||
App.session = null;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,7 +4,6 @@ import com.texttwist.client.App;
|
|||
import com.texttwist.client.pages.GamePage;
|
||||
import com.texttwist.client.pages.MenuPage;
|
||||
import com.texttwist.client.pages.Page;
|
||||
import com.texttwist.client.services.NotificationClient;
|
||||
import com.texttwist.client.tasks.InvitePlayers;
|
||||
import com.texttwist.client.ui.TTDialog;
|
||||
import constants.Config;
|
||||
|
|
@ -15,7 +14,6 @@ import models.Message;
|
|||
|
||||
import javax.swing.*;
|
||||
import java.io.*;
|
||||
import java.net.InetAddress;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.MulticastSocket;
|
||||
import java.nio.ByteBuffer;
|
||||
|
|
@ -24,7 +22,6 @@ import java.rmi.NotBoundException;
|
|||
import java.rmi.RemoteException;
|
||||
import java.rmi.registry.LocateRegistry;
|
||||
import java.rmi.registry.Registry;
|
||||
import java.rmi.server.UnicastRemoteObject;
|
||||
import java.util.concurrent.*;
|
||||
|
||||
/**
|
||||
|
|
@ -34,23 +31,24 @@ public class Game {
|
|||
|
||||
public Integer multicastId = 0 ;
|
||||
public DefaultListModel<String> pendingList = new DefaultListModel<String>();
|
||||
private 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 INotificationClient stub;
|
||||
public DefaultListModel<Pair<String,Integer>> globalRanks = new DefaultListModel<>();
|
||||
public DefaultListModel<Pair<String,Integer>> ranks = new DefaultListModel<>();
|
||||
public INotificationClient notificationStub;
|
||||
public MulticastSocket multicastSocket;
|
||||
public INotificationServer server;
|
||||
public Boolean isStarted = false;
|
||||
public SocketChannel clientSocket = null;
|
||||
public SocketChannel clientSocket;
|
||||
public INotificationServer notificationServer;
|
||||
|
||||
public Boolean gameIsStarted = false;
|
||||
private ByteBuffer buffer = ByteBuffer.allocate(1024);
|
||||
|
||||
public Game(){
|
||||
|
||||
Registry registry = null;
|
||||
try {
|
||||
registry = LocateRegistry.getRegistry(Config.NotificationServerStubPort);
|
||||
server = (INotificationServer) registry.lookup(Config.NotificationServerName);
|
||||
notificationServer = (INotificationServer) registry.lookup(Config.NotificationServerName);
|
||||
|
||||
} catch (RemoteException e) {
|
||||
e.printStackTrace();
|
||||
|
|
@ -62,8 +60,6 @@ public class Game {
|
|||
try {
|
||||
clientSocket = SocketChannel.open(socketAddress);
|
||||
clientSocket.configureBlocking(false);
|
||||
System.out.println("Join multicast group");
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
|
@ -71,35 +67,39 @@ public class Game {
|
|||
|
||||
|
||||
public void newMatch(String userName) {
|
||||
//Aggiungi alla lista di inviti
|
||||
//Add to pending invitation list
|
||||
try {
|
||||
this.addToPendingList(userName);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
if(!App.game.isStarted) {
|
||||
//Visualizza popup
|
||||
if(!App.game.gameIsStarted) {
|
||||
//Show invitation popup
|
||||
new TTDialog("success", "New invitation from: " + userName + "!",
|
||||
new Callable() {
|
||||
@Override
|
||||
public Object call() throws Exception {
|
||||
App.game.joinMatch(userName);
|
||||
return null;
|
||||
}
|
||||
},
|
||||
new Callable() {
|
||||
@Override
|
||||
public Object call() throws Exception {
|
||||
return new MenuPage(Page.window);
|
||||
}
|
||||
});
|
||||
new Callable() {
|
||||
@Override
|
||||
public Object call() throws Exception {
|
||||
App.game.joinMatch(userName);
|
||||
return null;
|
||||
}
|
||||
},
|
||||
new Callable() {
|
||||
@Override
|
||||
public Object call() throws Exception {
|
||||
return new MenuPage(Page.window);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void setWords(DefaultListModel<String> words){
|
||||
this.words = words;
|
||||
public DefaultListModel<String> getLetters(){
|
||||
return App.game.letters;
|
||||
}
|
||||
|
||||
public DefaultListModel<String> getWords() {
|
||||
return App.game.words;
|
||||
}
|
||||
|
||||
public void setLetters(DefaultListModel<String> letters){
|
||||
|
|
@ -107,19 +107,17 @@ public class Game {
|
|||
}
|
||||
|
||||
public void joinMatch(String matchName) {
|
||||
//Svuota la lista dei game pendenti e joina il game selezionato
|
||||
if(!isStarted) {
|
||||
//Clear pending invitation list and join selected match
|
||||
if(!gameIsStarted) {
|
||||
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.session.account.userName, App.session.token, matchNames);
|
||||
|
||||
byte[] byteMessage = new String(message.toString()).getBytes();
|
||||
byte[] byteMessage = message.toString().getBytes();
|
||||
buffer = ByteBuffer.wrap(byteMessage);
|
||||
clientSocket.write(buffer);
|
||||
|
||||
new GamePage(Page.window);
|
||||
|
||||
} catch (IOException e) {
|
||||
|
|
@ -128,7 +126,6 @@ public class Game {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
public Object play(DefaultListModel<String> userNames) throws IOException {
|
||||
SwingWorker worker = new InvitePlayers(userNames,clientSocket);
|
||||
worker.execute();
|
||||
|
|
@ -139,7 +136,7 @@ public class Game {
|
|||
this.multicastId = multicastId;
|
||||
}
|
||||
|
||||
public void addToPendingList(String username) throws IOException {
|
||||
private void addToPendingList(String username) throws IOException {
|
||||
pendingList.addElement(username);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,24 +8,24 @@ import java.awt.*;
|
|||
import java.io.IOException;
|
||||
import java.util.concurrent.*;
|
||||
|
||||
import static com.texttwist.client.App.game;
|
||||
|
||||
/**
|
||||
* GamePage Page
|
||||
*/
|
||||
public class GamePage extends Page {
|
||||
|
||||
private TTContainer gameContainer;
|
||||
private TTGameBox gameBox;
|
||||
private GameController gameController;
|
||||
public Timer timer;
|
||||
|
||||
/*Spawnig points fixed and not modifiable*/
|
||||
/*Spawning points fixed and not modifiable*/
|
||||
private final DefaultListModel<Point> letterSpawningPoints = setLetterSpawningPoint();
|
||||
|
||||
/*Available spawning points*/
|
||||
private DefaultListModel<Point> availableLetterSpawningPoint = new DefaultListModel<>();
|
||||
|
||||
public GamePage(JFrame window) throws IOException {
|
||||
|
||||
super(window);
|
||||
gameController = new GameController(this);
|
||||
availableLetterSpawningPoint.clear();
|
||||
|
|
@ -89,11 +89,12 @@ public class GamePage extends Page {
|
|||
|
||||
public void showLetters(){
|
||||
|
||||
/* Place letters in a available random spawning point */
|
||||
for(int i = 0; i < gameController.getLetters().size()-1; i++){
|
||||
/* Place letters in an available random spawning point */
|
||||
DefaultListModel<String> letters = game.getLetters();
|
||||
for(int i = 0; i < letters.size()-1; i++){
|
||||
new TTLetter(
|
||||
occupyRandomPosition(),
|
||||
gameController.getLetters().get(i),
|
||||
letters.get(i),
|
||||
gameContainer
|
||||
);
|
||||
}
|
||||
|
|
@ -115,11 +116,11 @@ public class GamePage extends Page {
|
|||
root
|
||||
);
|
||||
|
||||
gameBox = new TTGameBox(
|
||||
new TTGameBox(
|
||||
new Point(150, 90),
|
||||
new Dimension(250, 40),
|
||||
"Insert word and Press ENTER!",
|
||||
gameController.getWords(),
|
||||
game.getWords(),
|
||||
gameContainer
|
||||
);
|
||||
|
||||
|
|
|
|||
|
|
@ -15,7 +15,6 @@ public class HighscoresPage extends Page{
|
|||
|
||||
private TTContainer highscoreContainer;
|
||||
private Boolean isPartialScore;
|
||||
private TTScrollList highscoreList;
|
||||
public JFrame window;
|
||||
private HighscoresController highscoreController;
|
||||
|
||||
|
|
@ -30,7 +29,7 @@ public class HighscoresPage extends Page{
|
|||
}
|
||||
|
||||
public void showHighscoreList(){
|
||||
highscoreList = new TTScrollList(
|
||||
new TTScrollList(
|
||||
new Point(20, 60),
|
||||
new Dimension(515, 142),
|
||||
highscoreController.getRanks(isPartialScore),
|
||||
|
|
|
|||
|
|
@ -81,7 +81,7 @@ public class HomePage extends Page {
|
|||
new TTLabelBtn(
|
||||
new Point(360, 200),
|
||||
new Dimension(210, 50),
|
||||
"RegisterPage!",
|
||||
"Register!",
|
||||
new Font(Palette.inputBox_font.getFontName(), Font.BOLD, 34),
|
||||
null,
|
||||
new Callable<Object>() {
|
||||
|
|
|
|||
|
|
@ -22,7 +22,6 @@ public class MatchRequestsPage extends Page{
|
|||
window.setVisible(true);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void createUIComponents() throws IOException {
|
||||
addLogo(root);
|
||||
|
|
@ -56,7 +55,6 @@ public class MatchRequestsPage extends Page{
|
|||
super.mouseClicked(evt);
|
||||
JList thisList = (JList)evt.getSource();
|
||||
if (evt.getClickCount() == 2) {
|
||||
// Double-click detected
|
||||
int index = thisList.locationToIndex(evt.getPoint());
|
||||
App.game.joinMatch(App.game.pendingList.get(index));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -57,7 +57,7 @@ public class MatchSetupPage extends Page{
|
|||
new Callable<Object>() {
|
||||
@Override
|
||||
public Object call() throws Exception {
|
||||
//If server response ok, start play, else error
|
||||
//If notificationServer response ok, start play, else error
|
||||
return matchSetupController.play(searchUserBar.list);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -100,7 +100,7 @@ public class MenuPage extends Page{
|
|||
new Callable<Object>() {
|
||||
@Override
|
||||
public Object call() throws Exception {
|
||||
menuController.logout(App.session.account.userName, App.game.stub);
|
||||
menuController.logout(App.session.account.userName, App.game.notificationStub);
|
||||
return new HomePage(Page.window);
|
||||
}
|
||||
},
|
||||
|
|
|
|||
|
|
@ -57,12 +57,12 @@ public class Page {
|
|||
|
||||
public void addBack(TTContainer parent, Callable<Object> clickHandler) {
|
||||
try {
|
||||
TTImageBtn back = new TTImageBtn(
|
||||
new Point(0, 0),
|
||||
new Dimension(50, 50),
|
||||
new ImageIcon(new File("./Client/resources/images/back.png").getCanonicalPath()),
|
||||
clickHandler,
|
||||
parent);
|
||||
new TTImageBtn(
|
||||
new Point(0, 0),
|
||||
new Dimension(50, 50),
|
||||
new ImageIcon(new File("./Client/resources/images/back.png").getCanonicalPath()),
|
||||
clickHandler,
|
||||
parent);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
|
@ -70,11 +70,11 @@ public class Page {
|
|||
|
||||
public void addFooter(TTContainer root) {
|
||||
footer = new TTContainer(
|
||||
null,
|
||||
new Dimension(1150, 60),
|
||||
Palette.root_backgroundColor,
|
||||
-1,
|
||||
root);
|
||||
null,
|
||||
new Dimension(1150, 60),
|
||||
Palette.root_backgroundColor,
|
||||
-1,
|
||||
root);
|
||||
}
|
||||
|
||||
public void addNext(TTContainer parent, String caption, Callable<Object> clickHandler) {
|
||||
|
|
|
|||
|
|
@ -36,7 +36,7 @@ public class RegisterPage extends Page {
|
|||
TTLabel registerText = new TTLabel(
|
||||
new Point(70,35),
|
||||
new Dimension(400,40),
|
||||
"<html><h2>Insert your datas and press RegisterPage!</h2></html>",
|
||||
"Insert your datas and press Register!",
|
||||
new Font(Palette.inputBox_font.getFontName(), Font.ITALIC, 12),
|
||||
null,
|
||||
registerDataContainer
|
||||
|
|
@ -58,11 +58,10 @@ public class RegisterPage extends Page {
|
|||
TTButton register = new TTButton(
|
||||
new Point(70,150),
|
||||
new Dimension(430,50),
|
||||
"RegisterPage!",
|
||||
"Register!",
|
||||
new Callable<Object>() {
|
||||
@Override
|
||||
public Object call() throws Exception {
|
||||
//TODO CHIAMA API PER LOGIN E SE TUTTO OKEY MANDA A PAGINA DEL MENU
|
||||
Response res = registerController.register(usernameField.getText(), String.valueOf(passwordField.getPassword()));
|
||||
if (res.code == 200){
|
||||
return new TTDialog("success", res.message,
|
||||
|
|
|
|||
|
|
@ -1,17 +1,13 @@
|
|||
package com.texttwist.client.services;
|
||||
import com.texttwist.client.App;
|
||||
import com.texttwist.client.models.Game;
|
||||
import constants.Config;
|
||||
import interfaces.IAuth;
|
||||
import interfaces.INotificationClient;
|
||||
import interfaces.INotificationServer;
|
||||
import models.Response;
|
||||
import java.net.MalformedURLException;
|
||||
import java.rmi.Naming;
|
||||
import java.rmi.NotBoundException;
|
||||
import java.rmi.RemoteException;
|
||||
import java.rmi.registry.LocateRegistry;
|
||||
import java.rmi.registry.Registry;
|
||||
import java.rmi.server.UnicastRemoteObject;
|
||||
|
||||
/**
|
||||
|
|
@ -23,13 +19,9 @@ public class AuthService {
|
|||
|
||||
public Response login(String userName, String password) throws RemoteException, NotBoundException, MalformedURLException {
|
||||
try {
|
||||
|
||||
/* si registra per la callback */
|
||||
System.out.println("Registering for callback");
|
||||
INotificationClient callbackObj = new NotificationClient();
|
||||
App.game.stub = (INotificationClient) UnicastRemoteObject.exportObject(callbackObj, 0);
|
||||
|
||||
App.game.server.registerForCallback(App.game.stub);
|
||||
App.game.notificationStub = (INotificationClient) UnicastRemoteObject.exportObject(callbackObj, 0);
|
||||
App.game.notificationServer.registerForCallback(App.game.notificationStub);
|
||||
} catch (RemoteException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,39 +0,0 @@
|
|||
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();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -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() );
|
||||
Logger.write("Invoked invitation with username =" + userName + "|" + users.toString() );
|
||||
|
||||
if(App.session != null) {
|
||||
if (users.contains(App.session.account.userName)) {
|
||||
Logger.write(userName + " ti ha sfidato!");
|
||||
Logger.write(userName + " send a invitation!");
|
||||
App.game.newMatch(userName);
|
||||
} else {
|
||||
Logger.write("L'utente è sloggato");
|
||||
Logger.write("User " + userName + " is slogged");
|
||||
}
|
||||
}
|
||||
return null;
|
||||
|
|
|
|||
|
|
@ -25,9 +25,9 @@ public class StartGame extends SwingWorker<Void,Void> {
|
|||
|
||||
@Override
|
||||
public Void doInBackground(){
|
||||
App.game.isStarted=true;
|
||||
App.game.gameIsStarted =true;
|
||||
//Mostra pannello di conferma che le lettere sono tutte arrivate
|
||||
new TTDialog("success", "GamePage is ready. Press OK to start!",
|
||||
new TTDialog("success", "Game is ready. Press OK to start!",
|
||||
new Callable() {
|
||||
@Override
|
||||
public Object call() throws Exception {
|
||||
|
|
|
|||
|
|
@ -50,7 +50,7 @@ public class WaitForPlayers extends SwingWorker<DefaultListModel<String>,Default
|
|||
|
||||
if (line.startsWith("MESSAGE")) {
|
||||
buffer.clear();
|
||||
System.out.println("Mi arriva questo dal server " + line);
|
||||
System.out.println("Mi arriva questo dal notificationServer " + line);
|
||||
|
||||
Message msg = Message.toMessage(line);
|
||||
System.out.println(msg.message);
|
||||
|
|
|
|||
|
|
@ -9,7 +9,6 @@ import javax.swing.*;
|
|||
import java.io.IOException;
|
||||
import java.net.DatagramPacket;
|
||||
import java.net.InetAddress;
|
||||
import java.net.MulticastSocket;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
/**
|
||||
|
|
@ -69,7 +68,7 @@ public class WaitForScore extends SwingWorker<Void,Void> {
|
|||
e.printStackTrace();
|
||||
}
|
||||
App.game.multicastSocket.close();
|
||||
App.game.isStarted=false;
|
||||
App.game.gameIsStarted =false;
|
||||
try {
|
||||
this.callback.execute();
|
||||
} catch (Exception e) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue