173 lines
6.3 KiB
Java
173 lines
6.3 KiB
Java
import java.io.BufferedReader;
|
|
import java.io.IOException;
|
|
import java.io.InputStreamReader;
|
|
import java.io.OutputStreamWriter;
|
|
import java.io.PrintWriter;
|
|
import java.net.Socket;
|
|
import com.google.gson.Gson;
|
|
|
|
public class ComunacacionJugador extends Thread{
|
|
Socket socket;
|
|
public ComunacacionJugador(Socket socket) {
|
|
this.socket = socket;
|
|
this.start();
|
|
}
|
|
|
|
public void run() {
|
|
try {
|
|
Gson gson = new Gson();
|
|
BufferedReader bufferEntrada = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
|
|
PrintWriter bufferSalida = new PrintWriter(new OutputStreamWriter(this.socket.getOutputStream()),true);
|
|
bufferSalida.println(gson.toJson(Servidor.mostrarSalas()+"fin"));
|
|
int sala = Integer.parseInt(gson.fromJson(bufferEntrada.readLine(), String.class))-1;
|
|
if(Servidor.addJugador(sala, this.socket)) {
|
|
bufferSalida.println(gson.toJson("Has entrado en la sala"));
|
|
switch(Servidor.determinarJuego(sala)){
|
|
case Servidor.juego4:
|
|
sala -= Servidor.salas3.size();
|
|
bufferSalida.println(gson.toJson("4"));
|
|
partida4(bufferEntrada, bufferSalida, sala);
|
|
break;
|
|
case Servidor.juego3:
|
|
bufferSalida.println(gson.toJson("3"));
|
|
partida3(bufferEntrada, bufferSalida, sala);
|
|
break;
|
|
}
|
|
}else {
|
|
bufferSalida.println(gson.toJson("Error al entrar en la sala"));
|
|
socket.close();
|
|
return;
|
|
}
|
|
|
|
|
|
}catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
private void partida3(BufferedReader bufferEntrada, PrintWriter bufferSalida, int sala) throws InterruptedException, NumberFormatException, IOException {
|
|
Gson gson = new Gson();
|
|
boolean continuar = true;
|
|
int jugador = 2;
|
|
if(Servidor.salas3.get(sala).getJugador1().equals(this.socket)) {
|
|
bufferSalida.println(gson.toJson("Esperando al segundo jugador"));
|
|
synchronized (this.socket) {
|
|
this.socket.wait();
|
|
}
|
|
jugador = 1;
|
|
bufferSalida.println(gson.toJson("Primero"));
|
|
bufferSalida.println(gson.toJson(Servidor.salas3.get(sala).tablero.getTablero()));
|
|
Servidor.salas3.get(sala).tablero.colocarFicha(Integer.parseInt(gson.fromJson(bufferEntrada.readLine(), String.class)),
|
|
Integer.parseInt(gson.fromJson(bufferEntrada.readLine(), String.class)),jugador);
|
|
/*Calculamos el modulo 2 de jugador para que cuando este sea 2 se acceda al socket del primero,
|
|
y viceversa, ya que usamos los sockets como mutex*/
|
|
synchronized (Servidor.salas3.get(sala).jugadores[jugador%2]) {
|
|
Servidor.salas3.get(sala).jugadores[jugador%2].notify();
|
|
}
|
|
synchronized (this.socket) {
|
|
this.socket.wait();
|
|
}
|
|
}else {
|
|
bufferSalida.println(gson.toJson("Iniciando partida"));
|
|
bufferSalida.println(gson.toJson("Segundo"));
|
|
synchronized (Servidor.salas3.get(sala).jugadores[jugador%2]) {
|
|
Servidor.salas3.get(sala).jugadores[jugador%2].notify();
|
|
}
|
|
synchronized (this.socket) {
|
|
this.socket.wait();
|
|
}
|
|
}
|
|
while(continuar) {
|
|
int finalJuego = Servidor.salas3.get(sala).tablero.finalJuego();
|
|
/*funalJuego determinara quien ha ganado, mientras sea 0 nadie habra ganado,
|
|
y cuando alguien gane contendra el numero del jugador que ha ganado*/
|
|
if(finalJuego != 0) {
|
|
continuar = false;
|
|
if(finalJuego == jugador) {
|
|
bufferSalida.println(gson.toJson("v"));
|
|
Servidor.salas3.remove(sala);
|
|
}else {
|
|
bufferSalida.println(gson.toJson("f"));
|
|
synchronized (Servidor.salas3.get(sala).jugadores[jugador%2]) {
|
|
Servidor.salas3.get(sala).jugadores[jugador%2].notify();
|
|
}
|
|
}
|
|
|
|
}else {
|
|
bufferSalida.println(gson.toJson(Servidor.salas3.get(sala).tablero.getTablero()));
|
|
Servidor.salas3.get(sala).tablero.colocarFicha(Integer.parseInt(gson.fromJson(bufferEntrada.readLine(), String.class)),
|
|
Integer.parseInt(bufferEntrada.readLine()),jugador);
|
|
synchronized (Servidor.salas3.get(sala).jugadores[jugador%2]) {
|
|
Servidor.salas3.get(sala).jugadores[jugador%2].notify();
|
|
}
|
|
synchronized (this.socket) {
|
|
this.socket.wait();
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
private void partida4(BufferedReader bufferEntrada, PrintWriter bufferSalida, int sala) throws InterruptedException, NumberFormatException, IOException {
|
|
Gson gson = new Gson();
|
|
boolean continuar = true;
|
|
int jugador = 2;
|
|
if(Servidor.salas4.get(sala).getJugador1().equals(this.socket)) {
|
|
bufferSalida.println(gson.toJson("Esperando al segundo jugador"));
|
|
synchronized (this.socket) {
|
|
this.socket.wait();
|
|
}
|
|
jugador = 1;
|
|
bufferSalida.println(gson.toJson("Primero"));
|
|
bufferSalida.println(gson.toJson(Servidor.salas4.get(sala).tablero.getTablero()));
|
|
Servidor.salas4.get(sala).tablero.colocarFicha(Integer.parseInt(gson.fromJson(bufferEntrada.readLine(), String.class)),jugador);
|
|
/*Calculamos el modulo 2 de jugador para que cuando este sea 2 se acceda al socket del primero,
|
|
y viceversa, ya que usamos los sockets como mutex*/
|
|
synchronized (Servidor.salas4.get(sala).jugadores[jugador%2]) {
|
|
Servidor.salas4.get(sala).jugadores[jugador%2].notify();
|
|
}
|
|
synchronized (this.socket) {
|
|
this.socket.wait();
|
|
}
|
|
}else {
|
|
bufferSalida.println(gson.toJson("Iniciando partida"));
|
|
bufferSalida.println(gson.toJson("Segundo"));
|
|
synchronized (Servidor.salas4.get(sala).jugadores[jugador%2]) {
|
|
Servidor.salas4.get(sala).jugadores[jugador%2].notify();
|
|
}
|
|
synchronized (this.socket) {
|
|
this.socket.wait();
|
|
}
|
|
}
|
|
while(continuar) {
|
|
int finalJuego = Servidor.salas4.get(sala).tablero.finalJuego();
|
|
/*funalJuego determinara quien ha ganado, mientras sea 0 nadie habra ganado,
|
|
y cuando alguien gane contendra el numero del jugador que ha ganado*/
|
|
if(finalJuego != 0) {
|
|
continuar = false;
|
|
if(finalJuego == jugador) {
|
|
bufferSalida.println(gson.toJson("v"));
|
|
Servidor.salas4.remove(sala);
|
|
}else {
|
|
bufferSalida.println(gson.toJson("f"));
|
|
synchronized (Servidor.salas4.get(sala).jugadores[jugador%2]) {
|
|
Servidor.salas4.get(sala).jugadores[jugador%2].notify();
|
|
}
|
|
}
|
|
}else {
|
|
bufferSalida.println(gson.toJson(Servidor.salas4.get(sala).tablero.getTablero()));
|
|
Servidor.salas4.get(sala).tablero.colocarFicha(Integer.parseInt(gson.fromJson(bufferEntrada.readLine(), String.class)),jugador);
|
|
synchronized (Servidor.salas4.get(sala).jugadores[jugador%2]) {
|
|
Servidor.salas4.get(sala).jugadores[jugador%2].notify();
|
|
}
|
|
synchronized (this.socket) {
|
|
this.socket.wait();
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
}
|