Datasets:

ArXiv:
License:
File size: 2,528 Bytes
c574d3a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package serveur.src.modeles.tictactoe;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import commun.ITicTacToe;
import commun.ITicTacToeListener;

public class TicTacToe extends UnicastRemoteObject implements ITicTacToe {
    private HashMap<UUID, TicTacToeInstance> salons;

    /**
     * Initialise la création du serveur
     * 
     * @throws RemoteException
     */
    public TicTacToe() throws RemoteException {
        salons = new HashMap<UUID, TicTacToeInstance>();
    }

    @Override
    public UUID nouveauSalon(ITicTacToeListener listener) throws RemoteException {
        UUID id = UUID.randomUUID(); // Génère un ID
        salons.put(id, new TicTacToeInstance(listener));
        return id;
    }

    @Override
    public boolean rejoindreSalon(UUID salonId, ITicTacToeListener listener) throws RemoteException {
        try {
            return salons.get(salonId).ajouterJoueur(listener);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean quitterSalon(UUID salonId, ITicTacToeListener listener) throws RemoteException {
        try {
            if (salons.get(salonId) != null)
                salons.get(salonId).retirerJoueur(listener);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        // On supprime le salon si il n'est pas déjà supprimé
        if (salons.get(salonId) != null)
            if (salons.get(salonId).getNombreJoueurs() == 0) {
                System.out.println("[MAIN] - Suppression du salon " + salons.remove(salonId).getNom()
                        + ". Cause : 0 joueurs restants.");
            }
        return true;
    }

    @Override
    public boolean jouer(UUID salonId, int x, int y, ITicTacToeListener listener) throws RemoteException {
        try {
            salons.get(salonId).jouer(x, y, listener);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    @Override
    public Map<String, UUID> recupererNoms() throws RemoteException {
        HashMap<String, UUID> res = new HashMap<>();
        for (UUID id : salons.keySet()) {
            if (salons.get(id).getNombreJoueurs() < 2 && !salons.get(id).getEstLancee()) {
                res.put(salons.get(id).getNom(), id);
            }
        }
        return res;
    }
}