GameNetworkExample
import QtQuick 2.0
import Felgo 3.0
GameWindow {
property int highscore: gameNetwork.userHighscoreForCurrentActiveLeaderboard
property bool portrait: false
title: "Username: " + gameNetwork.user.name + ", UserId: " + gameNetwork.user.userId + ", UDID: " + gameNetwork.user.deviceId
screenWidth: portrait ? 640 : 960
screenHeight: portrait ? 960 : 640
color: "black"
activeScene: scene
Scene {
id: scene
width: portrait ? 320 : 480
height: portrait ? 480 : 320
property int testHeight: scene.height - testRow.height - footer.height
state: "user"
Row {
id: testRow
spacing: 3
SimpleButton {
text: "User"
onClicked: scene.state = "user"
color: scene.state ==="user" ? "darkgreen" : "darkblue"
}
SimpleButton {
text: "Scores"
onClicked: scene.state = "scores"
color: scene.state ==="scores" ? "darkgreen" : "darkblue"
}
SimpleButton {
text: "Ach."
onClicked: scene.state = "achievements"
color: scene.state ==="achievements" ? "darkgreen" : "darkblue"
}
SimpleButton {
text: "Chall."
onClicked: scene.state = "challenges"
color: scene.state ==="challenges" ? "darkgreen" : "darkblue"
}
SimpleButton {
text: "WebStore"
onClicked: scene.state = "keyvalue"
color: scene.state ==="keyvalue" ? "darkgreen" : "darkblue"
}
SimpleButton {
text: "Lev"
onClicked: scene.state = "level"
color: scene.state ==="level" ? "darkgreen" : "darkblue"
}
SimpleButton {
text: "GNView"
onClicked: {
gameNetworkView.initialStateBeforeShow = scene.state
scene.state = "gamenetworkview"
}
color: scene.state ==="gamenetworkview" ? "darkgreen" : "darkblue"
}
}
UserTest {
id: userTest
visible: false
}
ScoreTest {
id: scoreTest
visible: false
}
AchievementTest {
id: achievementTest
visible: false
}
ChallengeTest {
id: challengeTest
visible: false
}
KeyValueTest {
id: keyValueTest
visible: false
}
LevelTest {
id: levelTest
visible: false
}
Column {
id: footer
anchors.bottom: parent.bottom
Text {
text: "lastRequest: " + gameNetwork.lastRequest
color: "white"
font.pixelSize: 6
width: scene.width
wrapMode: Text.WrapAnywhere
}
Text {
text: "lastResponse: " + gameNetwork.lastResponse
color: "white"
font.pixelSize: 6
width: scene.width
wrapMode: Text.WrapAnywhere
}
}
GameNetworkView {
id: gameNetworkView
visible: false
anchors.fill: scene.gameWindowAnchorItem
onShowCalled: {
if(scene.state !== "gamenetworkview") {
initialStateBeforeShow = scene.state
scene.state = "gamenetworkview"
}
}
onBackClicked: {
if(initialStateBeforeShow) {
scene.state = initialStateBeforeShow
} else {
scene.state = "user"
}
}
}
states: [
State {
name: "user"
PropertyChanges { target: userTest; visible: true}
},
State {
name: "scores"
PropertyChanges { target: scoreTest; visible: true}
},
State {
name: "achievements"
PropertyChanges { target: achievementTest; visible: true}
},
State {
name: "challenges"
PropertyChanges { target: challengeTest; visible: true}
},
State {
name: "keyvalue"
PropertyChanges { target: keyValueTest; visible: true}
},
State {
name: "level"
PropertyChanges { target: levelTest; visible: true}
},
State {
name: "gamenetworkview"
PropertyChanges { target: gameNetworkView; visible: true}
}
]
}
GameCenter {
id: gameCenter
onPluginLoaded: authenticateLocalPlayer()
}
FelgoGameNetwork {
id: gameNetwork
gameId: 108
secret: "vpgnTest-password"
facebookItem: facebook
gameCenterItem: gameCenter
achievements: [
Achievement {
key: "achievement01"
name: "Achievement 1"
iconSource: "../assets/img/achievement_default.png"
target: 10
points: 10
description: "description1"
data: "testData1"
},
Achievement {
key: "achievement02"
name: "This is a super-long achievement name. This is a super-long achievement name."
iconSource: "../assets/img/achievement_default.png"
target: 15
points: 15
description: "This is a super-long description. This is a super-long description. This is a super-long description. This is a super-long description. This is a super-long description."
},
Achievement {
key: "achievement03"
name: "This is also a very long achievement name."
iconSource: "../assets/img/achievement_default.png"
target: 30
points: 20
description: "Descr3"
},
Achievement {
key: "achievement04"
name: "Achievement 4"
description: "A very long description of this achievement, spanning over two lines."
iconSource: "../assets/img/achievement_default.png"
},
Achievement {
key: "achievement05"
name: "Achievement 5"
iconSource: "../assets/img/achievement_default.png"
}
]
onAuthenticatedChanged: {
console.debug("userAuthenticated changed to:", authenticated)
}
gameNetworkView: gameNetworkView
property int defaultPerPageCount: 30
onAchievementUnlocked: {
nativeUtils.displayMessageBox("Achievement unlocked with key " + key)
}
onAchievementUnlockedAfterServerApproval: {
nativeUtils.displayMessageBox("Achievement unlocked and approved from server with key " + key)
}
onNewHighscore: {
nativeUtils.displayMessageBox("New highscore reached for leaderboard " + leaderboard + ": " + Math.round(highscore))
}
onNewHighscoreAfterServerApproval: {
nativeUtils.displayMessageBox("New highscore reached and approved from server for leaderboard " + leaderboard + ": " + Math.round(highscore))
}
onUserScoreInSync: console.debug("score in leaderboard", leaderboard, "in sync now with score", score)
onError: {
console.log("A GameNetwork error occured:", JSON.stringify(errorData))
}
onConnectionErrorChanged: {
console.debug("GameNetworkTestMain: connectionError changed to", connectionError)
if(!connectionError) {
nativeUtils.displayMessageBox("Active Connection", "You just got access to internet after previously being disconnected. Your local progress gets now synced with the server.")
} else {
nativeUtils.displayMessageBox("Lost Internet Connection", "You just got disconnected from the Internet. Your local progress will be stored for later until you get internet access again.")
}
}
onFacebookSuccessfullyConnected: nativeUtils.displayMessageBox("Facebook Connected", "You just successfully connected to facebook, congrats!")
onFacebookSuccessfullyDisconnected: nativeUtils.displayMessageBox("Facebook Disconnected", "You just successfully disconnected from facebook...")
onFacebookConnectionError: nativeUtils.displayMessageBox("Facebook Error", JSON.stringify(error))
}
Facebook {
id: facebook
appId: "714361931969954"
readPermissions: ["email", "user_friends"]
publishPermissions: ["publish_actions"]
onSessionStateChanged: {
console.log("New Facebook Session state: ", sessionState, "(0=Null, 1=Opened, 2=Closed, 3=Failed, 4=Opening)");
console.debug("grantedPermissions:", JSON.stringify(facebook.grantedPermissions))
if (sessionState === Facebook.SessionOpened) {
console.debug("Session opened.");
console.debug("New access token:", facebook.accessToken)
}
else if (sessionState === Facebook.SessionClosed) {
console.debug("Session closed. This is expected behavior when the user disconnects from facebook.");
}
else if (sessionState === Facebook.SessionFailed) {
console.debug("ERROR: GameNetworkTestMain: Session failed.");
}
}
onGetGraphRequestFinished: {
console.debug("GameNetworkTestMain: onGetGraphRequestFinished: graphPath:" + graphPath + ", resultState:" + resultState + ", result:" + result, ", grantedPermissions: " + JSON.stringify(facebook.grantedPermissions));
if(resultState === Facebook.ResultOk) {
console.debug("GameNetworkTestMain: resultState is Ok")
}
}
onPostGraphRequestFinished: {
console.debug("QML onPostGraphRequestFinished: graphPath:" + graphPath + ", resultState:" + resultState + ", result:" + result + ", grantedPermissions: ", JSON.stringify(facebook.grantedPermissions));
}