up
This commit is contained in:
parent
5512f1a358
commit
216ef81855
21 changed files with 2201 additions and 2725 deletions
|
|
@ -50,6 +50,7 @@ public class GameServer implements Runnable{
|
|||
|
||||
|
||||
public static List<Match> activeMatches = Collections.synchronizedList(new ArrayList<>());
|
||||
public static Integer multicastID = 4000;
|
||||
|
||||
public GameServer(int port){
|
||||
this.serverPort = port;
|
||||
|
|
@ -104,6 +105,7 @@ public class GameServer implements Runnable{
|
|||
Message msg = Message.toMessage(line);
|
||||
proxy = new ThreadProxy(msg, client, datagramSocket);
|
||||
Future<Boolean> identifyMessage = threadPool.submit(proxy);
|
||||
System.out.println(line);
|
||||
}
|
||||
|
||||
if (line.startsWith("CLOSE")) {
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
package com.texttwist.server.components;
|
||||
|
||||
import com.texttwist.client.App;
|
||||
import com.texttwist.server.models.Match;
|
||||
import com.texttwist.server.tasks.*;
|
||||
import constants.Config;
|
||||
|
|
@ -22,10 +21,10 @@ import static com.texttwist.server.components.GameServer.activeMatches;
|
|||
* Created by loke on 18/06/2017.
|
||||
*/
|
||||
public class ThreadProxy implements Callable<Boolean> {
|
||||
protected ExecutorService threadPool = Executors.newCachedThreadPool();
|
||||
private Message request;
|
||||
private SocketChannel socketChannel;
|
||||
private DatagramSocket datagramSocket;
|
||||
protected final ExecutorService threadPool = Executors.newCachedThreadPool();
|
||||
private final Message request;
|
||||
private final SocketChannel socketChannel;
|
||||
private final DatagramSocket datagramSocket;
|
||||
|
||||
|
||||
ThreadProxy(Message request, SocketChannel socketChannel, DatagramSocket datagramSocket){
|
||||
|
|
@ -58,12 +57,14 @@ public class ThreadProxy implements Callable<Boolean> {
|
|||
|
||||
//Crea nuova partita e attendi i giocatori
|
||||
request.data.addElement(request.sender);
|
||||
final Match match = new Match(request.sender, request.data);
|
||||
match.printAll();
|
||||
|
||||
Match match = new Match(request.sender, request.data);
|
||||
activeMatches.add(match);
|
||||
|
||||
DefaultListModel<String> matchName = new DefaultListModel<>();
|
||||
matchName.addElement(request.sender);
|
||||
|
||||
Future<Boolean> joinMatch = threadPool.submit(new JoinMatch(request.sender, matchName, socketChannel));
|
||||
Boolean joinMatchRes = joinMatch.get();
|
||||
|
||||
|
|
@ -140,7 +141,7 @@ public class ThreadProxy implements Callable<Boolean> {
|
|||
if(joinMatchRes){
|
||||
System.out.print("START THE GAME!!!!");
|
||||
|
||||
Match match = Match.findMatch(activeMatches, request.data.get(0));
|
||||
final Match match = Match.findMatch(activeMatches, request.data.get(0));
|
||||
|
||||
Future<DefaultListModel<String>> generateLetters = threadPool.submit(new GenerateLetters());
|
||||
match.setLetters(generateLetters.get());
|
||||
|
|
@ -148,8 +149,8 @@ public class ThreadProxy implements Callable<Boolean> {
|
|||
|
||||
for (int i =0; i< match.playersSocket.size(); i++) {
|
||||
System.out.println("INVIO");
|
||||
socketChannel = match.playersSocket.get(i).getValue();
|
||||
if(socketChannel != null) {
|
||||
SocketChannel socketClient = match.playersSocket.get(i).getValue();
|
||||
if(socketClient != null) {
|
||||
buffer.clear();
|
||||
Message message = new Message("GAME_STARTED", "", "", match.letters);
|
||||
match.startGame();
|
||||
|
|
@ -157,7 +158,7 @@ public class ThreadProxy implements Callable<Boolean> {
|
|||
|
||||
buffer = ByteBuffer.wrap(byteMessage);
|
||||
try {
|
||||
socketChannel.write(buffer);
|
||||
socketClient.write(buffer);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
|
@ -169,21 +170,27 @@ public class ThreadProxy implements Callable<Boolean> {
|
|||
//Start receive words: tempo masimo 5 minuti per completare l'invio delle lettere.
|
||||
Future<Boolean> receiveWords = threadPool.submit(new ReceiveWords(match, datagramSocket));
|
||||
Boolean receiveWordsRes = receiveWords.get();
|
||||
if(!receiveWordsRes){
|
||||
match.setUndefinedScorePlayersToZero();
|
||||
|
||||
if(receiveWordsRes){
|
||||
System.out.println("ZERO PUNTI a chi non ha ancora inviato le lettere, TIMER SCADUTO");
|
||||
} else {
|
||||
System.out.println("TUTTI I GIOCATORI HANNO CONSEGNATO IN TEMPO");
|
||||
}
|
||||
|
||||
//match.setUndefinedScorePlayersToZero();
|
||||
|
||||
Message msg = new Message("FINALSCORE","SERVER","", match.getMatchPlayersScoreAsStringList());
|
||||
|
||||
MulticastSocket multicastSocket = new MulticastSocket(match.multicastId);
|
||||
System.out.println(multicastSocket);
|
||||
System.out.println(match.multicastId);
|
||||
InetAddress ia = InetAddress.getByName(Config.ScoreMulticastServerURI);
|
||||
DatagramPacket hi = new DatagramPacket(msg.toString().getBytes(), msg.toString().length(), ia, match.multicastId);
|
||||
System.out.println(msg.toString());
|
||||
multicastSocket.send(hi);
|
||||
activeMatches.remove(Match.findMatchIndex(activeMatches,match.matchCreator));
|
||||
|
||||
activeMatches.remove(Match.findMatchIndex(activeMatches, match.matchCreator));
|
||||
multicastSocket.disconnect();
|
||||
multicastSocket.close();
|
||||
|
||||
//RISPONDI CON LA CLASSIFICA
|
||||
break;
|
||||
|
|
|
|||
|
|
@ -1,215 +0,0 @@
|
|||
package com.texttwist.server.components;
|
||||
|
||||
import com.texttwist.client.App;
|
||||
import com.texttwist.server.models.Match;
|
||||
import com.texttwist.server.tasks.*;
|
||||
import constants.Config;
|
||||
import models.Message;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.io.IOException;
|
||||
import java.net.DatagramPacket;
|
||||
import java.net.DatagramSocket;
|
||||
import java.net.InetAddress;
|
||||
import java.net.MulticastSocket;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.SocketChannel;
|
||||
import java.util.concurrent.*;
|
||||
|
||||
import static com.texttwist.server.components.GameServer.activeMatches;
|
||||
|
||||
/**
|
||||
* Created by loke on 18/06/2017.
|
||||
*/
|
||||
public class ThreadProxy implements Callable<Boolean> {
|
||||
protected ExecutorService threadPool = Executors.newCachedThreadPool();
|
||||
private Message request;
|
||||
private SocketChannel socketChannel;
|
||||
private DatagramSocket datagramSocket;
|
||||
|
||||
|
||||
ThreadProxy(Message request, SocketChannel socketChannel, DatagramSocket datagramSocket){
|
||||
this.request = request;
|
||||
this.socketChannel = socketChannel;
|
||||
this.datagramSocket = datagramSocket;
|
||||
}
|
||||
|
||||
|
||||
private Boolean isValidToken(String token){
|
||||
return SessionsManager.getInstance().isValidToken(token);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean call() {
|
||||
ByteBuffer buffer = ByteBuffer.allocate(1024);
|
||||
byte[] byteMessage = null;
|
||||
System.out.println("Selecting right task for new thread");
|
||||
if(isValidToken(request.token)){
|
||||
switch(request.message){
|
||||
case "START_GAME":
|
||||
Future<Boolean> onlineUsers = threadPool.submit(new CheckOnlineUsers(request.data));
|
||||
try {
|
||||
Boolean usersOnline = onlineUsers.get();
|
||||
if(usersOnline){
|
||||
Future<Boolean> sendInvitations = threadPool.submit(new SendInvitations(request.sender, request.data));
|
||||
try {
|
||||
Boolean invitationSended = sendInvitations.get();
|
||||
if (invitationSended) {
|
||||
|
||||
//Crea nuova partita e attendi i giocatori
|
||||
request.data.addElement(request.sender);
|
||||
|
||||
Match match = new Match(request.sender, request.data);
|
||||
activeMatches.add(match);
|
||||
|
||||
DefaultListModel<String> matchName = new DefaultListModel<>();
|
||||
matchName.addElement(request.sender);
|
||||
Future<Boolean> joinMatch = threadPool.submit(new JoinMatch(request.sender, matchName, socketChannel));
|
||||
Boolean joinMatchRes = joinMatch.get();
|
||||
|
||||
if(!joinMatchRes){
|
||||
//NON FARE NULLA, ASPETTA GLI ALTRI
|
||||
Message message = new Message("INVITES_ALL_SENDED", "", "", new DefaultListModel<String>());
|
||||
byteMessage = message.toString().getBytes();
|
||||
buffer = ByteBuffer.wrap(byteMessage);
|
||||
socketChannel.write(buffer);
|
||||
|
||||
|
||||
Future<Boolean> joinTimeout = threadPool.submit(new JoinTimeout(match));
|
||||
Boolean joinTimeoutRes = joinTimeout.get();
|
||||
if(!joinTimeoutRes){
|
||||
Future<Boolean> sendMessageJoinTimeout = threadPool.submit(
|
||||
new SendMessageToAllPlayers(match, new Message("JOIN_TIMEOUT", "", "", new DefaultListModel<>()), socketChannel));
|
||||
Boolean sendMessageJoinTimeoutRes = sendMessageJoinTimeout.get();
|
||||
if(!sendMessageJoinTimeoutRes){
|
||||
activeMatches.remove(Match.findMatchIndex(activeMatches,match.matchCreator));
|
||||
return sendMessageJoinTimeoutRes;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
} catch (ExecutionException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} else {
|
||||
|
||||
Message message = new Message("USER_NOT_ONLINE", "", "", new DefaultListModel<String>());
|
||||
byteMessage = new String(message.toString()).getBytes();
|
||||
buffer.clear();
|
||||
buffer = ByteBuffer.wrap(byteMessage);
|
||||
this.socketChannel.write(buffer);
|
||||
break;
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
} catch (ExecutionException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
case "FETCH_HIGHSCORES":
|
||||
Future<DefaultListModel<String>> computeHighscores = threadPool.submit(new ComputeHighscores());
|
||||
try {
|
||||
DefaultListModel<String> computeHighscoresRes = computeHighscores.get();
|
||||
Message message = new Message("HIGHSCORES", "", "", computeHighscoresRes);
|
||||
byteMessage = message.toString().getBytes();
|
||||
|
||||
buffer = ByteBuffer.wrap(byteMessage);
|
||||
try {
|
||||
socketChannel.write(buffer);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
break;
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
} catch (ExecutionException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
case "JOIN_GAME":
|
||||
Future<Boolean> joinMatch = threadPool.submit(new JoinMatch(request.sender, request.data, socketChannel));
|
||||
try {
|
||||
Boolean joinMatchRes = joinMatch.get();
|
||||
if(joinMatchRes){
|
||||
System.out.print("START THE GAME!!!!");
|
||||
|
||||
Match match = Match.findMatch(activeMatches, request.data.get(0));
|
||||
|
||||
Future<DefaultListModel<String>> generateWords = threadPool.submit(new GenerateLetters());
|
||||
match.setLetters(generateWords.get());
|
||||
match.letters.addElement(String.valueOf(match.multicastId));
|
||||
|
||||
for (int i =0; i< match.playersSocket.size(); i++) {
|
||||
System.out.println("INVIO");
|
||||
socketChannel = match.playersSocket.get(i).getValue();
|
||||
if(socketChannel != null) {
|
||||
buffer.clear();
|
||||
Message message = new Message("GAME_STARTED", "", "", match.letters);
|
||||
match.startGame();
|
||||
byteMessage = message.toString().getBytes();
|
||||
|
||||
buffer = ByteBuffer.wrap(byteMessage);
|
||||
try {
|
||||
socketChannel.write(buffer);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
//clientSocket.close();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//Start receive words: tempo masimo 5 minuti per completare l'invio delle lettere.
|
||||
Future<Boolean> receiveWords = threadPool.submit(new ReceiveWords(match, datagramSocket));
|
||||
Boolean receiveWordsRes = receiveWords.get();
|
||||
if(!receiveWordsRes){
|
||||
match.setUndefinedScorePlayersToZero();
|
||||
System.out.println("ZERO PUNTI a chi non ha ancora inviato le lettere, TIMER SCADUTO");
|
||||
} else {
|
||||
System.out.println("TUTTI I GIOCATORI HANNO CONSEGNATO IN TEMPO");
|
||||
}
|
||||
|
||||
Message msg = new Message("FINALSCORE","SERVER","", match.getMatchPlayersScoreAsStringList());
|
||||
|
||||
MulticastSocket multicastSocket = new MulticastSocket(match.multicastId);
|
||||
InetAddress ia = InetAddress.getByName(Config.ScoreMulticastServerURI);
|
||||
DatagramPacket hi = new DatagramPacket(msg.toString().getBytes(), msg.toString().length(), ia, match.multicastId);
|
||||
multicastSocket.send(hi);
|
||||
activeMatches.remove(Match.findMatchIndex(activeMatches,match.matchCreator));
|
||||
|
||||
|
||||
//RISPONDI CON LA CLASSIFICA
|
||||
break;
|
||||
//ULTIMO A JOINARE! INIZIA GIOCO
|
||||
} else {
|
||||
System.out.print("WAIT FRIENDS");
|
||||
//NON FARE NULLA, ASPETA GLI ALTRI
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
} catch (ExecutionException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
default:
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
} else {
|
||||
System.out.print("TOKEN NON VALIDO");
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,5 +1,6 @@
|
|||
package com.texttwist.server.models;
|
||||
|
||||
import com.texttwist.server.components.GameServer;
|
||||
import constants.Config;
|
||||
import javafx.util.Pair;
|
||||
|
||||
|
|
@ -15,13 +16,13 @@ import static com.texttwist.server.components.GameServer.activeMatches;
|
|||
* Created by loke on 23/06/2017.
|
||||
*/
|
||||
public class Match {
|
||||
public List<Pair<String,Integer>> playersStatus = Collections.synchronizedList(new ArrayList<>()); //Usare Liste!!!!!!!
|
||||
public List<Pair<String,SocketChannel>> playersSocket = Collections.synchronizedList(new ArrayList<>());
|
||||
public final List<Pair<String,Integer>> playersStatus = Collections.synchronizedList(new ArrayList<>()); //Usare Liste!!!!!!!
|
||||
public final List<Pair<String,SocketChannel>> playersSocket = Collections.synchronizedList(new ArrayList<>());
|
||||
private boolean started = false;
|
||||
public String matchCreator;
|
||||
public final String matchCreator;
|
||||
public Integer multicastId;
|
||||
public DefaultListModel<String> letters;
|
||||
public List<Pair<String,Integer>> playersScore = Collections.synchronizedList(new ArrayList<>());
|
||||
public final List<Pair<String,Integer>> playersScore = Collections.synchronizedList(new ArrayList<>());
|
||||
|
||||
public Match(String matchCreator, DefaultListModel<String> players){
|
||||
for (int i =0; i < players.size(); i++){
|
||||
|
|
@ -29,19 +30,20 @@ public class Match {
|
|||
this.playersScore.add(new Pair<>(players.get(i), -1));
|
||||
this.playersSocket.add(new Pair<>(players.get(i), null));
|
||||
}
|
||||
|
||||
this.multicastId = this.generateMulticastId();
|
||||
this.matchCreator = matchCreator;
|
||||
|
||||
}
|
||||
|
||||
public static Match findMatch(List<Match> matches, String matchName){
|
||||
synchronized (matches) {
|
||||
for (int i = 0; i < matches.size(); i++) {
|
||||
if (matches.get(i).matchCreator.equals(matchName)) {
|
||||
return matches.get(i);
|
||||
}
|
||||
for (int i = 0; i < matches.size(); i++) {
|
||||
if (matches.get(i).matchCreator.equals(matchName)) {
|
||||
return matches.get(i);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -52,77 +54,79 @@ public class Match {
|
|||
}
|
||||
}
|
||||
public static int findMatchIndex(List<Match> matches, String matchName){
|
||||
synchronized (matches) {
|
||||
for (int i = 0; i < matches.size(); i++) {
|
||||
if (matches.get(i).matchCreator.equals(matchName)) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isStarted(){
|
||||
return started;
|
||||
}
|
||||
|
||||
public Match findMatchByPlayer(String player){
|
||||
for (int i = 0; i < activeMatches.size(); i++) {
|
||||
for (int j = 0; j < playersStatus.size(); j++) {
|
||||
if (activeMatches.get(i).playersStatus.get(j).getKey().equals(player)) {
|
||||
return activeMatches.get(i);
|
||||
}
|
||||
}
|
||||
/* if (matches.get(i).matchCreator.equals(matchName)) {
|
||||
return i;
|
||||
}*/
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void startGame(){
|
||||
this.started=true;
|
||||
}
|
||||
|
||||
public void setScore(String player, Integer score){
|
||||
System.out.println(player + " prova a settare il suo score a " + score);
|
||||
synchronized (playersScore) {
|
||||
for (int i = 0; i < playersScore.size(); i++) {
|
||||
if (playersScore.get(i).getKey().equals(player)) {
|
||||
playersScore.set(i, new Pair<String, Integer>(player, score));
|
||||
System.out.println("SEtting score of " + playersScore.get(i).getKey() + " to " + score);
|
||||
final Match m = findMatchByPlayer(player);
|
||||
m.printAll();
|
||||
|
||||
System.out.println("ENTRO");
|
||||
for (int i = 0; i < m.playersScore.size(); i++) {
|
||||
if (m.playersScore.get(i).getKey().equals(player)) {
|
||||
m.playersScore.set(i, new Pair<String, Integer>(player, score));
|
||||
System.out.println("SEtting score of " + m.playersScore.get(i).getKey() + " to " + score);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Boolean allPlayersSendedHisScore(){
|
||||
printAll();
|
||||
synchronized (playersScore) {
|
||||
for (int i = 0; i < playersScore.size(); i++) {
|
||||
if (playersScore.get(i).getValue() == -1) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public void setUndefinedScorePlayersToZero(){
|
||||
synchronized (playersScore) {
|
||||
for (int i = 0; i < playersScore.size(); i++) {
|
||||
if (playersScore.get(i).getValue() == -1) {
|
||||
playersScore.set(i, new Pair<String, Integer>(playersScore.get(i).getKey(), 0));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public DefaultListModel<String> getMatchPlayersScoreAsStringList(){
|
||||
synchronized (playersScore) {
|
||||
DefaultListModel<String> l = new DefaultListModel<>();
|
||||
for (int i = 0; i < playersScore.size(); i++) {
|
||||
l.addElement(playersScore.get(i).getKey() + ":" + playersScore.get(i).getValue());
|
||||
}
|
||||
return l;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
private int generateMulticastId(){
|
||||
synchronized (playersScore) {
|
||||
if (activeMatches.size() != 0) {
|
||||
return activeMatches.get(activeMatches.size()).multicastId + 1;
|
||||
} else {
|
||||
return 4000;
|
||||
}
|
||||
}
|
||||
return GameServer.multicastID++;
|
||||
}
|
||||
public void setLetters(DefaultListModel<String> letters){
|
||||
this.letters = letters;
|
||||
|
|
|
|||
|
|
@ -14,8 +14,8 @@ import java.util.concurrent.Callable;
|
|||
public class ComputeScore implements Callable<Integer> {
|
||||
|
||||
public DefaultListModel<String> words;
|
||||
public String sender;
|
||||
public Match match;
|
||||
public final String sender;
|
||||
public final Match match;
|
||||
|
||||
public ComputeScore(String sender, Match match, DefaultListModel<String> words){
|
||||
this.words = words;
|
||||
|
|
@ -25,21 +25,16 @@ public class ComputeScore implements Callable<Integer> {
|
|||
|
||||
@Override
|
||||
public Integer call() throws Exception {
|
||||
System.out.println("INIT SET SCORE");
|
||||
System.out.println(words.size());
|
||||
System.out.println(words);
|
||||
System.out.println("SET SCORE");
|
||||
|
||||
Integer score = 0;
|
||||
for(int i = 0; i< words.size(); i++){
|
||||
if(isValid(words.get(i), match.letters)){
|
||||
score += words.get(i).length();
|
||||
Integer score = 0;
|
||||
for (int i = 0; i < words.size(); i++) {
|
||||
if (isValid(words.get(i), match.letters)) {
|
||||
score += words.get(i).length();
|
||||
}
|
||||
}
|
||||
}
|
||||
match.setScore(sender, score);
|
||||
User u = AccountsManager.getInstance().findUser(sender);
|
||||
u.addScore(score);
|
||||
return score;
|
||||
match.setScore(sender, score);
|
||||
User u = AccountsManager.getInstance().findUser(sender);
|
||||
u.addScore(score);
|
||||
return score;
|
||||
}
|
||||
|
||||
private Boolean isValid(String word, DefaultListModel<String> letters) {
|
||||
|
|
|
|||
|
|
@ -15,9 +15,9 @@ import static com.texttwist.server.models.Match.findMatch;
|
|||
* Created by loke on 23/06/2017.
|
||||
*/
|
||||
public class JoinMatch implements Callable<Boolean> {
|
||||
public String matchName;
|
||||
public String playerName;
|
||||
public SocketChannel socketChannel;
|
||||
public final String matchName;
|
||||
public final String playerName;
|
||||
public final SocketChannel socketChannel;
|
||||
|
||||
public JoinMatch(String playerName, DefaultListModel<String> matchName, SocketChannel socketChannel) {
|
||||
this.playerName = playerName;
|
||||
|
|
@ -28,21 +28,21 @@ public class JoinMatch implements Callable<Boolean> {
|
|||
|
||||
@Override
|
||||
public Boolean call() throws Exception {
|
||||
Match thisMatch = Match.findMatch(activeMatches, this.matchName);
|
||||
if(thisMatch!=null){
|
||||
for(int j = 0; j<thisMatch.playersStatus.size(); j++){
|
||||
String name = thisMatch.playersStatus.get(j).getKey();
|
||||
if (name.equals(playerName)){
|
||||
thisMatch.playersStatus.remove(j);
|
||||
thisMatch.playersStatus.add(new Pair<>(name,1));
|
||||
thisMatch.playersSocket.remove(j);
|
||||
thisMatch.playersSocket.add(new Pair<>(name,socketChannel));
|
||||
System.out.println(playerName + ": JOINED");
|
||||
return allJoined(thisMatch);
|
||||
final Match thisMatch = Match.findMatch(activeMatches, this.matchName);
|
||||
if (thisMatch != null) {
|
||||
for (int j = 0; j < thisMatch.playersStatus.size(); j++) {
|
||||
String name = thisMatch.playersStatus.get(j).getKey();
|
||||
if (name.equals(playerName)) {
|
||||
thisMatch.playersStatus.remove(j);
|
||||
thisMatch.playersStatus.add(new Pair<>(name, 1));
|
||||
thisMatch.playersSocket.remove(j);
|
||||
thisMatch.playersSocket.add(new Pair<>(name, socketChannel));
|
||||
System.out.println(playerName + ": JOINED");
|
||||
return allJoined(thisMatch);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return allJoined(thisMatch);
|
||||
return allJoined(thisMatch);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ import java.util.concurrent.*;
|
|||
*/
|
||||
public class JoinTimeout implements Callable<Boolean> {
|
||||
|
||||
public Match match;
|
||||
public final Match match;
|
||||
|
||||
public JoinTimeout(Match match) {
|
||||
this.match = match;
|
||||
|
|
|
|||
|
|
@ -10,10 +10,8 @@ import java.util.concurrent.Callable;
|
|||
*/
|
||||
public class MatchTimeout implements Callable<Boolean> {
|
||||
|
||||
Boolean receiveWords;
|
||||
|
||||
public MatchTimeout(Boolean receiveWords) {
|
||||
this.receiveWords = receiveWords;
|
||||
public MatchTimeout() {
|
||||
System.out.println("GamePage started, countdown for end words!");
|
||||
|
||||
}
|
||||
|
|
@ -23,13 +21,12 @@ public class MatchTimeout implements Callable<Boolean> {
|
|||
try {
|
||||
Thread.currentThread().sleep(3*60*1000); //TODO 5*60*1000
|
||||
System.out.println("timer scaduto");
|
||||
receiveWords = false;
|
||||
System.out.println("TIMEOUT - SETTA A 0 il punteggio degli utenti che non hanno inviato le parole");
|
||||
return false;
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
System.out.println("TIMER BLOCATO PRIMA");
|
||||
return true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -23,9 +23,9 @@ public class ReceiveWords implements Callable<Boolean>{
|
|||
|
||||
protected ExecutorService threadPool = Executors.newCachedThreadPool();
|
||||
|
||||
public Boolean receiveWords = true;
|
||||
public DatagramSocket datagramSocket;
|
||||
public Match match;
|
||||
public final DatagramSocket datagramSocket;
|
||||
public final Match match;
|
||||
|
||||
public ReceiveWords(Match match, DatagramSocket datagramSocket) {
|
||||
this.match = match;
|
||||
this.datagramSocket = datagramSocket;
|
||||
|
|
@ -35,32 +35,30 @@ public class ReceiveWords implements Callable<Boolean>{
|
|||
public Boolean call() throws Exception {
|
||||
System.out.print("READY TO Receive words !!!!");
|
||||
|
||||
|
||||
byte[] receiveData = new byte[1024];
|
||||
|
||||
Future<Boolean> matchTimeout = threadPool.submit(new MatchTimeout(receiveWords));
|
||||
Future<Boolean> matchTimeout = threadPool.submit(new MatchTimeout());
|
||||
|
||||
while(receiveWords)
|
||||
{
|
||||
receiveData = new byte[1024];
|
||||
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
|
||||
datagramSocket.receive(receivePacket);
|
||||
String rcv = new String( receivePacket.getData());
|
||||
System.out.println("RECEIVED: " + rcv);
|
||||
Message msg = Message.toMessage(rcv);
|
||||
Future<Integer> computeScore = threadPool.submit(new ComputeScore(msg.sender, match, msg.data));
|
||||
|
||||
//Se tutti hanno inviato le parole, blocca il timer e restituisci true
|
||||
computeScore.get();
|
||||
System.out.println("All player sended?");
|
||||
System.out.println(match.allPlayersSendedHisScore());
|
||||
if(match.allPlayersSendedHisScore()){
|
||||
System.out.println("TIMEOUT BLOCCATO, OK");
|
||||
matchTimeout.cancel(true);
|
||||
// datagramSocket.close();
|
||||
return true;
|
||||
}
|
||||
receiveData = new byte[1024];
|
||||
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
|
||||
datagramSocket.receive(receivePacket);
|
||||
String rcv = new String( receivePacket.getData());
|
||||
System.out.println("RECEIVED: " + rcv);
|
||||
Message msg = Message.toMessage(rcv);
|
||||
Future<Integer> computeScore = threadPool.submit(new ComputeScore(msg.sender, match, msg.data));
|
||||
|
||||
//Se tutti hanno inviato le parole, blocca il timer e restituisci true
|
||||
computeScore.get();
|
||||
System.out.println(match.matchCreator);
|
||||
System.out.println(match.allPlayersSendedHisScore());
|
||||
if(match.allPlayersSendedHisScore()){
|
||||
System.out.println("TIMEOUT BLOCCATO, OK");
|
||||
// match.setUndefinedScorePlayersToZero();
|
||||
|
||||
matchTimeout.cancel(true);
|
||||
// datagramSocket.close();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@ import java.util.concurrent.Callable;
|
|||
*/
|
||||
public class SendInvitations implements Callable<Boolean> {
|
||||
private final DefaultListModel<String> users;
|
||||
private String sender;
|
||||
private final String sender;
|
||||
|
||||
public SendInvitations(String sender, DefaultListModel<String> users) {
|
||||
this.users = users;
|
||||
|
|
|
|||
|
|
@ -14,8 +14,8 @@ import java.util.concurrent.Callable;
|
|||
public class SendMessageToAllPlayers implements Callable<Boolean> {
|
||||
|
||||
|
||||
public Match match;
|
||||
public Message message;
|
||||
public final Match match;
|
||||
public final Message message;
|
||||
public SocketChannel socketChannel;
|
||||
public SendMessageToAllPlayers(Match match, Message message, SocketChannel socketChannel){
|
||||
this.match = match;
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@ import java.util.concurrent.Callable;
|
|||
* Created by loke on 19/06/2017.
|
||||
*/
|
||||
public class WaitForPlayers implements Callable<Boolean> {
|
||||
private Match match;
|
||||
private final Match match;
|
||||
private String sender;
|
||||
|
||||
public WaitForPlayers(Match match) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue