我有一个程序可以使用Java中的Rabbitmq 3.7在线程(游戏引擎)和其他线程(玩家)之间发送消息。我正在使用ubuntu 19.04。协议如下: -游戏引擎通知游戏开始[玩家不回复]
然后无限期地(遵循一些我可以修改的条件) -游戏引擎发送游戏状态[每个玩家都会回复其选择或选择通过的动作]
我注意到,经过两个步骤,该应用程序停留在已接收玩家动作(并输出其分数)的游戏引擎上,但此后什么也不做,这是不正常的。你能帮我吗
//game engine
@Override
public void run() {
try {
initialiseSetOfActions();
initialiseSetOfPolicies();
initialiseStateOfGame();
initialiseScores();
String EXCHANGE_NAME = "cybergame_gameengine";
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.exchangeDeclare(EXCHANGE_NAME, "direct");
String queueName = channel.queueDeclare().getQueue();
channel.queueBind(queueName, EXCHANGE_NAME, "");
System.out.println("Game engine Waiting for the messages.........on queue " + queueName);
Message msg = new Message();
msg.setFrom("gameEngine");
msg.setTo("players");
msg.setHeader("inform-game-on");
msg.setContent("inform-game-on");
sendBulk(msg);
msg.setHeader("inform-state");
PropositionalFormula state_of_game = new Conjunction(stateOfGame);
msg.setContent(state_of_game.toString());
msg.ticks +=1;
msg.msgNo +=1;
sendBulk(msg);
msg.ticks +=1;
msg.msgNo +=1;
DeliverCallback deliverCallback = (consumerTag, delivery) -> {
String message = new String(delivery.getBody(), "UTF-8");
System.out.println("Game engine Received: " + Message.fromJSON(message).toString());
if (Message.fromJSON(message).content.toString().contentEquals("pass")) {
System.out.println("Player "+ Message.fromJSON(message).from + " decided to pass");
}
else {
Action selectedAction = actionMap.get(Message.fromJSON(message).content);
updateStateOfGame(selectedAction);
updateScore(Message.fromJSON(message).from, selectedAction);
System.out.println("Player " + Message.fromJSON(message).from + " new score is " + scoreMap.get(Message.fromJSON(message).from).toString());
}
};
while (!setOfActions.isEmpty()) {
Queue.DeclareOk feedback = channel.queueDeclarePassive(queueName);
if (feedback.getMessageCount() == playerMap.size()) {
channel.basicConsume(queueName, true, deliverCallback, consumerTag -> { });
System.out.println(stateOfGame.toString() + " is state of game");
state_of_game = new Conjunction(stateOfGame);
msg.setContent(state_of_game.toString());
sendBulk(msg);
msg.ticks += 1;
msg.msgNo +=1;
}
}
}
catch (Exception e) {
System.out.println("Error: " + e.toString());
}
}
//player
public void run() {
try {
String EXCHANGE_NAME = "cybergame_players";
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.exchangeDeclare(EXCHANGE_NAME, "fanout");
String queueName = channel.queueDeclare().getQueue();
channel.queueBind(queueName, EXCHANGE_NAME, "");
System.out.println(this.playerName + ": "+ "Waiting for the messages.........on queue " + queueName);
Message msg = new Message();
msg.setFrom(this.playerName);
msg.setTo("gameEngine");
msg.setHeader("action");
msg.setContent("pass");
msg.ticks =0;
msg.msgNo =0;
DeliverCallback deliverCallback = (consumerTag, delivery) -> {
String message = new String(delivery.getBody(), "UTF-8");
System.out.println(this.playerName + " Received: " + Message.fromJSON(message).toString());
if (Message.fromJSON(message).header.toString().contentEquals("inform-game-on")) {
//System.out.println(this.playerName +": just an information, i do nothing");
}
else {
if (Message.fromJSON(message).ticks != msg.ticks) {
msg.setContent("pass");
}
else {
System.out.println("In the situation where game content is not <<inform-game-on>> and ticks coincide");
PlParser parser = new PlParser();
PropositionalFormula state_of_game = (PropositionalFormula) parser.parseFormula(Message.fromJSON(message).content.toString());
Set<Action> availableActions = availableActions(setOfActions, state_of_game.getLiterals());
int actionSize = availableActions.size();
List<Action> listactions = new ArrayList<Action>(availableActions);
Random rand = new Random();
int numChoice = rand.nextInt(actionSize);
Action action = listactions.get(numChoice);
msg.setContent(action.actionName.getName().toString());
}
}
try {
sendSingle(msg);
msg.ticks += 1;
msg.msgNo +=1;
}
catch (Exception e){
}
};
while (!setOfActions.isEmpty()) {
channel.basicConsume(queueName, true, deliverCallback, consumerTag -> { });
}
}
catch (Exception e) {
System.out.println("Error: " + e.toString());
}
}
输出是这样的:
游戏引擎正在等待消息.........在队列上amq.gen-Y6vvpni5ezATWVOxH6qFIw agent_0:正在等待消息.........在队列amq.gen-SzSOxQuG1q2ZdOLiE8-bag上 发送消息:消息编号:0来自:gameEngine收件人:玩家标题:notify-game-on内容:notify-game-on标题:0 agent_0收到:消息号:0来自:gameEngine收件人:玩家标题:notify-game-on内容:notify-game-on标题:0 消息已发送:消息号:1来自:gameEngine收件人:玩家标题:notify-state内容:start技巧:1 已发送消息:消息编号:0来自:agent_0收件人:gameEngine标头:操作内容:通过技巧:0 agent_0收到:消息号:1来自:gameEngine收件人:玩家标题:notify-state内容:start技巧:1 在游戏内容不是<>并且刻度一致的情况下 [开始]是比赛状态 接收到的游戏引擎:消息号:0来自:agent_0收件人:gameEngine标头:操作内容:通过技巧:0 玩家agent_0决定通过 发送消息:消息编号:1来自:agent_0收件人:gameEngine标头:action内容:C技巧:1 发送消息:消息编号:2来自:gameEngine收件人:玩家标题:notify-state内容:启动技巧:2 agent_0收到:消息号:2来自:gameEngine收件人:玩家标题:notify-state内容:start技巧:2 收到的游戏引擎:消息编号:1发件人:代理_0收件人:gameEngine标头:操作内容:C技巧:1 在游戏内容不是<>并且刻度一致的情况下 球员agent_0的新得分是7 发送消息:消息编号:2来自:agent_0收件人:gameEngine标头:操作内容:treatSensitiveFolder滴答声:2 收到的游戏引擎:消息编号:2来自:agent_0收件人:gameEngine标头:操作内容:treatSensitiveFolder滴答声:2 玩家agent_0的新比分是16。
正如我之前所说,我希望它能够持续运行。