add proper states and a sane API for players
[multypo.git] / qt / multypo.cpp
index e6befe2535fd113b0ecaceb798ea04d7befda7c6..d58d247728286c90c3225df9dd2f0e72d83536f6 100644 (file)
 
 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; color: green; font-size: 45pt");
 
        /* 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 */
@@ -34,16 +35,40 @@ 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();
+       currentWord = QString::fromUtf8(tmp);
+       qDebug() << "New word" << currentWord;
+       if (currentWord.isEmpty()) { // game over
+               mainLabel->setText("GAME OVER");
+               for (QMap<int, Player*>::Iterator it = players.begin(); it != players.end(); ++it) {
+                       it.value()->showScore();
+               }
+               state = Scoring;
+       } else {
+               for (QMap<int, Player*>::Iterator it = players.begin(); it != players.end(); ++it) {
+            it.value()->nextWord();
+        }
+               mainLabel->setText(currentWord);
+        state = Playing;
        }
 }
 
-void MultypoWindow::nextWord() {
-       QByteArray tmp = words.readLine();
-       QString word = QString::fromUtf8(tmp);
-       mainLabel->setText(word);
+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)
@@ -56,24 +81,31 @@ 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
-
-       } 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;
+       Player *player = players[device];
+       player->handleKey(string);
+    
+    if (state == Naming) {
+        // someone's still naming (or nobody's there yet)
+        qDebug() << "checking for game started";
+        if (!players.empty() && allPlayersWaiting()) {
+            nextWord();
+        }
+    }
+    else if (state == Playing) { // all players are waiting or typing
+               qDebug() << "current player line" << player->getCurrentWord();
+               qDebug() << "current word" << currentWord;
+               if (player->getState() == Player::Typing && player->getCurrentWord() == currentWord) {
+            int points = typingPlayers()+1;
+            player->wordComplete(points);
+                       QString readyString = QString("READY: %1 points").arg(points);
+                       if (allPlayersWaiting()) {
+                               nextWord();
                        }
                }
-               if (!players.empty() && allHaveNames) {
-                       gameStarted = true;
-                       nextWord();
-               }
        }
 }
 
@@ -113,7 +145,7 @@ bool MultypoWindow::handleX11Event(XEvent *event)
                for (int i = 0; i < ndevices; ++i) {
                        if (devices[i].use != XIMasterKeyboard) continue;
                        qDebug() << "Found master keyboard with ID" << devices[i].deviceid;
-                       //XISetFocus(dpy, devices[i].deviceid, winId(), CurrentTime);
+                       XISetFocus(dpy, devices[i].deviceid, winId(), CurrentTime);
                }
                XIFreeDeviceInfo(devices);