Aggiunta classifica match + globale. Versione funzionante terminata

This commit is contained in:
Lorenzo Iovino 2017-06-29 15:09:55 +02:00
parent f7c9d0ff99
commit 429eb1cb02
40 changed files with 2152 additions and 1108 deletions

View file

@ -1,7 +1,5 @@
package com.texttwist.server.components;
import models.Account;
import models.Session;
import models.User;
import java.util.ArrayList;
@ -14,7 +12,7 @@ import java.util.List;
*/
public class AccountsManager {
private List<Account> accounts = Collections.synchronizedList(new ArrayList<Account>());
public List<User> users = Collections.synchronizedList(new ArrayList<User>());
private static class Holder {
static final AccountsManager INSTANCE = new AccountsManager();
@ -25,22 +23,22 @@ public class AccountsManager {
}
private AccountsManager(){
accounts.add(new Account("a","a"));
accounts.add(new Account("b","b"));
accounts.add(new Account("c","c"));
users.add(new User("a","a"));
users.add(new User("b","b"));
users.add(new User("c","c"));
}
public boolean register(String userName, String password) {
if(!exists(userName)){
return accounts.add(new Account(userName, password));
return users.add(new User(userName, password));
} else {
return false;
}
}
public boolean exists(String userName) {
synchronized(accounts) {
Iterator<Account> i = accounts.iterator();
synchronized(users) {
Iterator<User> i = users.iterator();
while (i.hasNext()) {
if (i.next().userName.equals(userName)) {
return true;
@ -51,10 +49,10 @@ public class AccountsManager {
}
public boolean checkPassword(String userName, String password) {
synchronized(accounts) {
Iterator<Account> i = accounts.iterator();
synchronized(users) {
Iterator<User> i = users.iterator();
while (i.hasNext()) {
Account account = i.next();
User account = i.next();
if (account.userName.equals(userName) && account.password.equals(password)) {
return true;
}
@ -63,8 +61,21 @@ public class AccountsManager {
}
}
public User findUser(String userName){
synchronized(users) {
Iterator<User> i = users.iterator();
while (i.hasNext()) {
User u = i.next();
if (u.userName.equals(userName)) {
return u;
}
}
return null;
}
}
public int size(){
return accounts.size();
return users.size();
}
}

View file

@ -1,14 +1,11 @@
package com.texttwist.server.components;
import interfaces.IAuth;
import models.Account;
import models.Response;
import org.json.simple.JsonObject;
import utilities.Logger;
import java.math.BigInteger;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.security.SecureRandom;

View file

@ -1,5 +1,4 @@
package com.texttwist.server.components;
import models.Account;
import models.Session;
import java.util.ArrayList;

View file

@ -109,6 +109,28 @@ public class ThreadProxy implements Callable<Boolean> {
e.printStackTrace();
}
case "FETCH_HIGHSCORES":
Future<DefaultListModel<String>> computeHighscores = threadPool.submit(new ComputeHighscores());
try {
System.out.println("FETCHHH");
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();
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
return true;
case "JOIN_GAME":
Future<Boolean> joinMatch = threadPool.submit(new JoinMatch(request.sender, request.data, socketChannel));
try {
@ -177,9 +199,7 @@ public class ThreadProxy implements Callable<Boolean> {
} catch (IOException e) {
e.printStackTrace();
}
{
}
default:
break;

View file

@ -0,0 +1,24 @@
package com.texttwist.server.tasks;
import com.texttwist.server.components.AccountsManager;
import javax.swing.*;
import java.util.concurrent.Callable;
/**
* Created by loke on 28/06/2017.
*/
public class ComputeHighscores implements Callable<DefaultListModel<String>> {
public ComputeHighscores(){}
@Override
public DefaultListModel<String> call() throws Exception {
System.out.println("COMPUTE HIGHSCORE");
DefaultListModel<String> l = new DefaultListModel<>();
for(int i =0; i< AccountsManager.getInstance().users.size(); i++){
l.addElement(AccountsManager.getInstance().users.get(i).userName+":"+AccountsManager.getInstance().users.get(i).score);
}
return l;
}
}

View file

@ -1,6 +1,9 @@
package com.texttwist.server.tasks;
import com.texttwist.client.App;
import com.texttwist.server.components.AccountsManager;
import com.texttwist.server.models.Dictionary;
import com.texttwist.server.models.Match;
import models.User;
import javax.swing.*;
import java.util.concurrent.Callable;
@ -30,6 +33,8 @@ public class ComputeScore implements Callable<Integer> {
}
}
match.setScore(sender, score);
User u = AccountsManager.getInstance().findUser(sender);
u.addScore(score);
return score;
}