QTfrontend/game.cpp
changeset 178 efdc2a63be8e
parent 177 c67c15e6fae3
child 180 ea83b9e9057f
equal deleted inserted replaced
177:c67c15e6fae3 178:efdc2a63be8e
    51 	this->gamecfg = gamecfg;
    51 	this->gamecfg = gamecfg;
    52 	TeamCount = 0;
    52 	TeamCount = 0;
    53 	seed = "";
    53 	seed = "";
    54 }
    54 }
    55 
    55 
    56 void HWGame::NewConnection()
    56 void HWGame::onClientDisconnect()
    57 {
       
    58 	QTcpSocket * client = IPCServer->nextPendingConnection();
       
    59 	if(!IPCSocket)
       
    60 	{
       
    61 		IPCServer->close();
       
    62 		IPCSocket = client;
       
    63 		connect(client, SIGNAL(disconnected()), this, SLOT(ClientDisconnect()));
       
    64 		connect(client, SIGNAL(readyRead()), this, SLOT(ClientRead()));
       
    65 		if (toSendBuf.size() > 0)
       
    66 			SENDIPC("?");
       
    67 	} else
       
    68 	{
       
    69 		qWarning("2nd IPC client?!");
       
    70 		client->disconnectFromHost();
       
    71 	}
       
    72 }
       
    73 
       
    74 void HWGame::ClientDisconnect()
       
    75 {
    57 {
    76 	SaveDemo(cfgdir->absolutePath() + "/Demos/LastRound.hwd_1");
    58 	SaveDemo(cfgdir->absolutePath() + "/Demos/LastRound.hwd_1");
    77     IPCSocket->deleteLater();
       
    78 	IPCSocket = 0;
       
    79 	deleteLater();
       
    80 }
    59 }
    81 
    60 
    82 void HWGame::SendTeamConfig(int index)
    61 void HWGame::SendTeamConfig(int index)
    83 {
    62 {
    84 	LocalCFG(teams[index]);
    63 	LocalCFG(teams[index]);
   216 void HWGame::FromNet(const QByteArray & msg)
   195 void HWGame::FromNet(const QByteArray & msg)
   217 {
   196 {
   218 	RawSendIPC(msg);
   197 	RawSendIPC(msg);
   219 }
   198 }
   220 
   199 
   221 void HWGame::ClientRead()
       
   222 {
       
   223 	readbuffer.append(IPCSocket->readAll());
       
   224 	onClientRead();
       
   225 }
       
   226 
       
   227 void HWGame::onClientRead()
   200 void HWGame::onClientRead()
   228 {
   201 {
   229 	quint8 msglen;
   202 	quint8 msglen;
   230 	quint32 bufsize;
   203 	quint32 bufsize;
   231 	while (((bufsize = readbuffer.size()) > 0) &&
   204 	while (((bufsize = readbuffer.size()) > 0) &&
   233 	{
   206 	{
   234 		QByteArray msg = readbuffer.left(msglen + 1);
   207 		QByteArray msg = readbuffer.left(msglen + 1);
   235 		readbuffer.remove(0, msglen + 1);
   208 		readbuffer.remove(0, msglen + 1);
   236 		ParseMessage(msg);
   209 		ParseMessage(msg);
   237 	}
   210 	}
   238 }
       
   239 
       
   240 void HWGame::Start()
       
   241 {
       
   242 	IPCServer = new QTcpServer(this);
       
   243 	connect(IPCServer, SIGNAL(newConnection()), this, SLOT(NewConnection()));
       
   244 	IPCServer->setMaxPendingConnections(1);
       
   245 	IPCSocket = 0;
       
   246 	if (!IPCServer->listen(QHostAddress::LocalHost, IPC_PORT))
       
   247 	{
       
   248 		QMessageBox::critical(0, tr("Error"),
       
   249 				tr("Unable to start the server: %1.")
       
   250 				.arg(IPCServer->errorString()));
       
   251 	}
       
   252 
       
   253 	demo = new QByteArray;
       
   254 	QProcess * process;
       
   255 	process = new QProcess;
       
   256 	connect(process, SIGNAL(error(QProcess::ProcessError)), this, SLOT(StartProcessError(QProcess::ProcessError)));
       
   257 	QStringList arguments=setArguments();
       
   258 	process->start(bindir->absolutePath() + "/hwengine", arguments);
       
   259 }
   211 }
   260 
   212 
   261 QStringList HWGame::setArguments()
   213 QStringList HWGame::setArguments()
   262 {
   214 {
   263 	QStringList arguments;
   215 	QStringList arguments;
   270 	arguments << tr("en.txt");
   222 	arguments << tr("en.txt");
   271 	arguments << "128";
   223 	arguments << "128";
   272 	return arguments;
   224 	return arguments;
   273 }
   225 }
   274 
   226 
   275 void HWGame::StartProcessError(QProcess::ProcessError error)
       
   276 {
       
   277 	QMessageBox::critical(0, tr("Error"),
       
   278 				tr("Unable to run engine: %1 (")
       
   279 				.arg(error) + bindir->absolutePath() + "/hwengine)");
       
   280 }
       
   281 
       
   282 void HWGame::AddTeam(const QString & teamname, unsigned char numHedgedogs)
   227 void HWGame::AddTeam(const QString & teamname, unsigned char numHedgedogs)
   283 {
   228 {
   284 	if (TeamCount == 5) return;
   229 	if (TeamCount == 5) return;
   285 	teams[TeamCount] = teamname;
   230 	teams[TeamCount] = teamname;
   286 	TeamCount++;
   231 	TeamCount++;
   331 
   276 
   332 	} while (readbytes > 0);
   277 	} while (readbytes > 0);
   333 	demofile.close();
   278 	demofile.close();
   334 
   279 
   335 	// run engine
   280 	// run engine
       
   281 	demo = new QByteArray;
   336 	Start();
   282 	Start();
   337 }
   283 }
   338 
   284 
   339 void HWGame::StartNet()
   285 void HWGame::StartNet()
   340 {
   286 {
   346 void HWGame::StartLocal()
   292 void HWGame::StartLocal()
   347 {
   293 {
   348 	gameType = gtLocal;
   294 	gameType = gtLocal;
   349 	if (TeamCount < 2) return;
   295 	if (TeamCount < 2) return;
   350 	seed = gamecfg->getCurrentSeed();//QUuid::createUuid().toString();
   296 	seed = gamecfg->getCurrentSeed();//QUuid::createUuid().toString();
       
   297 	demo = new QByteArray;
   351 	Start();
   298 	Start();
   352 }
   299 }
   353 
   300 
   354 void HWGame::StartQuick()
   301 void HWGame::StartQuick()
   355 {
   302 {
   356 	gameType = gtQLocal;
   303 	gameType = gtQLocal;
   357 	seed = gamecfg->getCurrentSeed();//QUuid::createUuid().toString();
   304 	seed = gamecfg->getCurrentSeed();//QUuid::createUuid().toString();
       
   305 	demo = new QByteArray;
   358 	Start();
   306 	Start();
   359 }
   307 }
   360 
   308 
   361 
   309 
   362 void HWGame::LocalCFG(const QString & teamname)
   310 void HWGame::LocalCFG(const QString & teamname)