crashtest/src/de/ctdo/crashtest/SteuerungFrame.java

360 lines
10 KiB
Java

package de.ctdo.crashtest;
import jerklib.*;
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.MPDDatabaseException;
import org.bff.javampd.exception.MPDPlayerException;
import org.bff.javampd.exception.MPDResponseException;
import org.bff.javampd.objects.MPDSong;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Random;
public class SteuerungFrame extends JFrame implements StateChangeListener, GuiControl, IRCEventListener {
private JTextArea textWall;
private JLabel countDown;
private JLabel extraField;
private JPanel lowerPanel;
private char lastKey = ' ';
private MPD mpd;
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;
public SteuerungFrame() {
initGui();
setDefaultCloseOperation(EXIT_ON_CLOSE);
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();
//initMPD();
this.addKeyListener(new KeyAdapter() {
@Override
public void keyTyped(KeyEvent e) {
lastKey = e.getKeyChar();
/*if (lastKey == KeyEvent.VK_F) {
Random r = new Random();
setLEDs(r.nextInt(255), r.nextInt(255),r.nextInt(255));
} */
if (lastKey == KeyEvent.VK_1) {
machine.reset();
} else {
machine.handleInput(e.getKeyChar());
}
updateGui();
}
});
this.addWindowStateListener(new WindowAdapter() {
@Override
public void windowClosed(WindowEvent e) {
System.out.print("oihdf");
}
@Override
public void windowClosing(WindowEvent e) {
try {
if (mpd != null) mpd.close();
if (server != null) server.stop();
} catch (MPDConnectionException e1) {
e1.printStackTrace();
} catch (MPDResponseException e1) {
e1.printStackTrace();
}
}
});
//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
public void stateChanged(Statemachine.state newState) {
updateGui();
if(ircsession != null) {
for(Channel chan: ircsession.getChannels()) {
chan.say("New State: " + newState);
}
}
switch (newState) {
case IDLE:
//setLEDs(0x40,0,0xff);
setLEDs(0,0,0);
bunti.setLampel(false,false,false);
break;
case ENTERED_ROOM:
bunti.setLampel(false,false,false);
setLEDs(20,0,100);
// start von Mo Do - Eins, Zwei Polizei
break;
case TABLE_GAME_ONE:
bunti.setLampel(true,false,false);
setLEDs(255,0,100);
break;
case TABLE_GAME_TWO:
bunti.setLampel(false,true,false);
setLEDs(255,0,100);
break;
case TABLE_GAME_THREE:
bunti.setLampel(false,true,false);
setLEDs(255,35,0);
break;
case TABLE_GAME_FOUR:
bunti.setLampel(false,true,false);
setLEDs(255,55,0);
break;
case TABLE_GAME_FIVE:
bunti.setLampel(false,true,false);
setLEDs(255,75,0);
break;
case TABLE_GAME_SIX:
bunti.setLampel(false,true,false);
setLEDs(255,100,0);
break;
case TABLE_GAME_SEVEN:
bunti.setLampel(false,false,true);
setLEDs(255,100,0);
break;
case TABLE_FINISH: // und roket muss starten
bunti.setLampel(false,false,true);
setLEDs(0, 255, 0);
break;
case ROKET_DONE:
bunti.setLampel(true,true,true);
setLEDs(255, 196, 0);
break;
}
}
private void setLEDs(int red, int green, int blue) {
bunti.setPar56(0,red,green,blue);
bunti.setPar56(1,red,green,blue);
bunti.setPar56(2,red,green,blue);
bunti.setPar56(3,red,green,blue);
}
private void initGui() {
textWall = new JTextArea();
countDown = new JLabel();
extraField = new JLabel();
lowerPanel = new JPanel();
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();
}
private void updateGui() {
/*lblState.setText("<html>LastKey: " + lastKey + "<br>CurrentState: " +
machine.getCurrentState() + "<br>ChangeCounter: " +
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?");
}
}
}