adding IRC support. currently with sockets server, will be removed.

improved GUI for display of WALL messages and a timer.
Need to refactor from gui into TheGame class
This commit is contained in:
Lucas Pleß 2012-06-01 03:48:20 +02:00
parent bef28b854d
commit 745a2cafff
11 changed files with 428 additions and 47 deletions

View File

@ -0,0 +1,9 @@
<component name="libraryTable">
<library name="jerklib">
<CLASSES>
<root url="jar://$PROJECT_DIR$/libs/jerklib.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</component>

View File

@ -9,6 +9,7 @@
<orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="commons-codec-1.4" level="project" /> <orderEntry type="library" name="commons-codec-1.4" level="project" />
<orderEntry type="library" name="javampd-4.0" level="project" /> <orderEntry type="library" name="javampd-4.0" level="project" />
<orderEntry type="library" name="jerklib" level="project" />
</component> </component>
</module> </module>

BIN
libs/jerklib.jar Normal file

Binary file not shown.

View File

@ -25,6 +25,7 @@ public class BuntiClient {
} }
public void setPar56(int id, int red, int green, int blue) { public void setPar56(int id, int red, int green, int blue) {
if(true) return;
try { try {
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");
@ -53,6 +54,8 @@ public class BuntiClient {
public void setLampel(boolean red, boolean yellow, boolean green) { public void setLampel(boolean red, boolean yellow, boolean green) {
if(true) return;
int value = 0; int value = 0;
if( green ) value |= 0x01; if( green ) value |= 0x01;

View File

@ -0,0 +1,73 @@
package de.ctdo.crashtest;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Communication implements Runnable {
private final static int LISTEN_PORT = 2342;
private Boolean isStopped = false;
private ServerSocket serverSocket;
private Thread runningThread;
private ExecutorService threadPool = Executors.newFixedThreadPool(10);
private GuiControl control;
public Communication(GuiControl control) {
this.control = control;
}
private synchronized boolean isStopped() {
return this.isStopped;
}
@Override
public void run() {
synchronized(this){
this.runningThread = Thread.currentThread();
}
openServerSocket();
while(!isStopped()){
Socket clientSocket = null;
try {
clientSocket = this.serverSocket.accept();
} catch (IOException e) {
if(isStopped()) {
System.out.println("Server Stopped.") ;
return;
}
throw new RuntimeException("Error accepting client connection", e);
}
this.threadPool.execute(new CommunicationRunner(clientSocket, this.control));
}
this.threadPool.shutdown();
System.out.println("Server Stopped.") ;
}
public synchronized void stop() {
this.isStopped = true;
try {
this.serverSocket.close();
} catch (IOException e) {
throw new RuntimeException("Error closing server", e);
}
}
private void openServerSocket() {
try {
this.serverSocket = new ServerSocket(LISTEN_PORT);
} catch (IOException e) {
throw new RuntimeException("Cannot open port 8080", e);
}
}
}

View File

@ -0,0 +1,76 @@
package de.ctdo.crashtest;
import java.io.*;
import java.net.Socket;
import java.net.SocketTimeoutException;
public class CommunicationRunner implements Runnable {
private Socket clientSocket = null;
private GuiControl control;
public CommunicationRunner(Socket clientSocket, GuiControl control) {
this.clientSocket = clientSocket;
this.control = control;
}
@Override
public void run() {
try {
clientSocket.setSoTimeout(5000);
InputStream input = clientSocket.getInputStream();
OutputStream output = clientSocket.getOutputStream();
long time = System.currentTimeMillis();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
String line = reader.readLine();
System.out.println("got line: " + line);
runCommand(line);
output.close();
input.close();
System.out.println("Request processed: " + time + clientSocket.getRemoteSocketAddress());
} catch (SocketTimeoutException e) {
System.out.println("Socket Timeout, closing " + clientSocket.getRemoteSocketAddress());
} catch (IOException e) {
e.printStackTrace();
}
try {
clientSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private void runCommand(String line) {
// must contain a ":"
// then split and switch on the command
int pos = line.indexOf(":");
if(pos > 0) {
String command = line.substring(0, pos).toLowerCase().trim();
String parameter = line.substring(pos+1, line.length()).trim();
if(command.equals("wall")) {
control.setWall(parameter);
} else if(command.equals("timerstart")) {
control.startTimer(Integer.parseInt(parameter));
} else if(command.equals("timerstop")) {
control.stopTimer();
} else if(command.equals("timerpause")) {
control.pauseTimer(Boolean.parseBoolean(parameter));
} else if(command.equals("setextra")) {
control.setExtra(parameter);
} else if(command == "reset") {
control.resetGame();
}
}
}
}

View File

@ -0,0 +1,18 @@
package de.ctdo.crashtest;
public interface GuiControl {
void startTimer(int seconds);
void stopTimer(); // and hide
void pauseTimer(Boolean pause);
void setWall(String message);
void setExtra(String text);
int getTimerSeconds();
int getTimerSecondsLast();
Boolean getTimerRunning();
void resetGame();
}

View File

@ -16,7 +16,7 @@ public class LampelClient {
try { try {
Socket client = new Socket(LAMPEL_HOST, ECMD_TCP_PORT); Socket client = new Socket(LAMPEL_HOST, ECMD_TCP_PORT);
client.setSoTimeout(2000); client.setSoTimeout(800);
DataOutputStream outToServer = new DataOutputStream(client.getOutputStream()); DataOutputStream outToServer = new DataOutputStream(client.getOutputStream());

View File

@ -1,9 +1,6 @@
package de.ctdo.crashtest; package de.ctdo.crashtest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.CharBuffer;
public class Steuerung { public class Steuerung {

View File

@ -1,62 +1,73 @@
package de.ctdo.crashtest; package de.ctdo.crashtest;
import org.bff.javampd.MPD; import jerklib.*;
import org.bff.javampd.MPDPlayer; import jerklib.events.IRCEvent;
import jerklib.events.JoinCompleteEvent;
import jerklib.events.listeners.IRCEventListener;
import org.bff.javampd.*;
import org.bff.javampd.exception.MPDConnectionException; import org.bff.javampd.exception.MPDConnectionException;
import org.bff.javampd.exception.MPDDatabaseException;
import org.bff.javampd.exception.MPDPlayerException;
import org.bff.javampd.exception.MPDResponseException; import org.bff.javampd.exception.MPDResponseException;
import org.bff.javampd.objects.MPDSong;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.awt.event.*; import java.awt.event.*;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Random; import java.util.Random;
public class SteuerungFrame extends JFrame implements StateChangeListener { public class SteuerungFrame extends JFrame implements StateChangeListener, GuiControl, IRCEventListener {
private JPanel pnlRoot; private JTextArea textWall;
private JLabel lblState; private JLabel countDown;
private JLabel extraField;
private JPanel lowerPanel;
private char lastKey = ' '; private char lastKey = ' ';
private MPD mpd; private MPD mpd;
private MPDPlayer player; private MPDPlayer player;
private int timerSeconds = 0;
private int timerSecondsLast = 0;
private int timeSpentTableGame = 0;
private int timeSpentRokets = 0;
private Timer timer;
private Statemachine machine = new Statemachine();
private BuntiClient bunti = new BuntiClient("bunti.ctdo.de", 8080);
private Communication server;
private ConnectionManager irc;
private Session ircsession;
Statemachine machine = new Statemachine();
BuntiClient bunti = new BuntiClient("bunti.ctdo.de", 8080);
public SteuerungFrame() { public SteuerungFrame() {
//setType(Type.UTILITY);
setBackground(Color.black);
setBounds(200,200, 400, 200);
machine.addStateChangedListener(this);
initGui(); initGui();
setDefaultCloseOperation(EXIT_ON_CLOSE); setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true); setVisible(true);
setBounds(200,200, 400, 200);
//server = new Communication(this);
irc = new ConnectionManager(new ProfileImpl("crashtest","crashtest", "crashtest2", "crashtest3"));
ircsession = irc.requestConnection("irc.chaostreff-dortmund.de");
ircsession.addIRCEventListener(this);
//initTimer();
machine.addStateChangedListener(this);
machine.reset(); machine.reset();
try { //initMPD();
mpd = new MPD("dampfradio.raum.chaostreff-dortmund.de", 6600);
player = mpd.getMPDPlayer();
} catch (UnknownHostException e) { this.addKeyListener(new KeyAdapter() {
e.printStackTrace();
return;
} catch (MPDConnectionException e) {
e.printStackTrace();
return;
}
addKeyListener(new KeyAdapter() {
@Override @Override
public void keyTyped(KeyEvent e) { public void keyTyped(KeyEvent e) {
lastKey = e.getKeyChar(); lastKey = e.getKeyChar();
if (lastKey == KeyEvent.VK_F) { /*if (lastKey == KeyEvent.VK_F) {
Random r = new Random(); Random r = new Random();
setLEDs(r.nextInt(255), r.nextInt(255),r.nextInt(255)); setLEDs(r.nextInt(255), r.nextInt(255),r.nextInt(255));
} } */
if (lastKey == KeyEvent.VK_1) { if (lastKey == KeyEvent.VK_1) {
machine.reset(); machine.reset();
@ -68,11 +79,19 @@ public class SteuerungFrame extends JFrame implements StateChangeListener {
} }
}); });
addWindowStateListener(new WindowAdapter() { this.addWindowStateListener(new WindowAdapter() {
@Override
public void windowClosed(WindowEvent e) {
System.out.print("oihdf");
}
@Override @Override
public void windowClosing(WindowEvent e) { public void windowClosing(WindowEvent e) {
try { try {
if (mpd != null) mpd.close(); if (mpd != null) mpd.close();
if (server != null) server.stop();
} catch (MPDConnectionException e1) { } catch (MPDConnectionException e1) {
e1.printStackTrace(); e1.printStackTrace();
} catch (MPDResponseException e1) { } catch (MPDResponseException e1) {
@ -80,20 +99,70 @@ public class SteuerungFrame extends JFrame implements StateChangeListener {
} }
} }
}); });
//new Thread(server).start();
}
private void initMPD() {
try {
mpd = new MPD("dampfradio.raum.chaostreff-dortmund.de", 6600);
player = mpd.getMPDPlayer();
MPDDatabase database = mpd.getMPDDatabase();
Collection<MPDSong> bla = database.findTitle("");
} catch (UnknownHostException e) {
e.printStackTrace();
return;
} catch (MPDConnectionException e) {
e.printStackTrace();
return;
} catch (MPDDatabaseException e) {
e.printStackTrace();
}
}
private void initTimer() {
timer = new Timer(100, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
timerSecondsLast--;
setTimerText();
if(timerSecondsLast <= 0) {
timerSecondsLast = 0;
timer.stop();
}
}
});
}
private void setTimerText() {
int mins = timerSecondsLast / 600;
int secs = timerSecondsLast % 600 / 10 ;
int tsecs = timerSecondsLast % 9;
countDown.setText(" " + mins + ":" + secs + "." + tsecs);
} }
@Override @Override
public void stateChanged(Statemachine.state newState) { public void stateChanged(Statemachine.state newState) {
updateGui(); updateGui();
if(ircsession != null) {
for(Channel chan: ircsession.getChannels()) {
chan.say("New State: " + newState);
}
}
switch (newState) { switch (newState) {
case IDLE: case IDLE:
setLEDs(0x40,0,0xff); //setLEDs(0x40,0,0xff);
setLEDs(0,0,0);
bunti.setLampel(false,false,false); bunti.setLampel(false,false,false);
break; break;
case ENTERED_ROOM: case ENTERED_ROOM:
bunti.setLampel(false,false,false); bunti.setLampel(false,false,false);
setLEDs(255,0,100); setLEDs(20,0,100);
// start von Mo Do - Eins, Zwei Polizei // start von Mo Do - Eins, Zwei Polizei
@ -135,8 +204,8 @@ public class SteuerungFrame extends JFrame implements StateChangeListener {
setLEDs(0, 255, 0); setLEDs(0, 255, 0);
break; break;
case ROKET_DONE: case ROKET_DONE:
bunti.setLampel(true,true,true);
setLEDs(255, 196, 0);
break; break;
} }
@ -151,21 +220,140 @@ public class SteuerungFrame extends JFrame implements StateChangeListener {
} }
private void initGui() { private void initGui() {
Container pane = getContentPane(); textWall = new JTextArea();
countDown = new JLabel();
extraField = new JLabel();
lowerPanel = new JPanel();
pnlRoot = new JPanel(new FlowLayout());
lblState = new JLabel("", JLabel.LEFT);
pnlRoot.add(lblState);
pane.add(pnlRoot); Container container = getContentPane();
container.setLayout(new BorderLayout());
container.add(textWall, BorderLayout.NORTH);
container.add(lowerPanel, BorderLayout.SOUTH);
lowerPanel.setLayout(new BorderLayout());
lowerPanel.add(countDown, BorderLayout.WEST);
lowerPanel.add(extraField, BorderLayout.EAST);
Font wallFont = new Font("Arcade Interlaced", Font.PLAIN, 66);
Font lowerFont = new Font("Arcade Interlaced", Font.PLAIN, 80);
Color fontColor = new Color(0x00, 190, 100);
textWall.setFont(wallFont);
textWall.setForeground(fontColor);
textWall.setBackground(Color.BLACK);
textWall.setText("follow the white rabbit...");
textWall.setLineWrap(true);
textWall.setWrapStyleWord(true);
textWall.setFocusable(false);
countDown.setFont(lowerFont);
countDown.setHorizontalAlignment(SwingConstants.LEFT);
countDown.setForeground(fontColor);
//countDown.setBackground(Color.BLACK);
extraField.setFont(lowerFont);
extraField.setHorizontalAlignment(SwingConstants.RIGHT);
extraField.setForeground(fontColor);
//extraField.setBackground(Color.BLACK);
lowerPanel.setBackground(Color.BLACK);
container.setBackground(Color.BLACK);
countDown.setText(" 15:00.0");
extraField.setText("B ");
updateGui(); updateGui();
} }
private void updateGui() { private void updateGui() {
lblState.setText("<html>LastKey: " + lastKey + "<br>CurrentState: " + /*lblState.setText("<html>LastKey: " + lastKey + "<br>CurrentState: " +
machine.getCurrentState() + "<br>ChangeCounter: " + machine.getCurrentState() + "<br>ChangeCounter: " +
machine.getStateChangeCounter() + "</html>"); machine.getStateChangeCounter() + "</html>"); */
}
@Override
public void startTimer(int seconds) {
timerSeconds = seconds*10;
timerSecondsLast = seconds*10;
timer.start();
}
@Override
public void stopTimer() {
timer.stop();
timerSeconds = 0;
timerSecondsLast = 0;
}
@Override
public void pauseTimer(Boolean pause) {
if(pause) {
timer.stop();
} else {
timer.start();
}
}
@Override
public void setWall(final String message) {
Runnable runnable = new Runnable() {
@Override
public void run() {
textWall.setText(message);
}
};
SwingUtilities.invokeLater(runnable);
}
@Override
public void setExtra(final String text) {
Runnable runnable = new Runnable() {
@Override
public void run() {
extraField.setText(text + " " + lastKey + " ");
}
};
SwingUtilities.invokeLater(runnable);
}
@Override
public int getTimerSeconds() {
return timerSeconds;
}
@Override
public int getTimerSecondsLast() {
return timerSecondsLast;
}
@Override
public Boolean getTimerRunning() {
return null;
}
@Override
public void resetGame() {
}
@Override
public void recieveEvent(IRCEvent e) {
if (e.getType() == IRCEvent.Type.CONNECT_COMPLETE) {
e.getSession().joinChannel("#crashtest");
} else if (e.getType() == IRCEvent.Type.JOIN_COMPLETE) {
JoinCompleteEvent jce = (JoinCompleteEvent) e;
jce.getChannel().say("hello master. what's your order?");
}
} }
} }

View File

@ -0,0 +1,16 @@
package de.ctdo.crashtest;
public class TheGame {
// hier müssen alle Verbindungen nach Aussen rein
// MPD
// Lampel
// Bunti
// IRC
// Die GUI muss als Interface bekannt sein
// fuer Updates der GUI und den Keypress Event
// Logik des Spiels muss hier programmiert werden
}