players get points now which are shown per word
[multypo.git] / qt / multypo.cpp
index 2584c0c7b1f7bca5635214db1eabf26ab2d9dab2..973686bf411edb33d7fc3e1aaa2c5ddfb9225628 100644 (file)
@@ -4,6 +4,7 @@
 #include <QDebug>
 #include <QVBoxLayout>
 #include <QLabel>
+#include <QMap>
 
 #include <X11/Xlib.h>
 #include <X11/extensions/XInput2.h>
@@ -14,7 +15,7 @@ MultypoWindow::MultypoWindow(QWidget *parent) :
        xiInited(false)
 {
        /* Prepare colors */
-       setStyleSheet("background-color: black; color: green");
+       setStyleSheet("background-color: black; color: green; font-size: 30pt");
 
        /* Prepare conents */
        setLayout(new QVBoxLayout(this));
@@ -25,25 +26,62 @@ 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;
 
-       if (string == "Escape")
+       if (string == "Escape") {
                close();
+               return;
+       }
 
        if (!players.contains(device)) {
-               QLabel *label = new QLabel(this);
-               layout()->addWidget(label);
-               players[device] = label;
+               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();
+               }
        }
-       players[device]->setText(players[device]->text() + string);
 }
 
 bool MultypoWindow::handleX11Event(XEvent *event)
@@ -58,7 +96,7 @@ bool MultypoWindow::handleX11Event(XEvent *event)
                }
 
                /* Which version of XI2? We support 2.0 */
-               int major = 2, minor = 0;
+               int major = 2, minor = 1;
                if (XIQueryVersion(dpy, &major, &minor) == BadRequest) {
                        qCritical() << "XI2 not available. Server supports" << major << "." <<
                                                minor;
@@ -82,7 +120,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);
 
@@ -92,7 +130,13 @@ bool MultypoWindow::handleX11Event(XEvent *event)
        }
        else if (xiInited && event->type == GenericEvent
                && event->xcookie.type == GenericEvent && event->xcookie.extension == xiOpcode
-               && event->xcookie.evtype == XI_KeyPress && XGetEventData(dpy, &event->xcookie)) {
+               && event->xcookie.evtype == XI_KeyPress) { 
+               
+               if (XGetEventData(dpy, &event->xcookie) != True) {
+                       qCritical() << "Error getting event data";
+                       // FIXME return true;
+               }
+               
                /* Handle XI event */
                XIDeviceEvent *d_ev = (XIDeviceEvent*) event->xcookie.data;
            KeyCode keycode = d_ev->detail;