package belotetime.application.server;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import sun.font.CreatedFontTracker;
import belotetime.application.game.Card;
import belotetime.application.game.Pack;
import belotetime.application.game.Player;
import belotetime.application.game.Set;
import belotetime.application.game.Team;
import belotetime.application.game.XmlFeeds;
public class GameClient implements Runnable
{
private Socket client;
private GameServer server;
private DataInputStream request;
private DataOutputStream response;
public GameClient(Socket client, GameServer server)
{
this.client = client;
this.server = server;
server.addPlayer(this);
}
public Socket getClient()
{
return client;
}
public void setClient(Socket client)
{
this.client = client;
}
public GameServer getServer()
{
return server;
}
public void setServer(GameServer server)
{
this.server = server;
}
public DataInputStream getRequest()
{
return request;
}
public void setRequest(DataInputStream request)
{
this.request = request;
}
public DataOutputStream getResponse()
{
return response;
}
public void setResponse(DataOutputStream response)
{
this.response = response;
}
/**
* Ajout d'un joueur dans la partie
* @param str Le fichier xml qui contient les informations du joueur
* @throws IOException
*/
private synchronized void connect() throws IOException
{
server.sendAll(XmlFeeds.gameToXml(server.getGame()));
}
/**
* Ajout du joueur dans une �quipe puis dans la liste des joueurs de la partie
* @param str Le fichier xml qui contient les informations du joueur
* @param numPlayer Le num�ro du joueur
* @throws IOException
*/
private synchronized void connect(String str, int numPlayer) throws IOException
{
Player p = new Player(XmlFeeds.xmlGetTag(str, "player"));
switch(numPlayer)
{
case 0 :
server.getGame().getT1().setP1(p);
break;
case 1 :
server.getGame().getT2().setP1(p);
break;
case 2 :
server.getGame().getT1().setP2(p);
break;
case 3 :
server.getGame().getT2().setP2(p);
break;
}
if(server.getGame().getT1().isFull() && server.getGame().getT2().isFull())
{
server.getGame().getPlayers().add(server.getGame().getT1().getP1());
server.getGame().getPlayers().add(server.getGame().getT2().getP1());
server.getGame().getPlayers().add(server.getGame().getT1().getP2());
server.getGame().getPlayers().add(server.getGame().getT2().getP2());
launchGame();
}
server.getGame().setStarted(true);
server.sendAll(XmlFeeds.gameToXml(server.getGame()));
}
/**
* Initialisation de la partie
*/
private synchronized void launchGame()
{
server.getGame().getPacket().shuffle();
server.getGame().getPacket().cut();
//Cr�ation de la premi�re manche avec un premier donneur choisi au pif
server.getGame().setCurrentSet(new Set(server.getGame(), server.getGame().getPlayers().get((int)(Math.random() * 4))));
server.getGame().getCurrentSet().defineStarter();
server.getGame().getCurrentSet().firstDeal(Set.THREE_CARDS);
}
private synchronized void addPlayer(String xml, int numPlayer) throws IOException
{
Player p = new Player(XmlFeeds.xmlGetTag(xml, "player"));
switch(numPlayer)
{
case 0 :
server.getGame().getT1().setP1(p);
break;
case 1 :
server.getGame().getT2().setP1(p);
break;
case 2 :
server.getGame().getT1().setP2(p);
break;
case 3 :
server.getGame().getT2().setP2(p);
break;
}
if(server.getGame().getT1().isFull() && server.getGame().getT2().isFull())
{
server.getGame().getPlayers().add(server.getGame().getT1().getP1());
server.getGame().getPlayers().add(server.getGame().getT2().getP1());
server.getGame().getPlayers().add(server.getGame().getT1().getP2());
server.getGame().getPlayers().add(server.getGame().getT2().getP2());
launchGame();
}
server.sendAll(XmlFeeds.gameToXml(server.getGame()));
}
@Override
synchronized public void run()
{
int player;
try
{
request = new DataInputStream(client.getInputStream());
response = new DataOutputStream(client.getOutputStream());
while(true)
{
try
{
String data = request.readUTF();
String action = XmlFeeds.xmlGetTag(data, "action");
switch(action)
{
case "createGame" :
connect(data, 0);
break;
case "joinGame" :
connect();
break;
case "getNum" :
try
{
int numPlayer = Integer.parseInt(XmlFeeds.xmlGetTag(data, "numPlayer"));
addPlayer(data, numPlayer);
}
catch(NumberFormatException e)
{
e.printStackTrace();
}
break;
case "passTurn" :
int numPlayer;
try
{
numPlayer = Integer.parseInt(XmlFeeds.xmlGetTag(data, "numPlayer"));
server.getGame().getCurrentSet().setTaker(server.getGame().getPlayers().get((numPlayer + 1) % 4));
GameServer.cnt++;
if(GameServer.cnt > 3)
{
server.getGame().getCurrentSet().setTurn(Set.SECOND_TURN);
}
if(GameServer.cnt > 7)
{
GameServer.cnt = 0;
//Gestion de la nouvelle manche
for (Player p : server.getGame().getPlayers())
{
server.getGame().getPacket().getCards().addAll((p.getHand().getCards()));
p.getHand().getCards().removeAll(p.getHand().getCards());
}
server.getGame().getPacket().cut();
int numDealer = server.getGame().getPlayers().indexOf(server.getGame().getCurrentSet().getDealer());
server.getGame().setCurrentSet(new Set(server.getGame(), server.getGame().getPlayers().get((numDealer + 1) % 4)));
server.getGame().getCurrentSet().defineStarter();
server.getGame().getCurrentSet().firstDeal(Set.THREE_CARDS);
}
connect();
}
catch(NumberFormatException e)
{
e.printStackTrace();
}
break;
case "passSet" :
//On ajoute le score global aux deux �quipes et on r�initialiste les plis
server.getGame().getT1().updatePoints();
server.getGame().getT2().updatePoints();
server.getGame().getPacket().cut();
int numDealer = server.getGame().getPlayers().indexOf(server.getGame().getCurrentSet().getDealer());
server.getGame().setCurrentSet(new Set(server.getGame(), server.getGame().getPlayers().get((numDealer + 1) % 4)));
server.getGame().getCurrentSet().defineStarter();
server.getGame().getCurrentSet().firstDeal(Set.THREE_CARDS);
break;
case "takeTrump" :
String color = XmlFeeds.xmlGetTag(data, "color");
player = Integer.parseInt(XmlFeeds.xmlGetTag(data, "numplayer"));
Card trump = server.getGame().getPacket().getCards().get(server.getGame().getPacket().getCards().size() - 1);
color = color.length() == 0 ? trump.getColor() : color;
server.getGame().getCurrentSet().setTrump(color);
server.getGame().getCurrentSet().takeTrump(server.getGame().getPlayers().get(player));
server.getGame().getCurrentSet().secondDeal();
server.getGame().getCurrentSet().alterTrumpValue();
connect();
break;
case "playCard" :
player = Integer.parseInt(XmlFeeds.xmlGetTag(data, "player"));
Player p = server.getGame().getPlayers().get(player);
boolean b = Boolean.parseBoolean(XmlFeeds.xmlGetTag(data, "belote"));
if(b) server.getGame().getCurrentSet().playBelote();
if(server.getGame().getCurrentSet().getBelotePlayed() == 2)
{
Team t = player % 2 == 0 ? server.getGame().getT1() : server.getGame().getT2();
t.setBelote(true);
}
int numCard = Integer.parseInt(XmlFeeds.xmlGetTag(data, "card"));
server.getGame().getCarpet().getCards().add(p.playCard(numCard));
server.getGame().getCurrentSet().nextStarter();
connect();
break;
case "initTrick" :
//Fin du pli
server.getGame().getCurrentSet().trickWin();
server.getGame().setCarpet(new Pack());
//Fin de la manche
if(server.getGame().getT1().getPoints() + server.getGame().getT2().getPoints() == 152)
{
server.getGame().getCurrentSet().result();
server.getGame().getPacket().cut();
int dealer = server.getGame().getPlayers().indexOf(server.getGame().getCurrentSet().getDealer());
server.getGame().setCurrentSet(new Set(server.getGame(), server.getGame().getPlayers().get((dealer + 1) % 4)));
server.getGame().getCurrentSet().defineStarter();
server.getGame().getCurrentSet().firstDeal(Set.THREE_CARDS);
}
//Fin de la partie
if(server.getGame().getT1().getGamePoints() > 1000 || server.getGame().getT2().getGamePoints() > 1000)
{
server.getGame().setStarted(false);
}
connect();
break;
case "sendMessage" :
server.sendAll(data);
break;
default :
break;
}
}
catch(Exception e)
{
client.close();
}
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}