network: 将 Client/Server 类实例由 ninechesswindow 移动到 GameController

并将 actions 调整为 action
This commit is contained in:
CalciteM 2019-07-28 10:19:01 +08:00
parent afab1be637
commit c00287db45
6 changed files with 34 additions and 16 deletions

View File

@ -37,6 +37,8 @@
#include "gamecontroller.h"
#include "graphicsconst.h"
#include "boarditem.h"
#include "server.h"
#include "client.h"
GameController::GameController(GameScene & scene, QObject * parent) :
QObject(parent),
@ -74,6 +76,10 @@ GameController::GameController(GameScene & scene, QObject * parent) :
// 安装事件过滤器监视scene的各个事件
// 由于我重载了QGraphicsScene相关事件在重载函数中已设定不必安装监视器。
//scene.installEventFilter(this);
// 网络
server = new Server();
client = new Client();
}
GameController::~GameController()
@ -88,6 +94,10 @@ GameController::~GameController()
ai1.wait();
ai2.wait();
// 网络相关
delete server;
delete client;
#ifdef BOOK_LEARNING
NineChessAi_ab::recordOpeningBookHashMapToFile();
#endif /* BOOK_LEARNING */
@ -790,7 +800,7 @@ bool GameController::giveUp()
}
// 关键槽函数棋谱的命令行执行与actionPiece独立
bool GameController::command(const QString &cmd, bool update /*= true*/)
bool GameController::command(const QString &cmd, bool update /* = true */)
{
Q_UNUSED(hasSound)
@ -1062,3 +1072,9 @@ bool GameController::updateScence(NineChess &chess)
return true;
}
void GameController::showNetworkWindow()
{
server->show();
client->show();
}

View File

@ -41,6 +41,8 @@
#include "gamescene.h"
#include "pieceitem.h"
#include "aithread.h"
#include "server.h"
#include "client.h"
class GameController : public QObject
{
@ -162,6 +164,9 @@ public slots:
bool updateScence();
bool updateScence(NineChess &chess);
// 显示网络配置窗口
void showNetworkWindow();
protected:
//bool eventFilter(QObject * watched, QEvent * event);
// 定时器
@ -242,6 +247,10 @@ private:
// 棋谱字符串列表模型
QStringListModel manualListModel;
// 网络
Server *server;
Client *client;
};
#endif // GAMECONTROLLER_H

View File

@ -118,9 +118,6 @@ NineChessWindow::~NineChessWindow()
}
qDeleteAll(ruleActionList);
delete server;
delete client;
}
void NineChessWindow::closeEvent(QCloseEvent *event)
@ -293,10 +290,6 @@ void NineChessWindow::initialize()
QWidget::setWindowFlags(Qt::WindowMaximizeButtonHint |
Qt::WindowCloseButtonHint | Qt::WindowMinimizeButtonHint);
#endif // SHOW_MAXIMIZED_ON_LOAD
// 网络
server = new Server();
client = new Client();
}
void NineChessWindow::ruleInfo()
@ -760,9 +753,7 @@ void NineChessWindow::on_actionInternet_I_triggered()
ui.actionLocal_L->setChecked(false);
ui.actionInternet_I->setChecked(true);
server->show();
client->show();
game->showNetworkWindow();
}
void NineChessWindow::on_actionEngine_E_triggered()

View File

@ -115,10 +115,6 @@ private:
// 定时器
QTimer autoRunTimer;
// 网络
Server *server;
Client *client;
};
#endif // NINECHESSWINDOW_H

View File

@ -144,6 +144,10 @@ void Server::sessionOpened()
.arg(ipAddress).arg(tcpServer->serverPort()));
}
void Server::setAction(QString action)
{
this->action = action;
}
void Server::sendAction()
{
@ -151,7 +155,7 @@ void Server::sendAction()
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_5_10);
out << actions[QRandomGenerator::global()->bounded(actions.size())];
out << action;
QTcpSocket *clientConnection = tcpServer->nextPendingConnection();

View File

@ -40,12 +40,14 @@ public:
private slots:
void sessionOpened();
void setAction(QString action);
void sendAction();
private:
QLabel *statusLabel = nullptr;
QTcpServer *tcpServer = nullptr;
QVector<QString> actions;
QString action;
QNetworkSession *networkSession = nullptr;
};