Compare commits

...

43 Commits

Author SHA1 Message Date
Christian Kroll 38dcffe18d ignore Eclipse metadata files 2012-06-29 17:33:40 +02:00
Lucas Pleß 8acace8c19 fixed mpd nullpointer exceptions 2012-06-28 20:12:36 +02:00
Lucas Pleß f106675753 extended mpd control to check if track is playing after starting new song 2012-06-28 17:03:39 +02:00
Lucas Pleß d50ee914ef extended mpd control to check if track is playing after starting new song 2012-06-28 16:45:01 +02:00
Lucas Pleß f35fc861c7 fixing mpd (again and again)
edited game, if time is >60 song will not change
2012-06-26 18:33:27 +02:00
Lucas Pleß e300e2325c - small fixes in the game
- rokets will be switched off, if not solved in time
2012-06-22 01:54:07 +02:00
Lucas Pleß 1b69470d9c - added extra state to table game
- added jingle player via pulseaudio
2012-06-22 01:32:34 +02:00
Lucas Pleß 0765e1c017 bugfixing in mpd 2012-06-22 01:32:01 +02:00
Lucas Pleß e416d44400 added extra state to table game 2012-06-22 01:31:41 +02:00
Lucas Pleß e95ea0359f cleanup 2012-06-22 01:30:18 +02:00
Lucas Pleß 2a40f0971e changed mpd back to thread and fixed playlist handling 2012-06-19 21:37:01 +02:00
Lucas Pleß 54fd08440b minor fixes 2012-06-19 20:01:14 +02:00
Lucas Pleß 377d3a5853 minor fixes 2012-06-19 19:09:25 +02:00
Lucas Pleß 3fead845ac minor fixes 2012-06-17 22:17:16 +02:00
Christian Kroll 60b0ff6ea9 The chat gem is working as intended. 2012-06-15 19:50:31 +02:00
Lucas Pleß 65a2237348 cleanup 2012-06-15 16:06:03 +02:00
Lucas Pleß 50ee753667 some refactoring 2012-06-15 09:38:08 +02:00
Lucas Pleß da3aef9704 removed threads from mpdcontroller 2012-06-15 09:33:15 +02:00
Lucas Pleß 28989f2511 hide the cursor 2012-06-15 01:05:03 +02:00
Lucas Pleß 92f1a7282a added wall limit per time 2012-06-15 01:04:53 +02:00
Lucas Pleß 03abf212c1 made lampel controlled via udp since lampel got new hardware and firmware 2012-06-15 00:30:11 +02:00
Lucas Pleß c127302dc1 reverted a window size request change.
taskbar was visible on pc
2012-06-15 00:29:34 +02:00
Lucas Pleß 1c99fe4956 time variable names corrected from "Last" to "Left" 2012-06-14 14:24:22 +02:00
Lucas Pleß 58a8028bd5 cleanup 2012-06-14 11:40:08 +02:00
Christian Kroll dc6a1f318c i dare you copy'n'paste 2012-06-14 06:14:20 +02:00
Christian Kroll 65a86f4040 narf 2012-06-14 06:12:17 +02:00
Christian Kroll 5278dc13e6 make gem feature more battle.net like and adapted it to xleave's code style 2012-06-14 06:06:35 +02:00
Christian Kroll ef9f37e12f chat gem added 2012-06-14 00:23:25 +02:00
Lucas Pleß 9b8791a468 made disco loop forever and controlable via irc 2012-06-13 23:31:12 +02:00
Lucas Pleß e97ae350ad debug system output 2012-06-13 23:30:45 +02:00
Lucas Pleß 49d6152369 removed annoying message 2012-06-13 23:09:23 +02:00
Lucas Pleß 1758c33e67 changed loosed to neutral state and made white light onto it 2012-06-13 23:06:12 +02:00
Lucas Pleß 104896b42d made commands valid with ">" prefix rather than nickname 2012-06-13 23:01:20 +02:00
Lucas Pleß 51f32a5e7a made relaisboard lamp blinking loopable 2012-06-13 23:00:51 +02:00
Lucas Pleß 6d43e934a9 changed player control to
- turn off random
- set crossfade to 0
2012-06-10 22:15:29 +02:00
Lucas Pleß d26fca6efb changed game a bit 2012-06-10 21:59:11 +02:00
Lucas Pleß 535a66a216 added playlist controls to mpdcontroller 2012-06-10 21:55:33 +02:00
Lucas Pleß 2bbd040f4d changed error messages 2012-06-10 21:39:42 +02:00
Lucas Pleß e3b45dbd9a added disco!!!111oneeleven 2012-06-08 04:22:22 +02:00
Lucas Pleß 4fbf9f91fc added relay to suppress light barrier and third circle contact when not needed 2012-06-08 03:47:59 +02:00
Lucas Pleß 8819f79f3c some test foo 2012-06-08 03:13:57 +02:00
Lucas Pleß 899cad5719 added rating explanation to irc 2012-06-08 03:13:48 +02:00
Lucas Pleß b388cce5f0 changed game behaviour 2012-06-08 00:58:08 +02:00
22 changed files with 939 additions and 450 deletions

2
.gitignore vendored
View File

@ -0,0 +1,2 @@
/.classpath
/.project

View File

@ -1,29 +1,10 @@
package de.ctdo.crashtest; package de.ctdo.crashtest;
import de.ctdo.crashtest.game.TheGame; import de.ctdo.crashtest.game.TheGame;
import de.ctdo.crashtest.gui.IGuiControl;
import de.ctdo.crashtest.gui.MainGui; import de.ctdo.crashtest.gui.MainGui;
public class Steuerung { public class Steuerung {
private TheGame game;
private IGuiControl gui;
public static void main(String args[]) { public static void main(String args[]) {
new TheGame(new MainGui());
new Steuerung();
} }
public Steuerung() {
gui = new MainGui();
game = new TheGame(gui);
}
} }

View File

@ -1,42 +1,48 @@
package de.ctdo.crashtest; package de.ctdo.crashtest;
import de.ctdo.crashtest.domotics.IRelaisboard; import de.ctdo.crashtest.domotics.BuntiClient;
import de.ctdo.crashtest.domotics.Relaisboard; import de.ctdo.crashtest.gui.GuiEventListener;
import de.ctdo.crashtest.mpd.IMPDController; import de.ctdo.crashtest.gui.MainGui;
import de.ctdo.crashtest.mpd.MPDController;
public class TestClass {
public TestClass() {
public class TestClass { BuntiClient bunti = new BuntiClient("bunti.ctdo.de",8080);
int pause = 500;
try {
public static void main(String args[]) throws InterruptedException { while(true) {
for(int i = 0; i< 3; i++) {
Thread.sleep(pause);
bunti.setLampel(true, false, true);
Thread.sleep(pause);
bunti.setLampel(false, true, true);
Thread.sleep(pause);
bunti.setLampel(false, false, true);
Thread.sleep(pause);
bunti.setLampel(false, false, false);
Thread.sleep(pause);
bunti.setLampel(true, true, true);
Thread.sleep(pause);
bunti.setLampel(false, false, true);
Thread.sleep(pause);
bunti.setLampel(true, true, true);
Thread.sleep(pause);
bunti.setLampel(false, false, false);
}
IRelaisboard board = new Relaisboard("/dev/ttyUSB0"); //Thread.sleep(2000);
board.open(); }
}
/* catch (InterruptedException ignored) {}
board.setRelais(1,false);
//board.setRelais(1, true);
board.toggleRelais(1, 500);
Thread.sleep(1000);
board.toggleRelais(1, 500);
Thread.sleep(1000);
board.toggleRelais(1, 500);
//board.setRelais(1, false);
Thread.sleep(4000); */
board.blinkRelais(2, 500, 6); // hint Button
Thread.sleep(6000);
board.close();
} }
public static void main(String args[]) {
new TestClass();
}
} }

View File

@ -8,11 +8,12 @@ import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.impl.client.DefaultHttpClient;
import java.io.*; import java.io.*;
import java.net.Socket; import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class BuntiClient implements IBuntiClient { public class BuntiClient implements IBuntiClient {
private String baseAddress; private final String baseAddress;
private final HttpClient client = new DefaultHttpClient();
public BuntiClient(String server, int port) { public BuntiClient(String server, int port) {
baseAddress = "http://" + server + ":" + port + "/"; baseAddress = "http://" + server + ":" + port + "/";
@ -25,6 +26,7 @@ public class BuntiClient implements IBuntiClient {
@Override @Override
public void run() { public void run() {
try { try {
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(baseAddress + "/control/devices"); HttpPost post = new HttpPost(baseAddress + "/control/devices");
post.addHeader("Content-Type", "application/json"); post.addHeader("Content-Type", "application/json");
@ -66,6 +68,52 @@ public class BuntiClient implements IBuntiClient {
new Thread(r).start(); new Thread(r).start();
} }
@Override
public void setPar56(final int par, final int red, final int green, final int blue) {
Runnable r = new Runnable() {
@Override
public void run() {
try {
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(baseAddress + "/control/devices");
post.addHeader("Content-Type", "application/json");
StringBuilder jsonString = new StringBuilder();
jsonString.append("{ \"timeStamp\": 0, \"updates\": [ ");
jsonString.append(" {\"deviceId\": ");
jsonString.append(par);
jsonString.append(", \"options\": { \"red\": ");
jsonString.append(red);
jsonString.append(", \"green\": ");
jsonString.append(green);
jsonString.append(", \"blue\": ");
jsonString.append(blue);
jsonString.append(" } } ");
jsonString.append("] }");
StringEntity entity = new StringEntity( jsonString.toString(), "UTF-8");
post.setEntity(entity);
client.execute(post);
post.abort();
} catch (UnsupportedEncodingException e) {
Logger.sLog("bunti error: " + e.getMessage());
} catch (ClientProtocolException e) {
Logger.sLog("bunti error: " + e.getMessage());
} catch (IOException e) {
Logger.sLog("bunti error: " + e.getMessage());
}
}
};
new Thread(r).start();
}
@Override @Override
public void setLampel(final boolean red, final boolean yellow, final boolean green) { public void setLampel(final boolean red, final boolean yellow, final boolean green) {
@ -78,18 +126,13 @@ public class BuntiClient implements IBuntiClient {
if( red ) value |= 0x04; if( red ) value |= 0x04;
try { try {
Socket client = new Socket("lampel.ctdo.de", 2701); byte buffer[] = ("io set port 2 " + Integer.toHexString(value) + '\n').getBytes();
client.setSoTimeout(800);
DatagramSocket udp = new DatagramSocket();
InetAddress ia = InetAddress.getByName("lampel.raum.ctdo.de");
DatagramPacket packet = new DatagramPacket(buffer, buffer.length, ia, 2701);
DataOutputStream outToServer = new DataOutputStream(client.getOutputStream()); udp.send(packet);
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(client.getInputStream()));
outToServer.writeBytes("io set port 2 " + Integer.toHexString(value) + '\n');
inFromServer.readLine();
client.close();
} catch (IOException e) { } catch (IOException e) {
Logger.sLog("lampel error: " + e.getMessage()); Logger.sLog("lampel error: " + e.getMessage());
} }
@ -98,30 +141,6 @@ public class BuntiClient implements IBuntiClient {
new Thread(r).start(); new Thread(r).start();
/* try {
HttpPost post = new HttpPost(baseAddress + "/control/devices");
post.addHeader("Content-Type", "application/json");
StringEntity entity = new StringEntity(
"{ \"timeStamp\": 0, \"updates\": [ {\"deviceId\": 4, \"options\": { \"red\": "+
red+", \"green\": "+green+", \"yellow\": "+yellow+" } } ] }" ,
"UTF-8");
post.setEntity(entity);
HttpResponse response = client.execute(post);
System.out.println(response);
post.abort();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}*/
} }
} }

View File

@ -0,0 +1,45 @@
package de.ctdo.crashtest.domotics;
import de.ctdo.crashtest.log.Logger;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
public class ExtraSoundControl implements IExtraSoundControl {
@Override
public void playJingle(final String name) {
System.out.println("ExtraSoundControl: playJingle name=" + name );
Runnable r = new Runnable() {
@Override
public void run() {
try {
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet("http://dampfradio.raum.ctdo.de/controller.php?command=jingle&mpd=mpd1&arg=" +name);
client.execute(get);
get.abort();
} catch (UnsupportedEncodingException e) {
Logger.sLog("ExtraSoundControl error: " + e.getMessage());
} catch (ClientProtocolException e) {
Logger.sLog("ExtraSoundControl error: " + e.getMessage());
} catch (IOException e) {
Logger.sLog("ExtraSoundControl error: " + e.getMessage());
}
}
};
new Thread(r).start();
}
}

View File

@ -1,11 +1,8 @@
package de.ctdo.crashtest.domotics; package de.ctdo.crashtest.domotics;
/**
* @author: lucas
* @date: 01.06.12 14:25
*/
public interface IBuntiClient { public interface IBuntiClient {
void setPar56(final int red, final int green, final int blue); void setPar56(final int red, final int green, final int blue);
void setPar56(final int par, final int red, final int green, final int blue);
void setLampel(final boolean red, final boolean yellow, final boolean green); void setLampel(final boolean red, final boolean yellow, final boolean green);
} }

View File

@ -0,0 +1,5 @@
package de.ctdo.crashtest.domotics;
public interface IExtraSoundControl {
void playJingle(String name);
}

View File

@ -1,11 +1,10 @@
package de.ctdo.crashtest.domotics; package de.ctdo.crashtest.domotics;
public interface IRelaisboard { public interface IRelaisboard {
void setRelais(final int relais, final boolean state); void setRelais(final int relais, final boolean state);
void toggleRelais(final int relais, final int milliseconds); void toggleRelais(final int relais, final int milliseconds);
void blinkRelais(final int relais, final int pause, final int count); void blinkRelais(final int relais, final int pause);
boolean open(); void blinkRelaisStop(final int relais);
void open();
void close(); void close();
} }

View File

@ -4,47 +4,54 @@ import de.ctdo.crashtest.log.Logger;
import gnu.io.*; import gnu.io.*;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.Enumeration; import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
public class Relaisboard implements IRelaisboard { public class Relaisboard implements IRelaisboard {
private SerialPort serialPort; private SerialPort serialPort;
private OutputStream outputStream; private OutputStream outputStream;
private Boolean serialPortGeoeffnet = false; private Boolean serialPortGeoeffnet = false;
private String portName = "/dev/ttyUSB0"; private String portName = "/dev/ttyUSB0";
private Object mLock = new Object(); private final Object mLock = new Object();
private final Map<Integer,Thread> threadMap = new ConcurrentHashMap<Integer, Thread>();
private final Map<Integer,Boolean> stopMap = new ConcurrentHashMap<Integer, Boolean>();
public Relaisboard(final String port) { public Relaisboard(final String port) {
this.portName = port; this.portName = port;
} }
private void sendData(final int relais, final boolean state) { private void sendData(final int relais, final boolean state) {
if(relais >= 0 && relais < 8 && outputStream != null) { synchronized (mLock) {
char charsOff[] = { 'a','b','c','d','e','f','g','h' }; if(relais >= 0 && relais < 8 && outputStream != null) {
char charsOn[] = { 'A','B','C','D','E','F','G','H' }; char charsOff[] = { 'a','b','c','d','e','f','g','h' };
char charsOn[] = { 'A','B','C','D','E','F','G','H' };
try { try {
if(state) { if(state) {
outputStream.write(charsOn[relais]); outputStream.write(charsOn[relais]);
} else { } else {
outputStream.write(charsOff[relais]); outputStream.write(charsOff[relais]);
}
System.out.println("Relaisboard: sendData " + relais + " = " + state);
outputStream.flush();
} catch (IOException e) {
Logger.sLog("Relaisboard error: Fehler beim Senden");
} }
outputStream.flush();
} catch (IOException e) {
Logger.sLog("Fehler beim Senden");
} }
} }
} }
@Override @Override
public boolean open() { public void open() {
serialPortGeoeffnet = false; serialPortGeoeffnet = false;
Boolean foundPort = false; Boolean foundPort = false;
if (serialPortGeoeffnet != false) { if (serialPortGeoeffnet) {
Logger.sLog("Serialport bereits geöffnet"); Logger.sLog("Serialport bereits geöffnet");
return false; return;
} }
Enumeration enumComm = CommPortIdentifier.getPortIdentifiers(); Enumeration enumComm = CommPortIdentifier.getPortIdentifiers();
@ -57,9 +64,9 @@ public class Relaisboard implements IRelaisboard {
break; break;
} }
} }
if (foundPort != true) { if (!foundPort) {
Logger.sLog("Serialport nicht gefunden: " + portName); Logger.sLog("Serialport nicht gefunden: " + portName);
return false; return;
} }
try { try {
@ -70,99 +77,109 @@ public class Relaisboard implements IRelaisboard {
outputStream = serialPort.getOutputStream(); outputStream = serialPort.getOutputStream();
serialPortGeoeffnet = true; serialPortGeoeffnet = true;
return true;
} catch (PortInUseException e) { } catch (PortInUseException e) {
Logger.sLog("Port belegt"); Logger.sLog("Relaisboard error: Port belegt " + portName);
} catch (IOException e) { } catch (IOException e) {
Logger.sLog("Keinen Zugriff auf OutputStream"); Logger.sLog("Relaisboard error: Keinen Zugriff auf OutputStream");
} catch(UnsupportedCommOperationException e) { } catch(UnsupportedCommOperationException e) {
Logger.sLog("Konnte Schnittstellen-Paramter nicht setzen"); Logger.sLog("Relaisboard error: Konnte Schnittstellen-Paramter nicht setzen");
} }
return false;
} }
@Override @Override
public void close() { public void close() {
if ( serialPortGeoeffnet ) { if ( serialPortGeoeffnet ) {
serialPort.close(); serialPort.close();
serialPortGeoeffnet = false;
outputStream = null;
} }
serialPortGeoeffnet = false;
outputStream = null;
} }
@Override @Override
public void setRelais(final int relais, final boolean state) { public void setRelais(final int relais, final boolean state) {
if(!serialPortGeoeffnet) return; if(!serialPortGeoeffnet) return;
synchronized (mLock) { stopRelaisThread(relais);
Runnable r = new Runnable() {
@Override
public void run() {
sendData(relais, state);
}
};
new Thread(r).start(); Runnable r = new Runnable() {
} @Override
public void run() {
sendData(relais, state);
}
};
new Thread(r).start();
} }
@Override @Override
public void toggleRelais(final int relais, final int milliseconds) { public void toggleRelais(final int relais, final int milliseconds) {
if(!serialPortGeoeffnet) return; if(!serialPortGeoeffnet) return;
synchronized (mLock) { stopRelaisThread(relais);
Runnable r = new Runnable() {
@Override
public void run() {
sendData(relais, true);
try { Runnable r = new Runnable() {
Thread.sleep(milliseconds); @Override
} catch (InterruptedException e) { public void run() {
e.printStackTrace(); sendData(relais, true);
}
sendData(relais, false); try {
} Thread.sleep(milliseconds);
}; } catch (InterruptedException ignored) { }
new Thread(r).start(); sendData(relais, false);
} }
};
new Thread(r).start();
} }
@Override @Override
public void blinkRelais(final int relais, final int pause, final int count) { public void blinkRelais(final int relais, final int pause) {
if(!serialPortGeoeffnet) return; if(!serialPortGeoeffnet) return;
stopRelaisThread(relais);
synchronized (mLock) { Runnable r = new Runnable() {
Runnable r = new Runnable() { @Override
@Override public void run() {
public void run() { while(true) {
int i; sendData(relais, true);
for(i = 0; i< count; i++) { try {
sendData(relais, true); Thread.sleep(pause);
} catch (InterruptedException ignored) { }
try { if(stopMap.get(relais)) return;
Thread.sleep(pause);
} catch (InterruptedException e) {
e.printStackTrace();
}
sendData(relais, false); sendData(relais, false);
try { try {
Thread.sleep(pause); Thread.sleep(pause);
} catch (InterruptedException e) { } catch (InterruptedException ignored) { }
e.printStackTrace();
} if(stopMap.get(relais)) return;
}
} }
}; }
};
new Thread(r).start(); stopMap.put(relais, false);
Thread thread = new Thread(r);
threadMap.put(relais,thread);
thread.start();
}
@Override
public void blinkRelaisStop(final int relais) {
stopRelaisThread(relais);
}
private void stopRelaisThread(final int relais) {
Thread thread = threadMap.get(relais);
if(thread != null) {
stopMap.put(relais, true);
thread.interrupt();
threadMap.remove(relais);
} }
} }
} }

View File

@ -4,12 +4,12 @@ public interface IStatemachine {
void addListener(StatemachineListener listener); void addListener(StatemachineListener listener);
void reset(); void reset();
Statemachine.state getCurrentState(); Statemachine.state getCurrentState();
Statemachine.state getLastState();
void setNewState(Statemachine.state newState); void setNewState(Statemachine.state newState);
int getStateChangeCounter(); int getStateChangeCounter();
void handleInput(char input); void handleInput(char input);
int getTimerSecondsLast(); int getTimerSecondsLeft();
int getTimerSeconds();
void startTimer(int seconds); void startTimer(int seconds);
void stopTimer(); void stopTimer();
void pauseTimer(boolean pause); void pauseTimer(boolean pause);

View File

@ -1,7 +1,5 @@
package de.ctdo.crashtest.game; package de.ctdo.crashtest.game;
import de.ctdo.crashtest.log.Logger;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Timer; import java.util.Timer;
@ -18,26 +16,28 @@ public class Statemachine implements IStatemachine {
TABLE_GAME_FIVE, TABLE_GAME_FIVE,
TABLE_GAME_SIX, TABLE_GAME_SIX,
TABLE_GAME_DONE, TABLE_GAME_DONE,
TABLE_GAME_WRONG,
ROKET_STARTED, ROKET_STARTED,
ROKET_DONE ROKET_DONE,
NEUTRAL
} }
private final char RESET = '1'; public static final char BLUE_BUTTON = 'E';
private final char BLUE_BUTTON = 'E'; public static final char LIGHT_BARRIER = 'F';
private final char LIGHT_BARRIER = 'F'; public static final char TABLE_ONE = 'G';
private final char TABLE_ONE = 'G'; public static final char TABLE_TWO = 'H';
private final char TABLE_TWO = 'H'; public static final char TABLE_THREE = 'I';
private final char TABLE_THREE = 'I'; public static final char ROKET_INPUT = 'B';
private final char ROKET_INPUT = 'B';
private final List<StatemachineListener> statemachineListenerList; private final List<StatemachineListener> statemachineListenerList;
private Timer timer; private Timer timer;
private long lastHandleInput; private long lastHandleInput;
private int stateChangeCounter; private int stateChangeCounter;
private state lastState;
private state currentState; private state currentState;
private int timertSecondsLast; private int timertSecondsLeft;
private int timertSeconds;
public Statemachine() { public Statemachine() {
currentState = state.IDLE; currentState = state.IDLE;
@ -53,7 +53,12 @@ public class Statemachine implements IStatemachine {
public state getCurrentState() { public state getCurrentState() {
return currentState; return currentState;
} }
@Override
public state getLastState() {
return lastState;
}
@Override @Override
public void setNewState(state newState) { public void setNewState(state newState) {
currentState = newState; currentState = newState;
@ -81,9 +86,8 @@ public class Statemachine implements IStatemachine {
state newState = getNewState(input); state newState = getNewState(input);
if( newState != currentState ) { if( newState != currentState ) {
lastState = currentState;
stateChangeCounter++; stateChangeCounter++;
Logger.sLog("newState = " + newState);
currentState = newState; currentState = newState;
onStateChanged(); onStateChanged();
} }
@ -92,27 +96,31 @@ public class Statemachine implements IStatemachine {
} }
@Override @Override
public int getTimerSecondsLast() { public int getTimerSecondsLeft() {
return timertSecondsLast / 10; return timertSecondsLeft / 10;
}
@Override
public int getTimerSeconds() {
return timertSeconds / 10;
} }
@Override @Override
public void startTimer(int seconds) { public void startTimer(int seconds) {
Logger.sLog("starting timer"); timertSecondsLeft = seconds*10;
timertSecondsLast = seconds*10; timertSeconds = seconds*10;
scheduleTimer(); scheduleTimer();
} }
@Override @Override
public void stopTimer() { public void stopTimer() {
Logger.sLog("stopping timer");
if(timer != null) timer.cancel(); if(timer != null) timer.cancel();
timertSecondsLast = 0; timertSecondsLeft = 0;
timertSeconds = 0;
} }
@Override @Override
public void pauseTimer(boolean pause) { public void pauseTimer(boolean pause) {
Logger.sLog("pausing timer: " + pause);
if(pause) { if(pause) {
if(timer != null) timer.cancel(); if(timer != null) timer.cancel();
} else { } else {
@ -132,7 +140,7 @@ public class Statemachine implements IStatemachine {
private void onTimerTick() { private void onTimerTick() {
for(StatemachineListener listener: statemachineListenerList) { for(StatemachineListener listener: statemachineListenerList) {
listener.timerTick(timertSecondsLast); listener.timerTick(timertSecondsLeft);
} }
} }
@ -144,77 +152,78 @@ public class Statemachine implements IStatemachine {
private state getNewState(char input) { private state getNewState(char input) {
state retVal = currentState; state retVal = currentState;
if(input == RESET) { switch (currentState) {
retVal = state.IDLE; case IDLE:
} else { if(input == LIGHT_BARRIER) {
retVal = state.ENTERED_ROOM;
switch (currentState) { }
case IDLE: break;
if(input == LIGHT_BARRIER) { case ENTERED_ROOM:
retVal = state.ENTERED_ROOM; if(input == TABLE_ONE) {
} retVal = state.TABLE_GAME_ONE;
break; }
case ENTERED_ROOM: break;
if(input == TABLE_ONE) { case TABLE_GAME_ONE:
retVal = state.TABLE_GAME_ONE; if(input == TABLE_TWO) {
} retVal = state.TABLE_GAME_TWO;
break; } else if (input == TABLE_THREE) {
case TABLE_GAME_ONE: retVal = state.TABLE_GAME_WRONG;
if(input == TABLE_TWO) { }
retVal = state.TABLE_GAME_TWO; break;
} case TABLE_GAME_TWO:
break; if(input == TABLE_THREE) {
case TABLE_GAME_TWO: retVal = state.TABLE_GAME_THREE;
if(input == TABLE_THREE) { } else if (input == TABLE_ONE) {
retVal = state.TABLE_GAME_THREE; retVal = state.TABLE_GAME_WRONG;
} else if (input == TABLE_ONE) { }
retVal = state.TABLE_GAME_ONE; break;
} case TABLE_GAME_THREE:
break; if(input == TABLE_TWO) {
case TABLE_GAME_THREE: retVal = state.TABLE_GAME_FOUR;
if(input == TABLE_TWO) { } else if (input == TABLE_ONE) {
retVal = state.TABLE_GAME_FOUR; retVal = state.TABLE_GAME_WRONG;
} else if (input == TABLE_ONE) { }
retVal = state.TABLE_GAME_ONE; break;
} case TABLE_GAME_FOUR:
break; if(input == TABLE_THREE) {
case TABLE_GAME_FOUR: retVal = state.TABLE_GAME_FIVE;
if(input == TABLE_THREE) { } else if (input == TABLE_ONE) {
retVal = state.TABLE_GAME_FIVE; retVal = state.TABLE_GAME_WRONG;
} else if (input == TABLE_ONE) { }
retVal = state.TABLE_GAME_ONE; break;
} case TABLE_GAME_FIVE:
break; if(input == TABLE_ONE) {
case TABLE_GAME_FIVE: retVal = state.TABLE_GAME_SIX;
if(input == TABLE_ONE) { } else if (input == TABLE_TWO) {
retVal = state.TABLE_GAME_SIX; retVal = state.TABLE_GAME_WRONG;
} else if (input == TABLE_TWO) { }
retVal = state.TABLE_GAME_ONE; break;
} case TABLE_GAME_SIX:
break; if(input == TABLE_THREE) {
case TABLE_GAME_SIX: retVal = state.TABLE_GAME_DONE;
if(input == TABLE_THREE) { } else if (input == TABLE_TWO) {
retVal = state.TABLE_GAME_DONE; retVal = state.TABLE_GAME_WRONG;
} else if (input == TABLE_TWO) { }
retVal = state.TABLE_GAME_ONE; break;
} case TABLE_GAME_DONE:
break; if(input == BLUE_BUTTON) {
case TABLE_GAME_DONE: retVal = state.ROKET_STARTED;
if(input == BLUE_BUTTON) { }
retVal = state.ROKET_STARTED; break;
} case TABLE_GAME_WRONG:
break; if(input == TABLE_ONE) {
case ROKET_STARTED: retVal = state.TABLE_GAME_ONE;
if(input == ROKET_INPUT) { }
retVal = state.ROKET_DONE; break;
} case ROKET_STARTED:
} if(input == ROKET_INPUT) {
retVal = state.ROKET_DONE;
}
} }
return retVal; return retVal;
} }
private void scheduleTimer() { private void scheduleTimer() {
if(timer != null) timer.cancel(); if(timer != null) timer.cancel();
@ -223,18 +232,15 @@ public class Statemachine implements IStatemachine {
TimerTask timerTask = new TimerTask() { TimerTask timerTask = new TimerTask() {
@Override @Override
public void run() { public void run() {
timertSecondsLast--; timertSecondsLeft--;
onTimerTick(); onTimerTick();
if(timertSecondsLast <= 0) { if(timertSecondsLeft <= 0) {
if(timer != null) timer.cancel(); if(timer != null) timer.cancel();
} }
} }
}; };
timer.scheduleAtFixedRate(timerTask, 100, 100); timer.scheduleAtFixedRate(timerTask, 100, 100);
} }
} }

View File

@ -6,37 +6,43 @@ import de.ctdo.crashtest.irc.*;
import de.ctdo.crashtest.mpd.IMPDController; import de.ctdo.crashtest.mpd.IMPDController;
import de.ctdo.crashtest.mpd.MPDController; import de.ctdo.crashtest.mpd.MPDController;
import java.util.Random;
public class TheGame implements StatemachineListener, GuiEventListener, IRCEventListener { public class TheGame implements StatemachineListener, GuiEventListener, IRCEventListener {
private IGuiControl guiControl; private final IGuiControl guiControl;
private IIrcClient ircClient; private final IIrcClient ircClient;
private IStatemachine machine; private final IStatemachine machine;
private IBuntiClient bunti; private final IBuntiClient bunti;
private IMPDController mpdController; private final IMPDController mpdController;
private IRelaisboard relaisboard; private final IRelaisboard relaisboard;
private final IExtraSoundControl extraSoundControl;
private int gamerRating = 3;
private Thread discoThread;
private boolean shouldStopDisco;
private boolean gemActivated = false;
private int gemCounter = 0;
private boolean startedHurrySound = false;
private char lastInput;
private boolean sproing;
public TheGame(IGuiControl guiControl) { public TheGame(IGuiControl guiControl1) {
this.guiControl = guiControl; guiControl = guiControl1;
ircClient = new IrcClient("crashtest", "#crashtest","irc.ctdo.de");
bunti = new BuntiClient("bunti.ctdo.de", 8080);
mpdController = new MPDController("dampfradio.raum.ctdo.de");
relaisboard = new Relaisboard("/dev/ttyUSB0");
machine = new Statemachine();
extraSoundControl = new ExtraSoundControl();
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); guiControl.addListener(this);
ircClient.addListener(this); ircClient.addListener(this);
machine.addListener(this); machine.addListener(this);
machine.reset(); machine.reset();
relaisboard.open(); relaisboard.open();
relaisboard.toggleRelais(2, 2000); resetDomotics();
} }
/** /**
* Event listener for state change events from statemachine * Event listener for state change events from statemachine
* @param newState the new game state from statemachine * @param newState the new game state from statemachine
@ -44,129 +50,241 @@ public class TheGame implements StatemachineListener, GuiEventListener, IRCEvent
@Override @Override
public void stateChanged(Statemachine.state newState) { public void stateChanged(Statemachine.state newState) {
ircClient.say("New State: " + newState); ircClient.say("New State: " + newState);
switch (newState) { switch (newState) {
case IDLE: case NEUTRAL:
discoStop();
bunti.setPar56(255,255,255);
bunti.setLampel(false,false,false);
machine.stopTimer(); machine.stopTimer();
break;
case IDLE:
gamerRating = 3;
mpdController.clearPlaylist();
machine.stopTimer();
startedHurrySound = false;
resetDomotics();
discoStop();
guiControl.setExtra(""); guiControl.setExtra("");
guiControl.setWall(""); guiControl.setWall("");
mpdController.setVolume(45);
mpdController.playSong("start", "mix");
bunti.setPar56(0,0,0);
bunti.setLampel(false,false,false);
guiControl.showCountDown(false); guiControl.showCountDown(false);
playRandomStartMix();
//mpdController.setVolume(50);
break; break;
case ENTERED_ROOM: case ENTERED_ROOM:
mpdController.setVolume(70); relaisboard.setRelais(7, false); // disable light barrier over relais
mpdController.playSong("tidirium", "welcome"); mpdController.playSong("crashtest", "entered_room");
bunti.setLampel(false,false,false); bunti.setLampel(false,false,false);
bunti.setPar56(20,0,100); bunti.setPar56(20,0,100);
guiControl.setWall("bitte die tuer schliessen. dann kann das spiel beginnen.");
guiControl.showCountDown(true); guiControl.showCountDown(true);
machine.startTimer(60*8); machine.startTimer(60*8);
mpdController.setVolume(50);
break;
case TABLE_GAME_WRONG:
extraSoundControl.playJingle("block");
if(!startedHurrySound) {
mpdController.playSong("crashtest", "table_game_one");
}
bunti.setLampel(true,false,false);
sproing = true;
break; break;
case TABLE_GAME_ONE: case TABLE_GAME_ONE:
guiControl.setWall("64K RAM SYSTEM 38911 BASIC BYTES FREE. **** COMMODORE 64 BASIC V2 ****"); relaisboard.setRelais(6, true); // enable third green circle
mpdController.setVolume(70); extraSoundControl.playJingle("jump");
mpdController.playSong("K2", "Der Berg Ruft");
bunti.setLampel(true,false,false); if(machine.getLastState() != Statemachine.state.TABLE_GAME_WRONG) {
bunti.setPar56(255,0,100); if(!startedHurrySound) {
mpdController.playSong("crashtest", "table_game_one");
}
}
bunti.setLampel(false, true, false);
bunti.setPar56(20,0,100);
guiControl.showCountDown(true); guiControl.showCountDown(true);
break; break;
case TABLE_GAME_TWO: case TABLE_GAME_TWO:
//mpdController.playSong("crashtest", "table_game_two");
extraSoundControl.playJingle("jump");
bunti.setLampel(false,true,false); bunti.setLampel(false,true,false);
bunti.setPar56(255,0,100); bunti.setPar56(100,0,100);
guiControl.showCountDown(true); guiControl.showCountDown(true);
break; break;
case TABLE_GAME_THREE: case TABLE_GAME_THREE:
//mpdController.playSong("crashtest", "table_game_three");
extraSoundControl.playJingle("jump");
bunti.setLampel(false,true,false); bunti.setLampel(false,true,false);
bunti.setPar56(255,35,0); bunti.setPar56(255,35,0);
guiControl.showCountDown(true); guiControl.showCountDown(true);
break; break;
case TABLE_GAME_FOUR: case TABLE_GAME_FOUR:
mpdController.setVolume(60); if(!startedHurrySound) {
mpdController.playSong("Mo-Do","9 Eins Zwei Polizei"); mpdController.playSong("crashtest", "table_game_four");
}
extraSoundControl.playJingle("jump");
bunti.setLampel(false,true,false); bunti.setLampel(false,true,false);
bunti.setPar56(255,55,0); bunti.setPar56(200,100,0);
guiControl.showCountDown(true); guiControl.showCountDown(true);
break; break;
case TABLE_GAME_FIVE: case TABLE_GAME_FIVE:
//mpdController.playSong("crashtest", "table_game_five");
extraSoundControl.playJingle("jump");
bunti.setLampel(false,true,false); bunti.setLampel(false,true,false);
bunti.setPar56(255,75,0); bunti.setPar56(150,150,0);
guiControl.showCountDown(true); guiControl.showCountDown(true);
break; break;
case TABLE_GAME_SIX: case TABLE_GAME_SIX:
mpdController.setVolume(60); //mpdController.playSong("crashtest", "table_game_six");
mpdController.playSong("Zlatko & Jürgen","Großer Bruder");
extraSoundControl.playJingle("jump");
bunti.setLampel(false,true,false); bunti.setLampel(false,true,false);
bunti.setPar56(255,100,0); bunti.setPar56(100,200,0);
guiControl.showCountDown(true); guiControl.showCountDown(true);
break; break;
case TABLE_GAME_DONE: case TABLE_GAME_DONE:
mpdController.playSong("crashtest", "table_game_done");
extraSoundControl.playJingle("jump");
bunti.setLampel(false,false,true); bunti.setLampel(false,false,true);
bunti.setPar56(255,100,0); bunti.setPar56(100,255,0);
guiControl.showCountDown(true); guiControl.showCountDown(true);
machine.pauseTimer(true); machine.pauseTimer(true);
// spieler haben 8 Minuten, wenn sie es in weniger als 4 minuten schaffen
// gibts +1, in weniger als 2 minuten gibts +2
if(machine.getTimerSecondsLeft() >= 6*60 ) {
rate(2, "table game faster than 2 minutes");
} else if(machine.getTimerSecondsLeft() > 4*60) {
rate(1, "table game faster than 4 minutes");
}
if(machine.getStateChangeCounter() > 100) {
rate(-1, "more than 100 tries");
}
sayScore(); sayScore();
relaisboard.blinkRelais(2, 500, 6); // hint Button relaisboard.setRelais(6, false); // disable third green circle
relaisboard.blinkRelais(2, 700); // hint Button
break; break;
case ROKET_STARTED: case ROKET_STARTED:
mpdController.setVolume(50); mpdController.playSong("crashtest", "roket_started");
mpdController.playSong("The Underdog Project", "Summer Jam"); mpdController.addToPlayList("crashtest", "roket_started2");
startedHurrySound = false;
bunti.setLampel(false,false,true); bunti.setLampel(false,true,false);
bunti.setPar56(0, 255, 0); bunti.setPar56(0, 255, 0);
ircClient.say("table game complete, r0kets now"); relaisboard.toggleRelais(0, 300); // r0kets toogle
relaisboard.toggleRelais(3, 10000); // oven
relaisboard.toggleRelais(0, 300); relaisboard.blinkRelaisStop(2); // stop hint button lamp
relaisboard.setRelais(2, false);
guiControl.setWall("Pizza ist fertig!");
guiControl.showCountDown(true); guiControl.showCountDown(true);
machine.startTimer(7*60); machine.startTimer(7*60);
break; break;
case ROKET_DONE: case ROKET_DONE:
mpdController.setVolume(50); mpdController.playSong("crashtest", "roket_done");
mpdController.playSong("Coldplay", "Amsterdam"); //mpdController.setVolume(60);
bunti.setLampel(false,false,true);
bunti.setPar56(255, 255, 255);
bunti.setLampel(true,true,true); guiControl.setWall("willkommen im chaostreff dortmund");
bunti.setPar56(255, 196, 0);
guiControl.showCountDown(true); guiControl.showCountDown(true);
machine.pauseTimer(true); machine.pauseTimer(true);
// spieler haben 7 Minuten, wenn sie es in weniger als 4 minuten schaffen
// gibts +1, in weniger als 2 minuten gibts +2
if(machine.getTimerSecondsLeft() >= 5*60 ) {
rate(2, "r0kets faster than 2 minutes");
} else if(machine.getTimerSecondsLeft() > 3*60) {
rate(1, "r0kets faster than 4 minutes");
}
sayScore();
discoStart();
break; break;
} }
} }
/**
* Event listener for timer ticks from statemachine.
* @param tsecondsLeft the seconds left on the current game
*/
@Override @Override
public void timerTick(int tsecondsLeft) { public void timerTick(int tsecondsLeft) {
if(gemCounter>0) gemCounter--;
guiControl.setCountDown(tsecondsLeft); guiControl.setCountDown(tsecondsLeft);
if(tsecondsLeft == 0) ircClient.say("timer expired"); if(tsecondsLeft == 0) {
} ircClient.say("timer expired");
Statemachine.state state = machine.getCurrentState();
if( state == Statemachine.state.TABLE_GAME_ONE || state == Statemachine.state.TABLE_GAME_TWO ||
state == Statemachine.state.TABLE_GAME_THREE || state == Statemachine.state.TABLE_GAME_FOUR ||
state == Statemachine.state.TABLE_GAME_FIVE || state == Statemachine.state.TABLE_GAME_SIX ||
state == Statemachine.state.TABLE_GAME_WRONG || state == Statemachine.state.ROKET_STARTED ) {
rate(-2, "game not done in time");
guiControl.setWall("die Zeit ist abgelaufen");
sayScore();
mpdController.playSong("crashtest","timeouted");
if(state == Statemachine.state.ROKET_STARTED) {
relaisboard.toggleRelais(0, 300); // r0kets toogle (so there will probably be switched off)
ircClient.say("if ready, use >reset");
} else {
ircClient.say("if ready, set state with >state TABLE_GAME_DONE");
}
/*
if(state != Statemachine.state.ROKET_STARTED) {
ircClient.say("switching to roket started");
machine.setNewState(Statemachine.state.ROKET_STARTED);
} */
}
} else {
if(tsecondsLeft % 600 == 0) {
sayScore();
}
if(tsecondsLeft <= 630 && !startedHurrySound) {
Statemachine.state state = machine.getCurrentState();
if( state != Statemachine.state.TABLE_GAME_DONE &&
state != Statemachine.state.ROKET_DONE ) {
startedHurrySound = true;
mpdController.playSong("crashtest", "hurry");
}
}
}
}
/** /**
* Event listener for keyPress events from the GUI * Event listener for keyPress events from the GUI
@ -175,8 +293,24 @@ public class TheGame implements StatemachineListener, GuiEventListener, IRCEvent
@Override @Override
public void keyPress(char key) { public void keyPress(char key) {
machine.handleInput(key); machine.handleInput(key);
//guiControl.setExtra("btn: " + key);
Statemachine.state state = machine.getCurrentState();
if(state == Statemachine.state.ENTERED_ROOM) {
if(key == Statemachine.TABLE_TWO || key == Statemachine.TABLE_THREE) {
if(lastInput != key) {
extraSoundControl.playJingle("block");
}
}
} else if(state == Statemachine.state.TABLE_GAME_WRONG) {
if(key == Statemachine.TABLE_ONE || key == Statemachine.TABLE_TWO || key == Statemachine.TABLE_THREE) {
if(lastInput != key && !sproing) {
extraSoundControl.playJingle("block");
}
sproing = false;
}
}
lastInput = key;
} }
/** /**
@ -184,10 +318,17 @@ public class TheGame implements StatemachineListener, GuiEventListener, IRCEvent
*/ */
@Override @Override
public void windowClosing() { public void windowClosing() {
machine.reset(); discoStop();
resetDomotics();
relaisboard.close();
try {
Thread.sleep(1000);
} catch (InterruptedException ignored) { }
bunti.setPar56(0xff,0xff,0xff); bunti.setPar56(0xff,0xff,0xff);
ircClient.say("bye"); ircClient.say("bye");
relaisboard.close(); System.exit(0);
} }
/** /**
@ -210,11 +351,26 @@ public class TheGame implements StatemachineListener, GuiEventListener, IRCEvent
guiControl.setWall(message.substring("wall".length()).trim()); guiControl.setWall(message.substring("wall".length()).trim());
} else if(message.startsWith("extra")) { } else if(message.startsWith("extra")) {
guiControl.setExtra(message.substring("extra".length()).trim()); guiControl.setExtra(message.substring("extra".length()).trim());
} else if(message.startsWith("relais")) {
handleRelaisCommand(message);
} else if(message.startsWith("disco on")) {
discoStart();
} else if(message.startsWith("disco off")) {
discoStop();
} else if(message.startsWith("gem")) {
handleGemCommand();
} else { } else {
ircClient.say("y u no use valid command?"); ircClient.say("y u no use valid command?");
} }
} }
private void rate(int rating, String text) {
gamerRating += rating;
ircClient.say("rated: " + rating + " (" + gamerRating + ") " + text);
if(gamerRating > 5) gamerRating = 5;
if(gamerRating < 1) gamerRating = 1;
}
private void handleTimerCommand(final String message) { private void handleTimerCommand(final String message) {
String params = message.substring("timer".length()).trim().toLowerCase(); String params = message.substring("timer".length()).trim().toLowerCase();
@ -238,7 +394,6 @@ public class TheGame implements StatemachineListener, GuiEventListener, IRCEvent
} else if(params.startsWith("stop")) { } else if(params.startsWith("stop")) {
machine.stopTimer(); machine.stopTimer();
guiControl.showCountDown(false); guiControl.showCountDown(false);
ircClient.say("timer stopped");
} else if(params.startsWith("pause")) { } else if(params.startsWith("pause")) {
machine.pauseTimer(true); machine.pauseTimer(true);
} else if(params.startsWith("resume")) { } else if(params.startsWith("resume")) {
@ -247,18 +402,16 @@ public class TheGame implements StatemachineListener, GuiEventListener, IRCEvent
} }
private void handleStateCommand(final String message) { private void handleStateCommand(final String message) {
if(message.equals("state")) { if(message.trim().equals("state")) {
ircClient.say(machine.getCurrentState().name()); ircClient.say(machine.getCurrentState().name());
} else { } else {
String params = message.substring("state".length()).trim().toLowerCase(); String params = message.substring("state".length()).trim().toLowerCase();
Boolean ok = false; Boolean ok = false;
for(Statemachine.state st: Statemachine.state.values()) { for(Statemachine.state st: Statemachine.state.values()) {
String stateName = st.name().toLowerCase(); if(st.name().toLowerCase().equals(params)) {
if(stateName.equals(params)) {
ok = true; ok = true;
machine.setNewState(st); machine.setNewState(st);
ircClient.say("consider it done, sir!");
break; break;
} }
} }
@ -267,9 +420,52 @@ public class TheGame implements StatemachineListener, GuiEventListener, IRCEvent
} }
} }
// Yes, it makes no sense, but I want have it anyway. For more Spass am Geraet!
private void handleGemCommand() {
if (!gemActivated) {
if(gemCounter > 1200) return;
java.util.Random random = new java.util.Random();
int scry = random.nextInt(100);
if (scry >= 99) {
ircClient.say("Perfect Gem Activated");
} else if (scry >= 90) {
ircClient.say("Moooooooo!");
} else {
ircClient.say("Gem Activated");
}
gemCounter+=100;
} else {
ircClient.say("Gem Deactivated");
}
gemActivated = !gemActivated;
}
private void handleRelaisCommand(final String message) {
String params = message.substring("relais".length()).trim().toLowerCase();
if(params.startsWith("lamp on")) {
relaisboard.setRelais(2, true);
} else if(params.startsWith("lamp off")) {
relaisboard.setRelais(2, false);
} else if(params.startsWith("oven on")) {
relaisboard.setRelais(3, true);
} else if(params.startsWith("oven off")) {
relaisboard.setRelais(3, false);
} else if(params.startsWith("rokets")) {
relaisboard.toggleRelais(0, 300);
} else if(params.startsWith("lamp blink")) {
relaisboard.blinkRelais(2, 700);
} else if(params.startsWith("lamp stop")) {
relaisboard.blinkRelaisStop(2);
}
}
private void handleHelpCommand(final String message) { private void handleHelpCommand(final String message) {
if(message.equals("help")) { if(message.equals("help")) {
ircClient.say("commands: help, reset, state, timer, wall, extra, score"); ircClient.say("commands: help, reset, state, timer, wall, extra, score, relais, disco, gem");
} else if(message.contains("reset")) { } else if(message.contains("reset")) {
ircClient.say("resets the game to IDLE"); ircClient.say("resets the game to IDLE");
} else if(message.contains("state")) { } else if(message.contains("state")) {
@ -302,24 +498,120 @@ public class TheGame implements StatemachineListener, GuiEventListener, IRCEvent
ircClient.say("set small extra message on the screen"); ircClient.say("set small extra message on the screen");
} else if(message.contains("score")) { } else if(message.contains("score")) {
ircClient.say("i will tell you the current game score"); ircClient.say("i will tell you the current game score");
} else if(message.contains("relais")) {
ircClient.say("control the relais board");
ircClient.say("valid commands: lamp on, lamp off, lamp blink, lamp stop, oven on, oven off, rokets");
} else if(message.contains("disco")) {
ircClient.say("party! use: disco {on,off}");
} else if(message.contains("gem")) {
ircClient.say("The chat gem is working as intended.");
} else { } else {
ircClient.say("dafuq?"); ircClient.say("dafuq?");
} }
} }
private void sayScore() { private void sayScore() {
ircClient.say("stateChangeCounter: " + machine.getStateChangeCounter()); StringBuilder sb = new StringBuilder();
// ircClient.say("timerlast: " + machine.getTimerSecondsLast()); sb.append("st.ch.: ");
sb.append(machine.getStateChangeCounter());
sb.append(" rating: ");
sb.append(gamerRating);
int secondsLeft = machine.getTimerSecondsLeft();
int seconds = machine.getTimerSeconds();
int secondsUsed = seconds - secondsLeft;
int seconds = machine.getTimerSecondsLast();
int mins = seconds / 60; int mins = seconds / 60;
int secs = seconds % 60; int minsUsed = secondsUsed / 60;
ircClient.say(" " + mins + ":" + secs + ""); int minsLeft = secondsLeft / 60;
sb.append(String.format(" time:%d:%02d u:%d:%02d l:%d:%02d",
mins, seconds % 60, minsUsed, secondsUsed % 60, minsLeft, secondsLeft % 60));
ircClient.say(sb.toString());
} }
private void resetDomotics() {
relaisboard.setRelais(7, true); // enable light barrier over relais
relaisboard.setRelais(6, true); // enable third green circle
relaisboard.setRelais(2, false); // disable the lamp
relaisboard.setRelais(3, false); // disable the oven
bunti.setPar56(0, 0, 0);
bunti.setLampel(false, false, false);
}
private void discoStop() {
if(discoThread != null) {
shouldStopDisco = true;
discoThread.interrupt();
try {
discoThread.join(500);
} catch (InterruptedException ignored) { }
}
}
private void discoStart() {
discoStop();
Runnable r = new Runnable() {
@Override
public void run() {
try {
while(true) {
if(shouldStopDisco) return;
bunti.setPar56(0, 255, 0, 0);
bunti.setPar56(1, 0, 255, 0);
bunti.setPar56(2, 0, 255, 0);
bunti.setPar56(3, 255, 0, 0);
bunti.setLampel(true, false, false);
if(shouldStopDisco) return;
Thread.sleep(500);
bunti.setPar56(0, 0, 0, 255);
bunti.setPar56(1, 255, 255, 0);
bunti.setPar56(2, 255, 255, 0);
bunti.setPar56(3, 0, 0, 255);
bunti.setLampel(false, true, false);
if(shouldStopDisco) return;
Thread.sleep(500);
bunti.setPar56(0, 255, 128, 0);
bunti.setPar56(1, 0, 255, 255);
bunti.setPar56(2, 0, 255, 255);
bunti.setPar56(3, 255, 128, 0);
bunti.setLampel(false, false, true);
if(shouldStopDisco) return;
Thread.sleep(500);
bunti.setPar56(0, 0, 255, 255);
bunti.setPar56(1, 0, 255, 0);
bunti.setPar56(2, 0, 255, 0);
bunti.setPar56(3, 0, 255, 255);
bunti.setLampel(true, true, true);
if(shouldStopDisco) return;
Thread.sleep(500);
}
} catch (InterruptedException ignored) { }
}
};
discoThread = new Thread(r);
shouldStopDisco = false;
discoThread.start();
}
private void playRandomStartMix() {
Random r = new Random();
int num = r.nextInt(3)+1;
if(num == 1) {
mpdController.playSong("crashtest", "idle");
} else if(num == 2) {
mpdController.playSong("crashtest", "idle2");
} else if(num == 3) {
mpdController.playSong("crashtest", "idle3");
} else if(num == 4) {
mpdController.playSong("crashtest", "idle4");
}
mpdController.skipRandomStart();
}
} }

View File

@ -1,14 +1,6 @@
package de.ctdo.crashtest.gui; package de.ctdo.crashtest.gui;
import java.awt.event.KeyEvent;
/**
* @author: lucas
* @date: 01.06.12 10:29
*/
public interface GuiEventListener { public interface GuiEventListener {
void keyPress(final char key); void keyPress(final char key);
void windowClosing(); void windowClosing();
} }

View File

@ -1,17 +1,9 @@
package de.ctdo.crashtest.gui; package de.ctdo.crashtest.gui;
/**
* @author: lucas
* @date: 01.06.12 10:13
*/
public interface IGuiControl { public interface IGuiControl {
void setWall(final String message); void setWall(final String message);
void setExtra(final String text); void setExtra(final String text);
void setCountDown(final int tseconds); void setCountDown(final int tseconds);
void showCountDown(final Boolean show); void showCountDown(final Boolean show);
void addListener(final GuiEventListener listener); void addListener(final GuiEventListener listener);
} }

View File

@ -3,6 +3,7 @@ package de.ctdo.crashtest.gui;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.awt.event.*; import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.util.*; import java.util.*;
import java.util.List; import java.util.List;
@ -11,17 +12,20 @@ public class MainGui extends JFrame implements IGuiControl {
private final JLabel countDown = new JLabel(); private final JLabel countDown = new JLabel();
private final JLabel extraField = new JLabel(); private final JLabel extraField = new JLabel();
private final JPanel lowerPanel = new JPanel(); private final JPanel lowerPanel = new JPanel();
private final List<GuiEventListener> listenerList = new ArrayList<GuiEventListener>(); private final List<GuiEventListener> listenerList = new ArrayList<GuiEventListener>();
public MainGui() { public MainGui() {
initGui(); initGui();
setDefaultCloseOperation(EXIT_ON_CLOSE); setDefaultCloseOperation(EXIT_ON_CLOSE);
//setBounds(0,0, 1280, 1024);
setBounds(0,0, 1280, 1024);
setExtendedState(Frame.MAXIMIZED_BOTH); setExtendedState(Frame.MAXIMIZED_BOTH);
setUndecorated(true); setUndecorated(true);
setCursor(getToolkit().createCustomCursor( new BufferedImage(3, 3, BufferedImage.TYPE_INT_ARGB),
new Point(0, 0), "null"));
textWall.setCursor(getToolkit().createCustomCursor( new BufferedImage(3, 3, BufferedImage.TYPE_INT_ARGB),
new Point(0, 0), "null"));
addKeyListener(new KeyAdapter() { addKeyListener(new KeyAdapter() {
@Override @Override
public void keyTyped(KeyEvent e) { public void keyTyped(KeyEvent e) {
@ -33,10 +37,10 @@ public class MainGui extends JFrame implements IGuiControl {
} }
}); });
addWindowStateListener(new WindowAdapter() { addWindowListener(new WindowAdapter() {
@Override @Override
public void windowClosing(WindowEvent e) { public void windowClosing(WindowEvent e) {
for(GuiEventListener listener: listenerList) { for (GuiEventListener listener : listenerList) {
listener.windowClosing(); listener.windowClosing();
} }
} }
@ -46,33 +50,25 @@ public class MainGui extends JFrame implements IGuiControl {
} }
private void setCountDownText(final int tseconds) { private void setCountDownText(final int tseconds) {
int mins = tseconds / 600;
int secs = tseconds % 600 / 10 ; countDown.setText(String.format(" %d:%02d.%1d", tseconds / 600, tseconds % 600 / 10, tseconds % 9));
int tsecs = tseconds % 9;
countDown.setText(" " + mins + ":" + secs + "." + tsecs);
if(tseconds < 400) { if(tseconds < 400) {
double percentile = ((tseconds-100.0)/300.0); double percentile = ((tseconds-100.0)/300.0);
double red = 255.0 - percentile * 255.0; double red = 255.0 - percentile * 255.0;
double green = 255.0 - red; double green = 255.0 - red;
double blue = percentile * 100.0;
// System.out.println("red= " + red + " green=" + green + " blue="+blue);
if(red > 255) red = 255; if(red > 255) red = 255;
if(red < 0) red = 0; if(red < 0) red = 0;
if(green > 255) green = 255; if(green > 255) green = 255;
if(green < 0) green = 0; if(green < 0) green = 0;
if(blue > 255) blue = 255;
if(blue < 0) blue = 0;
Color fColor = new Color((int)red,(int)green, (int)blue); Color fColor = new Color((int)red,(int)green, 0);
countDown.setForeground(fColor); countDown.setForeground(fColor);
} }
else { else {
countDown.setForeground(new Color(0x00, 190, 100)); countDown.setForeground(new Color(42, 190, 0));
} }
} }
private void initGui() { private void initGui() {
@ -87,7 +83,7 @@ public class MainGui extends JFrame implements IGuiControl {
Font wallFont = new Font("Arcade Interlaced", Font.PLAIN, 66); Font wallFont = new Font("Arcade Interlaced", Font.PLAIN, 66);
Font lowerFont = new Font("Arcade Interlaced", Font.PLAIN, 80); Font lowerFont = new Font("Arcade Interlaced", Font.PLAIN, 80);
Color fontColor = new Color(0x00, 190, 100); Color fontColor = new Color(42, 190, 0);
textWall.setFont(wallFont); textWall.setFont(wallFont);
textWall.setForeground(fontColor); textWall.setForeground(fontColor);
@ -97,6 +93,7 @@ public class MainGui extends JFrame implements IGuiControl {
textWall.setWrapStyleWord(true); textWall.setWrapStyleWord(true);
textWall.setFocusable(false); textWall.setFocusable(false);
countDown.setFont(lowerFont); countDown.setFont(lowerFont);
countDown.setHorizontalAlignment(SwingConstants.LEFT); countDown.setHorizontalAlignment(SwingConstants.LEFT);
countDown.setForeground(fontColor); countDown.setForeground(fontColor);
@ -128,7 +125,8 @@ public class MainGui extends JFrame implements IGuiControl {
final Runnable runnable = new Runnable() { final Runnable runnable = new Runnable() {
@Override @Override
public void run() { public void run() {
extraField.setText(text + " "); String txt = text.substring(0, Math.min(text.length(), 8));
extraField.setText(txt + " ");
} }
}; };
@ -164,6 +162,4 @@ public class MainGui extends JFrame implements IGuiControl {
listenerList.add(listener); listenerList.add(listener);
} }
} }

View File

@ -1,9 +1,5 @@
package de.ctdo.crashtest.irc; package de.ctdo.crashtest.irc;
/**
* @author: lucas
* @date: 01.06.12 10:05
*/
public interface IIrcClient { public interface IIrcClient {
void say(String message); void say(String message);
void addListener(IRCEventListener listenerIRC); void addListener(IRCEventListener listenerIRC);

View File

@ -1,9 +1,5 @@
package de.ctdo.crashtest.irc; package de.ctdo.crashtest.irc;
/**
* @author: lucas
* @date: 01.06.12 11:49
*/
public interface IRCEventListener { public interface IRCEventListener {
void handleMessage(final String message); void handleMessage(final String message);

View File

@ -20,7 +20,7 @@ import java.util.List;
*/ */
public class IrcClient implements IIrcClient, jerklib.events.listeners.IRCEventListener, ILogger { public class IrcClient implements IIrcClient, jerklib.events.listeners.IRCEventListener, ILogger {
private final List<IRCEventListener> listenerListIRC = new ArrayList<IRCEventListener>(); private final List<IRCEventListener> listenerListIRC = new ArrayList<IRCEventListener>();
private Session ircsession; private final Session ircsession;
private final String channel; private final String channel;
public IrcClient(String nick, String channel, String server) { public IrcClient(String nick, String channel, String server) {
@ -49,8 +49,14 @@ public class IrcClient implements IIrcClient, jerklib.events.listeners.IRCEventL
String message = cme.getMessage(); String message = cme.getMessage();
final String nick = ircsession.getNick(); final String nick = ircsession.getNick();
if(message.contains(nick)) { if(message.startsWith(nick) || message.startsWith(">")) {
message = message.substring(Math.max(nick.length(), message.indexOf(":")+1)); if(message.startsWith(nick)) {
message = message.substring(Math.max(nick.length(), message.indexOf(":")+1));
}
else {
message = message.substring(1);
}
message = message.trim(); message = message.trim();
for(IRCEventListener listenerIRC : listenerListIRC) { for(IRCEventListener listenerIRC : listenerListIRC) {

View File

@ -1,10 +1,5 @@
package de.ctdo.crashtest.log; package de.ctdo.crashtest.log;
/**
* @author: lucas
* @date: 01.06.12 16:46
*/
public interface ILogger { public interface ILogger {
void log(String message); void log(String message);
} }

View File

@ -20,7 +20,7 @@ public class Logger {
return instance; return instance;
} }
public void log(String message) { private void log(String message) {
System.out.println("LOG: " + message); System.out.println("LOG: " + message);

View File

@ -1,10 +1,9 @@
package de.ctdo.crashtest.mpd; package de.ctdo.crashtest.mpd;
/**
* @author: lucas
* @date: 01.06.12 10:36
*/
public interface IMPDController { public interface IMPDController {
void playSong(final String artist, final String title); void playSong(final String artist, final String title);
void setVolume(final int volume); void setVolume(final int volume);
void clearPlaylist();
void addToPlayList(final String artist, final String title);
void skipRandomStart();
} }

View File

@ -12,8 +12,10 @@ import org.bff.javampd.exception.MPDPlaylistException;
import org.bff.javampd.objects.MPDSong; import org.bff.javampd.objects.MPDSong;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.sql.SQLOutput;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Random;
/** /**
* A MPD abstraction to the org.bff.javampd library * A MPD abstraction to the org.bff.javampd library
@ -39,70 +41,154 @@ public class MPDController implements IMPDController {
*/ */
@Override @Override
public void playSong(final String artist, final String title) { public void playSong(final String artist, final String title) {
System.out.println("playSong: " + artist + " - " + title);
if(mpd != null) { if(mpd != null) {
Runnable r = new Runnable() { Runnable r = new Runnable() {
@Override @Override
public void run() { public void run() {
addToPlayListIfNeeded(artist, title); synchronized (mpd) {
try {
MPDPlaylist playlist = mpd.getMPDPlaylist(); doPlaySong(artist,title);
for(MPDSong song: playlist.getSongList()) {
if(song.getArtist() != null && song.getTitle() != null) {
if(song.getArtist().getName().toLowerCase().equals(artist.toLowerCase()) &&
song.getTitle().toLowerCase().equals(title.toLowerCase())) {
MPDPlayer player = mpd.getMPDPlayer();
player.stop();
player.playId(song);
break;
}
}
}
} catch (MPDConnectionException e) {
Logger.sLog("MPD error: " + e.getMessage());
} catch (MPDPlayerException e) {
Logger.sLog("MPD error: " + e.getMessage());
} }
} }
}; };
synchronized (mpd) { //new Thread(r).start();
new Thread(r).start(); r.run();
}
} }
} }
private void addToPlayListIfNeeded(final String artist, final String title) {
MPDDatabase db = mpd.getMPDDatabase();
MPDPlaylist playlist = mpd.getMPDPlaylist();
private void doPlaySong(final String artist, final String title) {
try { try {
List<MPDSong> tracks = new ArrayList<MPDSong>(db.findArtist(artist)); MPDPlaylist playlist = mpd.getMPDPlaylist();
MPDPlayer player = mpd.getMPDPlayer();
for(MPDSong song: tracks) { int tries = 3;
if(song.getName() != null &&
song.getName().toLowerCase().contains(title.toLowerCase())) {
if(!playlist.getSongList().contains(song)) { while(tries>0) {
playlist.addSong(song); addToPlayListIfNeeded(artist, title);
for(MPDSong song: playlist.getSongList()) {
if(checkSong(song, artist, title)) {
System.out.println("MPD: stopping, playing, go...");
player.stop();
player.playId(song);
break;
} }
break;
} }
// now check, if it is playing
MPDSong song = player.getCurrentSong();
if(checkSong(song, artist, title)) {
System.out.println("MPD: song is correctly playing");
return;
} else {
System.out.println("MPD: wrong track is playing");
}
System.out.println("MPD: next try");
tries--;
} }
Logger.sLog("MPD error: track not in playlist or not found... cannot play");
} catch (MPDConnectionException e) { } catch (MPDConnectionException e) {
Logger.sLog("MPD error: " + e.getMessage()); Logger.sLog("MPD error: " + e.getMessage());
} catch (MPDDatabaseException e) { } catch (MPDPlayerException e) {
Logger.sLog("MPD error: " + e.getMessage());
} catch (MPDPlaylistException e) {
Logger.sLog("MPD error: " + e.getMessage()); Logger.sLog("MPD error: " + e.getMessage());
} }
} }
private boolean checkSong(final MPDSong song, final String artist, final String title) {
if(song != null) {
if(song.getArtist() != null && song.getTitle() != null) {
if(song.getArtist().getName().toLowerCase().contentEquals(artist.toLowerCase()) &&
song.getTitle().toLowerCase().contentEquals(title.toLowerCase())) {
return true;
}
} else {
System.out.println("MPD: song is nullb");
}
} else {
System.out.println("MPD: track title or artist is null " + artist + " - " + title);
}
return false;
}
/**
* Add a song to current mpd playlist
* @param artist Artist of the track to play
* @param title Title of the track to play
*/
@Override
public void addToPlayList(final String artist, final String title) {
if(mpd != null) {
Runnable r = new Runnable() {
@Override
public void run() {
synchronized (mpd) {
MPDDatabase db = mpd.getMPDDatabase();
MPDPlaylist playlist = mpd.getMPDPlaylist();
try {
List<MPDSong> tracks = new ArrayList<MPDSong>(db.findArtist(artist));
for(MPDSong song: tracks) {
if(song.getName() != null && song.getName().toLowerCase().contains(title.toLowerCase())) {
playlist.addSong(song);
break;
}
}
} catch (MPDConnectionException e) {
Logger.sLog("MPD error: " + e.getMessage());
} catch (MPDDatabaseException e) {
Logger.sLog("MPD error: " + e.getMessage());
} catch (MPDPlaylistException e) {
Logger.sLog("MPD error: " + e.getMessage());
}
}
}
};
//new Thread(r).start();
r.run();
}
}
@Override
public void skipRandomStart() {
if(mpd != null) {
Runnable r = new Runnable() {
@Override
public void run() {
synchronized (mpd) {
MPDPlayer player = mpd.getMPDPlayer();
try {
int length;
MPDSong song = player.getCurrentSong();
if(song!= null) {
length = song.getLength();
int skip = new Random().nextInt(length/2)+10;
player.seek(skip);
}
} catch (MPDConnectionException e) {
e.printStackTrace();
} catch (MPDPlayerException e) {
e.printStackTrace();
}
}
}
};
//new Thread(r).start();
r.run();
}
}
/** /**
* Sets the current mpd volume * Sets the current mpd volume
@ -111,23 +197,85 @@ public class MPDController implements IMPDController {
@Override @Override
public void setVolume(final int volume) { public void setVolume(final int volume) {
if(mpd != null) { if(mpd != null) {
Runnable r = new Runnable() { Runnable r = new Runnable() {
@Override @Override
public void run() { public void run() {
try { synchronized (mpd) {
mpd.getMPDPlayer().setVolume(volume); try {
} catch (MPDConnectionException e) { mpd.getMPDPlayer().setVolume(volume);
Logger.sLog("MPD error: " + e.getMessage()); } catch (MPDConnectionException e) {
} catch (MPDPlayerException e) { Logger.sLog("MPD error: " + e.getMessage());
Logger.sLog("MPD error: " + e.getMessage()); } catch (MPDPlayerException e) {
Logger.sLog("MPD error: " + e.getMessage());
}
}
}
};
//new Thread(r).start();
r.run();
}
}
/**
* Clears the current mpd playlist
*/
@Override
public void clearPlaylist() {
if(mpd != null) {
Runnable r = new Runnable() {
@Override
public void run() {
synchronized (mpd) {
try {
MPDPlaylist playlist = mpd.getMPDPlaylist();
playlist.clearPlaylist();
mpd.getMPDPlayer().setRandom(false);
mpd.getMPDPlayer().setXFade(1);
} catch (MPDConnectionException e) {
Logger.sLog("MPD error: " + e.getMessage());
} catch (MPDPlaylistException e) {
Logger.sLog("MPD error: " + e.getMessage());
} catch (MPDPlayerException e) {
Logger.sLog("MPD error: " + e.getMessage());
}
} }
} }
}; };
synchronized (mpd) { //new Thread(r).start();
new Thread(r).start(); r.run();
}
}
private void addToPlayListIfNeeded(final String artist, final String title) {
MPDDatabase db = mpd.getMPDDatabase();
MPDPlaylist playlist = mpd.getMPDPlaylist();
int count;
for(count = 0; count < 3; count++) {
try {
List<MPDSong> tracks = new ArrayList<MPDSong>(db.findArtist(artist));
for(MPDSong song: tracks) {
if(song.getName() != null && song.getName().toLowerCase().contains(title.toLowerCase())) {
if(!playlist.getSongList().contains(song)) {
playlist.addSong(song);
}
break;
}
}
return;
} catch (MPDConnectionException e) {
Logger.sLog("MPD error: " + e.getMessage());
} catch (MPDDatabaseException e) {
Logger.sLog("MPD error: " + e.getMessage());
} catch (MPDPlaylistException e) {
Logger.sLog("MPD error: " + e.getMessage());
} }
} }
} }
} }