QTfrontend/game.cpp
changeset 41 5d7a505875cd
parent 40 416d54ce0c34
child 43 e297fea1a2f3
equal deleted inserted replaced
40:416d54ce0c34 41:5d7a505875cd
    43 
    43 
    44 HWGame::HWGame(int Resolution, bool Fullscreen)
    44 HWGame::HWGame(int Resolution, bool Fullscreen)
    45 {
    45 {
    46 	vid_Resolution = Resolution;
    46 	vid_Resolution = Resolution;
    47 	vid_Fullscreen = Fullscreen;
    47 	vid_Fullscreen = Fullscreen;
    48 	IPCServer.setMaxPendingConnections(1);
       
    49 	if (!IPCServer.listen(QHostAddress::LocalHost, IPC_PORT))
       
    50 	{
       
    51 		QMessageBox::critical(0, tr("Error"),
       
    52 				tr("Unable to start the server: %1.")
       
    53 				.arg(IPCServer.errorString()));
       
    54 	}
       
    55 	connect(&IPCServer, SIGNAL(newConnection()), this, SLOT(NewConnection()));
       
    56 	IPCSocket = 0;
       
    57 	TeamCount = 0;
    48 	TeamCount = 0;
    58 	seed = "";
    49 	seed = "";
    59 	cfgdir.setPath(cfgdir.homePath());
    50 	cfgdir.setPath(cfgdir.homePath());
    60 	cfgdir.cd(".hedgewars");
    51 	cfgdir.cd(".hedgewars");
    61 }
    52 }
    62 
    53 
    63 void HWGame::NewConnection()
    54 void HWGame::NewConnection()
    64 {
    55 {
    65 	QTcpSocket * client = IPCServer.nextPendingConnection();
    56 	QTcpSocket * client = IPCServer->nextPendingConnection();
    66 	if(!IPCSocket)
    57 	if(!IPCSocket)
    67 	{
    58 	{
       
    59 		IPCServer->close();
    68 		IPCSocket = client;
    60 		IPCSocket = client;
    69 		connect(client, SIGNAL(disconnected()), this, SLOT(ClientDisconnect()));
    61 		connect(client, SIGNAL(disconnected()), this, SLOT(ClientDisconnect()));
    70 		connect(client, SIGNAL(readyRead()), this, SLOT(ClientRead()));
    62 		connect(client, SIGNAL(readyRead()), this, SLOT(ClientRead()));
    71 		msgsize = 0;
    63 		msgsize = 0;
    72 		if (toSendBuf.size() > 0)
    64 		if (toSendBuf.size() > 0)
    73 			SENDIPC("?");
    65 			SENDIPC("?");
    74 	} else
    66 	} else
    75 	{
    67 	{
       
    68 		qWarning("2nd IPC client?!");
    76 		client->disconnectFromHost();
    69 		client->disconnectFromHost();
    77 		delete client;
       
    78 	}
    70 	}
    79 }
    71 }
    80 
    72 
    81 void HWGame::ClientDisconnect()
    73 void HWGame::ClientDisconnect()
    82 {
    74 {
       
    75 	SaveDemo("demo.hwd_1");
       
    76     IPCSocket->deleteLater();
    83 	IPCSocket = 0;
    77 	IPCSocket = 0;
    84 	SaveDemo("demo.hwd_1");
    78 	deleteLater();
    85 	delete this;
       
    86 }
    79 }
    87 
    80 
    88 void HWGame::SendTeamConfig(int index)
    81 void HWGame::SendTeamConfig(int index)
    89 {
    82 {
    90 	LocalCFG(teams[index]);
    83 	LocalCFG(teams[index]);
   216 	}
   209 	}
   217 }
   210 }
   218 
   211 
   219 void HWGame::Start()
   212 void HWGame::Start()
   220 {
   213 {
       
   214 	IPCServer = new QTcpServer(this);
       
   215 	connect(IPCServer, SIGNAL(newConnection()), this, SLOT(NewConnection()));
       
   216 	IPCServer->setMaxPendingConnections(1);
       
   217 	IPCSocket = 0;
       
   218 	if (!IPCServer->listen(QHostAddress::LocalHost, IPC_PORT))
       
   219 	{
       
   220 		QMessageBox::critical(0, tr("Error"),
       
   221 				tr("Unable to start the server: %1.")
       
   222 				.arg(IPCServer->errorString()));
       
   223 	}
       
   224 
   221 	QProcess * process;
   225 	QProcess * process;
   222 	QStringList arguments;
   226 	QStringList arguments;
   223 	process = new QProcess;
   227 	process = new QProcess;
   224 	arguments << resolutions[0][vid_Resolution];
   228 	arguments << resolutions[0][vid_Resolution];
   225 	arguments << resolutions[1][vid_Resolution];
   229 	arguments << resolutions[1][vid_Resolution];
   226 	arguments << GetThemeBySeed();
   230 	arguments << GetThemeBySeed();
   227 	arguments << "46631";
   231 	arguments << "46631";
   228 	arguments << seed;
   232 	arguments << seed;
   229 	arguments << (vid_Fullscreen ? "1" : "0");
   233 	arguments << (vid_Fullscreen ? "1" : "0");
   230 	process->start("hw", arguments);
   234 	process->start("./hw", arguments);
   231 }
   235 }
   232 
   236 
   233 void HWGame::AddTeam(const QString & teamname)
   237 void HWGame::AddTeam(const QString & teamname)
   234 {
   238 {
   235 	if (TeamCount == 5) return;
   239 	if (TeamCount == 5) return;
   263 		return "avematan";
   267 		return "avematan";
   264 	}
   268 	}
   265 	quint32 k = 0;
   269 	quint32 k = 0;
   266 	for (int i = 0; i < seed.length(); i++)
   270 	for (int i = 0; i < seed.length(); i++)
   267 	{
   271 	{
   268 		k += seed[i].unicode();
   272 		k += seed[i].cell();
   269 	}
   273 	}
   270 	return themes[k % len];
   274 	return themes[k % len];
   271 }
   275 }
   272 
   276 
   273 void HWGame::SaveDemo(const QString & filename)
   277 void HWGame::SaveDemo(const QString & filename)