-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsolitaire-games.qml
268 lines (226 loc) · 8.01 KB
/
solitaire-games.qml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
import QtQuick 2.0
import Ubuntu.Components 0.1
import U1db 1.0 as U1db
import QtQuick.XmlListModel 2.0
import org.nemomobile.folderlistmodel 1.0
import "layout"
/*!
\brief MainView with a Label and Button elements.
*/
MainView {
// objectName for functional testing purposes (autopilot-qt5)
objectName: "mainView"
// Note! applicationName needs to match the .desktop filename
applicationName: "solitaire-games"
/*
This property enables the application to change orientation
when the device is rotated. The default is false.
*/
automaticOrientation: true
headerColor: "#092200"
backgroundColor: "#144E00"
footerColor: "#1D7300"
width: units.gu(120)
height: units.gu(100)
id: mainView
property int selectedGameIndex:-1
property string selectedGameTitle: selectedGameIndex<0?"":gamesRepeater.itemAt(selectedGameIndex).title
property string selectedGameDbName: selectedGameIndex<0?"":gamesModel.get(selectedGameIndex).dbName
property bool small: width<units.gu(85)
XmlListModel {
id: gamesModel
source: "data/games.xml"
query: "/games/game"
XmlRole { name: "title"; query: "title/string()"}
XmlRole { name: "path"; query: "path/string()"}
XmlRole { name: "dbName"; query: "db-name/string()"}
XmlRole { name: "rules"; query: "rules/string()"}
XmlRole { name: "info"; query: "info/string()"}
}
Component.onCompleted: {
print("Select language: "+Qt.locale().name.substring(0,2))
gamesModelTranslation.source = "data/games_"+Qt.locale().name.substring(0,2)+".xml"
i18n.bindtextdomain(i18n.domain,"locale")
i18n.domain = i18n.domain
print("Set colors")
Theme.palette.normal.foreground = headerColor
Theme.palette.normal.background = backgroundColor
}
XmlListModel {
id: gamesModelTranslation
query: "/games/game"
XmlRole { name: "title"; query: "title/string()"}
XmlRole { name: "path"; query: "path/string()"}
XmlRole { name: "dbName"; query: "db-name/string()"}
XmlRole { name: "rules"; query: "rules/string()"}
XmlRole { name: "info"; query: "info/string()"}
function getIdByDbName(dbName) {
for(var index=0; index<gamesModelTranslation.count; index++) {
if(gamesModelTranslation.get(index)["dbName"]===dbName)
return index
}
return -1
}
}
Tabs {
id: tabs
anchors.fill: parent
Tab {
anchors.fill: parent
title: i18n.tr("Games")
page: GamesPage {
id: gamesPage
}
}
Tab {
anchors.fill: parent
title: selectedGameIndex===-1?i18n.tr("Game"):selectedGameTitle
page: GamePage {
id: gamePage
}
}
Tab {
anchors.fill: parent
title: i18n.tr("Stats")
page: StatsPage {
id: statsPage
}
}
}
Repeater {
id: gamesRepeater
model: gamesModel
delegate: Item {
property int languageId: gamesModelTranslation.getIdByDbName(dbName)
property string title: languageId!==-1 && gamesModelTranslation.get(languageId)["title"]?gamesModelTranslation.get(languageId)["title"]:gamesModel.get(index)["title"]
property string rules: languageId!==-1 && gamesModelTranslation.get(languageId)["rules"]?gamesModelTranslation.get(languageId)["rules"]:gamesModel.get(index)["rules"]
property string info: languageId!==-1 && gamesModelTranslation.get(languageId)["info"]?gamesModelTranslation.get(languageId)["info"]:gamesModel.get(index)["info"]
Component.onCompleted: {
print("Try "+dbName)
initDbForGame(dbName)
}
}
}
function newGame() {
tabs.selectedTabIndex=0
if(gamePage.loader.item)
gamePage.loader.item.preEnd(false)
gamePage.setSource("")
selectedGameIndex = -1
}
function startGame(index) {
var previousGameIndex = selectedGameIndex
selectedGameIndex = index
print("startGame: "+selectedGameDbName)
if(gamePage.loader.item) {
if(index===previousGameIndex) {
tabs.selectedTabIndex = 1
return
}
else
gamePage.loader.item.preEnd(true)
}
var savedGame = getSaveState(selectedGameDbName)
var savedGameIndex = getSaveStateIndex(selectedGameDbName)
var savedSeed = getSaveStateSeed(selectedGameDbName)
gamePage.setSource(Qt.resolvedUrl("games/"+gamesModel.get(selectedGameIndex)["path"]), {"savedGame":savedGame, "savedGameIndex": savedGameIndex, "savedSeed": savedSeed})
tabs.selectedTabIndex = 1
}
function restartGame() {
gamePage.loader.item.init([], 0, gamePage.loader.item.gameSeed)
tabs.selectedTabIndex = 1
}
function redealGame() {
gamePage.loader.item.init([], 0, -1)
tabs.selectedTabIndex = 1
}
function setStats(dbName, won) {
initDbForGame(dbName)
var tempContents = {};
tempContents = statsDoc.contents
if(won) {
tempContents[dbName]["won"]++
}
else {
tempContents[dbName]["lost"]++
}
statsDoc.contents = tempContents
}
function getStats(dbName) {
return statsDoc.contents[dbName]
}
function removeSaveState(dbName) {
print("removeSaveState")
setSaveState(dbName, [], 0, -1)
}
function setSaveState(dbName, json, index, savedSeed) {
print("setSaveState:"+dbName+" "+index+" "+savedSeed)
var tmpContents = {};
tmpContents = savesDoc.contents
tmpContents[dbName]["saveState"] = json
tmpContents[dbName]["savedHistoryIndex"] = index
tmpContents[dbName]["savedSeed"] = savedSeed
savesDoc.contents = tmpContents
}
function getSaveState(dbName) {
return savesDoc.contents[dbName]["saveState"]
}
function getSaveStateIndex(dbName) {
print("getSaveStateIndex::"+savesDoc.contents[dbName]["savedHistoryIndex"])
return savesDoc.contents[dbName]["savedHistoryIndex"]
}
function getSaveStateSeed(dbName) {
print("getSaveStateSeed::"+savesDoc.contents[dbName]["savedSeed"])
return savesDoc.contents[dbName]["savedSeed"]
}
function initDbForGame(dbName) {
print("initDbForGame: "+dbName)
var tempStats = {};
if(statsDoc.contents) {
tempStats = statsDoc.contents
}
if(!tempStats[dbName]) {
tempStats[dbName] = {}
tempStats[dbName]["won"] = 0
tempStats[dbName]["lost"] = 0
}
statsDoc.contents = tempStats
var tempSaves = {}
if(savesDoc.contents) {
tempSaves = savesDoc.contents
}
if(!tempSaves[dbName]) {
tempSaves[dbName] = {}
tempSaves[dbName]["saveState"] = []
tempSaves[dbName]["savedHistoryIndex"] = 0
tempSaves[dbName]["savedSeed"] = -1
}
savesDoc.contents = tempSaves
}
U1db.Database {
id: mainDb
path: folderModel.getLocalFolder()+"/solitaireDb"
}
U1db.Document {
id: statsDoc
database: mainDb
docId: 'stats'
create: true
defaults: {}
}
U1db.Document {
id: savesDoc
database: mainDb
docId: 'saveStates'
create: true
defaults: {}
}
FolderListModel {
id: folderModel
function getLocalFolder() {
// TODO: first check before making
folderModel.mkdir(folderModel.homePath()+"/.local/share/com.ubuntu.developer.labsin.solitaire-games")
return folderModel.homePath()+"/.local/share/com.ubuntu.developer.labsin.solitaire-games"
}
}
}