Skip to content

Commit

Permalink
WIP #9, WIP #11: Add non-proxy mode and modify command line to allow …
Browse files Browse the repository at this point in the history
…it to be invoked. Proxy mode now just a developer tool.
  • Loading branch information
Andrew Brindamour committed Feb 2, 2016
1 parent 3d1aab9 commit e785b15
Show file tree
Hide file tree
Showing 10 changed files with 208 additions and 69 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,19 @@ For each input that does not have a custom key binding specified in `input.cfg`,
### General
The entry point to the program is located in the `ClientMain.java` file.

### Developer Modes
The client has two special modes that are useful only to developers.

**Fake mode**: Fake mode runs a local simulation without actually connecting a real Artemis server. Highly recommended for basic UI development, as you
will not need to continuously connect and reconnect to a real server, speeding cycle times. A lot of functionality is simulated (e.g. overheating a
system works), but some is not (damcon teams don't really work). You can put the client in this mode by passing the `--fake` command line flag.

**Proxy mode**: Proxy mode allows a vanilla engineering client to connect to this client, which in turn connects to the server. With this setup, both clients will render the same data from the server, and either client can issue commands. This is a great way to check the behavior of the vanilla client
against this improved client without running afoul of the "one engineering console per ship" limit. You can put the client in this mode by passing
the `--proxy` command line flag.

You can also supply the server hostname/IP and optionally the port as command line arguments.

### IDE Configuration

#### Dependencies
Expand Down
82 changes: 57 additions & 25 deletions src/com/brindyblitz/artemis/ClientMain.java
Original file line number Diff line number Diff line change
@@ -1,54 +1,81 @@
package com.brindyblitz.artemis;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.brindyblitz.artemis.engconsole.EngineeringConsoleManager;
import com.brindyblitz.artemis.engconsole.FakeEngineeringConsoleManager;
import com.brindyblitz.artemis.engconsole.RealEngineeringConsoleManager;
import com.brindyblitz.artemis.engconsole.ui.UserInterfaceFrame;
import com.brindyblitz.artemis.protocol.WorldAwareRegularServer;
import com.brindyblitz.artemis.protocol.WorldAwareRobustProxyListener;
import com.brindyblitz.artemis.protocol.WorldAwareServer;

public class ClientMain {

public static void main(String[] args) throws IOException {
String host = "localhost";
String host = null;
int port = 2010;

if (args.length > 2) {
List<String> argList = new ArrayList<>(Arrays.asList(args));

boolean proxy = false;
boolean fake = false;
if (argList.contains("--proxy")) {
proxy = true;
argList.remove("--proxy");
}
if (argList.contains("--fake")) {
fake = true;
argList.remove("--fake");
}

if (proxy && fake) {
System.err.println("Cannot use proxy and fake modes together");
printUsage();
return;
}

if (!fake && (argList.size() > 2 || argList.size() < 1) || fake && argList.size() > 0) {
System.err.println("Incorrect number of arguments (" + args.length + ")");
printUsage();
return;
}

if (args.length == 0) {
new ClientMain();
} else {

if (args.length >= 1) {
host = args[0];
}
if (args.length == 2) {

if (argList.size() > 0) {
host = argList.get(0);
if (argList.size() == 2) {
try {
port = Integer.parseInt(args[1]);
port = Integer.parseInt(argList.get(1));
} catch (NumberFormatException e) {
System.err.println("Unable to parse port: '" + args[1] + "'!");
printUsage();
return;
}
}

new ClientMain(host, port);
}

new ClientMain(host, port, proxy, fake);
}

public ClientMain() {
EngineeringConsoleManager engineeringConsoleManager = new FakeEngineeringConsoleManager();
buildUIFrame(engineeringConsoleManager);
}

public ClientMain(String host, int port) throws IOException {
WorldAwareRobustProxyListener worldAwareRobustProxyListener = new WorldAwareRobustProxyListener(host, port, port);
EngineeringConsoleManager engineeringConsoleManager = new RealEngineeringConsoleManager(worldAwareRobustProxyListener);
public ClientMain(String host, int port, boolean proxy, boolean fake) throws IOException {
EngineeringConsoleManager engineeringConsoleManager;
if (fake) {
engineeringConsoleManager = new FakeEngineeringConsoleManager();
}
else {
WorldAwareServer worldAwareServer;
if (proxy) {
worldAwareServer = new WorldAwareRobustProxyListener(host, port, port);
}
else {
worldAwareServer = new WorldAwareRegularServer(host, port);
}
engineeringConsoleManager = new RealEngineeringConsoleManager(worldAwareServer);
}

buildUIFrame(engineeringConsoleManager);
}

Expand All @@ -59,8 +86,13 @@ private static UserInterfaceFrame buildUIFrame(EngineeringConsoleManager enginee
}

private static void printUsage() {
System.out.println("Either:\n" +
"Zero arguments: the client is set to local debug mode and where no network activity occurs\n" +
"Two arguments: <host> <port> (where host is either a hostname, e.g. 'localhost', or an IP address, e.g. '192.168.1.5')");
System.err.println("\nUsage:\n" +
" Normal Mode\n" +
" engineeringClient <host> [<port]\n" +
" Proxy Mode (a vanilla client must connect to this client and data can be seen on both clients)\n" +
" engineeringClient --proxy <host> [<port]\n" +
" Fake Mode (the client is set to local debug mode and where no network activity occurs)\n" +
" engineeringClient --fake\n"
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
import java.util.Map.Entry;

import com.brindyblitz.artemis.protocol.NotifyingSystemManager;
import com.brindyblitz.artemis.protocol.RobustProxyListener;
import com.brindyblitz.artemis.protocol.WorldAwareRobustProxyListener;
import com.brindyblitz.artemis.protocol.WorldAwareServer;

import net.dhleong.acl.enums.ShipSystem;
import net.dhleong.acl.protocol.core.eng.EngGridUpdatePacket.DamconStatus;
Expand All @@ -21,14 +20,14 @@

public class RealEngineeringConsoleManager extends BaseEngineeringConsoleManager {

private WorldAwareRobustProxyListener worldAwareRobustProxyListener;
private WorldAwareServer worldAwareServer;
private GameState gameState = GameState.DISCONNECTED;

public RealEngineeringConsoleManager(WorldAwareRobustProxyListener worldAwareRobustProxyListener) {
this.worldAwareRobustProxyListener = worldAwareRobustProxyListener;
this.worldAwareRobustProxyListener.events.on(RobustProxyListener.Events.CONNECTION_STATE_CHANGE, () -> this.updateGameState());
this.worldAwareRobustProxyListener.getSystemManager().events.on(NotifyingSystemManager.Events.CHANGE, () -> this.systemManagerChange());
this.worldAwareRobustProxyListener.getSystemManager().setSystemGrid(getShipSystemGrid());
public RealEngineeringConsoleManager(WorldAwareServer worldAwareServer) {
this.worldAwareServer = worldAwareServer;
this.worldAwareServer.onEvent(WorldAwareServer.Events.CONNECTION_STATE_CHANGE, () -> this.updateGameState());
this.worldAwareServer.getSystemManager().events.on(NotifyingSystemManager.Events.CHANGE, () -> this.systemManagerChange());
this.worldAwareServer.getSystemManager().setSystemGrid(getShipSystemGrid());
}

private void systemManagerChange() {
Expand All @@ -38,10 +37,10 @@ private void systemManagerChange() {

private void updateGameState() {
GameState oldGameState = gameState;
if (!worldAwareRobustProxyListener.isConnected()) {
if (!worldAwareServer.isConnected()) {
gameState = GameState.DISCONNECTED;
}
else if ( this.worldAwareRobustProxyListener.getSystemManager().getPlayerShip(0) != null) {
else if ( this.worldAwareServer.getSystemManager().getPlayerShip(0) != null) {
gameState = GameState.INGAME;
}
else {
Expand All @@ -60,40 +59,40 @@ public GameState getGameState() {

@Override
public int getSystemEnergyAllocated(ShipSystem system) {
if (this.worldAwareRobustProxyListener.getSystemManager().getPlayerShip(0) == null) {
if (this.worldAwareServer.getSystemManager().getPlayerShip(0) == null) {
return 100;
}
return (int)(this.worldAwareRobustProxyListener.getSystemManager().getPlayerShip(0).getSystemEnergy(system) * 300);
return (int)(this.worldAwareServer.getSystemManager().getPlayerShip(0).getSystemEnergy(system) * 300);
}

@Override
public int getSystemCoolantAllocated(ShipSystem system) {
if (this.worldAwareRobustProxyListener.getSystemManager().getPlayerShip(0) == null) {
if (this.worldAwareServer.getSystemManager().getPlayerShip(0) == null) {
return 0;
}
return this.worldAwareRobustProxyListener.getSystemManager().getPlayerShip(0).getSystemCoolant(system);
return this.worldAwareServer.getSystemManager().getPlayerShip(0).getSystemCoolant(system);
}

@Override
public int getSystemHeat(ShipSystem system) {
if (this.worldAwareRobustProxyListener.getSystemManager().getPlayerShip(0) == null) {
if (this.worldAwareServer.getSystemManager().getPlayerShip(0) == null) {
return 0;
}

return (int) (this.worldAwareRobustProxyListener.getSystemManager().getPlayerShip(0).getSystemHeat(system) * 100);
return (int) (this.worldAwareServer.getSystemManager().getPlayerShip(0).getSystemHeat(system) * 100);
}

@Override
public int getSystemHealth(ShipSystem system) {
if (this.worldAwareRobustProxyListener.getSystemManager().getPlayerShip(0) == null) {
if (this.worldAwareServer.getSystemManager().getPlayerShip(0) == null) {
return 100;
}
return (int) (this.worldAwareRobustProxyListener.getSystemManager().getHealthOfSystem(system) * 100);
return (int) (this.worldAwareServer.getSystemManager().getHealthOfSystem(system) * 100);
}

@Override
public int getTotalCoolantRemaining() {
if (this.worldAwareRobustProxyListener.getSystemManager().getPlayerShip(0) == null) {
if (this.worldAwareServer.getSystemManager().getPlayerShip(0) == null) {
return Artemis.DEFAULT_COOLANT;
}
final int totalCoolantUsed = Arrays.stream(ShipSystem.values()).mapToInt(system -> this.getSystemCoolantAllocated(system)).sum();
Expand All @@ -102,16 +101,16 @@ public int getTotalCoolantRemaining() {

@Override
public int getTotalShipCoolant() {
if (this.worldAwareRobustProxyListener.getSystemManager().getPlayerShip(0) == null) {
if (this.worldAwareServer.getSystemManager().getPlayerShip(0) == null) {
return 8;
}
return this.worldAwareRobustProxyListener.getSystemManager().getPlayerShip(0).getAvailableCoolant();
return this.worldAwareServer.getSystemManager().getPlayerShip(0).getAvailableCoolant();
}

@Override
public Map<GridCoord, Float> getGridHealth() {
Map<GridCoord, Float> result = new HashMap<>();
for (Entry<GridCoord, Float> entry : this.worldAwareRobustProxyListener.getSystemManager().getGridDamages()) {
for (Entry<GridCoord, Float> entry : this.worldAwareServer.getSystemManager().getGridDamages()) {
result.put(entry.getKey(), 1.0f - entry.getValue());
}
return result;
Expand All @@ -121,7 +120,7 @@ public Map<GridCoord, Float> getGridHealth() {
public List<DamconStatus> getRawDamconStatus() {
List<DamconStatus> teams = new ArrayList<>();
for(int teamNumber = 0; teamNumber < 16; teamNumber++) {
DamconStatus damcon = this.worldAwareRobustProxyListener.getSystemManager().getDamcon(teamNumber);
DamconStatus damcon = this.worldAwareServer.getSystemManager().getDamcon(teamNumber);
if (damcon != null) {
teams.add(damcon);
}
Expand All @@ -131,41 +130,41 @@ public List<DamconStatus> getRawDamconStatus() {

@Override
public float getTotalEnergyRemaining() {
if (this.worldAwareRobustProxyListener.getSystemManager().getPlayerShip(0) == null) {
if (this.worldAwareServer.getSystemManager().getPlayerShip(0) == null) {
return 0;
}

return this.worldAwareRobustProxyListener.getSystemManager().getPlayerShip(0).getEnergy();
return this.worldAwareServer.getSystemManager().getPlayerShip(0).getEnergy();
}

@Override
public void incrementSystemEnergyAllocated(ShipSystem system, int amount) {
if (this.worldAwareRobustProxyListener.getSystemManager().getPlayerShip(0) == null) {
if (this.worldAwareServer.getSystemManager().getPlayerShip(0) == null) {
return;
}
super.incrementSystemEnergyAllocated(system, amount);
}

@Override
public void incrementSystemCoolantAllocated(ShipSystem system, int amount) {
if (this.worldAwareRobustProxyListener.getSystemManager().getPlayerShip(0) == null) {
if (this.worldAwareServer.getSystemManager().getPlayerShip(0) == null) {
return;
}
super.incrementSystemCoolantAllocated(system, amount);
}

@Override
protected void updateSystemEnergyAllocated(ShipSystem system, int amount) {
this.worldAwareRobustProxyListener.getServer().send(new EngSetEnergyPacket(system, amount));
this.worldAwareServer.getServer().send(new EngSetEnergyPacket(system, amount));
}

@Override
protected void updateSystemCoolantAllocated(ShipSystem system, int amount) {
this.worldAwareRobustProxyListener.getServer().send(new EngSetCoolantPacket(system, amount));
this.worldAwareServer.getServer().send(new EngSetCoolantPacket(system, amount));
}

public void moveDamconTeam(int teamId, GridCoord coord) {
System.out.println("Moving DAMCON team " + teamId + " to grid " + coord);
this.worldAwareRobustProxyListener.getServer().send(new EngSendDamconPacket(teamId, coord));
this.worldAwareServer.getServer().send(new EngSendDamconPacket(teamId, coord));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public EnergySlider(ShipSystem system, EngineeringConsoleManager engineeringCons
@Override
protected int getStatusPctInt() {
// TODO: >>> TESTME
return 100 * (int)(this.engineeringConsoleManager.getTotalEnergyRemaining() / 1000f);
return (int)(100 * (this.engineeringConsoleManager.getTotalEnergyRemaining() / 1000f));
}

@Override
Expand Down
2 changes: 2 additions & 0 deletions src/com/brindyblitz/artemis/engconsole/ui/InGamePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ public void handleKeyPress(KeyEvent e) {
for (EnhancedDamconStatus damconStatus : this.engineeringConsoleManager.getDamconTeams()) {
System.out.println(damconStatus);
}

System.out.println("Energy remaining: " + this.engineeringConsoleManager.getTotalEnergyRemaining());

System.out.println("\n\n\n");
} else if (kc == KeyEvent.VK_EQUALS) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import com.brindyblitz.artemis.engconsole.EngineeringConsoleManager;
import com.brindyblitz.artemis.engconsole.EngineeringConsoleManager.Events;
import com.brindyblitz.artemis.engconsole.EngineeringConsoleManager.GameState;

public class UserInterfaceFrame extends JFrame implements KeyListener {

Expand Down Expand Up @@ -48,11 +49,18 @@ public UserInterfaceFrame(EngineeringConsoleManager engineeringConsoleManager) {
getContentPane().add(loading);
this.setVisible(true);

updateCurrentPanel();
engineeringConsoleManager.onEvent(Events.GAME_STATE_CHANGE, () -> {
switchToInGamePanel();
updateCurrentPanel();
});
}

private void updateCurrentPanel() {
if (engineeringConsoleManager.getGameState() == GameState.INGAME) {
switchToInGamePanel();
}
}

private void switchToInGamePanel() {
if (inGamePanel != null) {
this.getContentPane().remove(inGamePanel);
Expand Down
Loading

0 comments on commit e785b15

Please sign in to comment.