Refactoring App

This commit is contained in:
Lorenzo Iovino 2017-07-12 17:57:33 +02:00
parent e2070cf597
commit 97c88c46bb
24 changed files with 517 additions and 442 deletions

View file

@ -1,53 +1,51 @@
package com.texttwist.client;
import com.texttwist.client.services.GameService;
import com.texttwist.client.services.AuthService;
import com.texttwist.client.pages.HomePage;
import com.texttwist.client.models.Game;
import com.texttwist.client.services.NotificationClient;
import constants.Config;
import interfaces.INotificationClient;
import interfaces.INotificationServer;
import models.Session;
import utilities.Logger;
import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
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.lang.management.ManagementFactory;
/**
* Created by loke on 13/06/2017.
* Author: Lorenzo Iovino on 13/06/2017.
* Description: The App entrypoint.
* Here is possible to declare services globally accessible.
*/
public class App extends JFrame {
public static AuthService authService;
public static Session session;
public static Game game;
public static JFrame app;
public static GameService gameService;
public static Logger logger;
public static Session session;
public static JFrame app;
public App() throws IOException {
setPreferredSize( new Dimension( 640, 480 ));
setSize(new Dimension(640,480));
setLocation(100,100);
setResizable( false );
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
long id = Thread.currentThread().getId();
/*Setup logger*/
String id = ManagementFactory.getRuntimeMXBean().getName();
logger = new Logger(new File("./client_"+id+".log"), "Client"+id, true);
logger.write("Client starting ...");
//Init models
game = new Game();
/*Load fonts*/
try {
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) {
logger.write("APP: Font not found!");
}
//Init services
/*Services*/
gameService = new GameService();
authService = new AuthService();
app = this;

View file

@ -1,24 +1,14 @@
package com.texttwist.client;
import java.awt.*;
import java.io.File;
import java.io.IOException;
/**
* Author: Lorenzo Iovino on 13/06/2017.
* Description: Main
*/
public class Main {
public static void main(String[] args) throws IOException {
System.out.println("Client started");
//Load fonts
try {
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("ERROR: Font not found!");
}
App entrypoint = new App();
new App();
}
}

View file

@ -1,4 +1,5 @@
package com.texttwist.client.controllers;
import com.texttwist.client.App;
import com.texttwist.client.pages.GamePage;
import com.texttwist.client.tasks.SendWords;
@ -8,7 +9,8 @@ import com.texttwist.client.tasks.WaitForScore;
import javax.swing.*;
/**
* GamePage Controller
* Author: Lorenzo Iovino on 27/06/2017.
* Description: Controller of the Game Page
*/
public class GameController {
@ -27,11 +29,11 @@ public class GameController {
}
public SwingWorker sendWords(SwingWorker callback){
return new SendWords(App.game.words, waitForScore(callback));
return new SendWords(App.gameService.words, waitForScore(callback));
}
public SwingWorker startGame() {
return new StartGame(App.game.letters, game);
return new StartGame(App.gameService.letters, game);
}
}

View file

@ -1,13 +1,14 @@
package com.texttwist.client.controllers;
import com.texttwist.client.App;
import com.texttwist.client.pages.HighscoresPage;
import com.texttwist.client.tasks.FetchHighscore;
import javafx.util.Pair;
import javax.swing.*;
/**
* Highscores Controller
* Author: Lorenzo Iovino on 27/06/2017.
* Description: Controller of the Highscore Page
*/
public class HighscoresController {
@ -22,7 +23,7 @@ public class HighscoresController {
}
public DefaultListModel<Pair<String,Integer>> getRanks(Boolean isPartialRank) {
return isPartialRank ? App.game.ranks : App.game.globalRanks;
return isPartialRank ? App.gameService.ranks : App.gameService.globalRanks;
}
}

View file

@ -1,15 +1,16 @@
package com.texttwist.client.controllers;
import com.texttwist.client.App;
import models.Response;
import models.Session;
import models.User;
import java.net.MalformedURLException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
/**
* Created by loke on 15/06/2017.
* Author: Lorenzo Iovino on 20/06/2017.
* Description: Controller of the Home Page
*/
public class HomeController {

View file

@ -1,22 +1,21 @@
package com.texttwist.client.controllers;
import com.texttwist.client.App;
import javax.swing.*;
import java.io.IOException;
/**
* Created by loke on 18/06/2017.
* Author: Lorenzo Iovino on 18/06/2017.
* Description: Controller of the Match Setup Page
*/
public class MatchSetupController {
public Object play(DefaultListModel<String> userNames) {
try {
return App.game.play(userNames);
return App.gameService.play(userNames);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}

View file

@ -3,21 +3,20 @@ package com.texttwist.client.controllers;
import com.texttwist.client.App;
import interfaces.INotificationClient;
import models.Response;
import java.net.MalformedURLException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
/**
* Created by loke on 17/06/2017.
* Author: Lorenzo Iovino on 17/06/2017.
* Description: Controller of the Menu Page
*/
public class MenuController {
public Response logout(String userName, INotificationClient stub) throws RemoteException, NotBoundException, MalformedURLException {
public void logout(String userName, INotificationClient stub) throws RemoteException, NotBoundException, MalformedURLException {
Response res = App.authService.logout(userName, stub);
if (res.code == 200){
App.session = null;
}
return res;
}
}

View file

@ -2,13 +2,13 @@ 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.
* Author: Lorenzo Iovino on 17/06/2017.
* Description: Controller of the Register Page
*/
public class RegisterController {

View file

@ -8,7 +8,7 @@ import java.awt.*;
import java.io.IOException;
import java.util.concurrent.*;
import static com.texttwist.client.App.game;
import static com.texttwist.client.App.gameService;
/**
* GamePage Page
@ -90,7 +90,7 @@ public class GamePage extends Page {
public void showLetters(){
/* Place letters in an available random spawning point */
DefaultListModel<String> letters = game.getLetters();
DefaultListModel<String> letters = gameService.getLetters();
for(int i = 0; i < letters.size()-1; i++){
new TTLetter(
occupyRandomPosition(),
@ -120,7 +120,7 @@ public class GamePage extends Page {
new Point(150, 90),
new Dimension(250, 40),
"Insert word and Press ENTER!",
game.getWords(),
gameService.getWords(),
gameContainer
);

View file

@ -45,7 +45,7 @@ public class MatchRequestsPage extends Page{
TTScrollList pendingMatches = new TTScrollList(
new Point(20, 60),
new Dimension(520, 142),
App.game.pendingList,
App.gameService.pendingList,
matchsContainer
);
@ -56,7 +56,7 @@ public class MatchRequestsPage extends Page{
JList thisList = (JList)evt.getSource();
if (evt.getClickCount() == 2) {
int index = thisList.locationToIndex(evt.getPoint());
App.game.joinMatch(App.game.pendingList.get(index));
App.gameService.joinMatch(App.gameService.pendingList.get(index));
}
}
});

View file

@ -48,7 +48,7 @@ public class MenuPage extends Page{
TTButton newMatch = new TTButton(
new Point(25,70),
new Dimension(250,75),
"New Game!",
"New GameService!",
new Callable<Object>() {
@Override
public Object call() throws Exception {
@ -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.notificationStub);
menuController.logout(App.session.account.userName, App.gameService.notificationStub);
return new HomePage(Page.window);
}
},

View file

@ -20,8 +20,8 @@ public class AuthService {
public Response login(String userName, String password) throws RemoteException, NotBoundException, MalformedURLException {
try {
INotificationClient callbackObj = new NotificationClient();
App.game.notificationStub = (INotificationClient) UnicastRemoteObject.exportObject(callbackObj, 0);
App.game.notificationServer.registerForCallback(App.game.notificationStub);
App.gameService.notificationStub = (INotificationClient) UnicastRemoteObject.exportObject(callbackObj, 0);
App.gameService.notificationServer.registerForCallback(App.gameService.notificationStub);
} catch (RemoteException e) {
e.printStackTrace();
}

View file

@ -1,4 +1,4 @@
package com.texttwist.client.models;
package com.texttwist.client.services;
import com.texttwist.client.App;
import com.texttwist.client.pages.GamePage;
@ -27,7 +27,7 @@ import java.util.concurrent.*;
/**
* Created by loke on 18/06/2017.
*/
public class Game {
public class GameService {
public Integer multicastId = 0 ;
public DefaultListModel<String> pendingList = new DefaultListModel<String>();
@ -43,7 +43,7 @@ public class Game {
private ByteBuffer buffer = ByteBuffer.allocate(1024);
public Game(){
public GameService(){
Registry registry = null;
try {
@ -74,13 +74,13 @@ public class Game {
e.printStackTrace();
}
if(!App.game.gameIsStarted) {
if(!App.gameService.gameIsStarted) {
//Show invitation popup
new TTDialog("success", "New invitation from: " + userName + "!",
new Callable() {
@Override
public Object call() throws Exception {
App.game.joinMatch(userName);
App.gameService.joinMatch(userName);
return null;
}
},
@ -95,11 +95,11 @@ public class Game {
public DefaultListModel<String> getLetters(){
return App.game.letters;
return App.gameService.letters;
}
public DefaultListModel<String> getWords() {
return App.game.words;
return App.gameService.words;
}
public void setLetters(DefaultListModel<String> letters){
@ -133,12 +133,12 @@ public class Game {
}
public Void start(){
App.game.gameIsStarted = true;
App.gameService.gameIsStarted = true;
return null;
}
public Void stop(){
App.game.gameIsStarted = false;
App.gameService.gameIsStarted = false;
return null;
}

View file

@ -2,7 +2,6 @@ package com.texttwist.client.services;
import com.texttwist.client.App;
import interfaces.INotificationClient;
import models.Response;
import utilities.Logger;
import javax.swing.*;
import java.rmi.RemoteException;
@ -23,7 +22,7 @@ public class NotificationClient implements INotificationClient {
if(App.session != null) {
if (users.contains(App.session.account.userName)) {
App.logger.write(userName + " send a invitation!");
App.game.newMatch(userName);
App.gameService.newMatch(userName);
} else {
App.logger.write("User " + userName + " is slogged");
}

View file

@ -9,7 +9,6 @@ import javax.swing.*;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.util.*;
/**
* Job: FetchHighscore
@ -22,7 +21,7 @@ public class FetchHighscore extends SwingWorker<Void,Void> {
HighscoresPage highscoresPage;
public FetchHighscore(HighscoresPage highscoresPage){
this.socketChannel = App.game.clientSocket;
this.socketChannel = App.gameService.clientSocket;
this.highscoresPage = highscoresPage;
}
@ -67,7 +66,7 @@ public class FetchHighscore extends SwingWorker<Void,Void> {
}
public void done(){
App.game.globalRanks = globalRanks;
App.gameService.globalRanks = globalRanks;
this.highscoresPage.showHighscoreList();
}
}

View file

@ -23,9 +23,9 @@ public class StartGame extends SwingWorker<Void,Void> {
@Override
public Void doInBackground(){
App.game.start();
App.gameService.start();
//Mostra pannello di conferma che le lettere sono tutte arrivate
new TTDialog("success", "Game is ready. Press OK to start!",
new TTDialog("success", "GameService is ready. Press OK to start!",
new Callable() {
@Override
public Object call() throws Exception {

View file

@ -6,7 +6,6 @@ import com.texttwist.client.pages.Page;
import com.texttwist.client.ui.TTDialog;
import constants.Config;
import models.Message;
import org.omg.PortableInterceptor.SYSTEM_EXCEPTION;
import javax.swing.*;
import java.io.IOException;
@ -31,7 +30,7 @@ public class WaitForPlayers extends SwingWorker<DefaultListModel<String>,Default
public WaitForPlayers(SwingWorker callback) {
this.callback = callback;
this.words = words;
this.socketChannel = App.game.clientSocket;
this.socketChannel = App.gameService.clientSocket;
}
@Override
@ -97,11 +96,11 @@ public class WaitForPlayers extends SwingWorker<DefaultListModel<String>,Default
data= msg.data;
Integer multicastId = Integer.valueOf(data.remove(data.size()-2));
App.game.setMulticastId(multicastId);
App.gameService.setMulticastId(multicastId);
App.game.multicastSocket = new MulticastSocket(App.game.multicastId);
App.gameService.multicastSocket = new MulticastSocket(App.gameService.multicastId);
InetAddress ia = InetAddress.getByName(Config.ScoreMulticastServerURI);
App.game.multicastSocket.joinGroup(ia);
App.gameService.multicastSocket.joinGroup(ia);
letters = msg.data;
@ -129,7 +128,7 @@ public class WaitForPlayers extends SwingWorker<DefaultListModel<String>,Default
public void done(){
if(!joinTimeout) {
try {
App.game.setLetters(letters);
App.gameService.setLetters(letters);
this.callback.execute();
} catch (Exception e) {
e.printStackTrace();

View file

@ -29,7 +29,7 @@ public class WaitForScore extends SwingWorker<Void,Void> {
while(true) {
byte[] buf = new byte[1024];
DatagramPacket receivedDatagram = new DatagramPacket(buf, buf.length);
App.game.multicastSocket.receive(receivedDatagram);
App.gameService.multicastSocket.receive(receivedDatagram);
String s = new String(receivedDatagram.getData());
Message msg = Message.toMessage(s);
@ -37,8 +37,8 @@ public class WaitForScore extends SwingWorker<Void,Void> {
//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);
App.gameService.ranks.clear();
App.gameService.ranks = utilities.Parse.score(msg.data);
}
break;
}
@ -55,11 +55,11 @@ public class WaitForScore extends SwingWorker<Void,Void> {
public void done(){
try {
//Leave group and close multicast socket
App.game.multicastSocket.leaveGroup(InetAddress.getByName(Config.ScoreMulticastServerURI));
App.game.multicastSocket.close();
App.gameService.multicastSocket.leaveGroup(InetAddress.getByName(Config.ScoreMulticastServerURI));
App.gameService.multicastSocket.close();
//Stop game
App.game.stop();
//Stop gameService
App.gameService.stop();
//Call callback
this.callback.execute();