crashtest/src/de/ctdo/crashtest/game/TheGame.java

326 lines
10 KiB
Java

package de.ctdo.crashtest.game;
import de.ctdo.crashtest.domotics.*;
import de.ctdo.crashtest.gui.*;
import de.ctdo.crashtest.irc.*;
import de.ctdo.crashtest.mpd.IMPDController;
import de.ctdo.crashtest.mpd.MPDController;
public class TheGame implements StatemachineListener, GuiEventListener, IRCEventListener {
private IGuiControl guiControl;
private IIrcClient ircClient;
private IStatemachine machine;
private IBuntiClient bunti;
private IMPDController mpdController;
private IRelaisboard relaisboard;
public TheGame(IGuiControl guiControl) {
this.guiControl = guiControl;
this.ircClient = new IrcClient("crashtest", "#crashtest","irc.hackint.eu");
this.bunti = new BuntiClient("bunti.ctdo.de", 8080);
this.mpdController = new MPDController("dampfradio.raum.ctdo.de");
this.relaisboard = new Relaisboard("/dev/ttyUSB0");
this.machine = new Statemachine();
initGame();
}
private void initGame() {
guiControl.addListener(this);
ircClient.addListener(this);
machine.addListener(this);
machine.reset();
relaisboard.open();
relaisboard.toggleRelais(2, 2000);
}
/**
* Event listener for state change events from statemachine
* @param newState the new game state from statemachine
*/
@Override
public void stateChanged(Statemachine.state newState) {
ircClient.say("New State: " + newState);
switch (newState) {
case IDLE:
machine.stopTimer();
guiControl.setExtra("");
guiControl.setWall("");
mpdController.setVolume(45);
mpdController.playSong("start", "mix");
bunti.setPar56(0,0,0);
bunti.setLampel(false,false,false);
guiControl.showCountDown(false);
break;
case ENTERED_ROOM:
mpdController.setVolume(70);
mpdController.playSong("tidirium", "welcome");
bunti.setLampel(false,false,false);
bunti.setPar56(20,0,100);
guiControl.showCountDown(true);
machine.startTimer(60*8);
break;
case TABLE_GAME_ONE:
guiControl.setWall("64K RAM SYSTEM 38911 BASIC BYTES FREE. **** COMMODORE 64 BASIC V2 ****");
mpdController.setVolume(70);
mpdController.playSong("K2", "Der Berg Ruft");
bunti.setLampel(true,false,false);
bunti.setPar56(255,0,100);
guiControl.showCountDown(true);
break;
case TABLE_GAME_TWO:
bunti.setLampel(false,true,false);
bunti.setPar56(255,0,100);
guiControl.showCountDown(true);
break;
case TABLE_GAME_THREE:
bunti.setLampel(false,true,false);
bunti.setPar56(255,35,0);
guiControl.showCountDown(true);
break;
case TABLE_GAME_FOUR:
mpdController.setVolume(60);
mpdController.playSong("Mo-Do","9 Eins Zwei Polizei");
bunti.setLampel(false,true,false);
bunti.setPar56(255,55,0);
guiControl.showCountDown(true);
break;
case TABLE_GAME_FIVE:
bunti.setLampel(false,true,false);
bunti.setPar56(255,75,0);
guiControl.showCountDown(true);
break;
case TABLE_GAME_SIX:
mpdController.setVolume(60);
mpdController.playSong("Zlatko & Jürgen","Großer Bruder");
bunti.setLampel(false,true,false);
bunti.setPar56(255,100,0);
guiControl.showCountDown(true);
break;
case TABLE_GAME_DONE:
bunti.setLampel(false,false,true);
bunti.setPar56(255,100,0);
guiControl.showCountDown(true);
machine.pauseTimer(true);
sayScore();
relaisboard.blinkRelais(2, 500, 6); // hint Button
break;
case ROKET_STARTED:
mpdController.setVolume(50);
mpdController.playSong("The Underdog Project", "Summer Jam");
bunti.setLampel(false,false,true);
bunti.setPar56(0, 255, 0);
ircClient.say("table game complete, r0kets now");
relaisboard.toggleRelais(0, 300);
guiControl.showCountDown(true);
machine.startTimer(7*60);
break;
case ROKET_DONE:
mpdController.setVolume(50);
mpdController.playSong("Coldplay", "Amsterdam");
bunti.setLampel(true,true,true);
bunti.setPar56(255, 196, 0);
guiControl.showCountDown(true);
machine.pauseTimer(true);
break;
}
}
@Override
public void timerTick(int tsecondsLeft) {
guiControl.setCountDown(tsecondsLeft);
if(tsecondsLeft == 0) ircClient.say("timer expired");
}
/**
* Event listener for keyPress events from the GUI
* @param key the pressed key
*/
@Override
public void keyPress(char key) {
machine.handleInput(key);
//guiControl.setExtra("btn: " + key);
}
/**
* Event lister for window closing event from the GUI
*/
@Override
public void windowClosing() {
machine.reset();
bunti.setPar56(0xff,0xff,0xff);
ircClient.say("bye");
relaisboard.close();
}
/**
* Event listener for IRC Messages
* @param message the message from the IRC user
*/
@Override
public void handleMessage(final String message) {
if(message.startsWith("help")) {
handleHelpCommand(message);
} else if(message.equals("reset")) {
machine.reset();
} else if(message.startsWith("state")) {
handleStateCommand(message);
} else if(message.startsWith("timer")) {
handleTimerCommand(message);
} else if(message.startsWith("score")) {
sayScore();
} else if(message.startsWith("wall")) {
guiControl.setWall(message.substring("wall".length()).trim());
} else if(message.startsWith("extra")) {
guiControl.setExtra(message.substring("extra".length()).trim());
} else {
ircClient.say("y u no use valid command?");
}
}
private void handleTimerCommand(final String message) {
String params = message.substring("timer".length()).trim().toLowerCase();
if(params.startsWith("start")) {
String timeStr = params.substring("start".length()).trim();
if(timeStr.length() > 0) {
try {
int time = Integer.parseInt(timeStr);
guiControl.showCountDown(true);
machine.startTimer(time);
ircClient.say("got it, starting with " + time + " seconds");
}
catch (NumberFormatException e) {
ircClient.say("your number looks a bit odd...");
}
} else {
ircClient.say("invalid parameter");
}
} else if(params.startsWith("stop")) {
machine.stopTimer();
guiControl.showCountDown(false);
ircClient.say("timer stopped");
} else if(params.startsWith("pause")) {
machine.pauseTimer(true);
} else if(params.startsWith("resume")) {
machine.pauseTimer(false);
}
}
private void handleStateCommand(final String message) {
if(message.equals("state")) {
ircClient.say(machine.getCurrentState().name());
} else {
String params = message.substring("state".length()).trim().toLowerCase();
Boolean ok = false;
for(Statemachine.state st: Statemachine.state.values()) {
String stateName = st.name().toLowerCase();
if(stateName.equals(params)) {
ok = true;
machine.setNewState(st);
ircClient.say("consider it done, sir!");
break;
}
}
if(!ok) ircClient.say("ehm, impossibruu!");
}
}
private void handleHelpCommand(final String message) {
if(message.equals("help")) {
ircClient.say("commands: help, reset, state, timer, wall, extra, score");
} else if(message.contains("reset")) {
ircClient.say("resets the game to IDLE");
} else if(message.contains("state")) {
ircClient.say("get or set game state");
ircClient.say("> state <NEWSTATE>");
ircClient.say("valid states: ");
int counter = 0;
StringBuilder sb = new StringBuilder();
for(Statemachine.state st: Statemachine.state.values()) {
sb.append(st.name());
sb.append(" ");
if(++counter == 4) {
counter = 0;
ircClient.say(sb.toString());
sb.setLength(0);
}
}
if(sb.length() > 0) {
ircClient.say(sb.toString());
}
} else if(message.contains("timer")) {
ircClient.say("control timer. commands are: start, stop, pause, resume.");
ircClient.say("for start give lenght in seconds as parameter");
ircClient.say("e.g.: timer start 120");
} else if(message.contains("wall")) {
ircClient.say("set text message on the screen");
} else if(message.contains("extra")) {
ircClient.say("set small extra message on the screen");
} else if(message.contains("score")) {
ircClient.say("i will tell you the current game score");
} else {
ircClient.say("dafuq?");
}
}
private void sayScore() {
ircClient.say("stateChangeCounter: " + machine.getStateChangeCounter());
// ircClient.say("timerlast: " + machine.getTimerSecondsLast());
int seconds = machine.getTimerSecondsLast();
int mins = seconds / 60;
int secs = seconds % 60;
ircClient.say(" " + mins + ":" + secs + "");
}
}