📄 analysemsg.java~1~
字号:
package stock;
import java.net.*;
import java.util.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2005</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class AnalyseMsg
{
String username, second, third; //解析字符串得到的值
ThreadPool QueueWithComputer; //与机对战的用户队列,人人对战的队列
// ChessRoom Room;
Vector QueueWithMan;
static ChessRoom cr = new ChessRoom();
static int Waiting = 0;
static String[] One = new String[2];
MainFrame mf;
public AnalyseMsg(String s, ThreadPool Queue, MainFrame mf, Vector Q)
{ //s一般格式为:from.to.message--<
//Room = cr;
this.QueueWithMan = Q;
this.mf = mf;
MessageClass mc = new MessageClass(s);
username = mc.first; //第一部分肯定是username
second = mc.second; //根据第二部分判断
third = mc.third;
//System.out.println("用户名" + username);
//System.out.println(second);
//System.out.println(third);
QueueWithComputer = Queue;
int tag = 0;
if (second.equals("#"))
{
tag = 1; //如果是和电脑下棋
}
else if (second.equals("#sit#"))
{
tag = 2; //如果是在某人来了,等待或与一等待的人直接开始
}
else if (second.equals("#leave#"))
{
tag = 3; //如果是离开某桌子
}
else if (second.equals("#getRoom#"))
{ //在否则,second就是另一个客户的id了
tag = 4;
}
else if (second.equals("#updateRoom#"))
{
tag = 5;
}
switch (tag)
{
case 1: //*人机下:1111.#.3.4 //A在3,4落子
int index = -1;
//mf.log.AddLine("用户名是:" + username);
mf.log.AddLine("用户名是:" + username);
if (QueueWithComputer.capacity() > 0)
{ //如果用户列表不空,作查找
index = QueueWithComputer.getIndex(username); //查找用户所在位置
}
if (index < 0)
{ //如果没找到,则添加
mf.log.AddLine("列表中没有该用户,添加:" + username);
index = QueueWithComputer.addUser(username); //index是刚添加进来的元素的下标
}
//将消息传给指定的线程
//从消息中提取用户的x和y
mf.log.AddLine("用户:" + username + "在列表中的位置是" + index);
//mf.log.AddLine(third + ". ");
MessageClass temp = new MessageClass(third + ". ");
int x = new Integer(temp.first).intValue();
int y = new Integer(temp.second).intValue();
mf.log.AddLine("传入的坐标是:" + x + "," + y);
ChessCompute now = QueueWithComputer.getThreadAt(index); //从线程池里面找到需要的线程
now.setInput(x, y);
mf.log.AddLine("计算开始");
now.run();
mf.log.AddLine("计算完毕");
x = now.getBestX();
y = now.getBestY();
mf.log.AddLine("传出的坐标将是:" + x + "," + y);
SendOneMsg("wzq.#." + username + "." + new Integer(x).toString() + "." +
new Integer(y).toString()); ////给服务器的反馈
break;
case 2: //1111.#come#.3
int tableid = new Integer(third).intValue();
cr.userSit(username, tableid);
//System.out.println(cr.isFull(tableid).equals("#"));
if (! (cr.isFull(tableid).equals("#")))
{
System.out.println("wzq.#connect#." + cr.isFull(tableid));
SendOneMsg("wzq.#connect#." + cr.isFull(tableid)); //坐满了 通知服务器让两个用户建立连接
mf.log.AddLine(cr.isFull(tableid) + "坐在了" + tableid + "号桌,准备开始游戏");
}
else
{
System.out.println("wzq.#wait#." + username);
SendOneMsg("wzq.#wait#." + username); //没坐满 让座好的人等待
mf.log.AddLine(username + "坐在了" + tableid + "号桌,等待另一用户的加入");
}
System.out.println(cr.getTableInfo());
break;
case 3: //1111.#leave#.3
int tid = new Integer(third).intValue(); //桌子号
String otherside = cr.leaveTable(username, tid);
System.out.println(otherside);
if (!otherside.equals("#"))
{
SendOneMsg("wzq.##." + username + "." + otherside + "leave finished"); //正常离开座位,计算模块成功赶走两人:)
}
System.out.println(cr.getTableInfo());
mf.log.AddLine(username + "和" + otherside + "成功离开" + tid + "号桌");
break;
case 4:
mf.log.AddLine("向" + username + "发送大厅信息成功!");
this.SendOneMsg("wzq.##." + username + cr.getTableInfo()); //发送大厅信息
break;
case 5:
//传来的info在third里面
cr.updateTableInfo(third);
this.SendOneMsg("wzq.##." + username + ".ok"); //表明更新成功
break;
}
}
public void SendOneMsg(String s)
{
SocketConn sc = new SocketConn(mf.TextField_ip.getText(), 6001);
sc.send(s);
sc.close();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -