//================================================// // //================================================// GameScene::GameScene(int width, int height, QGraphicsView *parent) :QGraphicsScene(parent) ,maxRenderLen(15) ,gView(parent) ,inSence(false) { setSceneRect(0,0,width,height); initGame(); lastTime=QTime::currentTime(); saveTime=lastTime; QTimer *timer=new QTimer; timer->setInterval(10); connect(timer,SIGNAL(timeout()),this,SLOT(update())); connect(timer,SIGNAL(timeout()),camera,SLOT(cMove())); connect(timer,SIGNAL(timeout()),world,SLOT(updateDraw()),Qt::DirectConnection); //在主线程中执行 connect(timer,SIGNAL(timeout()),this,SLOT(mouseMove())); connect(timer,SIGNAL(timeout()),this,SLOT(handleGameMessage())); connect(timer,SIGNAL(timeout()),this,SLOT(autoSave())); // timer->start(); }
// used to handle message from a ClientConnection // those message can be // 'SYSTEM_REGISTER <CONSUMER | PROVIDER> #Game [Game]' --> no answer // 'SYSTEM_REQUEST_GAME GameKind' // 'SYSTEM_REQUEST_GAME_REFUSED ErrorMessage' // 'SYSTEM_REQUEST_GAME_ACCEPTED GameId #Consumer [Consumer]' // 'SYSTEM_JOIN_GAME GameId' // 'SYSTEM_LEAVE_GAME GameId' // '<gameId> MESSAGE' void ConnectionManager::handleMessage( ClientConnectionPtr connection, const std::string& message ) { // log the message AsyncLogger::getInstance()->log( "RECEIVE FROM (" + connection->getLogin() + ") : " + message ); // explode the message to be able to check the kind std::vector< std::string > messageParts; if ( StringUtils::explode( message, ' ', messageParts, 2 ) == 2 ) { if ( messageParts[ 0 ] == SYSTEM_REGISTER ) { registerConnection( connection, messageParts[ 1 ] ); dumpCurrentState(); } else if ( messageParts[ 0 ] == SYSTEM_REQUEST_GAME ) { requestGame( connection, messageParts[ 1 ] ); dumpCurrentState(); } else if ( messageParts[ 0 ] == SYSTEM_REQUEST_GAME_LIST ) { requestGameList( connection, messageParts[ 1 ] ); } else if ( messageParts[ 0 ] == SYSTEM_JOIN_OR_REQUEST_GAME ) { joinOrRequestGame( connection, messageParts[ 1 ] ); dumpCurrentState(); } else if ( messageParts[ 0 ] == SYSTEM_JOIN_GAME ) { joinGame( connection, messageParts[ 1 ] ); dumpCurrentState(); } else if ( messageParts[ 0 ] == SYSTEM_LEAVE_GAME ) { leaveGame( connection, messageParts[ 1 ] ); dumpCurrentState(); } else if ( messageParts[ 0 ] == SYSTEM_GAME_CREATION_REFUSED ) { // get the relevant information std::vector< std::string > messageInformation; StringUtils::explode( messageParts[ 1 ], ' ', messageInformation, 2 ); // and close the game closeGame( connection, messageInformation[ 0 ], messageInformation[ 1 ] ); dumpCurrentState(); } else if ( messageParts[ 0 ] == GAME_MESSAGE ) { // get the relevant information std::vector< std::string > messageInformation; StringUtils::explode( messageParts[ 1 ], ' ', messageInformation, 2 ); // and forward the message (gameId, message) handleGameMessage( connection, messageInformation[ 0 ], message ); } } }