Added random generator for the initial letters and sending command

This commit is contained in:
Lorenzo Iovino 2017-06-26 15:47:25 +02:00
parent 648ea2ef7c
commit 5f8fe0ae2d
19 changed files with 58861 additions and 436 deletions

View file

@ -4,6 +4,7 @@
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/resources" type="java-resource" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />

58110
Server/resources/dictionary Normal file

File diff suppressed because it is too large Load diff

View file

@ -1,6 +1,7 @@
package com.texttwist.server.components;
import com.sun.org.apache.xpath.internal.operations.Bool;
import com.texttwist.server.models.Dictionary;
import com.texttwist.server.models.Match;
import com.texttwist.server.tasks.SendInvitations;
import com.texttwist.server.tasks.WaitForPlayers;
@ -18,6 +19,8 @@ import java.net.Socket;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.*;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Iterator;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
@ -34,6 +37,9 @@ public class GameServer implements Runnable{
protected ThreadProxy proxy;
protected Selector selector = null;
protected ExecutorService threadPool = Executors.newCachedThreadPool();
private String dictionaryPath = "./Server/resources/dictionary";
public static Dictionary dict;
public static DefaultListModel<Match> activeMatches = new DefaultListModel<Match>();
@ -43,6 +49,7 @@ public class GameServer implements Runnable{
public void run(){
dict = new Dictionary(dictionaryPath);
ByteBuffer buffer = ByteBuffer.allocate(1024);
try {
selector = Selector.open();
@ -116,7 +123,6 @@ public class GameServer implements Runnable{
} catch (IOException e1) {
e1.printStackTrace();
}
System.out.println("DDD");
e.printStackTrace();
}
}

View file

@ -79,15 +79,4 @@ public class SessionsManager {
}
}
public void printSessions() {
for(int i = 0; i<sessions.size(); i++){
System.out.println(sessions.get(i).toString());
}
}
public int size(){
return sessions.size();
}
}

View file

@ -125,20 +125,22 @@ public class ThreadProxy implements Callable<Boolean> {
Boolean joinMatchRes = joinMatch.get();
if(joinMatchRes){
System.out.print("START THE FUCKING GAME!!!!");
//Find the game, send broadcast communication
// buffer.flip();
Match match = Match.findMatch(request.data.get(0));
Future<DefaultListModel<String>> generateWords = threadPool.submit(new GenerateWords());
match.setLetters(generateWords.get());
for (int i =0; i< match.playersSocket.size(); i++) {
System.out.println("INVIO");
socketChannel = match.playersSocket.get(i).getValue();
if(socketChannel!=null) {
buffer.clear();
Future<DefaultListModel<String>> generateWords = threadPool.submit(new GenerateWords());
Message message = new Message("GAME_STARTED", "", "", generateWords.get());
Message message = new Message("GAME_STARTED", "", "", match.letters);
match.startGame();
byteMessage = new String(message.toString()).getBytes();

View file

@ -0,0 +1,58 @@
package com.texttwist.server.models;
import javax.swing.*;
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Random;
import java.util.stream.Stream;
/**
* Created by loke on 26/06/2017.
*/
public class Dictionary {
DefaultListModel<String> wordList = new DefaultListModel<>();
private Random randomGenerator;
public Dictionary(String dictionaryPath) {
try (BufferedReader br = new BufferedReader(new FileReader(new File(dictionaryPath)))) {
for (String line; (line = br.readLine()) != null; ) {
wordList.addElement(line);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public String getRandomWord(int minimumWordSize, int maximumWordSize){
randomGenerator = new Random();
int index = randomGenerator.nextInt(wordList.size());
String word = wordList.get(index);
if(word.length() >= minimumWordSize && word.length() <= maximumWordSize) {
return word;
} else {
for(int i = index; i< wordList.size()-index; i++){
word = wordList.get(i);
if(word.length() >= minimumWordSize && word.length() <= maximumWordSize) {
return word;
}
}
for(int i = index; i>0 ; i--){
word = wordList.get(i);
if(word.length() >= minimumWordSize && word.length() <= maximumWordSize) {
return word;
}
}
return "";
}
}
}

View file

@ -17,6 +17,7 @@ public class Match {
public DefaultListModel<Pair<String,SocketChannel>> playersSocket = new DefaultListModel<Pair<String, SocketChannel>>();
private boolean started = false;
public String matchCreator;
public DefaultListModel<String> letters;
public DefaultListModel<Pair<String,Integer>> playersScore = new DefaultListModel<Pair<String, Integer>>();
public Match(String matchCreator, DefaultListModel<String> players){
@ -47,4 +48,7 @@ public class Match {
}
public void setLetters(DefaultListModel<String> letters){
this.letters = letters;
}
}

View file

@ -1,6 +1,12 @@
package com.texttwist.server.tasks;
import com.texttwist.server.components.GameServer;
import javax.swing.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.nio.file.*;
import java.util.concurrent.Callable;
/**
@ -15,13 +21,13 @@ public class GenerateWords implements Callable<DefaultListModel<String>> {
@Override
public DefaultListModel<String> call() throws Exception {
DefaultListModel l = new DefaultListModel<String>();
l.addElement("D");
l.addElement("S");
l.addElement("Q");
l.addElement("A");
DefaultListModel<String> l = new DefaultListModel<String>();
String word = GameServer.dict.getRandomWord(6, 7);
System.out.println(word);
for (int i = 0;i < word.length(); i++){
l.addElement(String.valueOf(word.charAt(i)));
}
return l;
}