X-Git-Url: https://git.ralfj.de/multypo.git/blobdiff_plain/48457df920b6f2c3747baa57366cc23389b3caca..4978fd909202554270ec12c87d78a990001b17e4:/qt/multypo.cpp diff --git a/qt/multypo.cpp b/qt/multypo.cpp index 5ccfed8..fff1da7 100644 --- a/qt/multypo.cpp +++ b/qt/multypo.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include @@ -12,16 +13,17 @@ MultypoWindow::MultypoWindow(QWidget *parent) : QWidget(parent), - xiInited(false), - gameStarted(false) + state(Naming), + xiInited(false) { /* Prepare colors */ - setStyleSheet("background-color: black; color: green; font-size: 45pt"); + setStyleSheet("background-color: black; font-size: 45pt"); /* Prepare conents */ setLayout(new QVBoxLayout(this)); - - mainLabel = new QLabel("Namen eingeben, dann Return", this); + mainLabel = new QLabel(this); + mainLabel->setTextFormat(Qt::RichText); + setLabel("Namen eingeben, dann Return", BASE_COLOR); layout()->addWidget(mainLabel); /* Fullscreen, no cursor */ @@ -29,37 +31,53 @@ MultypoWindow::MultypoWindow(QWidget *parent) : 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::setLabel(QString text, QString color) +{ + mainLabel->setText(QString("%2").arg(color).arg(Qt::escape(text))); } 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"); + currentWord = QString::fromUtf8(tmp); + qDebug() << "New word:" << currentWord; + if (currentWord.isEmpty()) { // game over + setLabel("GAME OVER", READY_COLOR); 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); + it.value()->showScore(); } + state = Scoring; } else { - resetPlayerText(); - mainLabel->setText(word); - typingPlayers = players.size(); + for (QMap::Iterator it = players.begin(); it != players.end(); ++it) { + it.value()->nextWord(); + } + setLabel(currentWord, BASE_COLOR); + state = Playing; } } +bool MultypoWindow::allPlayersWaiting() +{ + for (QMap::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::Iterator it = players.begin(); it != players.end(); ++it) { + if (it.value()->getState() == Player::Typing) ++n; + } + return n; +} + void MultypoWindow::handleKeyPress(int device, QString string) { qDebug() << "Device" << device << "String" << string; @@ -70,40 +88,29 @@ void MultypoWindow::handleKeyPress(int device, QString string) } if (!players.contains(device)) { - if (gameStarted) + if (state > Naming) return; players[device] = new Player(this); } - 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) { + Player *player = players[device]; + player->handleKey(string); + + if (state == Naming) { + // someone's still naming (or nobody's there yet) + if (!players.empty() && allPlayersWaiting()) { + qDebug() << "Game starting"; + nextWord(); + } + } + else if (state == Playing) { // all players are waiting or typing + 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(); } } - } 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(); - } } }