11/25
This commit is contained in:
@@ -0,0 +1,102 @@
|
||||
#include "gameclient.h"
|
||||
|
||||
GameClient::GameClient() : TcpClientEquipment()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
bool GameClient::setValue(int iValue)
|
||||
{
|
||||
QByteArray buffer;
|
||||
buffer.resize(0);
|
||||
buffer[0]=static_cast<u_int8_t>(iValue%0xff);
|
||||
buffer[1]=static_cast<u_int8_t>(iValue>>8);
|
||||
return this->sendMsg(buffer);
|
||||
}
|
||||
|
||||
bool GameClient::IsReady()
|
||||
{
|
||||
return IsReady();
|
||||
}
|
||||
|
||||
void GameClient::sendReady()
|
||||
{
|
||||
sendMsg(readyCheckString);
|
||||
}
|
||||
|
||||
void GameClient::sendStart()
|
||||
{
|
||||
sendMsg(gameStartString);
|
||||
}
|
||||
|
||||
void GameClient::sendStop()
|
||||
{
|
||||
sendMsg(gameStopString);
|
||||
}
|
||||
|
||||
void GameClient::sendLevelHard()
|
||||
{
|
||||
sendMsg(gameLevelHard);
|
||||
}
|
||||
|
||||
void GameClient::sendLevelMiddle()
|
||||
{
|
||||
sendMsg(gameLevelMiddle);
|
||||
}
|
||||
|
||||
void GameClient::sendLevelEasy()
|
||||
{
|
||||
sendMsg(gameLevelEasy);
|
||||
}
|
||||
|
||||
void GameClient::sendScore(int iValue)
|
||||
{
|
||||
sendMsg(gameScore+"|"+QString::number(iValue));
|
||||
}
|
||||
|
||||
void GameClient::onDataReceived()
|
||||
{
|
||||
QByteArray revBuffer=this->mClient->readAll();
|
||||
|
||||
QString strTempBuffer=QString(revBuffer);
|
||||
if(strTempBuffer.contains("|"))
|
||||
{
|
||||
QStringList list=strTempBuffer.split('|');
|
||||
for (int i=0;i<list.count();i++) {
|
||||
emit valueReceived(list.at(i).toInt());
|
||||
}
|
||||
}
|
||||
if(strTempBuffer.contains(readyCheckString))
|
||||
{
|
||||
emit allReady();
|
||||
}
|
||||
if(strTempBuffer.contains(gameLevelEasy))
|
||||
{
|
||||
emit gameLevel(GAME_LEVEL_EASY);
|
||||
}
|
||||
if(strTempBuffer.contains(gameLevelMiddle))
|
||||
{
|
||||
emit gameLevel(GAME_LEVEL_NORMAL);
|
||||
}
|
||||
if(strTempBuffer.contains(gameLevelHard))
|
||||
{
|
||||
emit gameLevel(GAME_LEVEL_HARD);
|
||||
}
|
||||
if(strTempBuffer.contains(gameStartString))
|
||||
{
|
||||
emit gameStatus(GAME_STATUS_START);
|
||||
}
|
||||
if(strTempBuffer.contains(gameStopString))
|
||||
{
|
||||
emit gameStatus(GAME_STATUS_STOP);
|
||||
}
|
||||
if(strTempBuffer.contains(gamePauseString))
|
||||
{
|
||||
emit gameStatus(GAME_STATUS_PAUSE);
|
||||
}
|
||||
if(strTempBuffer.contains(gameContinueString))
|
||||
{
|
||||
emit gameStatus(GAME_STATUS_RESUME);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
#ifndef GAMECLIENT_H
|
||||
#define GAMECLIENT_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QByteArray>
|
||||
#include "equipmentLib/tcpclientequipment.h"
|
||||
#include "XYComFunc_global.h"
|
||||
#include "commonStruct.h"
|
||||
//游戏通讯客户端类,连接游戏服务器,握手确定就绪,发送游戏数据
|
||||
class XYCOMFUNC_EXPORT GameClient : public TcpClientEquipment
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
GameClient();
|
||||
Q_INVOKABLE bool setValue(int iValue);
|
||||
Q_INVOKABLE bool IsReady();
|
||||
Q_INVOKABLE void sendReady();
|
||||
Q_INVOKABLE void sendStart();
|
||||
Q_INVOKABLE void sendStop();
|
||||
Q_INVOKABLE void sendLevelHard();
|
||||
Q_INVOKABLE void sendLevelMiddle();
|
||||
Q_INVOKABLE void sendLevelEasy();
|
||||
Q_INVOKABLE void sendScore(int iValue);
|
||||
|
||||
signals:
|
||||
void valueReceived(int iValue);
|
||||
void allReady();
|
||||
void gameLevel(GameLevel level);
|
||||
void gameStatus(GameStatus status);
|
||||
|
||||
public slots:
|
||||
|
||||
void onDataReceived() override;
|
||||
|
||||
private:
|
||||
bool isReady;
|
||||
const QString readyCheckString="XYYLGameReady";
|
||||
const QString gameStartString="XYYLGameStart";
|
||||
const QString gameStopString="XYYLGameStop";
|
||||
const QString gamePauseString="XYYLGamePause";
|
||||
const QString gameContinueString="XYYLGameContinue";
|
||||
const QString gameLevelHard="XYYLLevelHard";
|
||||
const QString gameLevelMiddle="XYYLLevelMiddle";
|
||||
const QString gameLevelEasy="XYYLLevelEasy";
|
||||
const QString gameScore="XYYLGameScore";
|
||||
};
|
||||
|
||||
#endif // GAMECLIENT_H
|
||||
@@ -0,0 +1,115 @@
|
||||
#include "gameserver.h"
|
||||
|
||||
GameServer::GameServer() : TcpEquipment()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void GameServer::sendValue(int value)
|
||||
{
|
||||
|
||||
QString tempBuffer=QString::number(value);
|
||||
tempBuffer+="|";
|
||||
send2All(tempBuffer);
|
||||
}
|
||||
|
||||
bool GameServer::IsReady()
|
||||
{
|
||||
return isReady;
|
||||
}
|
||||
|
||||
void GameServer::sendLevel(GameLevel level)
|
||||
{
|
||||
switch (level)
|
||||
{
|
||||
case GameLevel::GAME_LEVEL_EASY:
|
||||
send2All(gameLevelEasy);
|
||||
break;
|
||||
case GameLevel::GAME_LEVEL_NORMAL:
|
||||
send2All(gameLevelMiddle);
|
||||
break;
|
||||
case GameLevel::GAME_LEVEL_HARD:
|
||||
send2All(gameLevelHard);
|
||||
break;
|
||||
default:
|
||||
send2All(gameLevelEasy);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void GameServer::sendLevel(int level)
|
||||
{
|
||||
sendLevel(static_cast<GameLevel>(level));
|
||||
}
|
||||
|
||||
void GameServer::sendStart()
|
||||
{
|
||||
sendStatus(GameStatus::GAME_STATUS_START);
|
||||
}
|
||||
|
||||
void GameServer::sendPause()
|
||||
{
|
||||
sendStatus(GameStatus::GAME_STATUS_PAUSE);
|
||||
}
|
||||
|
||||
void GameServer::sendContinue()
|
||||
{
|
||||
sendStatus(GameStatus::GAME_STATUS_RESUME);
|
||||
}
|
||||
|
||||
void GameServer::sendStop()
|
||||
{
|
||||
sendStatus(GameStatus::GAME_STATUS_STOP);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void GameServer::dealRev(QString strIP, int iPort, QByteArray buffer)
|
||||
{
|
||||
Q_UNUSED(iPort);
|
||||
QString tempBufferString=QString(buffer);
|
||||
if(tempBufferString==readyCheckString){
|
||||
send2One(readyCheckString,strIP);
|
||||
}
|
||||
else if(tempBufferString==gameStartString){
|
||||
emit gameStartReceived(strIP,iPort);
|
||||
}
|
||||
else if(tempBufferString==gameStopString){
|
||||
emit gameStopReceived(strIP,iPort);
|
||||
}
|
||||
else if(tempBufferString==gameLevelEasy){
|
||||
emit gameLevelSet(0);
|
||||
}
|
||||
else if(tempBufferString==gameLevelMiddle){
|
||||
emit gameLevelSet(1);
|
||||
}
|
||||
else if(tempBufferString==gameLevelHard){
|
||||
emit gameLevelSet(2);
|
||||
}
|
||||
else if(tempBufferString.contains(gameScore)){
|
||||
QStringList tempList = tempBufferString.split('|');
|
||||
if(tempList.count()==2){
|
||||
emit gameScoreReceived(tempList.at(1).toInt());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void GameServer::sendStatus(GameStatus status)
|
||||
{
|
||||
switch (status)
|
||||
{
|
||||
case GameStatus::GAME_STATUS_STOP:
|
||||
send2All(gameStopString);
|
||||
break;
|
||||
case GameStatus::GAME_STATUS_PAUSE:
|
||||
send2All(gamePauseString);
|
||||
break;
|
||||
case GameStatus::GAME_STATUS_START:
|
||||
send2All(gameStartString);
|
||||
break;
|
||||
case GameStatus::GAME_STATUS_RESUME:
|
||||
send2All(gameContinueString);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
#ifndef GAMESERVER_H
|
||||
#define GAMESERVER_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QByteArray>
|
||||
#include <QString>
|
||||
#include <QStringList>
|
||||
#include "equipmentLib/tcpequipment.h"
|
||||
#include "XYComFunc_global.h"
|
||||
#include "commonStruct.h"
|
||||
//游戏服务器类,等待游戏连入,进行握手,等待游戏就绪,向游戏发送数据
|
||||
class XYCOMFUNC_EXPORT GameServer : public TcpEquipment
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
GameServer();
|
||||
Q_INVOKABLE void sendValue(int value);
|
||||
Q_INVOKABLE bool IsReady();
|
||||
Q_INVOKABLE void sendLevel(GameLevel level);
|
||||
Q_INVOKABLE void sendLevel(int level);
|
||||
Q_INVOKABLE void sendStart();
|
||||
Q_INVOKABLE void sendPause();
|
||||
Q_INVOKABLE void sendContinue();
|
||||
Q_INVOKABLE void sendStop();
|
||||
|
||||
|
||||
private:
|
||||
void dealRev(QString strIP,int iPort,QByteArray buffer) override;
|
||||
void sendStatus(GameStatus status);
|
||||
|
||||
signals:
|
||||
void gameStartReceived(QString strIP,int iPort);
|
||||
void gameStopReceived(QString strIP,int iPort);
|
||||
void gameLevelSet(int iLevel);
|
||||
void gameScoreReceived(int iScore);
|
||||
|
||||
public slots:
|
||||
|
||||
private:
|
||||
bool isReady;
|
||||
const QString readyCheckString="XYYLGameReady";
|
||||
const QString gameStartString="XYYLGameStart";
|
||||
const QString gameStopString="XYYLGameStop";
|
||||
const QString gamePauseString="XYYLGamePause";
|
||||
const QString gameContinueString="XYYLGameContinue";
|
||||
const QString gameLevelHard="XYYLLevelHard";
|
||||
const QString gameLevelMiddle="XYYLLevelMiddle";
|
||||
const QString gameLevelEasy="XYYLLevelEasy";
|
||||
const QString gameScore="XYYLGameScore";
|
||||
};
|
||||
|
||||
#endif // GAMESERVER_H
|
||||
Reference in New Issue
Block a user