Server_CardZone speed optimisation

This commit is contained in:
Max-Wilhelm Bruker 2013-01-03 18:16:17 +01:00
parent 8b6eace312
commit 733aa6c52b
3 changed files with 62 additions and 49 deletions

View file

@ -55,6 +55,8 @@ int Server_CardZone::removeCard(Server_Card *card)
{ {
int index = cards.indexOf(card); int index = cards.indexOf(card);
cards.removeAt(index); cards.removeAt(index);
if (has_coords)
coordinateMap[card->getY()].remove(card->getX());
card->setZone(0); card->setZone(0);
return index; return index;
@ -92,21 +94,24 @@ Server_Card *Server_CardZone::getCard(int id, int *position, bool remove)
int Server_CardZone::getFreeGridColumn(int x, int y, const QString &cardName) const int Server_CardZone::getFreeGridColumn(int x, int y, const QString &cardName) const
{ {
QMap<int, Server_Card *> coordMap; const QMap<int, Server_Card *> &coordMap = coordinateMap.value(y);
for (int i = 0; i < cards.size(); ++i)
if (cards[i]->getY() == y)
coordMap.insert(cards[i]->getX(), cards[i]);
int resultX = 0; int resultX = 0;
if (x == -1) { if (x == -1) {
for (int i = 0; i < cards.size(); ++i) QMapIterator<int, Server_Card *> cardIterator(coordMap);
if ((cards[i]->getName() == cardName) && !(cards[i]->getX() % 3) && (cards[i]->getY() == y)) { while (cardIterator.hasNext()) {
if (!cards[i]->getAttachedCards().isEmpty()) cardIterator.next();
const int cardX = cardIterator.key();
if (cardX % 3)
continue; continue;
if (!coordMap.value(cards[i]->getX() + 1)) Server_Card *card = cardIterator.value();
return cards[i]->getX() + 1; if (card->getName() == cardName) {
if (!coordMap.value(cards[i]->getX() + 2)) if (!card->getAttachedCards().isEmpty())
return cards[i]->getX() + 2; continue;
if (!coordMap.contains(cardX + 1))
return cardX + 1;
if (!coordMap.contains(cardX + 2))
return cardX + 2;
}
} }
} else if (x == -2) { } else if (x == -2) {
} else { } else {
@ -127,7 +132,7 @@ int Server_CardZone::getFreeGridColumn(int x, int y, const QString &cardName) co
} }
if (x < 0) if (x < 0)
while (coordMap.value(resultX)) while (coordMap.contains(resultX))
resultX += 3; resultX += 3;
return resultX; return resultX;
@ -138,12 +143,7 @@ bool Server_CardZone::isColumnStacked(int x, int y) const
if (!has_coords) if (!has_coords)
return false; return false;
QMap<int, Server_Card *> coordMap; return coordinateMap[y].contains((x / 3) * 3 + 1);
for (int i = 0; i < cards.size(); ++i)
if (cards[i]->getY() == y)
coordMap.insert(cards[i]->getX(), cards[i]);
return coordMap.contains((x / 3) * 3 + 1);
} }
bool Server_CardZone::isColumnEmpty(int x, int y) const bool Server_CardZone::isColumnEmpty(int x, int y) const
@ -151,60 +151,68 @@ bool Server_CardZone::isColumnEmpty(int x, int y) const
if (!has_coords) if (!has_coords)
return true; return true;
QMap<int, Server_Card *> coordMap; return !coordinateMap[y].contains((x / 3) * 3);
for (int i = 0; i < cards.size(); ++i)
if (cards[i]->getY() == y)
coordMap.insert(cards[i]->getX(), cards[i]);
return !coordMap.contains((x / 3) * 3);
} }
void Server_CardZone::moveCard(GameEventStorage &ges, QMap<int, Server_Card *> &coordMap, Server_Card *card, int x, int y) void Server_CardZone::moveCardInRow(GameEventStorage &ges, Server_Card *card, int x, int y)
{ {
coordMap.remove(card->getY() * 10000 + card->getX()); coordinateMap[y].remove(card->getX());
CardToMove *cardToMove = new CardToMove; CardToMove *cardToMove = new CardToMove;
cardToMove->set_card_id(card->getId()); cardToMove->set_card_id(card->getId());
player->moveCard(ges, this, QList<const CardToMove *>() << cardToMove, this, x, y, false, false); player->moveCard(ges, this, QList<const CardToMove *>() << cardToMove, this, x, y, false, false);
delete cardToMove; delete cardToMove;
coordMap.insert(y * 10000 + x, card); coordinateMap[y].insert(x, card);
} }
void Server_CardZone::fixFreeSpaces(GameEventStorage &ges) void Server_CardZone::fixFreeSpaces(GameEventStorage &ges)
{ {
QMap<int, Server_Card *> coordMap; if (!has_coords)
QSet<int> placesToLook; return;
for (int i = 0; i < cards.size(); ++i) {
coordMap.insert(cards[i]->getY() * 10000 + cards[i]->getX(), cards[i]);
placesToLook.insert(cards[i]->getY() * 10000 + (cards[i]->getX() / 3) * 3);
}
QSetIterator<int> placeIterator(placesToLook); QSet<QPair<int, int> > placesToLook;
for (int i = 0; i < cards.size(); ++i)
placesToLook.insert(QPair<int, int>((cards[i]->getX() / 3) * 3, cards[i]->getY()));
QSetIterator<QPair<int, int> > placeIterator(placesToLook);
while (placeIterator.hasNext()) { while (placeIterator.hasNext()) {
int foo = placeIterator.next(); const QPair<int, int> &foo = placeIterator.next();
int y = foo / 10000; int baseX = foo.first;
int baseX = foo - y * 10000; int y = foo.second;
if (!coordMap.contains(y * 10000 + baseX)) { if (!coordinateMap[y].contains(baseX)) {
if (coordMap.contains(y * 10000 + baseX + 1)) if (coordinateMap[y].contains(baseX + 1))
moveCard(ges, coordMap, coordMap.value(y * 10000 + baseX + 1), baseX, y); moveCardInRow(ges, coordinateMap[y].value(baseX + 1), baseX, y);
else if (coordMap.contains(y * 10000 + baseX + 2)) { else if (coordinateMap[y].contains(baseX + 2)) {
moveCard(ges, coordMap, coordMap.value(y * 10000 + baseX + 2), baseX, y); moveCardInRow(ges, coordinateMap[y].value(baseX + 2), baseX, y);
continue; continue;
} else } else
continue; continue;
} }
if (!coordMap.contains(y * 10000 + baseX + 1) && coordMap.contains(y * 10000 + baseX + 2)) if (!coordinateMap[y].contains(baseX + 1) && coordinateMap[y].contains(baseX + 2))
moveCard(ges, coordMap, coordMap.value(y * 10000 + baseX + 2), baseX + 1, y); moveCardInRow(ges, coordinateMap[y].value(baseX + 2), baseX + 1, y);
} }
} }
void Server_CardZone::updateCardCoordinates(Server_Card *card, int oldX, int oldY)
{
if (!has_coords)
return;
if (oldX != -1)
coordinateMap[oldY].remove(oldX);
if (card->getX() != -1)
coordinateMap[card->getY()].insert(card->getX(), card);
}
void Server_CardZone::insertCard(Server_Card *card, int x, int y) void Server_CardZone::insertCard(Server_Card *card, int x, int y)
{ {
if (hasCoords()) { if (hasCoords()) {
card->setCoords(x, y); card->setCoords(x, y);
cards.append(card); cards.append(card);
if (x != -1)
coordinateMap[y].insert(x, card);
} else { } else {
card->setCoords(0, 0); card->setCoords(0, 0);
if (x == -1) if (x == -1)
@ -220,6 +228,7 @@ void Server_CardZone::clear()
for (int i = 0; i < cards.size(); i++) for (int i = 0; i < cards.size(); i++)
delete cards.at(i); delete cards.at(i);
cards.clear(); cards.clear();
coordinateMap.clear();
playersWithWritePermission.clear(); playersWithWritePermission.clear();
} }

View file

@ -41,6 +41,7 @@ private:
QSet<int> playersWithWritePermission; QSet<int> playersWithWritePermission;
bool alwaysRevealTopCard; bool alwaysRevealTopCard;
QList<Server_Card *> cards; QList<Server_Card *> cards;
QMap<int, QMap<int, Server_Card *> > coordinateMap; // y -> (x -> card)
public: public:
Server_CardZone(Server_Player *_player, const QString &_name, bool _has_coords, ServerInfo_Zone::ZoneType _type); Server_CardZone(Server_Player *_player, const QString &_name, bool _has_coords, ServerInfo_Zone::ZoneType _type);
~Server_CardZone(); ~Server_CardZone();
@ -61,8 +62,9 @@ public:
bool isColumnEmpty(int x, int y) const; bool isColumnEmpty(int x, int y) const;
bool isColumnStacked(int x, int y) const; bool isColumnStacked(int x, int y) const;
void fixFreeSpaces(GameEventStorage &ges); void fixFreeSpaces(GameEventStorage &ges);
void moveCard(GameEventStorage &ges, QMap<int, Server_Card *> &coordMap, Server_Card *card, int x, int y); void moveCardInRow(GameEventStorage &ges, Server_Card *card, int x, int y);
void insertCard(Server_Card *card, int x, int y); void insertCard(Server_Card *card, int x, int y);
void updateCardCoordinates(Server_Card *card, int oldX, int oldY);
void shuffle(); void shuffle();
void clear(); void clear();
void addWritePermission(int playerId); void addWritePermission(int playerId);

View file

@ -1024,6 +1024,11 @@ Response::ResponseCode Server_Player::cmdAttachCard(const Command_AttachCard &cm
for (int i = 0; i < attachedList.size(); ++i) for (int i = 0; i < attachedList.size(); ++i)
attachedList[i]->getZone()->getPlayer()->unattachCard(ges, attachedList[i]); attachedList[i]->getZone()->getPlayer()->unattachCard(ges, attachedList[i]);
card->setParentCard(targetCard);
const int oldX = card->getX();
card->setCoords(-1, card->getY());
startzone->updateCardCoordinates(card, oldX, card->getY());
if (targetzone->isColumnStacked(targetCard->getX(), targetCard->getY())) { if (targetzone->isColumnStacked(targetCard->getX(), targetCard->getY())) {
CardToMove *cardToMove = new CardToMove; CardToMove *cardToMove = new CardToMove;
cardToMove->set_card_id(targetCard->getId()); cardToMove->set_card_id(targetCard->getId());
@ -1031,9 +1036,6 @@ Response::ResponseCode Server_Player::cmdAttachCard(const Command_AttachCard &cm
delete cardToMove; delete cardToMove;
} }
card->setParentCard(targetCard);
card->setCoords(-1, card->getY());
Event_AttachCard event; Event_AttachCard event;
event.set_start_zone(startzone->getName().toStdString()); event.set_start_zone(startzone->getName().toStdString());
event.set_card_id(card->getId()); event.set_card_id(card->getId());