mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-11-09 03:23:24 -08:00
[Refactor] Move ArrowRegistry out of game/ into game_graphics/board
This commit is contained in:
parent
8029310764
commit
7c4c541a81
4 changed files with 10 additions and 4 deletions
|
|
@ -1,51 +0,0 @@
|
|||
#include "arrow_registry.h"
|
||||
|
||||
#include "../game_graphics/board/arrow_item.h"
|
||||
#include "board/arrow_data.h"
|
||||
|
||||
#include <QSharedPointer>
|
||||
|
||||
void ArrowRegistry::insert(QSharedPointer<ArrowData> data, ArrowItem *arrow)
|
||||
{
|
||||
const ArrowKey key{data->creatorId, data->id};
|
||||
|
||||
if (auto *existing = take(data->creatorId, data->id)) {
|
||||
existing->delArrow();
|
||||
}
|
||||
|
||||
dataStore.insert(key, data);
|
||||
items.insert(key, arrow);
|
||||
byPlayer[data->creatorId].insert(data->id);
|
||||
}
|
||||
|
||||
ArrowItem *ArrowRegistry::take(int creatorId, int arrowId)
|
||||
{
|
||||
const ArrowKey key{creatorId, arrowId};
|
||||
dataStore.remove(key);
|
||||
auto &playerSet = byPlayer[creatorId];
|
||||
playerSet.remove(arrowId);
|
||||
if (playerSet.isEmpty()) {
|
||||
byPlayer.remove(creatorId);
|
||||
}
|
||||
return items.take(key);
|
||||
}
|
||||
|
||||
ArrowItem *ArrowRegistry::get(int creatorId, int arrowId) const
|
||||
{
|
||||
return items.value(ArrowKey{creatorId, arrowId}, nullptr);
|
||||
}
|
||||
|
||||
bool ArrowRegistry::contains(int creatorId, int arrowId) const
|
||||
{
|
||||
return items.contains(ArrowKey{creatorId, arrowId});
|
||||
}
|
||||
|
||||
QSet<int> ArrowRegistry::idsForPlayer(int playerId) const
|
||||
{
|
||||
return byPlayer.value(playerId);
|
||||
}
|
||||
|
||||
QList<ArrowItem *> ArrowRegistry::all() const
|
||||
{
|
||||
return items.values();
|
||||
}
|
||||
|
|
@ -1,43 +0,0 @@
|
|||
#ifndef COCKATRICE_ARROW_REGISTRY_H
|
||||
#define COCKATRICE_ARROW_REGISTRY_H
|
||||
|
||||
#include <QList>
|
||||
#include <QMap>
|
||||
#include <QSet>
|
||||
|
||||
class ArrowItem;
|
||||
struct ArrowData;
|
||||
template <class T> class QSharedPointer;
|
||||
|
||||
struct ArrowKey
|
||||
{
|
||||
int creatorId;
|
||||
int arrowId;
|
||||
|
||||
bool operator<(const ArrowKey &other) const
|
||||
{
|
||||
if (creatorId != other.creatorId) {
|
||||
return creatorId < other.creatorId;
|
||||
}
|
||||
return arrowId < other.arrowId;
|
||||
}
|
||||
};
|
||||
|
||||
class ArrowRegistry
|
||||
{
|
||||
public:
|
||||
void insert(QSharedPointer<ArrowData> data, ArrowItem *arrow);
|
||||
ArrowItem *take(int creatorId, int arrowId);
|
||||
|
||||
[[nodiscard]] ArrowItem *get(int creatorId, int arrowId) const;
|
||||
[[nodiscard]] bool contains(int creatorId, int arrowId) const;
|
||||
[[nodiscard]] QSet<int> idsForPlayer(int playerId) const;
|
||||
[[nodiscard]] QList<ArrowItem *> all() const;
|
||||
|
||||
private:
|
||||
QMap<ArrowKey, QSharedPointer<ArrowData>> dataStore;
|
||||
QMap<ArrowKey, ArrowItem *> items;
|
||||
QMap<int, QSet<int>> byPlayer;
|
||||
};
|
||||
|
||||
#endif
|
||||
Loading…
Add table
Add a link
Reference in a new issue