more stuff from the schönen künste
[multypo.git] / qt / multypo.cpp
index 1667c0fcd87e1107e54018714bee1b0490f53ca9..c9e710187163be7fe2e3db99f836ed1f988bf911 100644 (file)
 
 MultypoWindow::MultypoWindow(QWidget *parent) :
        QWidget(parent),
-       xiInited(false)
+       xiInited(false),
+       gameStarted(false)
 {
        /* Prepare colors */
-       setStyleSheet("background-color: black; color: green; font-size: 30pt");
+       setStyleSheet("background-color: black; color: green; font-size: 55pt");
 
        /* Prepare conents */
        setLayout(new QVBoxLayout(this));
 
-       mainLabel = new QLabel("Hit <Esc> to quit", this);
+       mainLabel = new QLabel("Namen eingeben, dann Return", this);
        layout()->addWidget(mainLabel);
 
        /* Fullscreen, no cursor */
@@ -28,6 +29,8 @@ MultypoWindow::MultypoWindow(QWidget *parent) :
        setCursor(QCursor(Qt::BlankCursor));
 
        words.open(stdin,QIODevice::ReadOnly);
+
+       typingPlayers = 0;
 }
 
 MultypoWindow::~MultypoWindow()
@@ -43,9 +46,13 @@ void MultypoWindow::resetPlayerText() {
 void MultypoWindow::nextWord() {
        QByteArray tmp = words.readLine().trimmed();
        QString word = QString::fromUtf8(tmp);
+       qDebug() << "New word" << word;
        if (word.isEmpty()) { // game over
-               qDebug() << "TODO: implement gameover";
-               exit (0);
+               mainLabel->setText("GAME OVER");
+               for (QMap<int, Player*>::Iterator it = players.begin(); it != players.end(); ++it) {
+                       QString tmp = QString ("Spieler %1 hat %2 Punkte.").arg(it.value()->getName()).arg(it.value()->score);
+                       it.value()->setWaiting(tmp);
+               }
        } else {
                resetPlayerText();
                mainLabel->setText(word);
@@ -63,14 +70,16 @@ void MultypoWindow::handleKeyPress(int device, QString string)
        }
 
        if (!players.contains(device)) {
+               if (gameStarted)
+                       return;
                players[device] = new Player(this);
        }
-       players[device]->handleKey(string);
+       bool newChar = players[device]->handleKey(string);
 
        if (gameStarted) { // ingame
-               qDebug() << players[device]->getCurrentLine();
-               qDebug() << mainLabel->text();
-               if (players[device]->getCurrentLine() == mainLabel->text()) {
+               qDebug() << "current player line" << players[device]->getCurrentLine();
+               qDebug() << "current word" << mainLabel->text();
+               if (newChar && players[device]->getCurrentLine() == mainLabel->text()) {
                        players[device]->score += typingPlayers;
                        QString readyString = QString("READY: %1 points").arg(typingPlayers);
                        players[device]->setWaiting(readyString);
@@ -82,6 +91,7 @@ void MultypoWindow::handleKeyPress(int device, QString string)
                        }
                }
        } else { // name entering phase
+               qDebug() << "checking for game started";
                bool allHaveNames = true;
                for (QMap<int, Player*>::Iterator it = players.begin(); it != players.end(); ++it) {
                        if (! it.value()->hasName()) {
@@ -89,6 +99,7 @@ void MultypoWindow::handleKeyPress(int device, QString string)
                                break;
                        }
                }
+               qDebug() << "Players empty?" << players.empty() << "All have names?" << allHaveNames;
                if (!players.empty() && allHaveNames) {
                        gameStarted = true;
                        nextWord();