File indexing completed on 2024-04-21 15:07:59
0001 // Copyright (c) 2002-2003 Rob Kaper <cap@capsi.com> 0002 // 0003 // This library is free software; you can redistribute it and/or 0004 // modify it under the terms of the GNU Lesser General Public 0005 // License version 2.1 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 // Lesser General Public License for more details. 0011 // 0012 // You should have received a copy of the GNU Lesser 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 #include <algorithm> 0018 #include <iostream> 0019 0020 #include "atlantic_core.h" 0021 0022 #include "auction.h" 0023 #include "card.h" 0024 #include "configoption.h" 0025 #include "estate.h" 0026 #include "estategroup.h" 0027 #include "game.h" 0028 #include "player.h" 0029 #include "trade.h" 0030 0031 AtlanticCore::AtlanticCore(QObject *parent) 0032 : QObject(parent) 0033 , m_playerSelf(nullptr) 0034 { 0035 } 0036 0037 void AtlanticCore::reset(bool deletePermanents) 0038 { 0039 qDeleteAll(m_auctions); 0040 m_auctions.clear(); 0041 qDeleteAll(m_estates); 0042 m_estates.clear(); 0043 qDeleteAll(m_estateGroups); 0044 m_estateGroups.clear(); 0045 qDeleteAll(m_cards); 0046 m_cards.clear(); 0047 0048 foreach (Trade *trade, m_trades) 0049 { 0050 Q_EMIT removeGUI(trade); 0051 trade->deleteLater(); 0052 } 0053 m_trades.clear(); 0054 0055 foreach (Player *player, m_players) 0056 { 0057 if (deletePermanents) 0058 { 0059 Q_EMIT removeGUI(player); 0060 player->deleteLater(); 0061 } 0062 else 0063 { 0064 player->setLocation(nullptr); 0065 player->setDestination(nullptr); 0066 } 0067 } 0068 if (deletePermanents) 0069 { 0070 m_players.clear(); 0071 m_playerSelf = nullptr; 0072 0073 foreach (Game *game, m_games) 0074 { 0075 Q_EMIT removeGUI(game); 0076 game->deleteLater(); 0077 } 0078 m_games.clear(); 0079 } 0080 } 0081 0082 bool AtlanticCore::selfIsMaster() const 0083 { 0084 return (m_playerSelf && m_playerSelf->game() && m_playerSelf->game()->master() == m_playerSelf); 0085 } 0086 0087 void AtlanticCore::setPlayerSelf(Player *player) 0088 { 0089 if (player == m_playerSelf) 0090 return; 0091 0092 Player *oldSelf = m_playerSelf; 0093 m_playerSelf = player; 0094 if (oldSelf) { 0095 oldSelf->setIsSelf(false); 0096 oldSelf->update(true); 0097 } 0098 if (player) { 0099 player->setIsSelf(true); 0100 player->update(true); 0101 } 0102 } 0103 0104 Player *AtlanticCore::playerSelf() const 0105 { 0106 return m_playerSelf; 0107 } 0108 0109 QList<Player *> AtlanticCore::players() const 0110 { 0111 return m_players; 0112 } 0113 0114 Player *AtlanticCore::newPlayer(int playerId, bool playerSelf) 0115 { 0116 Player *player = new Player(playerId); 0117 m_players.append(player); 0118 0119 if (playerSelf) 0120 { 0121 player->setIsSelf(playerSelf); 0122 m_playerSelf = player; 0123 } 0124 0125 Q_EMIT createGUI(player); 0126 0127 return player; 0128 } 0129 0130 Player *AtlanticCore::findPlayer(int playerId) const 0131 { 0132 foreach (Player *player, m_players) 0133 if (player->id() == playerId) 0134 return player; 0135 0136 return nullptr; 0137 } 0138 0139 void AtlanticCore::removePlayer(Player *player) 0140 { 0141 m_players.removeOne(player); 0142 Q_EMIT removeGUI(player); 0143 if (player == m_playerSelf) 0144 m_playerSelf = nullptr; 0145 player->deleteLater(); 0146 } 0147 0148 QList<Game *> AtlanticCore::games() const 0149 { 0150 return m_games; 0151 } 0152 0153 Game *AtlanticCore::newGame(int gameId, const QString &type) 0154 { 0155 Game *game = new Game(gameId); 0156 m_games.append(game); 0157 0158 if ( !type.isNull() ) 0159 game->setType(type); 0160 0161 Q_EMIT createGUI(game); 0162 0163 return game; 0164 } 0165 0166 Game *AtlanticCore::findGame(const QString &type) const 0167 { 0168 foreach (Game *game, m_games) 0169 if (game->id() == -1 && game->type() == type) 0170 return game; 0171 0172 return nullptr; 0173 } 0174 0175 Game *AtlanticCore::findGame(int gameId) const 0176 { 0177 if (gameId == -1) 0178 return nullptr; 0179 0180 foreach (Game *game, m_games) 0181 if (game->id() == gameId) 0182 return game; 0183 0184 return nullptr; 0185 } 0186 0187 Game *AtlanticCore::gameSelf() const 0188 { 0189 return( m_playerSelf ? m_playerSelf->game() : nullptr ); 0190 } 0191 0192 void AtlanticCore::removeGame(Game *game) 0193 { 0194 m_games.removeOne(game); 0195 foreach (Player *player, m_players) 0196 if (player->game() && player->game()->id() == game->id()) 0197 { 0198 player->setGame(nullptr); 0199 player->update(); 0200 } 0201 Q_EMIT removeGUI(game); 0202 game->deleteLater(); 0203 } 0204 0205 void AtlanticCore::emitGames() 0206 { 0207 foreach (Game *game, m_games) 0208 Q_EMIT createGUI(game); 0209 } 0210 0211 QList<Estate *> AtlanticCore::estates() const 0212 { 0213 return m_estates; 0214 } 0215 0216 Estate *AtlanticCore::newEstate(int estateId) 0217 { 0218 Estate *estate = new Estate(estateId); 0219 m_estates.append(estate); 0220 return estate; 0221 } 0222 0223 Estate *AtlanticCore::findEstate(int estateId) const 0224 { 0225 foreach (Estate *estate, m_estates) 0226 if (estate->id() == estateId) 0227 return estate; 0228 0229 return nullptr; 0230 } 0231 0232 Estate *AtlanticCore::estateAfter(Estate *estate) const 0233 { 0234 if (m_estates.isEmpty()) 0235 return nullptr; 0236 const QList<Estate *>::const_iterator itEnd = m_estates.constEnd(); 0237 QList<Estate *>::const_iterator it = std::find(m_estates.constBegin(), itEnd, estate); 0238 if (it == itEnd) 0239 return m_estates.at(0); 0240 ++it; 0241 return it != itEnd ? *it : m_estates.at(0); 0242 } 0243 0244 QList<EstateGroup *> AtlanticCore::estateGroups() const 0245 { 0246 return m_estateGroups; 0247 } 0248 0249 EstateGroup *AtlanticCore::newEstateGroup(int groupId) 0250 { 0251 EstateGroup *estateGroup = new EstateGroup(groupId); 0252 m_estateGroups.append(estateGroup); 0253 return estateGroup; 0254 } 0255 0256 EstateGroup *AtlanticCore::findEstateGroup(int groupId) const 0257 { 0258 foreach (EstateGroup *estateGroup, m_estateGroups) 0259 if (estateGroup->id() == groupId) 0260 return estateGroup; 0261 0262 return nullptr; 0263 } 0264 0265 QList<Trade *> AtlanticCore::trades() const 0266 { 0267 return m_trades; 0268 } 0269 0270 Trade *AtlanticCore::newTrade(int tradeId) 0271 { 0272 Trade *trade = new Trade(tradeId); 0273 m_trades.append(trade); 0274 0275 Q_EMIT createGUI(trade); 0276 0277 return trade; 0278 } 0279 0280 Trade *AtlanticCore::findTrade(int tradeId) const 0281 { 0282 foreach (Trade *trade, m_trades) 0283 if (trade->tradeId() == tradeId) 0284 return trade; 0285 0286 return nullptr; 0287 } 0288 0289 void AtlanticCore::removeTrade(Trade *trade) 0290 { 0291 m_trades.removeOne(trade); 0292 Q_EMIT removeGUI(trade); 0293 trade->deleteLater(); 0294 } 0295 0296 QList<Auction *> AtlanticCore::auctions() const 0297 { 0298 return m_auctions; 0299 } 0300 0301 Auction *AtlanticCore::newAuction(int auctionId, Estate *estate) 0302 { 0303 Auction *auction = new Auction(auctionId, estate); 0304 m_auctions.append(auction); 0305 return auction; 0306 } 0307 0308 Auction *AtlanticCore::findAuction(int auctionId) const 0309 { 0310 foreach (Auction *auction, m_auctions) 0311 if (auction->auctionId() == auctionId) 0312 return auction; 0313 0314 return nullptr; 0315 } 0316 0317 void AtlanticCore::delAuction(Auction *auction) 0318 { 0319 m_auctions.removeOne(auction); 0320 delete auction; 0321 } 0322 0323 QList<Card *> AtlanticCore::cards() const 0324 { 0325 return m_cards; 0326 } 0327 0328 Card *AtlanticCore::newCard(int cardId) 0329 { 0330 Card *card = new Card(cardId); 0331 m_cards.append(card); 0332 return card; 0333 } 0334 0335 Card *AtlanticCore::findCard(int cardId) const 0336 { 0337 foreach (Card *card, m_cards) 0338 if (card->cardId() == cardId) 0339 return card; 0340 0341 return nullptr; 0342 } 0343 0344 void AtlanticCore::printDebug() const 0345 { 0346 #define LP(string) (string).toLatin1().constData() 0347 0348 foreach (Player *player, m_players) 0349 if (player == m_playerSelf) 0350 std::cout << "PS: " << LP(player->name()) << ", game " << (player->game() ? player->game()->id() : -1) << std::endl; 0351 else 0352 std::cout << " P: " << LP(player->name()) << ", game " << (player->game() ? player->game()->id() : -1) << std::endl; 0353 0354 foreach (Game *game, m_games) 0355 { 0356 std::cout << " G: " << game->id() << ", master: " << (game->master() ? game->master()->id() : -1 ) << std::endl; 0357 foreach (ConfigOption *configOption, game->configOptions()) 0358 std::cout << " CO:" << configOption->id() << " " << LP(configOption->name()) << " " << LP(configOption->value()) << std::endl; 0359 } 0360 0361 foreach (Estate *estate, m_estates) 0362 std::cout << " E: " << LP(estate->name()) << std::endl; 0363 0364 foreach (EstateGroup *estateGroup, m_estateGroups) 0365 std::cout << "EG: " << LP(estateGroup->name()) << std::endl; 0366 0367 foreach (Auction *auction, m_auctions) 0368 std::cout << " A: " << auction->auctionId() << std::endl; 0369 0370 foreach (Trade *trade, m_trades) 0371 std::cout << " T: " << trade->tradeId() << std::endl; 0372 0373 foreach (Card *card, m_cards) 0374 std::cout << "CA: " << card->cardId() << std::endl; 0375 0376 #undef LP 0377 } 0378 0379 #include "moc_atlantic_core.cpp"