Cavoke  1.1.0
A Platform for creating and hosting multiplayer turn-based board games
Loading...
Searching...
No Matches
cavokeclientmodel.cpp
1#include "cavokeclientmodel.h"
2#include <QtQuick>
3
4CavokeClientModel::CavokeClientModel(QObject *parent) : QObject{parent} {
5}
6
7void CavokeClientModel::loadQmlGame(const QString &gameId) {
8 // TODO: change to url loading from cache, not resources
9 auto *gameModel =
10 new CavokeQmlGameModel(QUrl::fromUserInput(gameId),
11 this); // must be alive when accessed from QML
12
13 qDebug() << "Starting game from: " << gameId;
14
15 emit startQmlApplication(gameModel);
16}
17void CavokeClientModel::updateGamesList(const QJsonArray &newGamesList) {
18 std::vector<GameInfo> got_from;
19 std::transform(newGamesList.begin(), newGamesList.end(),
20 std::back_inserter(got_from), [](const QJsonValue &obj) {
21 GameInfo game_info;
22 game_info.read(obj.toObject());
23 return game_info;
24 });
25 gamesList = std::move(got_from);
26
27 if (!newGamesList.empty()) {
28 qDebug() << "First In Model: " << gamesList[0].id;
29 }
30 emit gamesListUpdated(gamesList);
31}
32void CavokeClientModel::receivedGameIndexChange(int newIndex) {
33 emit updateSelectedGame(gamesList[newIndex]);
34}
35
36void CavokeClientModel::receivedGameIndexChangeInList(int newIndex) {
37 emit updateSelectedGameInList(gamesList[newIndex]);
38}
39
40void CavokeClientModel::gotIndexToDownload(int index) {
41 gotGameIdToDownload(gamesList[index].id);
42}
43
44GameInfo CavokeClientModel::getGameByIndex(int index) {
45 return gamesList[index];
46}
47
48void CavokeClientModel::gotGameIdToDownload(const QString &gameId) {
49 qDebug() << gameId;
50 emit downloadGame(gameId);
51}