Added synchronization for match
This commit is contained in:
parent
9cc0297a5b
commit
648ea2ef7c
17 changed files with 2902 additions and 615 deletions
|
|
@ -1,35 +1,61 @@
|
|||
package com.texttwist.client.pages;
|
||||
|
||||
import com.texttwist.client.App;
|
||||
import com.texttwist.client.tasks.StartGame;
|
||||
import constants.Palette;
|
||||
import com.texttwist.client.ui.*;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
import java.util.concurrent.*;
|
||||
|
||||
/**
|
||||
* Created by loke on 14/06/2017.
|
||||
*/
|
||||
public class Game extends Page {
|
||||
|
||||
private TTContainer gamecontainer;
|
||||
private DefaultListModel<String> letters = new DefaultListModel<String>();
|
||||
private DefaultListModel<String> words = new DefaultListModel<String>();
|
||||
private DefaultListModel<Point> letterSpawningPoint = new DefaultListModel<Point>();
|
||||
private TTContainer gameContainer;
|
||||
public GameController gameController;
|
||||
public DefaultListModel<Point> letterSpawningPoint = new DefaultListModel<Point>();
|
||||
|
||||
public Timer timer = null;
|
||||
|
||||
public Point getRandomPosition(){
|
||||
if(letterSpawningPoint.size()>1) {
|
||||
int index = ThreadLocalRandom.current().nextInt(0, letterSpawningPoint.size() - 1);
|
||||
Point placeholder = letterSpawningPoint.get(index);
|
||||
letterSpawningPoint.remove(index);
|
||||
return placeholder;
|
||||
}
|
||||
return new Point(0,0);
|
||||
}
|
||||
|
||||
public Thread startGame(){
|
||||
Thread t = new Thread(new StartGame(this));
|
||||
t.start();
|
||||
return t;
|
||||
}
|
||||
|
||||
|
||||
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();
|
||||
letters = fetchLetters();
|
||||
gameController = new GameController();
|
||||
letterSpawningPoint = setLetterSpawningPoint();
|
||||
if(letters.size() > 0) {
|
||||
showLetters();
|
||||
} else {
|
||||
joiningPhase();
|
||||
}
|
||||
this.gameController.waitForPlayers();
|
||||
startGame();
|
||||
window.setVisible(true);
|
||||
}
|
||||
|
||||
|
|
@ -59,61 +85,15 @@ public class Game extends Page {
|
|||
return l;
|
||||
}
|
||||
|
||||
private DefaultListModel<String> fetchLetters(){
|
||||
DefaultListModel l = new DefaultListModel<String>();/*
|
||||
l.addElement("C");
|
||||
l.addElement("A");
|
||||
l.addElement("E");
|
||||
l.addElement("P");
|
||||
l.addElement("C");
|
||||
l.addElement("I");
|
||||
l.addElement("L");
|
||||
l.addElement("S");*/
|
||||
|
||||
return l;
|
||||
}
|
||||
|
||||
private Callable<Object> getWords(){
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean addWordToWordsList(String word) {
|
||||
words.addElement(word);
|
||||
return true;
|
||||
}
|
||||
|
||||
private Point getRandomPosition(){
|
||||
if(letterSpawningPoint.size()>1) {
|
||||
int index = ThreadLocalRandom.current().nextInt(0, letterSpawningPoint.size() - 1);
|
||||
Point placeholder = letterSpawningPoint.get(index);
|
||||
letterSpawningPoint.remove(index);
|
||||
return placeholder;
|
||||
}
|
||||
return new Point(0,0);
|
||||
}
|
||||
|
||||
public void showLetters(){
|
||||
for(int i = 0; i< letters.size(); i++){
|
||||
TTLetter letter = new TTLetter(
|
||||
getRandomPosition(),
|
||||
letters.get(i),
|
||||
gamecontainer);
|
||||
}
|
||||
window.repaint();
|
||||
window.revalidate();
|
||||
}
|
||||
|
||||
public void joiningPhase(){
|
||||
//Visualizza popup
|
||||
new TTDialog("success", "Waiting for users joins",null,null);
|
||||
window.repaint();
|
||||
window.revalidate();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void createUIComponents() throws IOException {
|
||||
addLogo(root);
|
||||
gamecontainer = new TTContainer(
|
||||
gameContainer = new TTContainer(
|
||||
null,
|
||||
new Dimension(1150,220),
|
||||
Palette.root_backgroundColor,
|
||||
|
|
@ -126,7 +106,7 @@ public class Game extends Page {
|
|||
"Word!",
|
||||
new DefaultListModel(),
|
||||
getWords(),
|
||||
gamecontainer);
|
||||
gameContainer);
|
||||
|
||||
addFooter(root);
|
||||
addNext(footer,
|
||||
|
|
@ -140,10 +120,11 @@ public class Game extends Page {
|
|||
}
|
||||
});
|
||||
|
||||
addTimer(footer,
|
||||
timer = addTimer(footer,
|
||||
new Font(Palette.inputBox_font.getFontName(), Font.BOLD, 40),
|
||||
null,
|
||||
"00:00");
|
||||
"00:00",
|
||||
120);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
33
Client/src/com/texttwist/client/pages/GameController.java
Normal file
33
Client/src/com/texttwist/client/pages/GameController.java
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
package com.texttwist.client.pages;
|
||||
|
||||
import com.texttwist.client.App;
|
||||
import com.texttwist.client.ui.TTDialog;
|
||||
import com.texttwist.client.ui.TTLetter;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.io.IOException;
|
||||
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 boolean addWordToWordsList(String word) {
|
||||
words.addElement(word);
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,18 +1,18 @@
|
|||
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 models.Response;
|
||||
import org.json.simple.JsonObject;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.io.*;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.SocketChannel;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.*;
|
||||
|
||||
/**
|
||||
* Created by loke on 18/06/2017.
|
||||
|
|
@ -21,6 +21,9 @@ public class MatchService {
|
|||
|
||||
public DefaultListModel<String> pendingList = new DefaultListModel<String>();
|
||||
ByteBuffer buffer = ByteBuffer.allocate(1024);
|
||||
|
||||
public DefaultListModel<String> words = new DefaultListModel<String>();
|
||||
|
||||
SocketChannel clientSocket = null;
|
||||
|
||||
public MatchService(){
|
||||
|
|
@ -44,22 +47,34 @@ public class MatchService {
|
|||
}
|
||||
//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);
|
||||
}
|
||||
});
|
||||
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();
|
||||
|
|
@ -72,6 +87,7 @@ public class MatchService {
|
|||
byte[] byteMessage = new String(message.toString()).getBytes();
|
||||
buffer = ByteBuffer.wrap(byteMessage);
|
||||
clientSocket.write(buffer);
|
||||
|
||||
new Game(Page.window);
|
||||
|
||||
} catch (IOException e) {
|
||||
|
|
@ -81,62 +97,9 @@ public class MatchService {
|
|||
|
||||
|
||||
public Object play(DefaultListModel<String> userNames) throws IOException {
|
||||
|
||||
Message message = new Message("START_GAME", App.sessionService.account.userName, App.sessionService.account.token, userNames);
|
||||
|
||||
byte[] byteMessage = new String(message.toString()).getBytes();
|
||||
buffer = ByteBuffer.wrap(byteMessage);
|
||||
clientSocket.write(buffer);
|
||||
|
||||
while (true) {
|
||||
if (clientSocket.read(buffer) != -1) {
|
||||
buffer.clear();
|
||||
|
||||
String line = new String(buffer.array(), buffer.position(), buffer.remaining());
|
||||
if (line.startsWith("MESSAGE")) {
|
||||
|
||||
Message msg = Message.toMessage(line);
|
||||
if (msg.message.equals("USER_NOT_ONLINE")) {
|
||||
new TTDialog("alert", "Users not online!",
|
||||
new Callable() {
|
||||
@Override
|
||||
public Object call() throws Exception {
|
||||
return null;
|
||||
}
|
||||
}, null);
|
||||
return null;
|
||||
}
|
||||
|
||||
if (msg.message.equals("INVITES_ALL_SENDED")) {
|
||||
// clientSocket.close();
|
||||
new TTDialog("success", "Invite all sended!",
|
||||
new Callable() {
|
||||
@Override
|
||||
public Object call() throws Exception {
|
||||
//In attesa dei giocatori
|
||||
return new Game(Page.window);
|
||||
}
|
||||
}, null);
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
if (msg.message.equals("TIMEOUT")) {
|
||||
clientSocket.close();
|
||||
new TTDialog("alert", "TIMEOUT!",
|
||||
new Callable() {
|
||||
@Override
|
||||
public Object call() throws Exception {
|
||||
return new Menu(Page.window);
|
||||
|
||||
}
|
||||
}, null);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
SwingWorker worker = new InvitePlayers(userNames,clientSocket);
|
||||
worker.execute();
|
||||
return null;
|
||||
}
|
||||
|
||||
public void addToPendingList(String username) throws IOException {
|
||||
|
|
|
|||
|
|
@ -23,4 +23,5 @@ public class MatchSetupController {
|
|||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,8 +5,13 @@ import com.texttwist.client.ui.*;
|
|||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
/**
|
||||
|
|
@ -88,16 +93,32 @@ public class Page {
|
|||
parent);
|
||||
}
|
||||
|
||||
public void addTimer(TTContainer parent, Font font, Color fontColor, String caption) {
|
||||
TTLabel next = new TTLabel(
|
||||
public Timer addTimer(TTContainer parent, Font font, Color fontColor, String caption, Integer value) {
|
||||
TTLabel lblTimer = new TTLabel(
|
||||
new Point(0, 0),
|
||||
new Dimension(150, 50),
|
||||
caption,
|
||||
font,
|
||||
fontColor,
|
||||
parent);
|
||||
|
||||
Timer timer = new Timer(1000, new ActionListener() {
|
||||
private int count = value;
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if (count <= 0) {
|
||||
lblTimer.setText("00:00");
|
||||
((Timer)e.getSource()).stop();
|
||||
} else {
|
||||
SimpleDateFormat dateFormat = new SimpleDateFormat("mm:ss:S");
|
||||
|
||||
lblTimer.setText(dateFormat.format(count));
|
||||
count--;
|
||||
|
||||
}
|
||||
}
|
||||
});
|
||||
return timer;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
89
Client/src/com/texttwist/client/tasks/InvitePlayers.java
Normal file
89
Client/src/com/texttwist/client/tasks/InvitePlayers.java
Normal file
|
|
@ -0,0 +1,89 @@
|
|||
package com.texttwist.client.tasks;
|
||||
|
||||
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.ui.TTDialog;
|
||||
import models.Message;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.SocketChannel;
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
/**
|
||||
* Created by loke on 25/06/2017.
|
||||
*/
|
||||
public class InvitePlayers extends SwingWorker<Boolean,Void> {
|
||||
|
||||
ByteBuffer buffer = ByteBuffer.allocate(1024);
|
||||
|
||||
public DefaultListModel<String> userNames;
|
||||
public SocketChannel socketChannel;
|
||||
|
||||
public InvitePlayers(DefaultListModel<String> userNames, SocketChannel socketChannel) {
|
||||
this.socketChannel = socketChannel;
|
||||
this.userNames = userNames;
|
||||
}
|
||||
@Override
|
||||
public Boolean doInBackground() {
|
||||
Message message = new Message("START_GAME", App.sessionService.account.userName, App.sessionService.account.token, userNames);
|
||||
|
||||
byte[] byteMessage = new String(message.toString()).getBytes();
|
||||
buffer = ByteBuffer.wrap(byteMessage);
|
||||
try {
|
||||
socketChannel.write(buffer);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
try {
|
||||
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);
|
||||
if (msg.message.equals("USER_NOT_ONLINE")) {
|
||||
new TTDialog("alert", "Users not online!",
|
||||
new Callable() {
|
||||
@Override
|
||||
public Object call() throws Exception {
|
||||
return null;
|
||||
}
|
||||
}, null);
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
if (msg.message.equals("INVITES_ALL_SENDED")) {
|
||||
// clientSocket.close();
|
||||
new TTDialog("success", "Invite all sended!",
|
||||
new Callable() {
|
||||
@Override
|
||||
public Object call() throws Exception {
|
||||
//In attesa dei giocatori
|
||||
new Game(Page.window);
|
||||
return null;
|
||||
}
|
||||
}, null);
|
||||
return null;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void done() {
|
||||
System.out.println("DDDDDDDD22");
|
||||
|
||||
}
|
||||
}
|
||||
35
Client/src/com/texttwist/client/tasks/StartGame.java
Normal file
35
Client/src/com/texttwist/client/tasks/StartGame.java
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
package com.texttwist.client.tasks;
|
||||
|
||||
import com.texttwist.client.App;
|
||||
import com.texttwist.client.pages.Game;
|
||||
import com.texttwist.client.pages.GameController;
|
||||
import oracle.jrockit.jfr.JFR;
|
||||
|
||||
import javax.swing.*;
|
||||
|
||||
/**
|
||||
* Created by loke on 25/06/2017.
|
||||
*/
|
||||
public class StartGame implements Runnable {
|
||||
|
||||
public Game game;
|
||||
public StartGame(Game game){
|
||||
this.game = game;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(){
|
||||
|
||||
while(!(this.game.gameController.letters.size() > 0)) {
|
||||
System.out.println(this.game.gameController.letters.size());
|
||||
this.game.gameController.letters = App.matchService.words;
|
||||
}
|
||||
System.out.println(this.game.gameController.letters);
|
||||
game.showLetters();
|
||||
if(this.game.gameController.letters.size()>0){
|
||||
this.game.timer.start();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
80
Client/src/com/texttwist/client/tasks/WaitForPlayers.java
Normal file
80
Client/src/com/texttwist/client/tasks/WaitForPlayers.java
Normal file
|
|
@ -0,0 +1,80 @@
|
|||
package com.texttwist.client.tasks;
|
||||
|
||||
import com.texttwist.client.App;
|
||||
import com.texttwist.client.pages.Menu;
|
||||
import com.texttwist.client.pages.Page;
|
||||
import com.texttwist.client.ui.TTDialog;
|
||||
import models.Message;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectOutput;
|
||||
import java.nio.Buffer;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.SocketChannel;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
/**
|
||||
* Created by loke on 25/06/2017.
|
||||
*/
|
||||
public class WaitForPlayers extends SwingWorker<DefaultListModel<String>,DefaultListModel<String>> {
|
||||
|
||||
public SocketChannel socketChannel;
|
||||
public DefaultListModel<String> words;
|
||||
ByteBuffer buffer = ByteBuffer.allocate(1024);
|
||||
|
||||
public WaitForPlayers(SocketChannel socketChannel, DefaultListModel<String> words) {
|
||||
this.socketChannel = socketChannel;
|
||||
this.words = words;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DefaultListModel<String> doInBackground() {
|
||||
try {
|
||||
TTDialog loading = new TTDialog("alert", "Waiting for users joins",null,null);
|
||||
buffer.flip();
|
||||
while (this.socketChannel.read(this.buffer) != -1) {
|
||||
|
||||
String line = new String(this.buffer.array(), this.buffer.position(), this.buffer.remaining());
|
||||
buffer.clear();
|
||||
|
||||
if (line.startsWith("MESSAGE")) {
|
||||
buffer.clear();
|
||||
|
||||
Message msg = Message.toMessage(line);
|
||||
System.out.println("HEY");
|
||||
System.out.println(msg);
|
||||
if (msg.message.equals("TIMEOUT")) {
|
||||
socketChannel.close();
|
||||
loading.dispose();
|
||||
new TTDialog("alert", "TIMEOUT!",
|
||||
new Callable() {
|
||||
@Override
|
||||
public Object call() throws Exception {
|
||||
return new Menu(Page.window);
|
||||
|
||||
}
|
||||
}, null);
|
||||
return new DefaultListModel<String>();
|
||||
}
|
||||
|
||||
if (msg.message.equals("GAME_STARTED")) {
|
||||
loading.dispose();
|
||||
words = msg.data;
|
||||
//socketChannel.close();
|
||||
App.matchService.setWords(words);
|
||||
return words;
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return new DefaultListModel<String>();
|
||||
}
|
||||
|
||||
public void done(){
|
||||
System.out.println("DONEEE");
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue