fix score
[multypo.git] / qt / multypo.cpp
index 1667c0fcd87e1107e54018714bee1b0490f53ca9..07d60b39dc49450fdbcf66e21d2ab36bde5b23c6 100644 (file)
@@ -5,6 +5,7 @@
 #include <QVBoxLayout>
 #include <QLabel>
 #include <QMap>
+#include <QTextDocument>
 
 #include <X11/Xlib.h>
 #include <X11/extensions/XInput2.h>
 
 MultypoWindow::MultypoWindow(QWidget *parent) :
        QWidget(parent),
+    state(Naming),
        xiInited(false)
 {
        /* Prepare colors */
-       setStyleSheet("background-color: black; color: green; font-size: 30pt");
+       setStyleSheet("background-color: black; font-size: 45pt");
 
        /* Prepare conents */
        setLayout(new QVBoxLayout(this));
-
-       mainLabel = new QLabel("Hit <Esc> to quit", this);
+       mainLabel = new QLabel(this);
+    mainLabel->setTextFormat(Qt::RichText);
+    setLabel("Namen eingeben, dann Return", BASE_COLOR);
        layout()->addWidget(mainLabel);
 
        /* Fullscreen, no cursor */
@@ -34,25 +37,47 @@ MultypoWindow::~MultypoWindow()
 {
 }
 
-void MultypoWindow::resetPlayerText() {
-       for (QMap<int, Player*>::Iterator it = players.begin(); it != players.end(); ++it) {
-               it.value()->resetText();
-       }
+void MultypoWindow::setLabel(QString text, QString color)
+{
+    mainLabel->setText(QString("<font color=%1>%2</span>").arg(color).arg(Qt::escape(text)));
 }
 
 void MultypoWindow::nextWord() {
        QByteArray tmp = words.readLine().trimmed();
-       QString word = QString::fromUtf8(tmp);
-       if (word.isEmpty()) { // game over
-               qDebug() << "TODO: implement gameover";
-               exit (0);
+       currentWord = QString::fromUtf8(tmp);
+       qDebug() << "New word:" << currentWord;
+       if (currentWord.isEmpty()) { // game over
+               setLabel("GAME OVER", READY_COLOR);
+               for (QMap<int, Player*>::Iterator it = players.begin(); it != players.end(); ++it) {
+                       it.value()->showScore();
+               }
+               state = Scoring;
        } else {
-               resetPlayerText();
-               mainLabel->setText(word);
-               typingPlayers = players.size();
+               for (QMap<int, Player*>::Iterator it = players.begin(); it != players.end(); ++it) {
+            it.value()->nextWord();
+        }
+               setLabel(currentWord, BASE_COLOR);
+        state = Playing;
        }
 }
 
+bool MultypoWindow::allPlayersWaiting()
+{
+    for (QMap<int, Player*>::Iterator it = players.begin(); it != players.end(); ++it) {
+        if (it.value()->getState() != Player::Waiting) return false;
+    }
+    return true;
+}
+
+int MultypoWindow::typingPlayers()
+{
+    int n = 0;
+    for (QMap<int, Player*>::Iterator it = players.begin(); it != players.end(); ++it) {
+        if (it.value()->getState() == Player::Typing) ++n;
+    }
+    return n;
+}
+
 void MultypoWindow::handleKeyPress(int device, QString string)
 {
        qDebug() << "Device" << device << "String" << string;
@@ -63,36 +88,29 @@ void MultypoWindow::handleKeyPress(int device, QString string)
        }
 
        if (!players.contains(device)) {
+               if (state > Naming)
+                       return;
                players[device] = new Player(this);
        }
-       players[device]->handleKey(string);
-
-       if (gameStarted) { // ingame
-               qDebug() << players[device]->getCurrentLine();
-               qDebug() << mainLabel->text();
-               if (players[device]->getCurrentLine() == mainLabel->text()) {
-                       players[device]->score += typingPlayers;
-                       QString readyString = QString("READY: %1 points").arg(typingPlayers);
-                       players[device]->setWaiting(readyString);
-                       qDebug() << "typingPlayers " << typingPlayers;
-                       typingPlayers--;
-                       qDebug() << "typingPlayers " << typingPlayers;
-                       if (typingPlayers <= 0) {
+       Player *player = players[device];
+       player->handleKey(string);
+    
+    if (state == Naming) {
+        // someone's still naming (or nobody's there yet)
+        if (!players.empty() && allPlayersWaiting()) {
+            qDebug() << "Game starting";
+            nextWord();
+        }
+    }
+    else if (state == Playing) { // all players are waiting or typing
+               if (player->getState() == Player::Typing && player->getCurrentWord() == currentWord) {
+            int points = typingPlayers();
+            player->wordComplete(points);
+                       QString readyString = QString("READY: %1 points").arg(points);
+                       if (allPlayersWaiting()) {
                                nextWord();
                        }
                }
-       } else { // name entering phase
-               bool allHaveNames = true;
-               for (QMap<int, Player*>::Iterator it = players.begin(); it != players.end(); ++it) {
-                       if (! it.value()->hasName()) {
-                               allHaveNames = false;
-                               break;
-                       }
-               }
-               if (!players.empty() && allHaveNames) {
-                       gameStarted = true;
-                       nextWord();
-               }
        }
 }