Store isWinnable as QVariant

QML doesn't natively support the complex type returned from isWinnable
property (`std::pair<std::optional<bool>, int>`), instead use
QVariantMap to implement custom attributes and return as QVariant.
This commit is contained in:
ItsDrike 2024-12-09 19:34:27 +01:00
parent c5e68601a4
commit b0630c2690
Signed by: ItsDrike
GPG key ID: FA2745890B7048C0
2 changed files with 15 additions and 4 deletions

View file

@ -879,8 +879,18 @@ bool GameState::gameWon() const {
return m_gameWon;
}
std::pair<std::optional<bool>, int> GameState::isWinnable() const {
return m_isWinnable;
QVariant GameState::isWinnable() const {
QVariantMap map;
if (m_isWinnable.first.has_value()) {
map["winnable"] = QVariant(m_isWinnable.first.value());
} else {
// bit annoyingly, in QML, this will map to undefined, not null
// there doesn't seem to be an easy way to specify that this is null from C++
map["winnable"] = QVariant();
}
map["optionalBool"] = m_isWinnable.first.has_value() ? QVariant(m_isWinnable.first.value()) : QVariant::fromValue(QVariant());
map["depth"] = m_isWinnable.second;
return map;
}
std::pair<std::optional<bool>, int> GameState::checkWinnable() {

View file

@ -10,6 +10,7 @@
#include <qqmlintegration.h>
#include <qqmllist.h>
#include <qtmetamacros.h>
#include <qvariant.h>
// Limits for checking winnability
#define MAX_EVAL_TIME 100 // Evaluation time limit (ms)
@ -25,7 +26,7 @@ class GameState : public QObject {
Q_PROPERTY(int moveAmount READ moveAmount NOTIFY moveAmountChanged)
Q_PROPERTY(bool gameWon READ gameWon NOTIFY gameWonChanged)
Q_PROPERTY(bool preliminaryWin READ preliminaryWin NOTIFY preliminaryWinChanged)
Q_PROPERTY(std::pair<std::optional<bool>, int> isWinnable READ isWinnable NOTIFY isWinnableChanged)
Q_PROPERTY(QVariant isWinnable READ isWinnable NOTIFY isWinnableChanged)
public:
explicit GameState(QObject* parent = nullptr, bool preDealCards = true, bool enableWinnabilitySim = true);
@ -39,7 +40,7 @@ class GameState : public QObject {
int moveAmount() const;
bool preliminaryWin() const;
bool gameWon() const;
std::pair<std::optional<bool>, int> isWinnable() const;
QVariant isWinnable() const;
// General functions
Q_INVOKABLE void dealCards();