QTfrontend/game.cpp
changeset 533 eebb7684ac22
parent 529 812682c1ab62
child 541 bc7be7f6d3e8
equal deleted inserted replaced
532:8136a5363586 533:eebb7684ac22
    21 #include <QTimer>
    21 #include <QTimer>
    22 #include <QString>
    22 #include <QString>
    23 #include <QByteArray>
    23 #include <QByteArray>
    24 #include <QFile>
    24 #include <QFile>
    25 #include <QTextStream>
    25 #include <QTextStream>
    26 #include <QUuid>
       
    27 #include <QTextStream>
       
    28 
    26 
    29 #include "game.h"
    27 #include "game.h"
    30 #include "hwconsts.h"
    28 #include "hwconsts.h"
    31 #include "gameuiconfig.h"
    29 #include "gameuiconfig.h"
    32 #include "gamecfgwidget.h"
    30 #include "gamecfgwidget.h"
    48 {
    46 {
    49 }
    47 }
    50 
    48 
    51 void HWGame::onClientDisconnect()
    49 void HWGame::onClientDisconnect()
    52 {
    50 {
    53 	SaveDemo(cfgdir->absolutePath() + "/Demos/LastRound.hwd_" + cProtoVer);
    51 	if (gameState == gsInterrupted) emit HaveRecord(false, demo);
    54 	emit GameStateChanged(gsStopped);
    52 	else if (gameState == gsFinished) emit HaveRecord(true, demo);
       
    53 	SetGameState(gsStopped);
    55 }
    54 }
    56 
    55 
    57 void HWGame::commonConfig()
    56 void HWGame::commonConfig()
    58 {
    57 {
    59 	QByteArray buf;
    58 	QByteArray buf;
   164 		case 'i': {
   163 		case 'i': {
   165 			emit GameStats(msg.at(2), QString::fromUtf8(msg.mid(3)));
   164 			emit GameStats(msg.at(2), QString::fromUtf8(msg.mid(3)));
   166 			break;
   165 			break;
   167 		}
   166 		}
   168 		case 'Q': {
   167 		case 'Q': {
   169 			emit GameStateChanged(gsInterrupted);
   168 			SetGameState(gsInterrupted);
   170 			break;
   169 			break;
   171 		}
   170 		}
   172 		case 'q': {
   171 		case 'q': {
   173 			emit GameStateChanged(gsFinished);
   172 			SetGameState(gsFinished);
   174 			break;
   173 			break;
   175 		}
   174 		}
   176 		default: {
   175 		default: {
   177 			if (gameType == gtNet)
   176 			if (gameType == gtNet)
   178 			{
   177 			{
   179 				emit SendNet(msg);
   178 				emit SendNet(msg);
   180 			}
   179 			}
   181 			demo->append(msg);
   180 			demo.append(msg);
   182 		}
   181 		}
   183 	}
   182 	}
   184 }
   183 }
   185 
   184 
   186 void HWGame::FromNet(const QByteArray & msg)
   185 void HWGame::FromNet(const QByteArray & msg)
   225 	if (TeamCount == 5) return;
   224 	if (TeamCount == 5) return;
   226 	teams[TeamCount] = teamname;
   225 	teams[TeamCount] = teamname;
   227 	TeamCount++;
   226 	TeamCount++;
   228 }
   227 }
   229 
   228 
   230 void HWGame::SaveDemo(const QString & filename)
       
   231 {
       
   232 	demo->replace(QByteArray("\x02TL"), QByteArray("\x02TD"));
       
   233 	demo->replace(QByteArray("\x02TN"), QByteArray("\x02TD"));
       
   234 
       
   235 	QFile demofile(filename);
       
   236 	if (!demofile.open(QIODevice::WriteOnly))
       
   237 	{
       
   238 		emit ErrorMessage(tr("Cannot save demo to file %1").arg(filename));
       
   239 		return ;
       
   240 	}
       
   241 	QDataStream stream(&demofile);
       
   242 	stream.writeRawData(demo->constData(), demo->size());
       
   243 	demofile.close();
       
   244 	delete demo;
       
   245 	demo=0;
       
   246 }
       
   247 
       
   248 void HWGame::PlayDemo(const QString & demofilename)
   229 void HWGame::PlayDemo(const QString & demofilename)
   249 {
   230 {
   250 	gameType = gtDemo;
   231 	gameType = gtDemo;
   251 	QFile demofile(demofilename);
   232 	QFile demofile(demofilename);
   252 	if (!demofile.open(QIODevice::ReadOnly))
   233 	if (!demofile.open(QIODevice::ReadOnly))
   257 
   238 
   258 	// read demo
   239 	// read demo
   259 	toSendBuf = demofile.readAll();
   240 	toSendBuf = demofile.readAll();
   260 
   241 
   261 	// run engine
   242 	// run engine
   262 	demo = new QByteArray;
   243 	demo.clear();
   263 	Start();
   244 	Start();
   264 	emit GameStateChanged(gsStarted);
   245 	SetGameState(gsStarted);
   265 }
   246 }
   266 
   247 
   267 void HWGame::StartNet()
   248 void HWGame::StartNet()
   268 {
   249 {
   269 	gameType = gtNet;
   250 	gameType = gtNet;
   270 	demo = new QByteArray;
   251 	demo.clear();
   271 	Start();
   252 	Start();
   272 	emit GameStateChanged(gsStarted);
   253 	SetGameState(gsStarted);
   273 }
   254 }
   274 
   255 
   275 void HWGame::StartLocal()
   256 void HWGame::StartLocal()
   276 {
   257 {
   277 	gameType = gtLocal;
   258 	gameType = gtLocal;
   278 	seed = gamecfg->getCurrentSeed();
   259 	seed = gamecfg->getCurrentSeed();
   279 	demo = new QByteArray;
   260 	demo.clear();
   280 	Start();
   261 	Start();
   281 	emit GameStateChanged(gsStarted);
   262 	SetGameState(gsStarted);
   282 }
   263 }
   283 
   264 
   284 void HWGame::StartQuick()
   265 void HWGame::StartQuick()
   285 {
   266 {
   286 	gameType = gtQLocal;
   267 	gameType = gtQLocal;
   287 	seed = gamecfg->getCurrentSeed();
   268 	seed = gamecfg->getCurrentSeed();
   288 	demo = new QByteArray;
   269 	demo.clear();
   289 	Start();
   270 	Start();
   290 	emit GameStateChanged(gsStarted);
   271 	SetGameState(gsStarted);
   291 }
   272 }
       
   273 
       
   274 void HWGame::SetGameState(GameState state)
       
   275 {
       
   276 	gameState = state;
       
   277 	emit GameStateChanged(state);
       
   278 }