X-Git-Url: https://git.ralfj.de/multypo.git/blobdiff_plain/036922ba5e90ae7f6f6abe3f99b1783dec5132fc..48457df920b6f2c3747baa57366cc23389b3caca:/qt/multypo.cpp diff --git a/qt/multypo.cpp b/qt/multypo.cpp index 0c5451e..5ccfed8 100644 --- a/qt/multypo.cpp +++ b/qt/multypo.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include @@ -11,26 +12,54 @@ MultypoWindow::MultypoWindow(QWidget *parent) : QWidget(parent), - xiInited(false) + xiInited(false), + gameStarted(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 to quit", this); + mainLabel = new QLabel("Namen eingeben, dann Return", this); layout()->addWidget(mainLabel); /* Fullscreen, no cursor */ setWindowState(Qt::WindowFullScreen); setCursor(QCursor(Qt::BlankCursor)); + + words.open(stdin,QIODevice::ReadOnly); + + typingPlayers = 0; } 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); + qDebug() << "New word" << word; + if (word.isEmpty()) { // game over + mainLabel->setText("GAME OVER"); + for (QMap::Iterator it = players.begin(); it != players.end(); ++it) { + QString tmp = QString ("Spieler %1 hat %2 Punkte.").arg(it.value()->getName()).arg(it.value()->score); + it.value()->setWaiting(tmp); + } + } else { + resetPlayerText(); + mainLabel->setText(word); + typingPlayers = players.size(); + } +} + void MultypoWindow::handleKeyPress(int device, QString string) { qDebug() << "Device" << device << "String" << string; @@ -41,9 +70,41 @@ void MultypoWindow::handleKeyPress(int device, QString string) } if (!players.contains(device)) { + if (gameStarted) + return; players[device] = new Player(this); } - players[device]->handleKey(string); + bool newChar = players[device]->handleKey(string); + + if (gameStarted) { // ingame + qDebug() << "current player line" << players[device]->getCurrentLine(); + qDebug() << "current word" << mainLabel->text(); + if (newChar && 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 + qDebug() << "checking for game started"; + bool allHaveNames = true; + for (QMap::Iterator it = players.begin(); it != players.end(); ++it) { + if (! it.value()->hasName()) { + allHaveNames = false; + break; + } + } + qDebug() << "Players empty?" << players.empty() << "All have names?" << allHaveNames; + if (!players.empty() && allHaveNames) { + gameStarted = true; + nextWord(); + } + } } bool MultypoWindow::handleX11Event(XEvent *event) @@ -82,7 +143,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);