players get points now which are shown per word
[multypo.git] / qt / multypo.cpp
index 0c5451ece7c8be2deed7c237b6407a6938b87cfd..973686bf411edb33d7fc3e1aaa2c5ddfb9225628 100644 (file)
@@ -4,6 +4,7 @@
 #include <QDebug>
 #include <QVBoxLayout>
 #include <QLabel>
+#include <QMap>
 
 #include <X11/Xlib.h>
 #include <X11/extensions/XInput2.h>
@@ -25,12 +26,27 @@ MultypoWindow::MultypoWindow(QWidget *parent) :
        /* Fullscreen, no cursor */
        setWindowState(Qt::WindowFullScreen);
        setCursor(QCursor(Qt::BlankCursor));
+
+       words.open(stdin,QIODevice::ReadOnly);
 }
 
 MultypoWindow::~MultypoWindow()
 {
 }
 
+void MultypoWindow::resetPlayerText() {
+       for (QMap<int, Player*>::Iterator it = players.begin(); it != players.end(); ++it) {
+               it.value()->resetText();
+       }
+}
+
+void MultypoWindow::nextWord() {
+       QByteArray tmp = words.readLine().trimmed();
+       QString word = QString::fromUtf8(tmp);
+       mainLabel->setText(word);
+       typingPlayers = players.size();
+}
+
 void MultypoWindow::handleKeyPress(int device, QString string)
 {
        qDebug() << "Device" << device << "String" << string;
@@ -44,6 +60,28 @@ void MultypoWindow::handleKeyPress(int device, QString string)
                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);
+               }
+       } 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();
+               }
+       }
 }
 
 bool MultypoWindow::handleX11Event(XEvent *event)