File indexing completed on 2024-04-28 07:51:26

0001 /***************************************************************************
0002 *   KBlocks, a falling blocks game by KDE                                *
0003 *   SPDX-FileCopyrightText: 2010 Zhongjie Cai <squall.leonhart.cai@gmail.com>       *
0004 *                                                                         *
0005 *   SPDX-License-Identifier: GPL-2.0-or-later
0006 ***************************************************************************/
0007 #include "KBlocksPlayNetwork.h"
0008 
0009 #ifdef Q_OS_WIN
0010 #include <windows.h>
0011 #else
0012 #include <unistd.h>
0013 #endif
0014 
0015 KBlocksPlayNetwork::KBlocksPlayNetwork(int capacity, const string &serverIP, int localPort)
0016 {
0017     mServerIP = serverIP;
0018     mLocalPort = localPort;
0019 
0020     mRunning = false;
0021 
0022     mPlayerCount = 0;
0023     mMaxCapacity = capacity;
0024 
0025     maPlayerList = new KBlocksNetPlayer*[mMaxCapacity];
0026 
0027     mpGameLogic = new KBlocksGameLogic(mMaxCapacity);
0028     mpGameLogic->setGameSeed(0);
0029     mpGameLogic->setGamePunish(false);
0030     mpGameLogic->setGameStandbyMode(true);
0031     mpGameLogic->setInitInterval(0);
0032     mpGameLogic->setLevelUpInterval(0);
0033 }
0034 
0035 KBlocksPlayNetwork::~KBlocksPlayNetwork()
0036 {
0037     mpGameLogic->stopGame();
0038     delete mpGameLogic;
0039 
0040     delete [] maPlayerList;
0041 }
0042 
0043 bool KBlocksPlayNetwork::addGamePlayer(GamePlayerInterface *p)
0044 {
0045     if (mPlayerCount == mMaxCapacity) {
0046         return false;
0047     }
0048     maPlayerList[mPlayerCount] = new KBlocksNetPlayer(p, mServerIP, mLocalPort + mPlayerCount);
0049     mPlayerCount++;
0050     return true;
0051 }
0052 
0053 void KBlocksPlayNetwork::clearGamePlayer()
0054 {
0055     for (int i = 0; i < mPlayerCount; i++) {
0056         delete maPlayerList[i];
0057         maPlayerList[i] = nullptr;
0058     }
0059     mPlayerCount = 0;
0060 }
0061 
0062 void KBlocksPlayNetwork::startGame()
0063 {
0064     mpGameLogic->startGame(mPlayerCount);
0065     for (int i = 0; i < mPlayerCount; i++) {
0066         mpGameLogic->getSingleGame(i)->stopGame();
0067         maPlayerList[i]->joinGame(i);
0068     }
0069     for (int i = 0; i < mPlayerCount; i++) {
0070         maPlayerList[i]->startGame(mpGameLogic->getSingleGame(i));
0071     }
0072 }
0073 
0074 void KBlocksPlayNetwork::stopGame()
0075 {
0076     for (int i = 0; i < mPlayerCount; i++) {
0077         maPlayerList[i]->stopGame();
0078         maPlayerList[i]->quitGame();
0079     }
0080     mpGameLogic->stopGame();
0081 }
0082 
0083 int KBlocksPlayNetwork::execute()
0084 {
0085     mRunning = true;
0086 
0087     while (mRunning) {
0088         for (int i = 0; i < mPlayerCount; i++) {
0089             if (!maPlayerList[i]->execute()) {
0090                 mRunning = false;
0091                 break;
0092             }
0093         }
0094 #ifndef Q_OS_WIN
0095         usleep(100000);
0096 #else
0097         Sleep(100);
0098 #endif
0099     }
0100 
0101     return 0;
0102 }
0103 
0104 void KBlocksPlayNetwork::cancelExecute()
0105 {
0106     mRunning = false;
0107 }