Crossfire JXClient, Trunk
CommandQueue.java
Go to the documentation of this file.
1 /*
2  * This file is part of JXClient, the Fullscreen Java Crossfire Client.
3  *
4  * JXClient is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * JXClient is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with JXClient; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17  *
18  * Copyright (C) 2005-2008 Yann Chachkoff
19  * Copyright (C) 2006-2017,2019-2023 Andreas Kirschbaum
20  * Copyright (C) 2010-2012,2014-2018,2020-2023 Nicolas Weeger
21  */
22 
23 package com.realtime.crossfire.jxclient.queue;
24 
30 import java.util.LinkedList;
31 import java.util.List;
32 import org.jetbrains.annotations.NotNull;
33 
38 public class CommandQueue {
39 
44  private static final int MAX_PENDING_COMMANDS = 10;
45 
49  @NotNull
51 
57  @NotNull
58  private final List<Integer> pendingCommands = new LinkedList<>();
59 
63  @NotNull
65 
69  private int repeatCount;
70 
75  private boolean isRunning;
76 
81  @NotNull
82  @SuppressWarnings("FieldCanBeLocal")
84 
85  @Override
86  public void start() {
87  // ignore
88  }
89 
90  @Override
91  public void metaserver() {
92  // ignore
93  }
94 
95  @Override
96  public void preConnecting(@NotNull final String serverInfo) {
97  // ignore
98  }
99 
100  @Override
101  public void connecting(@NotNull final String serverInfo) {
102  clear();
103  }
104 
105  @Override
106  public void connecting(@NotNull final ClientSocketState clientSocketState, @NotNull final String param) {
107  // ignore
108  }
109 
110  @Override
111  public void connected() {
112  // ignore
113  }
114 
115  @Override
116  public void connectFailed(@NotNull final String reason) {
117  // ignore
118  }
119 
120  };
121 
129  public CommandQueue(@NotNull final CrossfireServerConnection crossfireServerConnection, @NotNull final GuiStateManager guiStateManager, @NotNull final PendingDirections pendingDirections) {
130  this.crossfireServerConnection = crossfireServerConnection;
131  this.pendingDirections = pendingDirections;
135  guiStateManager.addGuiStateListener(guiStateListener);
136  }
137 
142  private int getRepeatCount() {
143  final int oldRepeatCount = repeatCount;
145  return oldRepeatCount;
146  }
147 
151  private void resetRepeatCount() {
152  repeatCount = 0;
153  }
154 
159  public void addToRepeatCount(final int digit) {
160  assert 0 <= digit && digit <= 9;
161  repeatCount = (10*repeatCount+digit)%100000;
162  }
163 
167  private void clear() {
169  synchronized (pendingCommands) {
170  pendingCommands.clear();
172  isRunning = false;
173  }
174  }
175 
184  public void sendNcom(final boolean mustSend, @NotNull final String command) {
185  sendNcom(mustSend, getRepeatCount(), command);
186  }
187 
196  public void sendNcom(final boolean mustSend, final int repeat, @NotNull final String command) {
197  synchronized (pendingCommands) {
198  if (!mustSend && pendingCommands.size() >= MAX_PENDING_COMMANDS) {
199  return;
200  }
201 
202  final int packetNo = crossfireServerConnection.sendNcom(repeat, command);
203  pendingCommands.add(packetNo);
204 
205  final String[] tmp = command.split(" ", 2);
206  if (tmp[0].equalsIgnoreCase("run")) {
207  isRunning = true;
209  if (tmp.length >= 2) {
210  try {
211  final int direction = Integer.parseInt(tmp[1]);
212  if (1 <= direction && direction <= 8) {
213  pendingDirections.run(direction-1);
214  }
215  } catch (final NumberFormatException ignored) {
216  }
217  }
218  } else if (tmp[0].equalsIgnoreCase("run_stop")) {
219  isRunning = false;
221  } else {
222  final int direction;
223  if (command.equalsIgnoreCase("north")) {
224  direction = 0;
225  } else if (command.equalsIgnoreCase("northeast")) {
226  direction = 1;
227  } else if (command.equalsIgnoreCase("east")) {
228  direction = 2;
229  } else if (command.equalsIgnoreCase("southeast")) {
230  direction = 3;
231  } else if (command.equalsIgnoreCase("south")) {
232  direction = 4;
233  } else if (command.equalsIgnoreCase("southwest")) {
234  direction = 5;
235  } else if (command.equalsIgnoreCase("west")) {
236  direction = 6;
237  } else if (command.equalsIgnoreCase("northwest")) {
238  direction = 7;
239  } else {
240  direction = -1;
241  }
242  if (direction != -1) {
243  if (isRunning) {
244  pendingDirections.run(direction);
245  } else {
246  pendingDirections.add(direction, packetNo);
247  }
248  }
249  }
250  }
251  }
252 
258  public void sendRawString(@NotNull final String command) {
259  synchronized (pendingCommands) {
261  }
262  }
263 
269  public boolean stopRunning() {
270  synchronized (pendingCommands) {
271  final boolean result = isRunning;
272  if (result) {
273  sendNcom(true, 0, "run_stop");
274  assert !isRunning;
275  }
276  return result;
277  }
278  }
279 
284  public boolean checkRun() {
285  synchronized (pendingCommands) {
286  return isRunning;
287  }
288  }
289 
294  public boolean checkFire() {
295  return false; // XXX: implement
296  }
297 
304  public void sendMove(final int to, final int tag) {
306  }
307 
313  private void mapScrollReceived(final int dx, final int dy) {
314  final int direction;
315  if (dx == 0 && dy == -1) {
316  direction = 0;
317  } else if (dx == 1 && dy == -1) {
318  direction = 1;
319  } else if (dx == 1 && dy == 0) {
320  direction = 2;
321  } else if (dx == 1 && dy == 1) {
322  direction = 3;
323  } else if (dx == 0 && dy == 1) {
324  direction = 4;
325  } else if (dx == -1 && dy == 1) {
326  direction = 5;
327  } else if (dx == -1 && dy == 0) {
328  direction = 6;
329  } else if (dx == -1 && dy == -1) {
330  direction = 7;
331  } else {
332  return;
333  }
334  pendingDirections.mapScroll(direction);
335  }
336 
342  private void comcReceived(final int packetNo, final int time) {
343  synchronized (pendingCommands) {
344  final int index = pendingCommands.indexOf(packetNo);
345  if (index == -1) {
346  System.err.println("Error: got unexpected comc command #"+packetNo);
347  return;
348  }
349  if (index > 0) {
350  System.err.println("Warning: got out of order comc command #"+packetNo);
351  }
352 
353  pendingCommands.subList(0, index+1).clear();
354  }
355  pendingDirections.comc(packetNo);
356  }
357 
362  private void tickReceived(final int tickNo) {
364  }
365 
366 }
com.realtime.crossfire.jxclient.queue.CommandQueue.pendingDirections
final PendingDirections pendingDirections
Definition: CommandQueue.java:64
com.realtime.crossfire.jxclient
com.realtime.crossfire.jxclient.server
com.realtime.crossfire.jxclient.guistate.ClientSocketState
Definition: ClientSocketState.java:30
com.realtime.crossfire.jxclient.map
Definition: CfMap.java:23
com.realtime.crossfire.jxclient.queue.CommandQueue.tickReceived
void tickReceived(final int tickNo)
Definition: CommandQueue.java:362
com.realtime.crossfire.jxclient.queue.CommandQueue.getRepeatCount
int getRepeatCount()
Definition: CommandQueue.java:142
com.realtime.crossfire.jxclient.queue.CommandQueue.addToRepeatCount
void addToRepeatCount(final int digit)
Definition: CommandQueue.java:159
com.realtime.crossfire.jxclient.server.crossfire.CrossfireServerConnection.sendMove
void sendMove(int to, int tag, int nrof)
com.realtime.crossfire.jxclient.queue.CommandQueue.crossfireServerConnection
final CrossfireServerConnection crossfireServerConnection
Definition: CommandQueue.java:50
com.realtime.crossfire.jxclient.queue.CommandQueue.isRunning
boolean isRunning
Definition: CommandQueue.java:75
com.realtime.crossfire.jxclient.queue.CommandQueue.guiStateListener
final GuiStateListener guiStateListener
Definition: CommandQueue.java:83
com.realtime.crossfire.jxclient.queue.CommandQueue.mapScrollReceived
void mapScrollReceived(final int dx, final int dy)
Definition: CommandQueue.java:313
com.realtime.crossfire.jxclient.queue.CommandQueue.clear
void clear()
Definition: CommandQueue.java:167
com.realtime.crossfire.jxclient.map.PendingDirections.comc
void comc(final int packetNo)
Definition: PendingDirections.java:123
com.realtime.crossfire.jxclient.map.PendingDirections.add
void add(final int direction, final int packetNo)
Definition: PendingDirections.java:103
com.realtime.crossfire.jxclient.queue.CommandQueue.sendRawString
void sendRawString(@NotNull final String command)
Definition: CommandQueue.java:258
com.realtime.crossfire.jxclient.map.PendingDirections.run
void run(final int direction)
Definition: PendingDirections.java:113
com.realtime.crossfire.jxclient.queue.CommandQueue.comcReceived
void comcReceived(final int packetNo, final int time)
Definition: CommandQueue.java:342
com.realtime.crossfire.jxclient.guistate.GuiStateManager
Definition: GuiStateManager.java:34
com.realtime.crossfire.jxclient.guistate
Definition: ClientSocketState.java:23
com.realtime.crossfire.jxclient.queue.CommandQueue.CommandQueue
CommandQueue(@NotNull final CrossfireServerConnection crossfireServerConnection, @NotNull final GuiStateManager guiStateManager, @NotNull final PendingDirections pendingDirections)
Definition: CommandQueue.java:129
com.realtime.crossfire.jxclient.server.crossfire.CrossfireServerConnection.sendRawString
void sendRawString(@NotNull String command)
com.realtime.crossfire.jxclient.guistate.GuiStateListener
Definition: GuiStateListener.java:32
com.realtime.crossfire.jxclient.queue.CommandQueue.sendNcom
void sendNcom(final boolean mustSend, final int repeat, @NotNull final String command)
Definition: CommandQueue.java:196
com.realtime.crossfire.jxclient.server.crossfire.CrossfireServerConnection
Definition: CrossfireServerConnection.java:37
com.realtime.crossfire.jxclient.server.crossfire.CrossfireServerConnection.addCrossfireComcListener
void addCrossfireComcListener(@NotNull CrossfireComcListener listener)
com.realtime.crossfire.jxclient.queue.CommandQueue
Definition: CommandQueue.java:38
com.realtime.crossfire.jxclient.queue.CommandQueue.repeatCount
int repeatCount
Definition: CommandQueue.java:69
com.realtime.crossfire.jxclient.queue.CommandQueue.stopRunning
boolean stopRunning()
Definition: CommandQueue.java:269
com.realtime.crossfire.jxclient.queue.CommandQueue.resetRepeatCount
void resetRepeatCount()
Definition: CommandQueue.java:151
com.realtime.crossfire.jxclient.server.crossfire.CrossfireServerConnection.addCrossfireMapScrollListener
void addCrossfireMapScrollListener(@NotNull CrossfireMapScrollListener listener)
com.realtime.crossfire.jxclient.queue.CommandQueue.checkRun
boolean checkRun()
Definition: CommandQueue.java:284
com.realtime.crossfire.jxclient.map.PendingDirections.tick
void tick()
Definition: PendingDirections.java:169
com.realtime.crossfire.jxclient.server.crossfire
Definition: AbstractCrossfireServerConnection.java:23
com.realtime.crossfire.jxclient.map.PendingDirections
Definition: PendingDirections.java:40
com.realtime.crossfire
com.realtime
com.realtime.crossfire.jxclient.queue.CommandQueue.sendNcom
void sendNcom(final boolean mustSend, @NotNull final String command)
Definition: CommandQueue.java:184
com.realtime.crossfire.jxclient.queue.CommandQueue.checkFire
boolean checkFire()
Definition: CommandQueue.java:294
com
com.realtime.crossfire.jxclient.queue.CommandQueue.pendingCommands
final List< Integer > pendingCommands
Definition: CommandQueue.java:58
com.realtime.crossfire.jxclient.server.crossfire.CrossfireServerConnection.addCrossfireTickListener
void addCrossfireTickListener(@NotNull CrossfireTickListener listener)
com.realtime.crossfire.jxclient.queue.CommandQueue.MAX_PENDING_COMMANDS
static final int MAX_PENDING_COMMANDS
Definition: CommandQueue.java:44
com.realtime.crossfire.jxclient.map.PendingDirections.mapScroll
void mapScroll(final int direction)
Definition: PendingDirections.java:145
com.realtime.crossfire.jxclient.map.PendingDirections.clear
void clear()
Definition: PendingDirections.java:91
com.realtime.crossfire.jxclient.server.crossfire.CrossfireServerConnection.sendNcom
int sendNcom(int repeat, @NotNull String command)
com.realtime.crossfire.jxclient.queue.CommandQueue.sendMove
void sendMove(final int to, final int tag)
Definition: CommandQueue.java:304