This commit is contained in:
Lorenzo Iovino 2017-07-15 04:32:35 +02:00
parent 44a9e43cd4
commit 8eab4d97c1
23 changed files with 714 additions and 11795 deletions

1055
.idea/workspace.xml generated

File diff suppressed because it is too large Load diff

View file

@ -1,6 +1,8 @@
package com.texttwist.client.controllers; package com.texttwist.client.controllers;
import com.texttwist.client.App; import com.texttwist.client.App;
import com.texttwist.client.tasks.JoinMatch;
import javax.swing.*; import javax.swing.*;
/** /**
@ -14,6 +16,6 @@ public class MatchRequestController {
} }
public void joinMatch(String matchName){ public void joinMatch(String matchName){
App.gameService.joinMatch(matchName); new JoinMatch(matchName).execute();
} }
} }

View file

@ -1,5 +1,7 @@
package com.texttwist.client.controllers; package com.texttwist.client.controllers;
import com.texttwist.client.tasks.InvitePlayers;
import javax.swing.*; import javax.swing.*;
import static com.texttwist.client.App.gameService; import static com.texttwist.client.App.gameService;
@ -10,6 +12,7 @@ import static com.texttwist.client.App.gameService;
public class MatchSetupController { public class MatchSetupController {
public void play(DefaultListModel<String> userNames) { public void play(DefaultListModel<String> userNames) {
gameService.beginMatch(userNames); new InvitePlayers(userNames).execute();
} }
} }

View file

@ -58,7 +58,7 @@ public class MatchSetupPage extends Page{
new Callable<Object>() { new Callable<Object>() {
@Override @Override
public Void call() throws Exception { public Void call() throws Exception {
//If notificationServer response ok, start beginMatch, else error //If notificationServer response ok, start newMatch, else error
matchSetupController.play(searchUserBar.list); matchSetupController.play(searchUserBar.list);
return null; return null;
} }

View file

@ -5,6 +5,7 @@ import com.texttwist.client.pages.GamePage;
import com.texttwist.client.pages.MenuPage; import com.texttwist.client.pages.MenuPage;
import com.texttwist.client.pages.Page; import com.texttwist.client.pages.Page;
import com.texttwist.client.tasks.InvitePlayers; import com.texttwist.client.tasks.InvitePlayers;
import com.texttwist.client.tasks.JoinMatch;
import com.texttwist.client.ui.TTDialog; import com.texttwist.client.ui.TTDialog;
import javafx.util.Pair; import javafx.util.Pair;
import models.Message; import models.Message;
@ -26,10 +27,10 @@ public class GameService {
public DefaultListModel<String> letters = new DefaultListModel<>(); public DefaultListModel<String> letters = new DefaultListModel<>();
public DefaultListModel<Pair<String,Integer>> globalRanks = new DefaultListModel<>(); public DefaultListModel<Pair<String,Integer>> globalRanks = new DefaultListModel<>();
public DefaultListModel<Pair<String,Integer>> ranks = new DefaultListModel<>(); public DefaultListModel<Pair<String,Integer>> ranks = new DefaultListModel<>();
private Boolean gameIsStarted = false; public Boolean gameIsStarted = false;
public Boolean isWaiting = false; public Boolean isWaiting = false;
private void addToPendingList(String username) throws IOException { public void addToPendingList(String username) throws IOException {
pendingList.addElement(username); pendingList.addElement(username);
} }
@ -37,33 +38,6 @@ public class GameService {
App.openClientTCPSocket(); App.openClientTCPSocket();
} }
public void newMatch(String userName) {
//Add to pending invites list
try {
this.addToPendingList(userName);
} catch (IOException e) {
e.printStackTrace();
}
if(!App.gameService.gameIsStarted && !App.gameService.isWaiting) {
//Show invite popup
new TTDialog("success", "New invite from: " + userName + "!",
new Callable() {
@Override
public Object call() throws Exception {
App.gameService.joinMatch(userName);
return null;
}
},
new Callable() {
@Override
public Object call() throws Exception {
return new MenuPage(Page.window);
}
});
}
}
public DefaultListModel<String> getLetters(){ public DefaultListModel<String> getLetters(){
return App.gameService.letters; return App.gameService.letters;
} }
@ -76,32 +50,6 @@ public class GameService {
this.letters = letters; this.letters = letters;
} }
public void joinMatch(String matchName) {
//Clear pending invitation list and join selected match
if(!gameIsStarted) {
this.pendingList.clear();
try {
DefaultListModel<String> matchNames = new DefaultListModel<>();
matchNames.addElement(matchName);
Message message = new Message("JOIN_GAME", App.session.account.userName, App.session.token, matchNames);
byte[] byteMessage = message.toString().getBytes();
ByteBuffer buffer = ByteBuffer.wrap(byteMessage);
App.clientTCP.write(buffer);
new GamePage(Page.window);
} catch (IOException e) {
e.printStackTrace();
}
}
}
//Start game and wait for other players
public void beginMatch(DefaultListModel<String> userNames) {
new InvitePlayers(userNames).execute();
}
public void start(){ public void start(){
gameIsStarted = true; gameIsStarted = true;
} }

View file

@ -1,6 +1,7 @@
package com.texttwist.client.services; package com.texttwist.client.services;
import com.texttwist.client.App; import com.texttwist.client.App;
import com.texttwist.client.tasks.BeginMatch;
import interfaces.INotificationClient; import interfaces.INotificationClient;
import javax.swing.*; import javax.swing.*;
import java.rmi.RemoteException; import java.rmi.RemoteException;
@ -18,7 +19,7 @@ public class NotificationClientService implements INotificationClient {
if(App.session != null) { if(App.session != null) {
if (users.contains(App.session.account.userName)) { if (users.contains(App.session.account.userName)) {
App.logger.write(userName + " send a invitation!"); App.logger.write(userName + " send a invitation!");
App.gameService.newMatch(userName); new BeginMatch(userName).execute();
} else { } else {
App.logger.write("User " + userName + " is slogged"); App.logger.write("User " + userName + " is slogged");
} }

View file

@ -0,0 +1,52 @@
package com.texttwist.client.tasks;
import com.texttwist.client.App;
import com.texttwist.client.pages.MenuPage;
import com.texttwist.client.pages.Page;
import com.texttwist.client.ui.TTDialog;
import javax.swing.*;
import java.io.IOException;
import java.util.concurrent.Callable;
/**
* Author: Lorenzo Iovino on 15/07/2017.
* Description: Task: BeginMatch
*
*/
public class BeginMatch extends SwingWorker<Void,Void> {
private String userName;
public BeginMatch(String userName) {
this.userName = userName;
}
@Override
public Void doInBackground() {
//Add to pending invites list
try {
App.gameService.addToPendingList(userName);
} catch (IOException e) {
e.printStackTrace();
}
if(!App.gameService.gameIsStarted && !App.gameService.isWaiting) {
//Show invite popup
new TTDialog("success", "New invite from: " + userName + "!",
new Callable() {
@Override
public Object call() throws Exception {
new JoinMatch(userName).execute();
return null;
}
},
new Callable() {
@Override
public Object call() throws Exception {
return new MenuPage(Page.window);
}
});
}
return null;
}
}

View file

@ -0,0 +1,47 @@
package com.texttwist.client.tasks;
import com.texttwist.client.App;
import com.texttwist.client.pages.GamePage;
import com.texttwist.client.pages.Page;
import models.Message;
import javax.swing.*;
import java.io.IOException;
import java.nio.ByteBuffer;
/**
* Author: Lorenzo Iovino on 25/06/2017.
* Description: Task: JoinMatch
*
*/
public class JoinMatch extends SwingWorker<Void,Void> {
private String matchName;
public JoinMatch(String matchName) {
this.matchName = matchName;
}
@Override
public Void doInBackground() {
//Clear pending invitation list and join selected match
if(!App.gameService.gameIsStarted) {
App.gameService.pendingList.clear();
try {
DefaultListModel<String> matchNames = new DefaultListModel<>();
matchNames.addElement(matchName);
Message message = new Message("JOIN_GAME", App.session.account.userName, App.session.token, matchNames);
byte[] byteMessage = message.toString().getBytes();
ByteBuffer buffer = ByteBuffer.wrap(byteMessage);
App.clientTCP.write(buffer);
new GamePage(Page.window);
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
}

View file

@ -79,6 +79,7 @@ public class WaitForPlayers extends SwingWorker<Void,Void> {
if(msg.data !=null ) { if(msg.data !=null ) {
DefaultListModel<String> data = msg.data; DefaultListModel<String> data = msg.data;
App.openClientMulticastSocket(Integer.valueOf(data.remove(data.size()-2))); App.openClientMulticastSocket(Integer.valueOf(data.remove(data.size()-2)));
App.gameService.setLetters(msg.data); App.gameService.setLetters(msg.data);
break; break;

View file

@ -1,5 +1,6 @@
package interfaces; package interfaces;
import javax.swing.*;
import java.rmi.Remote; import java.rmi.Remote;
import java.rmi.RemoteException; import java.rmi.RemoteException;
@ -10,4 +11,5 @@ import java.rmi.RemoteException;
public interface INotificationServer extends Remote { public interface INotificationServer extends Remote {
void registerForCallback (INotificationClient ClientInterface) throws RemoteException; void registerForCallback (INotificationClient ClientInterface) throws RemoteException;
void unregisterForCallback (INotificationClient ClientInterface) throws RemoteException; void unregisterForCallback (INotificationClient ClientInterface) throws RemoteException;
void sendInvitations(String username, DefaultListModel<String> users) throws RemoteException;
} }

View file

@ -1,5 +1,6 @@
package com.texttwist.server.services; package com.texttwist.server.managers;
import com.texttwist.server.services.JedisService;
import models.User; import models.User;
import java.io.Serializable; import java.io.Serializable;
import java.util.*; import java.util.*;
@ -7,21 +8,21 @@ import java.util.*;
/** /**
* Author: Lorenzo Iovino on 18/06/2017. * Author: Lorenzo Iovino on 18/06/2017.
* Description: AccountsService * Description: AccountsManager
*/ */
public class AccountsService { public class AccountsManager {
public List<User> users = Collections.synchronizedList(new ArrayList<User>()); public List<User> users = Collections.synchronizedList(new ArrayList<User>());
private static class Holder { private static class Holder {
static final AccountsService INSTANCE = new AccountsService(); static final AccountsManager INSTANCE = new AccountsManager();
} }
public static AccountsService getInstance() { public static AccountsManager getInstance() {
return AccountsService.Holder.INSTANCE; return AccountsManager.Holder.INSTANCE;
} }
private AccountsService(){ private AccountsManager(){
List<Serializable> l = JedisService.get("users"); List<Serializable> l = JedisService.get("users");
for(int i=0; i<l.size(); i++) { for(int i=0; i<l.size(); i++) {
users.add((User) l.get(i)); users.add((User) l.get(i));

View file

@ -1,26 +1,25 @@
package com.texttwist.server.services; package com.texttwist.server.managers;
import models.Session; import models.Session;
import models.User; import models.User;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Iterator;
import java.util.List; import java.util.List;
/** /**
* Author: Lorenzo Iovino on 17/06/2017. * Author: Lorenzo Iovino on 17/06/2017.
* Description: SessionsService. It is a singleton that provides the model and methods for manage sessions * Description: SessionsManager. It is a singleton that provides the model and methods for manage sessions
*/ */
public class SessionsService { public class SessionsManager {
private List<Session> sessions = Collections.synchronizedList(new ArrayList<Session>()); private List<Session> sessions = Collections.synchronizedList(new ArrayList<Session>());
private static class Holder { private static class Holder {
static final SessionsService INSTANCE = new SessionsService(); static final SessionsManager INSTANCE = new SessionsManager();
} }
public static SessionsService getInstance() { public static SessionsManager getInstance() {
return Holder.INSTANCE; return Holder.INSTANCE;
} }

View file

@ -1,7 +1,7 @@
package com.texttwist.server.models; package com.texttwist.server.models;
import com.texttwist.server.services.MessageService; import com.texttwist.server.services.MessageService;
import com.texttwist.server.tasks.MatchTimeout; import com.texttwist.server.tasks.TimeoutMatch;
import javafx.util.Pair; import javafx.util.Pair;
import javax.swing.*; import javax.swing.*;
@ -99,7 +99,7 @@ public class Match {
public void startGame(){ public void startGame(){
this.started = true; this.started = true;
matchTimeoutThread.submit(new MatchTimeout(this)); matchTimeoutThread.submit(new TimeoutMatch(this));
} }
public void setScore(String player, Integer score){ public void setScore(String player, Integer score){

View file

@ -1,7 +1,7 @@
package com.texttwist.server.proxies; package com.texttwist.server.proxies;
import com.texttwist.server.Server; import com.texttwist.server.Server;
import com.texttwist.server.services.SessionsService; import com.texttwist.server.managers.SessionsManager;
import com.texttwist.server.models.Match; import com.texttwist.server.models.Match;
import com.texttwist.server.tasks.*; import com.texttwist.server.tasks.*;
import models.Message; import models.Message;
@ -33,7 +33,7 @@ public class MessageDispatcher implements Callable<Boolean> {
public Boolean call() { public Boolean call() {
bufferMessage = ByteBuffer.allocate(1024); bufferMessage = ByteBuffer.allocate(1024);
byte[] byteMessage = null; byte[] byteMessage = null;
if(SessionsService.getInstance().isValidToken(request.token)){ if(SessionsManager.getInstance().isValidToken(request.token)){
switch(request.message){ switch(request.message){
case "START_GAME": case "START_GAME":
@ -68,7 +68,7 @@ public class MessageDispatcher implements Callable<Boolean> {
} }
//Starts to wait until all player joins //Starts to wait until all player joins
Future<Boolean> joinTimeout = threadPool.submit(new JoinTimeout(match)); Future<Boolean> joinTimeout = threadPool.submit(new TimeoutJoin(match));
Boolean joinTimeoutRes = joinTimeout.get(); Boolean joinTimeoutRes = joinTimeout.get();
//If joinTimeoutRes==true timeout happen, need to notify to all waiting clients //If joinTimeoutRes==true timeout happen, need to notify to all waiting clients
if(joinTimeoutRes){ if(joinTimeoutRes){

View file

@ -1,6 +1,8 @@
package com.texttwist.server.services; package com.texttwist.server.services;
import com.texttwist.server.Server; import com.texttwist.server.Server;
import com.texttwist.server.managers.AccountsManager;
import com.texttwist.server.managers.SessionsManager;
import constants.Config; import constants.Config;
import interfaces.IAuth; import interfaces.IAuth;
import interfaces.INotificationClient; import interfaces.INotificationClient;
@ -29,7 +31,7 @@ public class AuthService extends UnicastRemoteObject implements IAuth {
public Response register(String userName, String password) throws RemoteException { public Response register(String userName, String password) throws RemoteException {
Server.logger.write("Invoked register with username=" + userName + " AND " + " password=" + password); Server.logger.write("Invoked register with username=" + userName + " AND " + " password=" + password);
if ((userName != null && !userName.isEmpty()) && (password != null && !password.equals(""))) { if ((userName != null && !userName.isEmpty()) && (password != null && !password.equals(""))) {
if(AccountsService.getInstance().register(userName, password)){ if(AccountsManager.getInstance().register(userName, password)){
Server.logger.write("Registration successfull"); Server.logger.write("Registration successfull");
return new Response("Registration successfull", 200, null); return new Response("Registration successfull", 200, null);
} else { } else {
@ -44,11 +46,11 @@ public class AuthService extends UnicastRemoteObject implements IAuth {
public Response login(String userName, String password) throws RemoteException { public Response login(String userName, String password) throws RemoteException {
Server.logger.write("Invoked login with username=" + userName + " AND " + " password=" + password); Server.logger.write("Invoked login with username=" + userName + " AND " + " password=" + password);
if ((userName != null && !userName.isEmpty()) && (password != null && !password.equals(""))) { if ((userName != null && !userName.isEmpty()) && (password != null && !password.equals(""))) {
if(AccountsService.getInstance().exists(userName) && AccountsService.getInstance().checkPassword(userName, password)) { if(AccountsManager.getInstance().exists(userName) && AccountsManager.getInstance().checkPassword(userName, password)) {
JsonObject data = new JsonObject(); JsonObject data = new JsonObject();
String token = nextSessionId(); String token = nextSessionId();
data.put("token", token); data.put("token", token);
SessionsService.getInstance().add(userName,token); SessionsManager.getInstance().add(userName,token);
Server.logger.write("Login successfull"); Server.logger.write("Login successfull");
return new Response("Login successfull", 200, data); return new Response("Login successfull", 200, data);
} }
@ -63,14 +65,14 @@ public class AuthService extends UnicastRemoteObject implements IAuth {
notificationServer.unregisterForCallback(stub); notificationServer.unregisterForCallback(stub);
if ((userName != null && !userName.isEmpty()) && (token != null && !token.isEmpty())) { if ((userName != null && !userName.isEmpty()) && (token != null && !token.isEmpty())) {
boolean res = SessionsService.getInstance().remove(userName); boolean res = SessionsManager.getInstance().remove(userName);
if(res) { if(res) {
Server.logger.write("Logout successfull"); Server.logger.write("Logout successfull");
} }
} }
SessionsService.getInstance().remove(userName); SessionsManager.getInstance().remove(userName);
Server.logger.write("Logout successfull (but something gone wrong)"); Server.logger.write("Logout successfull (but something gone wrong)");
return new Response("Logout successfull (but something gone wrong)", 200, null); return new Response("Logout successfull (but something gone wrong)", 200, null);
} }

View file

@ -1,6 +1,7 @@
package com.texttwist.server.services; package com.texttwist.server.services;
import com.texttwist.server.Server; import com.texttwist.server.Server;
import com.texttwist.server.managers.SessionsManager;
import com.texttwist.server.models.Match; import com.texttwist.server.models.Match;
import com.texttwist.server.tasks.ComputeScore; import com.texttwist.server.tasks.ComputeScore;
import constants.Config; import constants.Config;
@ -47,7 +48,7 @@ public class ReceiveWordsService implements Runnable {
String rcv = new String(packet.getData()); String rcv = new String(packet.getData());
if (rcv.startsWith("MESSAGE")) { if (rcv.startsWith("MESSAGE")) {
msg = Message.toMessage(rcv); msg = Message.toMessage(rcv);
if(SessionsService.getInstance().isValidToken(msg.token)) { if(SessionsManager.getInstance().isValidToken(msg.token)) {
Match match = Match.findMatchByPlayerName(msg.sender); Match match = Match.findMatchByPlayerName(msg.sender);
threadPool.submit(new ComputeScore(msg.sender, msg.data, match)); threadPool.submit(new ComputeScore(msg.sender, msg.data, match));
} }

View file

@ -1,6 +1,7 @@
package com.texttwist.server.tasks; package com.texttwist.server.tasks;
import com.texttwist.server.services.SessionsService; import com.texttwist.server.managers.SessionsManager;
import javax.swing.*; import javax.swing.*;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
@ -18,7 +19,7 @@ public class CheckOnlineUsers implements Callable<Boolean> {
@Override @Override
public Boolean call() throws Exception { public Boolean call() throws Exception {
for(int i = 0; i < users.size(); i++){ for(int i = 0; i < users.size(); i++){
if(!(SessionsService.getInstance().exists(users.get(i)))){ if(!(SessionsManager.getInstance().exists(users.get(i)))){
return false; return false;
} }
} }

View file

@ -1,6 +1,6 @@
package com.texttwist.server.tasks; package com.texttwist.server.tasks;
import com.texttwist.server.services.AccountsService; import com.texttwist.server.managers.AccountsManager;
import com.texttwist.server.services.JedisService; import com.texttwist.server.services.JedisService;
import models.User; import models.User;
@ -18,16 +18,16 @@ public class ComputeHighscores implements Callable<DefaultListModel<String>> {
public DefaultListModel<String> call() throws Exception { public DefaultListModel<String> call() throws Exception {
DefaultListModel<String> l = new DefaultListModel<>(); DefaultListModel<String> l = new DefaultListModel<>();
AccountsService.getInstance().users.sort(new Comparator<User>() { AccountsManager.getInstance().users.sort(new Comparator<User>() {
@Override @Override
public int compare(User o1, User o2) { public int compare(User o1, User o2) {
return o2.score.compareTo(o1.score); return o2.score.compareTo(o1.score);
} }
}); });
JedisService.removeAll("users"); JedisService.removeAll("users");
for(int i = 0; i< AccountsService.getInstance().users.size(); i++){ for(int i = 0; i< AccountsManager.getInstance().users.size(); i++){
l.addElement(AccountsService.getInstance().users.get(i).userName+":"+ AccountsService.getInstance().users.get(i).score); l.addElement(AccountsManager.getInstance().users.get(i).userName+":"+ AccountsManager.getInstance().users.get(i).score);
JedisService.add("users", AccountsService.getInstance().users.get(i)); JedisService.add("users", AccountsManager.getInstance().users.get(i));
} }
return l; return l;
} }

View file

@ -1,6 +1,6 @@
package com.texttwist.server.tasks; package com.texttwist.server.tasks;
import com.texttwist.server.services.AccountsService; import com.texttwist.server.managers.AccountsManager;
import com.texttwist.server.models.Dictionary; import com.texttwist.server.models.Dictionary;
import com.texttwist.server.models.Match; import com.texttwist.server.models.Match;
import models.User; import models.User;
@ -35,7 +35,7 @@ public class ComputeScore implements Callable<Integer> {
} }
match.setScore(sender, score); match.setScore(sender, score);
User u = AccountsService.getInstance().findUser(sender); User u = AccountsManager.getInstance().findUser(sender);
u.addScore(score); u.addScore(score);
if(match.allPlayersSendedHisScore()) { if(match.allPlayersSendedHisScore()) {

View file

@ -8,11 +8,11 @@ import java.util.concurrent.*;
* Author: Lorenzo Iovino on 23/06/2017. * Author: Lorenzo Iovino on 23/06/2017.
* Description: Task: Join Timeout * Description: Task: Join Timeout
*/ */
public class JoinTimeout implements Callable<Boolean> { public class TimeoutJoin implements Callable<Boolean> {
public final Match match; public final Match match;
public JoinTimeout(Match match) { public TimeoutJoin(Match match) {
this.match = match; this.match = match;
} }

View file

@ -8,10 +8,10 @@ import java.util.concurrent.Callable;
* Author: Lorenzo Iovino on 27/06/2017. * Author: Lorenzo Iovino on 27/06/2017.
* Description: Task: Match Timeout * Description: Task: Match Timeout
*/ */
public class MatchTimeout implements Callable<Boolean> { public class TimeoutMatch implements Callable<Boolean> {
private Match match; private Match match;
public MatchTimeout(Match match) { public TimeoutMatch(Match match) {
this.match = match; this.match = match;
} }

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff