X-Git-Url: https://git.ralfj.de/multypo.git/blobdiff_plain/036922ba5e90ae7f6f6abe3f99b1783dec5132fc..0712b0f435879a9fc49f8b814f7b3b388da88441:/qt/multypo.cpp diff --git a/qt/multypo.cpp b/qt/multypo.cpp index 0c5451e..1667c0f 100644 --- a/qt/multypo.cpp +++ b/qt/multypo.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include @@ -25,12 +26,33 @@ 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::Iterator it = players.begin(); it != players.end(); ++it) { + it.value()->resetText(); + } +} + +void MultypoWindow::nextWord() { + QByteArray tmp = words.readLine().trimmed(); + QString word = QString::fromUtf8(tmp); + if (word.isEmpty()) { // game over + qDebug() << "TODO: implement gameover"; + exit (0); + } else { + resetPlayerText(); + mainLabel->setText(word); + typingPlayers = players.size(); + } +} + void MultypoWindow::handleKeyPress(int device, QString string) { qDebug() << "Device" << device << "String" << string; @@ -44,6 +66,34 @@ 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); + qDebug() << "typingPlayers " << typingPlayers; + typingPlayers--; + qDebug() << "typingPlayers " << typingPlayers; + if (typingPlayers <= 0) { + nextWord(); + } + } + } else { // name entering phase + bool allHaveNames = true; + for (QMap::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) @@ -82,7 +132,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);