GameNetworkExample
import Felgo 3.0
import QtQuick 2.0
import "."
TestBase {
id: keyValueTest
property string applicationStorageKey: "key1"
property string applicationStorageValueStringified: "unset"
property string userStorageKey: "myHighscoreKey"
property string userStorageValueStringified: "unset"
property string textInputMode
WebStorage {
id: userStorage
Component.onCompleted: {
var readValue = userStorage.getValue(userStorageKey)
console.debug("readValue in onCompleted:", readValue)
setUserStorageValueValue(readValue)
userStorage.setValue("keyBeforeInitiallyInServerSync", 3)
if(typeof readValue === "number")
slider.value = readValue
}
onInitiallyInServerSyncChanged: {
if(initiallyInServerSync) {
var readValue = userStorage.getValue(userStorageKey)
console.debug("KeyValueTest: initiallyInServerSync, read user value:", readValue)
setUserStorageValueValue(readValue)
if(typeof readValue === "number") {
slider.value = userStorage.getValue(userStorageKey)
}
}
}
onInServerSyncChanged: {
console.debug("KeyValueTest: WebStorage.onInServerSyncChanged to", inServerSync)
}
onInitiallyInServerSyncOrErrorChanged: {
console.debug("onInitiallyInServerSync changed to:", initiallyInServerSyncOrError)
}
onWriteValueFinished: console.debug("KeyValueTest: writeValueFinished:", JSON.stringify(data))
onReadValueFinished: console.debug("KeyValueTest: readValueFinished:", JSON.stringify(data))
onClearValueFinished: console.debug("KeyValueTest: clearValueFinished:", JSON.stringify(data))
onWriteConflict: {
console.debug("KeyValueTest: writeConflict:", JSON.stringify(data))
var readValue = userStorage.getValue(userStorageKey)
console.debug("UserStorage readValue stringified after write conflict:", JSON.stringify(readValue))
setUserStorageValueValue(readValue)
}
}
Item {
anchors.fill: parent
clip: true
SimpleButton {
y: 30
anchors.right: parent.right
color: userStorage.inServerSync ? "green" : "red"
text: userStorage.inServerSync ? "sync" : "busy"
}
Flickable {
anchors.fill: parent
contentWidth: column.width
contentHeight: column.height
flickableDirection: Flickable.VerticalFlick
Flow {
id: column
spacing: 5
width: scene.width
GameSlider {
id: slider
minimumValue: 0
maximumValue: 100
width: 200
height: 20
}
Text {
color: "white"
text: " --- UserStorage, key: " + userStorageKey + " --- "
}
SimpleButton {
text: "Set key"
onClicked: {
textInputMode = "userStorage"
nativeUtils.displayTextInput("UserStorageKey", "", userStorageKey)
}
}
SimpleButton {
text: "Set value"
onClicked: {
textInputMode = "userStorageValue"
nativeUtils.displayTextInput("UserStorageValue", "", userStorageValueStringified)
}
}
SimpleButton {
text: "Get value"
onClicked: {
var readValue = userStorage.getValue(userStorageKey)
console.debug("UserStorage readValue stringified:", JSON.stringify(readValue))
setUserStorageValueValue(readValue)
}
}
SimpleButton {
text: "Get serverValue"
onClicked: {
userStorage.getServerValue(userStorageKey, keyValueTest.myCallback)
}
}
SimpleButton {
text: "ServerSynch"
onClicked: {
userStorage.synchWithServer()
}
}
SimpleButton {
text: "Clear value"
color: "red"
onClicked: {
userStorage.clearValue(userStorageKey)
}
}
SimpleButton {
text: "Clear userStorage"
color: "red"
onClicked: {
userStorage.clearAll()
}
}
SimpleButton {
text: "Clear local userStorage"
color: "red"
onClicked: {
userStorage.clearAllLocally()
}
}
SimpleButton {
text: "Set value to " + Math.round(slider.value)
onClicked: {
userStorage.setValue(userStorageKey, Math.round(slider.value), keyValueTest.myCallback)
setUserStorageValueValue(Math.round(slider.value))
}
}
SimpleButton {
text: "Increase value by 1 to " + Math.round(slider.value + 1)
onClicked: {
slider.value++
userStorage.setValue(userStorageKey, Math.round(slider.value), keyValueTest.myCallback)
setUserStorageValueValue(Math.round(slider.value))
}
}
SimpleButton {
text: "Set complex value"
onClicked: {
userStorage.setValue(userStorageKey, {"key1":"value1","key2":2,"array1":[5,6]}, keyValueTest.myCallback)
}
}
Text {
color: "white"
text: "userStorageValue: " + userStorageValueStringified
}
}
}
}
Connections {
target: nativeUtils
onTextInputFinished: {
if(!accepted) {
textInputMode = ""
return;
}
if(textInputMode === "userStorage") {
userStorageKey = enteredText
} else if(textInputMode === "userStorageValue") {
setUserStorageValueValue(enteredText)
var parsedInput
try {
parsedInput = JSON.parse(userStorageValueStringified)
} catch(e) {
console.debug("caught parse exception, happens e.g. when a string was entered:", e)
parsedInput = userStorageValueStringified
}
userStorage.setValue(userStorageKey, parsedInput, keyValueTest.myCallback)
}
textInputMode = ""
}
}
function myCallback(data) {
console.debug("myCallback is called:", JSON.stringify(data))
if(data["mergedData"]) {
setUserStorageValueValue(data.mergedData)
}
}
function setUserStorageValueValue(value) {
if(value === undefined) {
userStorageValueStringified = "unset"
} else if(typeof value === "string") {
userStorageValueStringified = value
} else {
userStorageValueStringified = JSON.stringify(value)
}