Test: 增加进程对战配置窗口可用于配置共享内存的 key

This commit is contained in:
Calcitem 2019-12-08 13:35:07 +08:00
parent cc2d80660e
commit 502fc471de
5 changed files with 171 additions and 44 deletions

View File

@ -24,13 +24,85 @@
#include <QDataStream>
#include <QString>
#include <QThread>
#include <QtWidgets>
#include <QtCore>
#include <random>
#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();
}

View File

@ -23,36 +23,65 @@
#include "config.h"
#include <QObject>
#include <QComboBox>
#include <QLabel>
#include <QSharedMemory>
#include <QString>
#include <QBuffer>
#include <QDialog>
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

View File

@ -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<int, QStringList> 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 {

View File

@ -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;

View File

@ -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()