1 /*
2    This library is free software; you can redistribute it and/or
3    modify it under the terms of the GNU General Public
4    License either version 2
5    of the License, or (at your option) any later version.as published by the Free Software Foundation.
6 
7    This library is distributed in the hope that it will be useful,
8    but WITHOUT ANY WARRANTY; without even the implied warranty of
9    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10    Library General Public License for more details.
11 
12    You should have received a copy of the GNU General Public License
13    along with this library; see the file COPYING.LIB.  If not, write to
14    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
15    Boston, MA 02110-1301, USA.
16 */
17 
18 #include "newgamesetup.h"
19 #include "GameLogic/newplayerdata.h"
20 #include "GameLogic/onu.h"
21 
22 #include <KLocalizedString>
23 #include "ksirk_debug.h"
24 #include <QStandardPaths>
25 #include <KMessageBox>
26 
27 #include <QDir>
28 
29 using namespace Ksirk;
30 
NewGameSetup(Ksirk::GameLogic::GameAutomaton * automaton)31 NewGameSetup::NewGameSetup(Ksirk::GameLogic::GameAutomaton* automaton) :
32 m_automaton(automaton), m_skin(""), m_worlds(), m_players(),
33                                m_nbPlayers(0),m_nbNetworkPlayers(0),
34                                m_useGoals(true), m_networkGameType(Ksirk::GameLogic::GameAutomaton::None),
35                                m_tcpPort(20000)
36 {
37   QStringList skinsDirs = QStandardPaths::locateAll(QStandardPaths::AppDataLocation, "skins", QStandardPaths::LocateDirectory);
38   qCDebug(KSIRK_LOG) << skinsDirs;
39   foreach (const QString &skinsDirName, skinsDirs)
40   {
41     if (skinsDirName.isEmpty())
42     {
43       KMessageBox::error(0,
44                          i18n("Skins directory not found - Verify your installation\nProgram cannot continue"),
45                          i18n("Fatal Error!"));
46                          exit(2);
47     }
48     qCDebug(KSIRK_LOG) << "Got skins dir name: " << skinsDirName;
49     QDir skinsDir(skinsDirName);
50     QStringList skinsDirsNames = skinsDir.entryList(QStringList("[a-zA-Z]*"), QDir::Dirs);
51 
52     foreach (const QString& name, skinsDirsNames)
53     {
54       qCDebug(KSIRK_LOG) << "Got skin dir name: " << name;
55       QDir skinDir(skinsDirName + '/' + name);
56       if (skinDir.exists())
57       {
58         qCDebug(KSIRK_LOG) << "Got skin dir: " << skinDir.dirName();
59         GameLogic::ONU* world = new GameLogic::ONU(automaton,skinsDirName + '/' + skinDir.dirName() + "/Data/world.desktop");
60         if (!world->skin().isEmpty())
61         {
62           m_worlds[i18n(world->name().toUtf8().data())] = world;
63         }
64         else
65         {
66           delete world;
67         }
68       }
69     }
70   }
71 }
72 
nbLocalPlayers() const73 int NewGameSetup::nbLocalPlayers() const
74 {
75   int n = 0;
76   foreach (Ksirk::NewPlayerData* player, m_players)
77   {
78     if (!player->network())
79     {
80       n++;
81     }
82   }
83   return n;
84 }
85 
addPlayer(NewPlayerData * player)86 bool NewGameSetup::addPlayer(NewPlayerData* player)
87 {
88   qCDebug(KSIRK_LOG) << player->name();
89   bool found = false;
90   foreach (Ksirk::NewPlayerData* p, m_players)
91   {
92     if (p->name() == player->name())
93     {
94       found = true;
95       break;
96     }
97   }
98   if (!found)
99   {
100     m_players.push_back(player);
101   }
102   return !found;
103 }
104 
clear()105 void NewGameSetup::clear()
106 {
107   qCDebug(KSIRK_LOG);
108   m_players.clear();
109 }
110 
operator <<(QDataStream & stream,const NewGameSetup & ngs)111 QDataStream& operator<<(QDataStream& stream, const NewGameSetup& ngs)
112 {
113   qCDebug(KSIRK_LOG);
114   stream << ngs.skin();
115 
116   stream << (quint32)ngs.players().size();
117   foreach (Ksirk::NewPlayerData* newPlayer, ngs.players())
118   {
119     stream << newPlayer->name();
120     stream << newPlayer->nation();
121     stream << newPlayer->password();
122     stream << (quint32)newPlayer->computer();
123     stream << (quint32)newPlayer->network();
124 
125   }
126   stream << (quint32)ngs.nbPlayers();
127   stream << (quint32)ngs.nbNetworkPlayers();
128   stream << (quint32)ngs.useGoals();
129   stream << (quint32)ngs.networkGameType();
130   stream << (quint32)ngs.tcpPort();
131   stream << ngs.host();
132 
133   return stream;
134 }
135 
operator >>(QDataStream & stream,NewGameSetup & ngs)136 QDataStream& operator>>(QDataStream& stream, NewGameSetup& ngs)
137 {
138   qCDebug(KSIRK_LOG);
139   QString skin;
140   stream >> skin;
141   ngs.setSkin(skin);
142   quint32 players;
143   stream >> players;
144   qCDebug(KSIRK_LOG) << "nb players" << players;
145   for (quint32 i = 0; i < players; i++)
146   {
147     QString name;
148     stream >> name;
149     QString nation;
150     stream >> nation;
151     QString password;
152     stream >> password;
153     quint32 computer;
154     stream >> computer;
155     quint32 network;
156     stream >> network;
157     qCDebug(KSIRK_LOG) << "player" << name << nation << password << computer << !network;
158     Ksirk::NewPlayerData* newPlayer = new NewPlayerData(name,nation,password,computer,!network);
159     ngs.players().push_back(newPlayer);
160   }
161   quint32 nbPlayers;
162   stream >> nbPlayers;
163   ngs.setNbPlayers(nbPlayers);
164   quint32 nbNetworkPlayers;
165   stream >> nbNetworkPlayers;
166   ngs.setNbNetworkPlayers(nbNetworkPlayers);
167   quint32 useGoals;
168   stream >> useGoals;
169   ngs.setUseGoals(useGoals);
170   quint32 networkGameType;
171   stream >> networkGameType;
172   ngs.setNetworkGameType((GameLogic::GameAutomaton::NetworkGameType)networkGameType);
173   quint32 tcpPort;
174   stream >> tcpPort;
175   ngs.setTcpPort(tcpPort);
176   QString host;
177   stream >> host;
178   ngs.setHost(host);
179   return stream;
180 }
181