Crossfire JXClient, Trunk
TestScriptProcess.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.scripts;
24 
44 import java.io.BufferedReader;
45 import java.io.IOException;
46 import java.io.StringReader;
47 import java.util.concurrent.Semaphore;
48 import java.util.concurrent.atomic.AtomicBoolean;
49 import java.util.regex.Matcher;
50 import java.util.regex.Pattern;
51 import org.jetbrains.annotations.NotNull;
52 import org.jetbrains.annotations.Nullable;
53 import org.junit.Assert;
54 
66 
70  @NotNull
71  private static final Pattern PATTERN_MAP_SCROLL = Pattern.compile("@map_scroll (-?[0-9]+) (-?[0-9]+)");
72 
76  @NotNull
77  private final StringBuilder sb = new StringBuilder();
78 
82  @NotNull
84 
88  @NotNull
89  private final BufferedReader br;
90 
94  @NotNull
95  private final Semaphore sem = new Semaphore(0);
96 
102  @NotNull
103  public static TestScriptProcess newTestScriptProcess(@NotNull final String scriptCommands) {
104  return newTestScriptProcess(scriptCommands, new TestCrossfireServerConnection());
105  }
106 
113  @NotNull
114  public static TestScriptProcess newTestScriptProcess(@NotNull final String scriptCommands, @NotNull final CrossfireServerConnection crossfireServerConnection) {
115  final GuiStateManager guiStateManager = new TestGuiStateManager();
117  final SkillSet skillSet = new SkillSet(guiStateManager);
118  final ExperienceTable experienceTable = new ExperienceTable();
119  final Stats stats = new Stats(experienceTable, skillSet, guiStateManager);
120  final ItemSet itemSet = new ItemSet();
121  final FloorView floorView = new FloorView(itemSet);
122  final FaceCache faceCache = new FaceCache();
123  final FacesManager facesManager = new AbstractFacesManager(faceCache) {
124 
125  @NotNull
126  @Override
127  protected FaceImages getFaceImages(final int faceNum, @Nullable final AtomicBoolean returnIsUnknownImage) {
128  throw new AssertionError("not implemented");
129  }
130 
131  };
132  final MapUpdaterState mapUpdaterState = new MapUpdaterState(facesManager, new Animations(guiStateManager), new CfAnimations(itemSet, facesManager, new Animations(null)));
133  mapUpdaterState.newMap(25, 17);
134  final SpellsManager spellsManager = new SpellsManager(guiStateManager, skillSet, stats);
136  }
137 
152  private TestScriptProcess(final int scriptId, @NotNull final String filename, @NotNull final CommandQueue commandQueue, @NotNull final CrossfireServerConnection crossfireServerConnection, @NotNull final Stats stats, @NotNull final FloorView floorView, @NotNull final ItemSet itemSet, @NotNull final SpellsManager spellsManager, @NotNull final MapUpdaterState mapUpdaterState, @NotNull final SkillSet skillSet, @NotNull final String scriptCommands) {
154  this.mapUpdaterState = mapUpdaterState;
155  br = new BufferedReader(new StringReader(scriptCommands));
156  }
157 
158  @Override
159  public void killScript() {
160  sb.append("killScript\n");
161  }
162 
163  @Nullable
164  @Override
165  protected String readFromScript() throws IOException {
166  while (true) {
167  final String line = br.readLine();
168  if (line == null) {
169  sb.append("readFromScript: EOF\n");
170  sem.release();
171  return null;
172  }
173 
174  sb.append("readFromScript: ").append(line).append("\n");
175  final Matcher matcherMapScroll = PATTERN_MAP_SCROLL.matcher(line);
176  if (matcherMapScroll.matches()) {
177  final int dx = Integer.parseInt(matcherMapScroll.group(1));
178  final int dy = Integer.parseInt(matcherMapScroll.group(2));
179  synchronized (mapUpdaterState.mapBegin()) {
180  mapUpdaterState.mapScroll(dx, dy);
182  }
183  continue;
184  }
185 
186  if (line.equals("@map_new")) {
187  synchronized (mapUpdaterState.mapBegin()) {
188  mapUpdaterState.newMap(25, 17);
190  }
191  continue;
192  }
193 
194  if (line.startsWith("@")) {
195  Assert.fail("unrecognized command: "+line);
196  }
197 
198  return line;
199  }
200  }
201 
202  @Nullable
203  @Override
204  protected String waitForTermination() {
205  try {
206  sem.acquire();
207  } catch (final InterruptedException ignored) {
208  return "current thread has been interrupted";
209  }
210  return null;
211  }
212 
213  @Override
214  protected void writeToScript(@NotNull final String data) {
215  sb.append("writeToScript: ").append(data);
216  }
217 
222  @NotNull
223  public String getAndClearEvents() {
224  final String result = sb.toString();
225  sb.setLength(0);
226  return result;
227  }
228 
229 }
com.realtime.crossfire.jxclient
com.realtime.crossfire.jxclient.faces.FacesManager
Maintains a mapping of face numbers to face data.
Definition: FacesManager.java:40
com.realtime.crossfire.jxclient.scripts.AbstractScriptProcess.filename
final String filename
The script command including arguments.
Definition: AbstractScriptProcess.java:73
com.realtime.crossfire.jxclient.map.MapUpdaterState.mapScroll
void mapScroll(final int dx, final int dy)
Part of "map2" parsing: scroll the map view.
Definition: MapUpdaterState.java:344
com.realtime.crossfire.jxclient.scripts.TestScriptProcess.TestScriptProcess
TestScriptProcess(final int scriptId, @NotNull final String filename, @NotNull final CommandQueue commandQueue, @NotNull final CrossfireServerConnection crossfireServerConnection, @NotNull final Stats stats, @NotNull final FloorView floorView, @NotNull final ItemSet itemSet, @NotNull final SpellsManager spellsManager, @NotNull final MapUpdaterState mapUpdaterState, @NotNull final SkillSet skillSet, @NotNull final String scriptCommands)
Creates a new instance.
Definition: TestScriptProcess.java:152
com.realtime.crossfire.jxclient.scripts.AbstractScriptProcess.commandQueue
final CommandQueue commandQueue
The CommandQueue for sending commands.
Definition: AbstractScriptProcess.java:79
com.realtime.crossfire.jxclient.server
com.realtime.crossfire.jxclient.items.ItemSet
Model class maintaining the CfItems known to the player.
Definition: ItemSet.java:44
com.realtime.crossfire.jxclient.faces.FaceCache
A cache for Face instances.
Definition: FaceCache.java:33
com.realtime.crossfire.jxclient.scripts.TestScriptProcess.mapUpdaterState
final MapUpdaterState mapUpdaterState
The MapUpdaterState for updating the current map information.
Definition: TestScriptProcess.java:83
com.realtime.crossfire.jxclient.map
Implements the map model which is shown in the map and magic map views.
Definition: CfMap.java:23
com.realtime.crossfire.jxclient.server.crossfire.CrossfireServerConnection
Adds encoding/decoding of crossfire protocol packets to a ServerConnection.
Definition: CrossfireServerConnection.java:37
com.realtime.crossfire.jxclient.scripts.TestScriptProcess.getAndClearEvents
String getAndClearEvents()
Returns the collected events.
Definition: TestScriptProcess.java:223
com.realtime.crossfire.jxclient.animations.Animations
Manages animations received from the server.
Definition: Animations.java:38
com.realtime.crossfire.jxclient.map.MapUpdaterState.newMap
void newMap(final int mapWidth, final int mapHeight)
A "newmap" command has been received.
Definition: MapUpdaterState.java:377
com.realtime.crossfire.jxclient.scripts.TestScriptProcess.br
final BufferedReader br
The script commands not yet executed.
Definition: TestScriptProcess.java:89
com.realtime.crossfire.jxclient.faces
Manages image information ("faces") needed to display the map view, items, and spell icons.
Definition: AbstractFaceQueue.java:23
com.realtime.crossfire.jxclient.scripts.TestScriptProcess.writeToScript
void writeToScript(@NotNull final String data)
Sends some data to the script process.
Definition: TestScriptProcess.java:214
com.realtime.crossfire.jxclient.spells.SpellsManager
Manages all known spells.
Definition: SpellsManager.java:50
com.realtime.crossfire.jxclient.guistate
Definition: ClientSocketState.java:23
com.realtime.crossfire.jxclient.scripts.TestScriptProcess.newTestScriptProcess
static TestScriptProcess newTestScriptProcess(@NotNull final String scriptCommands, @NotNull final CrossfireServerConnection crossfireServerConnection)
Creates a new instance.
Definition: TestScriptProcess.java:114
com.realtime.crossfire.jxclient.scripts.AbstractScriptProcess.itemSet
final ItemSet itemSet
The ItemSet instance to use.
Definition: AbstractScriptProcess.java:103
com.realtime.crossfire.jxclient.scripts.TestScriptProcess.newTestScriptProcess
static TestScriptProcess newTestScriptProcess(@NotNull final String scriptCommands)
Creates a new instance.
Definition: TestScriptProcess.java:103
com.realtime.crossfire.jxclient.map.PendingDirections
Maintains pending movements of the character.
Definition: PendingDirections.java:40
com.realtime.crossfire.jxclient.skills
Definition: Skill.java:23
com.realtime.crossfire.jxclient.guistate.GuiStateManager
Maintains the current GuiState.
Definition: GuiStateManager.java:34
com.realtime.crossfire.jxclient.animations
Definition: Animation.java:23
com.realtime.crossfire.jxclient.scripts.AbstractScriptProcess.scriptId
final int scriptId
The script ID identifying this script instance.
Definition: AbstractScriptProcess.java:67
com.realtime.crossfire.jxclient.map.MapUpdaterState.mapBegin
Object mapBegin()
Parsing of a "map2" command has been started.
Definition: MapUpdaterState.java:228
com.realtime.crossfire.jxclient.scripts.TestScriptProcess.sb
final StringBuilder sb
Records all events.
Definition: TestScriptProcess.java:77
com.realtime.crossfire.jxclient.scripts.AbstractScriptProcess.crossfireServerConnection
final CrossfireServerConnection crossfireServerConnection
The connection instance.
Definition: AbstractScriptProcess.java:85
com.realtime.crossfire.jxclient.stats.Stats
This is the representation of all the statistics of a player, like its speed or its experience.
Definition: Stats.java:44
com.realtime.crossfire.jxclient.scripts.TestScriptProcess.sem
final Semaphore sem
Signals the end of the simulated script process.
Definition: TestScriptProcess.java:95
com.realtime.crossfire.jxclient.map.CfAnimations
Manages a set of animated faces.
Definition: CfAnimations.java:47
com.realtime.crossfire.jxclient.scripts.TestScriptProcess
A ScriptProcess for regression tests.
Definition: TestScriptProcess.java:65
com.realtime.crossfire.jxclient.guistate.TestGuiStateManager
Runs the action directly.
Definition: TestGuiStateManager.java:8
com.realtime.crossfire.jxclient.server.crossfire
Definition: AbstractCrossfireServerConnection.java:23
com.realtime.crossfire.jxclient.scripts.AbstractScriptProcess.stats
final Stats stats
The Stats instance to watch.
Definition: AbstractScriptProcess.java:91
com.realtime.crossfire.jxclient.faces.FaceImages
Consists of three ImageIcons representing a Face.
Definition: FaceImages.java:32
com.realtime.crossfire.jxclient.queue
Definition: CommandQueue.java:23
com.realtime.crossfire.jxclient.map.MapUpdaterState.mapEnd
void mapEnd()
Parsing of "map2" has been finished.
Definition: MapUpdaterState.java:319
com.realtime.crossfire.jxclient.scripts.TestScriptProcess.waitForTermination
String waitForTermination()
Waits until the script has terminated.
Definition: TestScriptProcess.java:204
com.realtime.crossfire
com.realtime
com.realtime.crossfire.jxclient.faces.AbstractFacesManager
Abstract base class for FacesManager implementations.
Definition: AbstractFacesManager.java:35
com.realtime.crossfire.jxclient.stats.ExperienceTable
Stores experience <-> level mappings.
Definition: ExperienceTable.java:33
com
com.realtime.crossfire.jxclient.scripts.AbstractScriptProcess.floorView
final FloorView floorView
The FloorView to use.
Definition: AbstractScriptProcess.java:97
com.realtime.crossfire.jxclient.scripts.TestScriptProcess.killScript
void killScript()
Kills the script process.
Definition: TestScriptProcess.java:159
com.realtime.crossfire.jxclient.items
Definition: AbstractItemView.java:23
com.realtime.crossfire.jxclient.server.crossfire.TestCrossfireServerConnection
Implements CrossfireServerConnection for regression tests.
Definition: TestCrossfireServerConnection.java:39
com.realtime.crossfire.jxclient.scripts.AbstractScriptProcess
Default implementation for ScriptProcesses.
Definition: AbstractScriptProcess.java:55
com.realtime.crossfire.jxclient.scripts.AbstractScriptProcess.spellsManager
final SpellsManager spellsManager
The SpellsManager instance to use.
Definition: AbstractScriptProcess.java:109
com.realtime.crossfire.jxclient.items.FloorView
Provides a view to all items comprising the current floor view.
Definition: FloorView.java:36
com.realtime.crossfire.jxclient.skills.SkillSet
Maintain the set of skills as sent by the server.
Definition: SkillSet.java:39
com.realtime.crossfire.jxclient.map.MapUpdaterState
Update a CfMap model from protocol commands.
Definition: MapUpdaterState.java:49
com.realtime.crossfire.jxclient.scripts.TestScriptProcess.readFromScript
String readFromScript()
Reads a line from the script process.
Definition: TestScriptProcess.java:165
com.realtime.crossfire.jxclient.scripts.AbstractScriptProcess.skillSet
final SkillSet skillSet
The SkillSet for looking up skill names.
Definition: AbstractScriptProcess.java:121
com.realtime.crossfire.jxclient.scripts.TestScriptProcess.PATTERN_MAP_SCROLL
static final Pattern PATTERN_MAP_SCROLL
Matches the "@map_scroll" command.
Definition: TestScriptProcess.java:71
com.realtime.crossfire.jxclient.queue.CommandQueue
Maintains the pending (ncom) commands sent to the server.
Definition: CommandQueue.java:38
com.realtime.crossfire.jxclient.stats
Definition: ActiveSkillWatcher.java:23
com.realtime.crossfire.jxclient.spells
Definition: Spell.java:23