Save saves in Save directory
authorunc0rr
Wed, 30 May 2007 20:48:57 +0000
changeset 533 eebb7684ac22
parent 532 8136a5363586
child 534 92fb2b0d5117
Save saves in Save directory
QTfrontend/game.cpp
QTfrontend/game.h
QTfrontend/hwform.cpp
QTfrontend/hwform.h
QTfrontend/main.cpp.in
QTfrontend/tcpBase.cpp
QTfrontend/tcpBase.h
--- a/QTfrontend/game.cpp	Wed May 30 19:29:27 2007 +0000
+++ b/QTfrontend/game.cpp	Wed May 30 20:48:57 2007 +0000
@@ -23,8 +23,6 @@
 #include <QByteArray>
 #include <QFile>
 #include <QTextStream>
-#include <QUuid>
-#include <QTextStream>
 
 #include "game.h"
 #include "hwconsts.h"
@@ -50,8 +48,9 @@
 
 void HWGame::onClientDisconnect()
 {
-	SaveDemo(cfgdir->absolutePath() + "/Demos/LastRound.hwd_" + cProtoVer);
-	emit GameStateChanged(gsStopped);
+	if (gameState == gsInterrupted) emit HaveRecord(false, demo);
+	else if (gameState == gsFinished) emit HaveRecord(true, demo);
+	SetGameState(gsStopped);
 }
 
 void HWGame::commonConfig()
@@ -166,11 +165,11 @@
 			break;
 		}
 		case 'Q': {
-			emit GameStateChanged(gsInterrupted);
+			SetGameState(gsInterrupted);
 			break;
 		}
 		case 'q': {
-			emit GameStateChanged(gsFinished);
+			SetGameState(gsFinished);
 			break;
 		}
 		default: {
@@ -178,7 +177,7 @@
 			{
 				emit SendNet(msg);
 			}
-			demo->append(msg);
+			demo.append(msg);
 		}
 	}
 }
@@ -227,24 +226,6 @@
 	TeamCount++;
 }
 
-void HWGame::SaveDemo(const QString & filename)
-{
-	demo->replace(QByteArray("\x02TL"), QByteArray("\x02TD"));
-	demo->replace(QByteArray("\x02TN"), QByteArray("\x02TD"));
-
-	QFile demofile(filename);
-	if (!demofile.open(QIODevice::WriteOnly))
-	{
-		emit ErrorMessage(tr("Cannot save demo to file %1").arg(filename));
-		return ;
-	}
-	QDataStream stream(&demofile);
-	stream.writeRawData(demo->constData(), demo->size());
-	demofile.close();
-	delete demo;
-	demo=0;
-}
-
 void HWGame::PlayDemo(const QString & demofilename)
 {
 	gameType = gtDemo;
@@ -259,33 +240,39 @@
 	toSendBuf = demofile.readAll();
 
 	// run engine
-	demo = new QByteArray;
+	demo.clear();
 	Start();
-	emit GameStateChanged(gsStarted);
+	SetGameState(gsStarted);
 }
 
 void HWGame::StartNet()
 {
 	gameType = gtNet;
-	demo = new QByteArray;
+	demo.clear();
 	Start();
-	emit GameStateChanged(gsStarted);
+	SetGameState(gsStarted);
 }
 
 void HWGame::StartLocal()
 {
 	gameType = gtLocal;
 	seed = gamecfg->getCurrentSeed();
-	demo = new QByteArray;
+	demo.clear();
 	Start();
-	emit GameStateChanged(gsStarted);
+	SetGameState(gsStarted);
 }
 
 void HWGame::StartQuick()
 {
 	gameType = gtQLocal;
 	seed = gamecfg->getCurrentSeed();
-	demo = new QByteArray;
+	demo.clear();
 	Start();
-	emit GameStateChanged(gsStarted);
+	SetGameState(gsStarted);
 }
+
+void HWGame::SetGameState(GameState state)
+{
+	gameState = state;
+	emit GameStateChanged(state);
+}
--- a/QTfrontend/game.h	Wed May 30 19:29:27 2007 +0000
+++ b/QTfrontend/game.h	Wed May 30 20:48:57 2007 +0000
@@ -19,13 +19,9 @@
 #ifndef GAME_H
 #define GAME_H
 
-#include <QObject>
-#include <QByteArray>
 #include <QString>
 #include "team.h"
 
-#include <map>
-
 #include "tcpBase.h"
 
 class GameUIConfig;
@@ -62,6 +58,7 @@
 	void SendNet(const QByteArray & msg);
 	void GameStateChanged(GameState gameState);
 	void GameStats(char type, const QString & info);
+	void HaveRecord(bool isDemo, const QByteArray & record);
 	void ErrorMessage(const QString &);
 
 public slots:
@@ -82,13 +79,14 @@
 	GameCFGWidget * gamecfg;
 	TeamSelWidget* m_pTeamSelWidget;
 	GameType gameType;
+	GameState gameState;
 
 	void commonConfig();
 	void SendConfig();
 	void SendQuickConfig();
 	void SendNetConfig();
 	void ParseMessage(const QByteArray & msg);
-	void SaveDemo(const QString & filename);
+	void SetGameState(GameState state);
 };
 
 #endif
--- a/QTfrontend/hwform.cpp	Wed May 30 19:29:27 2007 +0000
+++ b/QTfrontend/hwform.cpp	Wed May 30 20:48:57 2007 +0000
@@ -455,6 +455,7 @@
 	connect(game, SIGNAL(GameStateChanged(GameState)), this, SLOT(GameStateChanged(GameState)));
 	connect(game, SIGNAL(GameStats(char, const QString &)), this, SLOT(GameStats(char, const QString &)));
 	connect(game, SIGNAL(ErrorMessage(const QString &)), this, SLOT(ShowErrorMessage(const QString &)), Qt::QueuedConnection);
+	connect(game, SIGNAL(HaveRecord(bool, const QByteArray &)), this, SLOT(GetRecord(bool, const QByteArray &)));
 }
 
 void HWForm::ShowErrorMessage(const QString & msg)
@@ -463,3 +464,30 @@
 			"Hedgewars",
 			msg);
 }
+
+void HWForm::GetRecord(bool isDemo, const QByteArray & record)
+{
+	QString filename;
+	QByteArray demo = record;
+	if (isDemo)
+	{
+		demo.replace(QByteArray("\x02TL"), QByteArray("\x02TD"));
+		demo.replace(QByteArray("\x02TN"), QByteArray("\x02TD"));
+		filename = cfgdir->absolutePath() + "/Demos/LastRound.hwd_" + cProtoVer;
+	} else
+	{
+		demo.replace(QByteArray("\x02TL"), QByteArray("\x02TS"));
+		demo.replace(QByteArray("\x02TN"), QByteArray("\x02TS"));
+		filename = cfgdir->absolutePath() + "/Saves/LastRound.hws_" + cProtoVer;
+	}
+
+
+	QFile demofile(filename);
+	if (!demofile.open(QIODevice::WriteOnly))
+	{
+		ShowErrorMessage(tr("Cannot save record to file %1").arg(filename));
+		return ;
+	}
+	demofile.write(demo.constData(), demo.size());
+	demofile.close();
+}
--- a/QTfrontend/hwform.h	Wed May 30 19:29:27 2007 +0000
+++ b/QTfrontend/hwform.h	Wed May 30 20:48:57 2007 +0000
@@ -72,6 +72,7 @@
 	void GameStats(char type, const QString & info);
 	void ForcedDisconnect();
 	void ShowErrorMessage(const QString &);
+	void GetRecord(bool isDemo, const QByteArray & record);
 
 private:
 	void _NetConnect(const QString & hostName, quint16 port, const QString & nick);
--- a/QTfrontend/main.cpp.in	Wed May 30 19:29:27 2007 +0000
+++ b/QTfrontend/main.cpp.in	Wed May 30 20:48:57 2007 +0000
@@ -66,6 +66,7 @@
 	if (checkForDir(cfgdir->absolutePath() + "/.hedgewars"))
 	{
 		checkForDir(cfgdir->absolutePath() + "/.hedgewars/Demos");
+		checkForDir(cfgdir->absolutePath() + "/.hedgewars/Saves");
 	}
 	cfgdir->cd(".hedgewars");
 
--- a/QTfrontend/tcpBase.cpp	Wed May 30 19:29:27 2007 +0000
+++ b/QTfrontend/tcpBase.cpp	Wed May 30 20:48:57 2007 +0000
@@ -150,12 +150,12 @@
 		if (toSendBuf.size() > 0)
 		{
 			IPCSocket->write(toSendBuf);
-			if(m_isDemoMode) demo->append(toSendBuf);
+			if(m_isDemoMode) demo.append(toSendBuf);
 			toSendBuf.clear();
 		}
 		if(!buf.isEmpty()) {
 		  IPCSocket->write(buf);
-		  if(m_isDemoMode && demo) demo->append(buf);
+		  if(m_isDemoMode) demo.append(buf);
 		}
 	}
 }
--- a/QTfrontend/tcpBase.h	Wed May 30 19:29:27 2007 +0000
+++ b/QTfrontend/tcpBase.h	Wed May 30 20:48:57 2007 +0000
@@ -51,7 +51,7 @@
   QByteArray readbuffer;
 
   QByteArray toSendBuf;
-  QByteArray * demo;
+  QByteArray demo;
 
   void SendIPC(const QByteArray & buf);
   void RawSendIPC(const QByteArray & buf);