File indexing completed on 2025-04-13 03:49:35
0001 /* 0002 This library is free software; you can redistribute it and/or 0003 modify it under the terms of the GNU General Public 0004 License either version 2 0005 of the License, or (at your option) any later version.as published by the Free Software Foundation. 0006 0007 This library is distributed in the hope that it will be useful, 0008 but WITHOUT ANY WARRANTY; without even the implied warranty of 0009 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0010 Library General Public License for more details. 0011 0012 You should have received a copy of the GNU General Public License 0013 along with this library; see the file COPYING.LIB. If not, write to 0014 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 0015 Boston, MA 02110-1301, USA. 0016 */ 0017 0018 #include "newgamesetup.h" 0019 #include "GameLogic/newplayerdata.h" 0020 #include "GameLogic/onu.h" 0021 0022 #include <KLocalizedString> 0023 #include "ksirk_debug.h" 0024 #include <QStandardPaths> 0025 #include <KMessageBox> 0026 0027 #include <QDir> 0028 0029 using namespace Ksirk; 0030 0031 NewGameSetup::NewGameSetup(Ksirk::GameLogic::GameAutomaton* automaton) : 0032 m_automaton(automaton), m_skin(""), m_worlds(), m_players(), 0033 m_nbPlayers(0),m_nbNetworkPlayers(0), 0034 m_useGoals(true), m_networkGameType(Ksirk::GameLogic::GameAutomaton::None), 0035 m_tcpPort(20000) 0036 { 0037 QStringList skinsDirs = QStandardPaths::locateAll(QStandardPaths::AppDataLocation, "skins", QStandardPaths::LocateDirectory); 0038 qCDebug(KSIRK_LOG) << skinsDirs; 0039 foreach (const QString &skinsDirName, skinsDirs) 0040 { 0041 if (skinsDirName.isEmpty()) 0042 { 0043 KMessageBox::error(nullptr, 0044 i18n("Skins directory not found - Verify your installation\nProgram cannot continue"), 0045 i18n("Fatal Error!")); 0046 exit(2); 0047 } 0048 qCDebug(KSIRK_LOG) << "Got skins dir name: " << skinsDirName; 0049 QDir skinsDir(skinsDirName); 0050 QStringList skinsDirsNames = skinsDir.entryList(QStringList("[a-zA-Z]*"), QDir::Dirs); 0051 0052 foreach (const QString& name, skinsDirsNames) 0053 { 0054 qCDebug(KSIRK_LOG) << "Got skin dir name: " << name; 0055 QDir skinDir(skinsDirName + '/' + name); 0056 if (skinDir.exists()) 0057 { 0058 qCDebug(KSIRK_LOG) << "Got skin dir: " << skinDir.dirName(); 0059 GameLogic::ONU* world = new GameLogic::ONU(automaton,skinsDirName + '/' + skinDir.dirName() + "/Data/world.desktop"); 0060 if (!world->skin().isEmpty()) 0061 { 0062 m_worlds[i18n(world->name().toUtf8().data())] = world; 0063 } 0064 else 0065 { 0066 delete world; 0067 } 0068 } 0069 } 0070 } 0071 } 0072 0073 int NewGameSetup::nbLocalPlayers() const 0074 { 0075 int n = 0; 0076 foreach (Ksirk::NewPlayerData* player, m_players) 0077 { 0078 if (!player->network()) 0079 { 0080 n++; 0081 } 0082 } 0083 return n; 0084 } 0085 0086 bool NewGameSetup::addPlayer(NewPlayerData* player) 0087 { 0088 qCDebug(KSIRK_LOG) << player->name(); 0089 bool found = false; 0090 foreach (Ksirk::NewPlayerData* p, m_players) 0091 { 0092 if (p->name() == player->name()) 0093 { 0094 found = true; 0095 break; 0096 } 0097 } 0098 if (!found) 0099 { 0100 m_players.push_back(player); 0101 } 0102 return !found; 0103 } 0104 0105 void NewGameSetup::clear() 0106 { 0107 qCDebug(KSIRK_LOG); 0108 m_players.clear(); 0109 } 0110 0111 QDataStream& operator<<(QDataStream& stream, const NewGameSetup& ngs) 0112 { 0113 qCDebug(KSIRK_LOG); 0114 stream << ngs.skin(); 0115 0116 stream << (quint32)ngs.players().size(); 0117 foreach (Ksirk::NewPlayerData* newPlayer, ngs.players()) 0118 { 0119 stream << newPlayer->name(); 0120 stream << newPlayer->nation(); 0121 stream << newPlayer->password(); 0122 stream << (quint32)newPlayer->computer(); 0123 stream << (quint32)newPlayer->network(); 0124 0125 } 0126 stream << (quint32)ngs.nbPlayers(); 0127 stream << (quint32)ngs.nbNetworkPlayers(); 0128 stream << (quint32)ngs.useGoals(); 0129 stream << (quint32)ngs.networkGameType(); 0130 stream << (quint32)ngs.tcpPort(); 0131 stream << ngs.host(); 0132 0133 return stream; 0134 } 0135 0136 QDataStream& operator>>(QDataStream& stream, NewGameSetup& ngs) 0137 { 0138 qCDebug(KSIRK_LOG); 0139 QString skin; 0140 stream >> skin; 0141 ngs.setSkin(skin); 0142 quint32 players; 0143 stream >> players; 0144 qCDebug(KSIRK_LOG) << "nb players" << players; 0145 for (quint32 i = 0; i < players; i++) 0146 { 0147 QString name; 0148 stream >> name; 0149 QString nation; 0150 stream >> nation; 0151 QString password; 0152 stream >> password; 0153 quint32 computer; 0154 stream >> computer; 0155 quint32 network; 0156 stream >> network; 0157 qCDebug(KSIRK_LOG) << "player" << name << nation << password << computer << !network; 0158 Ksirk::NewPlayerData* newPlayer = new NewPlayerData(name,nation,password,computer,!network); 0159 ngs.players().push_back(newPlayer); 0160 } 0161 quint32 nbPlayers; 0162 stream >> nbPlayers; 0163 ngs.setNbPlayers(nbPlayers); 0164 quint32 nbNetworkPlayers; 0165 stream >> nbNetworkPlayers; 0166 ngs.setNbNetworkPlayers(nbNetworkPlayers); 0167 quint32 useGoals; 0168 stream >> useGoals; 0169 ngs.setUseGoals(useGoals); 0170 quint32 networkGameType; 0171 stream >> networkGameType; 0172 ngs.setNetworkGameType((GameLogic::GameAutomaton::NetworkGameType)networkGameType); 0173 quint32 tcpPort; 0174 stream >> tcpPort; 0175 ngs.setTcpPort(tcpPort); 0176 QString host; 0177 stream >> host; 0178 ngs.setHost(host); 0179 return stream; 0180 }