From 502fc471deaaf15091ab9b6dc9b34557de2e0eee Mon Sep 17 00:00:00 2001 From: Calcitem Date: Sun, 8 Dec 2019 13:35:07 +0800 Subject: [PATCH] =?UTF-8?q?Test:=20=E5=A2=9E=E5=8A=A0=E8=BF=9B=E7=A8=8B?= =?UTF-8?q?=E5=AF=B9=E6=88=98=E9=85=8D=E7=BD=AE=E7=AA=97=E5=8F=A3=E5=8F=AF?= =?UTF-8?q?=E7=94=A8=E4=BA=8E=E9=85=8D=E7=BD=AE=E5=85=B1=E4=BA=AB=E5=86=85?= =?UTF-8?q?=E5=AD=98=E7=9A=84=20key?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/test.cpp | 132 +++++++++++++++++++++++++++++++---- src/test/test.h | 35 +++++++++- src/ui/qt/gamecontroller.cpp | 24 +++---- src/ui/qt/gamecontroller.h | 15 ++-- src/ui/qt/gamewindow.cpp | 9 +-- 5 files changed, 171 insertions(+), 44 deletions(-) diff --git a/src/test/test.cpp b/src/test/test.cpp index b03950fc..2ee0fa76 100644 --- a/src/test/test.cpp +++ b/src/test/test.cpp @@ -24,13 +24,85 @@ #include #include #include +#include +#include #include #include "test.h" -Test::Test() +Test::Test(QWidget *parent, QString k) + : QDialog(parent) + , keyCombo(new QComboBox) + , startButton(new QPushButton(tr("Start"))) + , stopButton(new QPushButton(tr("Stop"))) { - sharedMemory.setKey("MillGameSharedMemory"); + setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint); + + this->key = k; + + readMemoryTimer = new QTimer(this); + connect(readMemoryTimer, SIGNAL(timeout()), this, SLOT(onTimeOut())); + readMemoryTimer->stop(); + + keyCombo->setEditable(true); + keyCombo->addItem(QString("MillGame-Key-0")); + keyCombo->addItem(QString("MillGame-Key-1")); + auto keyLabel = new QLabel(tr("&Key:")); + keyLabel->setBuddy(keyCombo); + + startButton->setDefault(true); + startButton->setEnabled(true); + stopButton->setEnabled(false); + + auto closeButton = new QPushButton(tr("Close")); + auto buttonBox = new QDialogButtonBox; + buttonBox->addButton(startButton, QDialogButtonBox::ActionRole); + buttonBox->addButton(stopButton, QDialogButtonBox::ActionRole); + buttonBox->addButton(closeButton, QDialogButtonBox::RejectRole); + + connect(startButton, &QAbstractButton::clicked, this, &Test::startAction); + connect(stopButton, &QAbstractButton::clicked, this, &Test::stopAction); + connect(closeButton, &QAbstractButton::clicked, this, &QWidget::close); + + QGridLayout *mainLayout = nullptr; + if (QGuiApplication::styleHints()->showIsFullScreen() || QGuiApplication::styleHints()->showIsMaximized()) { + auto outerVerticalLayout = new QVBoxLayout(this); + outerVerticalLayout->addItem(new QSpacerItem(0, 0, QSizePolicy::Ignored, QSizePolicy::MinimumExpanding)); + auto outerHorizontalLayout = new QHBoxLayout; + outerHorizontalLayout->addItem(new QSpacerItem(0, 0, QSizePolicy::MinimumExpanding, QSizePolicy::Ignored)); + auto groupBox = new QGroupBox(QGuiApplication::applicationDisplayName()); + mainLayout = new QGridLayout(groupBox); + outerHorizontalLayout->addWidget(groupBox); + outerHorizontalLayout->addItem(new QSpacerItem(0, 0, QSizePolicy::MinimumExpanding, QSizePolicy::Ignored)); + outerVerticalLayout->addLayout(outerHorizontalLayout); + outerVerticalLayout->addItem(new QSpacerItem(0, 0, QSizePolicy::Ignored, QSizePolicy::MinimumExpanding)); + } else { + mainLayout = new QGridLayout(this); + } + + mainLayout->addWidget(keyLabel, 0, 0); + mainLayout->addWidget(keyCombo, 0, 1); + mainLayout->addWidget(buttonBox, 3, 0, 1, 2); + + setWindowTitle(QGuiApplication::applicationDisplayName()); +} + +Test::~Test() +{ + detach(); + readMemoryTimer->stop(); +} + +void Test::stop() +{ + detach(); + isTestMode = false; + readMemoryTimer->stop(); +} + +void Test::attach() +{ + sharedMemory.setKey(key); if (sharedMemory.attach()) { loggerDebug("Attached shared memory segment.\n"); @@ -50,19 +122,31 @@ Test::Test() assert(uuidSize == 38); } -Test::~Test() +void Test::detach() { - detach(); + if (sharedMemory.isAttached()) { + if (sharedMemory.detach()) { + loggerDebug("Detached shared memory segment.\n"); + } + } } void Test::writeToMemory(const QString &cmdline) { + if (!isTestMode) { + return; + } + if (cmdline == readStr) { return; } - char from[128] = { 0 }; + char from[BUFSIZ] = { 0 }; +#ifdef _WIN32 + strcpy_s(from, cmdline.toStdString().c_str()); +#else strcpy(from, cmdline.toStdString().c_str()); +#endif while (true) { sharedMemory.lock(); @@ -84,6 +168,10 @@ void Test::writeToMemory(const QString &cmdline) void Test::readFromMemory() { + if (!isTestMode) { + return; + } + sharedMemory.lock(); QString str = to; sharedMemory.unlock(); @@ -104,14 +192,34 @@ void Test::readFromMemory() } } -void Test::detach() -{ - if (sharedMemory.isAttached()) { - sharedMemory.detach(); - } -} - QString Test::createUuidString() { return QUuid::createUuid().toString(); } + +void Test::startAction() +{ + key = keyCombo->currentText(); + + detach(); + attach(); + + isTestMode = true; + readMemoryTimer->start(100); + + startButton->setEnabled(false); + stopButton->setEnabled(true); +} + +void Test::stopAction() +{ + stop(); + + startButton->setEnabled(true); + stopButton->setEnabled(false); +} + +void Test::onTimeOut() +{ + readFromMemory(); +} diff --git a/src/test/test.h b/src/test/test.h index f8d1adf8..4ac325a5 100644 --- a/src/test/test.h +++ b/src/test/test.h @@ -23,36 +23,65 @@ #include "config.h" #include +#include +#include #include #include #include +#include -class Test : public QObject +class Test : public QDialog { Q_OBJECT public: - Test(); + explicit Test(QWidget *parent = nullptr, QString key = "MillGame-Key-0"); ~Test(); + void setKey(QString k) + { + key = k; + } + + QString getKey() + { + return key; + } + + void stop(); + signals: void command(const QString &cmd, bool update = true); public slots: void writeToMemory(const QString &str); void readFromMemory(); + void startAction(); + void stopAction(); + void onTimeOut(); private: + void attach(); void detach(); QString createUuidString(); private: - const int SHARED_MEMORY_SIZE = 4096; + static const int SHARED_MEMORY_SIZE = 4096; QSharedMemory sharedMemory; QString uuid; int uuidSize; char *to { nullptr }; QString readStr; + + QString key; + + QComboBox *keyCombo = nullptr; + QLabel *statusLabel = nullptr; + QPushButton *startButton = nullptr; + QPushButton *stopButton = nullptr; + + bool isTestMode { false }; + QTimer *readMemoryTimer; }; #endif // TEST_H diff --git a/src/ui/qt/gamecontroller.cpp b/src/ui/qt/gamecontroller.cpp index 3c311fb2..b5503294 100644 --- a/src/ui/qt/gamecontroller.cpp +++ b/src/ui/qt/gamecontroller.cpp @@ -80,10 +80,6 @@ GameController::GameController( gameTest = new Test(); - readMemoryTimer = new QTimer(this); - connect(readMemoryTimer, SIGNAL(timeout()), this, SLOT(onTimeOut())); - readMemoryTimer->stop(); - // 关联AI和控制器的着法命令行 connect(aiThread[BLACK], SIGNAL(command(const QString &, bool)), this, SLOT(command(const QString &, bool))); @@ -121,13 +117,6 @@ GameController::~GameController() #endif /* ENDGAME_LEARNING */ } -void GameController::onTimeOut() -{ - if (isTestMode) { - gameTest->readFromMemory(); - } -} - const map GameController::getActions() { // 主窗口更新菜单栏 @@ -945,10 +934,8 @@ bool GameController::command(const QString &cmd, bool update /* = true */) QMessageBox::about(NULL, "游戏结果", message); #endif } - - if (isTestMode) { - gameTest->writeToMemory(cmd); - } + + gameTest->writeToMemory(cmd); #ifndef TRAINING_MODE // 网络: 将着法放到服务器的发送列表中 @@ -1148,6 +1135,11 @@ void GameController::showNetworkWindow() #endif // TRAINING_MODE } +void GameController::showTestWindow() +{ + gameTest->show(); +} + void GameController::saveScore() { qint64 pid = QCoreApplication::applicationPid(); @@ -1175,6 +1167,8 @@ void GameController::saveScore() textStream << QCoreApplication::applicationFilePath() << endl << endl; + textStream << gameTest->getKey() << endl << endl; + if (isAiPlayer[BLACK]) { textStream << "Black:\tAI Player" << endl; } else { diff --git a/src/ui/qt/gamecontroller.h b/src/ui/qt/gamecontroller.h index b611a29f..5697d458 100644 --- a/src/ui/qt/gamecontroller.h +++ b/src/ui/qt/gamecontroller.h @@ -274,10 +274,15 @@ public slots: // 显示网络配置窗口 void showNetworkWindow(); + // 显示引擎对战窗口 + void showTestWindow(); + void saveScore(); - // 定时器超时 - void onTimeOut(); + Test *getTest() + { + return gameTest; + } protected: //bool eventFilter(QObject * watched, QEvent * event); @@ -322,12 +327,6 @@ public: // 电脑执先手时为 true bool isAiPlayer[COLOR_COUNT]; - // 是否为引擎对战模式 - bool isTestMode {false}; - - // 读取共享内存的定时器 - QTimer *readMemoryTimer; - private: // 是否有落子动画 bool hasAnimation; diff --git a/src/ui/qt/gamewindow.cpp b/src/ui/qt/gamewindow.cpp index 3e6ab1eb..f8dc25c2 100644 --- a/src/ui/qt/gamewindow.cpp +++ b/src/ui/qt/gamewindow.cpp @@ -852,8 +852,7 @@ void MillGameWindow::on_actionLocal_L_triggered() ui.actionEngineFight_E->setChecked(false); ui.actionInternet_I->setChecked(false); - gameController->isTestMode = false; - gameController->readMemoryTimer->stop(); + gameController->getTest()->stop(); } void MillGameWindow::on_actionInternet_I_triggered() @@ -862,8 +861,7 @@ void MillGameWindow::on_actionInternet_I_triggered() ui.actionEngineFight_E->setChecked(false); ui.actionInternet_I->setChecked(true); - gameController->isTestMode = false; - gameController->readMemoryTimer->stop(); + gameController->getTest()->stop(); gameController->showNetworkWindow(); } @@ -874,8 +872,7 @@ void MillGameWindow::on_actionEngineFight_E_triggered() ui.actionEngineFight_E->setChecked(true); ui.actionInternet_I->setChecked(false); - gameController->isTestMode = true; - gameController->readMemoryTimer->start(100); + gameController->showTestWindow(); } void MillGameWindow::on_actionEngine_E_triggered()