QTfrontend/game.cpp
changeset 25 27aa8030322b
parent 21 dff476dcaaa3
child 26 e32fa14529f8
equal deleted inserted replaced
24:79c411363184 25:27aa8030322b
    32  */
    32  */
    33 
    33 
    34 #include <QMessageBox>
    34 #include <QMessageBox>
    35 #include <QProcess>
    35 #include <QProcess>
    36 #include <QTimer>
    36 #include <QTimer>
    37 #include <QFile>
       
    38 #include <QString>
    37 #include <QString>
    39 #include <QByteArray>
    38 #include <QByteArray>
    40 #include <QTextStream>
    39 #include <QTextStream>
       
    40 #include <QFile>
    41 #include "game.h"
    41 #include "game.h"
    42 #include "hwconsts.h"
    42 #include "hwconsts.h"
    43 
    43 
    44 HWGame::HWGame()
    44 HWGame::HWGame()
    45 {
    45 {
    46 	IPCServer = new QTcpServer(this);
    46 	IPCServer = new QTcpServer(this);
    47 	IPCServer->setMaxPendingConnections(1);
    47 	IPCServer->setMaxPendingConnections(1);
    48 	if (!IPCServer->listen(QHostAddress("127.0.0.1"), IPC_PORT)) 
    48 	if (!IPCServer->listen(QHostAddress("127.0.0.1"), IPC_PORT))
    49 	{
    49 	{
    50 		QMessageBox::critical(this, tr("Error"),
    50 		QMessageBox::critical(this, tr("Error"),
    51 				tr("Unable to start the server: %1.")
    51 				tr("Unable to start the server: %1.")
    52 				.arg(IPCServer->errorString()));
    52 				.arg(IPCServer->errorString()));
    53 	}
    53 	}
    74 }
    74 }
    75 
    75 
    76 void HWGame::ClientDisconnect()
    76 void HWGame::ClientDisconnect()
    77 {
    77 {
    78 	IPCSocket = 0;
    78 	IPCSocket = 0;
       
    79 	SaveDemo("demo.hwd_1");
    79 	delete this;
    80 	delete this;
    80 }
    81 }
    81 
    82 
    82 void HWGame::SendTeamConfig(int index)
    83 void HWGame::SendTeamConfig(int index)
    83 {
    84 {
    85 	if (!teamcfg.open(QIODevice::ReadOnly))
    86 	if (!teamcfg.open(QIODevice::ReadOnly))
    86 	{
    87 	{
    87 		return ;
    88 		return ;
    88 	}
    89 	}
    89 	QTextStream stream(&teamcfg);
    90 	QTextStream stream(&teamcfg);
    90 	stream.setCodec("UTF-8");	
    91 	stream.setCodec("UTF-8");
    91 	QString str;
    92 	QString str;
    92 	
    93 
    93 	while (!stream.atEnd())
    94 	while (!stream.atEnd())
    94 	{
    95 	{
    95 		str = stream.readLine();
    96 		str = stream.readLine();
    96 		if (str.startsWith(";")) continue;
    97 		if (str.startsWith(";") || (str.length() > 254)) continue;
    97 		str.prepend("e");
    98 		str.prepend("e");
    98 		SendIPC(str.toLocal8Bit());
    99 		SendIPC(str.toLocal8Bit());
    99 	}
   100 	}
   100 	teamcfg.close();
   101 	teamcfg.close();
   101 }
   102 }
   120 	SENDIPC("eadd hh3 1");
   121 	SENDIPC("eadd hh3 1");
   121 }
   122 }
   122 
   123 
   123 void HWGame::ParseMessage()
   124 void HWGame::ParseMessage()
   124 {
   125 {
   125 	switch(msgsize) {
   126 	switch(msgbuf[0])
   126 		case 1: switch(msgbuf[0]) {
   127 	{
   127 			case '?': {
   128 		case '?':
   128 				SENDIPC("!");
   129 		{
   129 				break;
   130 			SENDIPC("!");
   130 			}
   131 			break;
   131 		}
   132 		}
   132 		case 5: switch(msgbuf[0]) {
   133 		case 'C':
   133 			case 'C': {
   134 		{
   134 				SendConfig();
   135 			SendConfig();
   135 				break;
   136 			break;
   136 			}
   137 		}
   137 		}
   138 		case '+':
   138 	}
   139 		{
   139 }
   140 			break;
   140 
   141 		}
   141 void HWGame::SendIPC(const char* msg, unsigned char len)
   142 		default:
       
   143 		{
       
   144 			demo->append(msgsize);
       
   145 			demo->append(QByteArray::fromRawData(msgbuf, msgsize));
       
   146 		}
       
   147 	}
       
   148 }
       
   149 
       
   150 void HWGame::SendIPC(const char * msg, quint8 len)
   142 {
   151 {
   143 	IPCSocket->write((char *)&len, 1);
   152 	IPCSocket->write((char *)&len, 1);
   144 	IPCSocket->write(msg, len);
   153 	IPCSocket->write(msg, len);
   145 }
   154 	if ((len > 5) && !((msg[0] == 'e') && (msg[1] == 'b')))
   146 
   155 	{
   147 void HWGame::SendIPC(const QByteArray buf)
   156 		demo->append(len);
       
   157 		demo->append(QByteArray::fromRawData(msg, len));
       
   158 	}
       
   159 }
       
   160 
       
   161 void HWGame::SendIPC(const QByteArray & buf)
   148 {
   162 {
   149 	if (buf.size() > 255) return;
   163 	if (buf.size() > 255) return;
   150 	unsigned char len = buf.size();
   164 	quint8 len = buf.size();
   151 	IPCSocket->write((char *)&len, 1);
   165 	IPCSocket->write((char *)&len, 1);
   152 	IPCSocket->write(buf);
   166 	IPCSocket->write(buf);
       
   167 	demo->append(len);
       
   168 	demo->append(buf);
   153 }
   169 }
   154 
   170 
   155 void HWGame::ClientRead()
   171 void HWGame::ClientRead()
   156 {
   172 {
   157 	qint64 readbytes = 1;
   173 	qint64 readbytes = 1;
   158 	while (readbytes > 0) 
   174 	while (readbytes > 0)
   159 	{
   175 	{
   160 		if (msgsize == 0) 
   176 		if (msgsize == 0)
   161 		{
   177 		{
   162 			msgbufsize = 0;
   178 			msgbufsize = 0;
   163 			readbytes = IPCSocket->read((char *)&msgsize, 1);
   179 			readbytes = IPCSocket->read((char *)&msgsize, 1);
   164 		} else
   180 		} else
   165 		{
   181 		{
   166 			msgbufsize += 
   182 			msgbufsize +=
   167 			readbytes = IPCSocket->read((char *)&msgbuf[msgbufsize], msgsize - msgbufsize);
   183 			readbytes = IPCSocket->read((char *)&msgbuf[msgbufsize], msgsize - msgbufsize);
   168 			if (msgbufsize = msgsize)
   184 			if (msgbufsize = msgsize)
   169 			{
   185 			{
   170 				ParseMessage();
   186 				ParseMessage();
   171 				msgsize = 0;
   187 				msgsize = 0;
   181 	QStringList arguments;
   197 	QStringList arguments;
   182 	seedgen.GenRNDStr(seed, 10);
   198 	seedgen.GenRNDStr(seed, 10);
   183 	process = new QProcess;
   199 	process = new QProcess;
   184 	arguments << resolutions[0][Resolution];
   200 	arguments << resolutions[0][Resolution];
   185 	arguments << resolutions[1][Resolution];
   201 	arguments << resolutions[1][Resolution];
   186 	arguments << "avematan";
   202 	arguments << GetThemeBySeed();
   187 	arguments << "46631";
   203 	arguments << "46631";
   188 	arguments << seed;
   204 	arguments << seed;
   189 	arguments << (Fullscreen ? "1" : "0");
   205 	arguments << (Fullscreen ? "1" : "0");
   190 	process->start("hw", arguments);
   206 	process->start("hw", arguments);
       
   207 	demo = new QByteArray;
       
   208 	demo->append(seed.toLocal8Bit());
       
   209 	demo->append(10);
   191 }
   210 }
   192 
   211 
   193 void HWGame::AddTeam(const QString & teamname)
   212 void HWGame::AddTeam(const QString & teamname)
   194 {
   213 {
   195 	if (TeamCount == 5) return;
   214 	if (TeamCount == 5) return;
   196 	teams[TeamCount] = teamname;
   215 	teams[TeamCount] = teamname;
   197 	TeamCount++;
   216 	TeamCount++;
   198 }
   217 }
       
   218 
       
   219 QString HWGame::GetThemeBySeed()
       
   220 {
       
   221 	QFile themesfile("Data/Themes/themes.cfg");
       
   222 	QStringList themes;
       
   223 	if (themesfile.open(QIODevice::ReadOnly))
       
   224 	{
       
   225 		QTextStream stream(&themesfile);
       
   226 		QString str;
       
   227 		while (!stream.atEnd())
       
   228 		{
       
   229 			themes << stream.readLine();
       
   230 		}
       
   231 		themesfile.close();
       
   232 	}
       
   233 	quint32 len = themes.size();
       
   234 	if (len == 0)
       
   235 	{
       
   236 		QMessageBox::critical(this, "Error", "Cannot access themes.cfg or bad data", "OK");
       
   237 		return "avematan";
       
   238 	}
       
   239 	if (seed.isEmpty())
       
   240 	{
       
   241 		QMessageBox::critical(this, "Error", "seed not defined", "OK");
       
   242 		return "avematan";
       
   243 	}
       
   244 	quint32 k = 0;
       
   245 	for (int i = 0; i < seed.length(); i++)
       
   246 	{
       
   247 		k += seed[i].unicode();
       
   248 	}
       
   249 	return themes[k % len];
       
   250 }
       
   251 
       
   252 void HWGame::SaveDemo(const QString & filename)
       
   253 {
       
   254 	QFile demofile(filename);
       
   255 	if (!demofile.open(QIODevice::WriteOnly))
       
   256 	{
       
   257 		QMessageBox::critical(this,
       
   258 				tr("Error"),
       
   259 				tr("Cannot save demo to file %s").arg(filename),
       
   260 				tr("Quit"));
       
   261 		return ;
       
   262 	}
       
   263 	QDataStream stream(&demofile);
       
   264 	stream.writeRawData(demo->constData(), demo->size());
       
   265 	demofile.close();
       
   266 }