package net;
import game.GameBoard;
import game.GameRule;
import game.Point;
import game.Side;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Vector;
public class GameServer{
ServerSocket server;
Vector<Conversation> conversations;
Thread acceptThread;
Thread checkThread;
public GameServer(int port){
try {
server = new ServerSocket(port);
} catch (IOException e) {
System.out.println("服务端错误:" + e.getMessage());
}
conversations = new Vector<Conversation>();
acceptThread = new Thread(){
public void run(){
try {
while(true){
Conversation conversation = new Conversation(conversations.size()+1);
System.out.println("等待客户端连接");
Socket socket1 = server.accept();
conversation.client1 = conversation.new Client("黑方", Side.BLACK, socket1);
System.out.println("客户端1已连接");
Socket socket2 = server.accept();
conversation.client2 = conversation.new Client("白方", Side.WHITE, socket2);
System.out.println("客户端2已连接");
//conversation.start();//不在这里开始
conversations.add(conversation);
System.out.println(conversations.size() + "组游戏已开始");
}
} catch (IOException e) {
System.out.println("连接错误:" + e.getMessage());
}
}
};
checkThread = new Thread(){
public void run(){
while(true){
for(Conversation cnv : conversations){
if(cnv.client1 == null || cnv.client2 == null)
continue;
if(!cnv.client1.isConnect || !cnv.client2.isConnect){
cnv.Close();
System.out.println("第"+ cnv.id +"组对话结束");
}
}
try{
Thread.sleep(100);
}catch(InterruptedException ie){ }
}
}
};
}
public void start(){
this.acceptThread.start();
//this.checkThread.start();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
GameServer gs = new GameServer(2000);
gs.start();
}
}
class Conversation{
int id;
Client client1;
Client client2;
GameRule gameRule;
public Conversation(int id){
this.id = id;
gameRule = new GameRule();
gameRule.gameboard = new GameBoard();
}
public void Start(){
SendMessage(GameMessage.startGame, "游戏开始");
}
public void Close(){
client1.isConnect = false;
client2.isConnect = false;
try {
client1.reader.close();
client2.reader.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("第"+id+"组对话结束");
}
GameRule getGameRule(){
return gameRule;
}
class Client extends Thread{
String name;
Side side;
Socket socket;
ObjectInputStream reader;
ObjectOutputStream writer;
boolean isConnect;
public Client(String name, Side side, Socket socket){
this.name = name;
this.side = side;
this.socket = socket;
if(socket.isConnected())
isConnect = true;
this.start();
}
public void run(){
System.out.println("服务器端第"+id+"组对话"+name+"客户端已建立连接");
try {
writer = new ObjectOutputStream(socket.getOutputStream());//先
reader = new ObjectInputStream(socket.getInputStream());//后
System.out.println("服务器端第"+id+"组对话"+name+"客户端已建立数据流连接");
} catch (IOException e1) {
e1.printStackTrace();
return;
}
send(GameMessage.setSide, side);
while(isConnect){
try {
GameMessage message = (GameMessage)reader.readObject();
System.out.println("收到消息" + message.GetTitleString());
if(message.GetTitle() == GameMessage.startGame){//开始游戏的信号
}
else if(message.GetTitle() == GameMessage.loginGame){//客户端登录
if(client1 != null && client2 != null){
if(client1.isConnect && client2.isConnect){
Start();
}
}
}
else if(message.GetTitle() == GameMessage.endGame){//结束游戏的信号
isConnect = false;
}
else if(message.GetTitle() == GameMessage.setSide){//设置黑白方信息
}
else if(message.GetTitle() == GameMessage.blackGo){//黑棋落子的信号
if(side != Side.BLACK)
{
SendMessage(GameMessage.message, "作弊!");
return;
}
Point p = (Point)message.GetData();
if(gameRule.gameboard.GetSide(p) == Side.VACANCY){
gameRule.gameboard.SetPoint(p, Side.BLACK);
SendMessage(GameMessage.blackGo, p);
}
if(gameRule.CheckWin(p))
SendMessage(GameMessage.blackWin,"黑棋获胜");
}
else if(message.GetTitle() == GameMessage.whiteGo){//白棋落子的信号
if(side != Side.WHITE)
{
SendMessage(GameMessage.message, "作弊!");
return;
}
Point p = (Point)message.GetData();
if(gameRule.gameboard.GetSide(p) == Side.VACANCY){
gameRule.gameboard.SetPoint(p, Side.WHITE);
SendMessage(GameMessage.whiteGo, p);
}
if(gameRule.CheckWin(p))
SendMessage(GameMessage.whiteWin,"白棋获胜");
}
else if(message.GetTitle() == GameMessage.blackWin){//黑棋获胜的信号
}
else if(message.GetTitle() == GameMessage.whiteWin){//白棋获胜的信号
}
else if(message.GetTitle() == GameMessage.exitGame){//中断游戏的信号
SendMessage(GameMessage.exitGame, "游戏结束");
isConnect = false;
}
else if(message.GetTitle() == GameMessage.message){//发送信息的信号
SendMessage(GameMessage.message, message.GetData());
}
else{}
} catch (IOException e) {
System.out.println("服务器端第"+id+"组对话"+name+"客户端接收信息失败");
isConnect = false;
} catch (ClassNotFoundException e) {
System.out.println("服务器端第"+id+"组对话"+name+"客户端接收的信息类型错误");
isConnect = false;
}
}
System.out.println("服务器端第"+id+"组对话"+name+"客户端连接已退出");
}
//向单方发送信息
boolean send(byte title, Object data){
GameMessage message = new GameMessage(title, data);
try {
writer.writeObject(message);
isConnect = true;
System.out.println("服务器端第"+id+"组对话向"+name+"客户端对话发送信息" + message.GetTitleString());
} catch (IOException e) {
System.out.println("服务器端第"+id+"组对话向"+name+"客户端对话发送信息" + message.GetTitleString());
isConnect = false;
}
return isConnect;
}
}
//向双方发送信息
boolean SendMessage(byte title, Object data){
GameMessage message = new GameMessage(title, data);
try {
client1.writer.writeObject(message);
client1.isConnect = true;
System.out.println("服务器端第"+id+"组对话向黑方发送信息" + message.GetTitleString());
} catch (Exception e) {
System.out.println("服务器端第"+id+"组对话向黑方发送信息失败");
System.out.println("原因:");
e.printStackTrace();
client1.isConnect = false;
}
try {
client2.writer.writeObject(message);
client2.isConnect = true;
System.out.println("服务器端第"+id+"组对话向白方发送信息" + message.GetTitleString());
} catch (Exception e) {
System.out.println("服务器端第"+id+"组对话向白方发送信息失败");
System.out.println("原因:");
e.printStackTrace();
client2.isConnect = false;
}
return client1.isConnect && client2.isConnect;
}
}