File indexing completed on 2024-04-21 04:02:07

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 "KBlocksNetPlayer.h"
0008 
0009 #include "AI/KBlocksAILog.h"
0010 
0011 KBlocksNetPlayer::KBlocksNetPlayer(GamePlayerInterface *player, const string &serverIP, int localPort)
0012 {
0013     mpNetClient = new KBlocksNetClient(QString::fromStdString(serverIP), localPort);
0014 
0015     mpPlayer = player;
0016     mpGame = nullptr;
0017 
0018     mSendLength = 0;
0019     mActionList.clear();
0020 }
0021 
0022 KBlocksNetPlayer::~KBlocksNetPlayer()
0023 {
0024     delete mpNetClient;
0025 }
0026 
0027 void KBlocksNetPlayer::joinGame(int gameIndex)
0028 {
0029     int tmpByteCount = 0;
0030     char tmpByteData[256];
0031     string tmpName = mpPlayer->getName();
0032 
0033     tmpByteData[tmpByteCount++] = '|';
0034     tmpByteData[tmpByteCount++] = 'a';
0035     tmpByteData[tmpByteCount++] = 'p';
0036     tmpByteData[tmpByteCount++] = '|';
0037     tmpByteData[tmpByteCount++] = (char)gameIndex + '0';
0038     tmpByteData[tmpByteCount++] = '|';
0039     for (size_t i = 0; i < tmpName.length(); ++i) {
0040         tmpByteData[tmpByteCount++] = (char)tmpName[i];
0041     }
0042     tmpByteData[tmpByteCount++] = 0;
0043 
0044     mpNetClient->sendData(tmpByteCount, tmpByteData);
0045 }
0046 
0047 void KBlocksNetPlayer::quitGame()
0048 {
0049     char tmpByteData[5] = {'|', 'd', 'p', '|', '\0'};
0050     mpNetClient->sendData(5, tmpByteData);
0051 }
0052 
0053 void KBlocksNetPlayer::startGame(KBlocksSingleGame *p)
0054 {
0055     char tmpByteData[4] = {'|', 's', '|', '\0'};
0056     mpNetClient->sendData(4, tmpByteData);
0057 
0058     mpGame = p;
0059     mpPlayer->startGame(mpGame);
0060 
0061     mActionList.clear();
0062 }
0063 
0064 void KBlocksNetPlayer::stopGame()
0065 {
0066     char tmpByteData[4] = {'|', 'c', '|', '\0'};
0067     mpNetClient->sendData(4, tmpByteData);
0068 
0069     mpPlayer->stopGame();
0070     mpGame = nullptr;
0071 
0072     mActionList.clear();
0073 }
0074 
0075 bool KBlocksNetPlayer::execute()
0076 {
0077     bool execResult = true;
0078     char *tmpByteData = new char[256];
0079 
0080     int ret = mpNetClient->recvData(256, tmpByteData);
0081     if (ret < 0) {
0082         // Do nothing...
0083         //printf("--Nothing received\n");
0084     } else if (tmpByteData[0] == -1) {
0085         mSendLength = (unsigned char)tmpByteData[1];
0086         //printf("--Send Length = %d\n", mSendLength);
0087     } else if (tmpByteData[0] == 127) {
0088         execResult = false;
0089         //printf("--Game Ended\n");
0090     } else {
0091         //printf("++Game Updates (%d) of [%d bytes]\n", (int)tmpByteData[0], ret);
0092         int tmpPieceCount = formIntFromByte(tmpByteData + 13);
0093         for (int i = 0; i < tmpPieceCount; ++i) {
0094             mpGame->getPiece(i)->decodeData((unsigned char *)tmpByteData + 17 + i * 4);
0095         }
0096 
0097         mpGame->getField()->decodeData((unsigned char *)tmpByteData + 18 + tmpPieceCount * 4);
0098 
0099         mActionList.clear();
0100         mpPlayer->think(&mActionList);
0101 
0102         GamePlayer_ActionList::iterator it;
0103         int byteCounter = 0;
0104         char *tmpSendData = new char[256];
0105         tmpSendData[byteCounter++] = '|';
0106         tmpSendData[byteCounter++] = 'r';
0107         tmpSendData[byteCounter++] = 'p';
0108         tmpSendData[byteCounter++] = '|';
0109         if (mSendLength == 0) {
0110             for (it = mActionList.begin(); it != mActionList.end(); ++it) {
0111                 tmpSendData[byteCounter++] = (char) * it + '0';
0112             }
0113         } else if (!mActionList.empty()) {
0114             for (int i = 0; i < mSendLength; i++) {
0115                 tmpSendData[byteCounter++] = (char)mActionList.front() + '0';
0116                 mActionList.pop_front();
0117                 if (mActionList.empty()) {
0118                     break;
0119                 }
0120             }
0121         }
0122         tmpSendData[byteCounter++] = '|';
0123         tmpSendData[byteCounter++] = '\0';
0124 
0125         mpNetClient->sendData(byteCounter, tmpSendData);
0126         //printf("Sending : [%s]\n", tmpSendData);
0127 
0128         delete [] tmpSendData;
0129     }
0130 
0131     delete [] tmpByteData;
0132 
0133     return execResult;
0134 }
0135 
0136 int KBlocksNetPlayer::formIntFromByte(char *data)
0137 {
0138     int value = 0;
0139     value += ((int)data[0]) & 0x000000FF;
0140     value += (((int)data[1]) <<  8) & 0x0000FF00;
0141     value += (((int)data[2]) << 16) & 0x00FF0000;
0142     value += (((int)data[3]) << 24) & 0xFF000000;
0143     return value;
0144 }