GameNetworkExample
import Felgo 3.0
import QtQuick 2.0
TestBase {
property string textInputState
property int toChallengeUserId
property int lastCreatedChallengeId: -1
Flow {
anchors.fill: parent
spacing: 5
Text {
color: "white"
text: "To challenge userId:" + toChallengeUserId + "\nLast created challengeId: " + lastCreatedChallengeId
}
SimpleButton {
text: "Create Open Challenge"
onClicked: {
gameNetwork.api.createChallenge()
}
}
SimpleButton {
text: "Create Challenge against UserId"
onClicked: {
textInputState = "toChallengeUserId"
nativeUtils.displayTextInput("Enter UserId to challenge", "", "", toChallengeUserId)
}
}
Row {
spacing: 20
GameSlider {
id: scoreSlider
minimumValue: -1000
maximumValue: 1000
value: 10
width: 200
height: 20
}
Text {
text: Math.round(scoreSlider.value)
color: "white"
}
}
SimpleButton {
text: "Submit score to last created challenge"
onClicked: {
gameNetwork.reportScore(Math.round(scoreSlider.value), undefined, [lastCreatedChallengeId])
}
}
SimpleButton {
text: "List pending direct challenges from other users"
onClicked: {
gameNetwork.api.getUserChallenges("as_challenged", "pending")
}
}
SimpleButton {
text: "List public challenges"
onClicked: {
gameNetwork.api.getChallenges("public", gameNetwork.user.userId)
}
}
SimpleButton {
text: "List Challenge History"
onClicked: {
gameNetwork.api.getUserChallenges(undefined, "completed")
}
}
SimpleButton {
text: "List All User Challenges"
onClicked: {
gameNetwork.api.getUserChallenges()
}
}
}
VPListView {
id: challengeList
width: 400
height: parent.height
clip: true
anchors.horizontalCenter: parent.horizontalCenter
visible: false
model: gameNetwork.lastQueriedChallengeList
delegate: SimpleButton {
width: challengeList.width/2
text: textForChallengeDelegate(modelData)
font.pixelSize: 10
onClicked: {
handleClickedChallenge(modelData)
}
}
function textForChallengeDelegate(modelData) {
var displayString = "State:" + modelData.state + "\nChallenger: "
if(modelData["challenger"])
displayString += modelData.challenger.name
else
displayString += "?name?"
displayString += "("
if(modelData["challenger_score"])
displayString += modelData["challenger_score"].value
else
displayString += "-"
displayString += ")"
displayString += "\nChallenged: "
if(modelData["challenged"])
displayString += modelData.challenged.name
else
displayString += "?name?"
displayString += "("
if(modelData["challenged_score"])
displayString += modelData["challenged_score"].value
else
displayString += "-"
displayString += ")"
return displayString
}
function handleClickedChallenge(modelData) {
console.debug("handleClickedChallenge called for challengeData:", JSON.stringify(modelData))
if(modelData.state === "completed")
return
lastCreatedChallengeId = modelData.id
if(modelData.state === "pending" || modelData.state === "public") {
if(modelData.state === "public" && modelData.challenger.id === gameNetwork.user.userId) {
nativeUtils.displayMessageBox("This is your challenge", "You cannot accept your own challenge")
} else {
textInputState = "acceptChallenge"
nativeUtils.displayMessageBox("Accept this challenge?", "You can submit a score this challenge after accepting it.", 2)
}
} else {
challengeList.visible = false
}
}
Rectangle {
anchors.fill: parent
z: -1
color: "lightgrey"
}
SimpleButton {
text: "Close"
onClicked: challengeList.visible = false
anchors.right: challengeList.right
z: 1
}
}
Connections {
target: nativeUtils
onTextInputFinished: {
if(textInputState === "toChallengeUserId" && accepted) {
toChallengeUserId = enteredText
gameNetwork.api.createChallenge(toChallengeUserId )
}
textInputState = ""
}
onMessageBoxFinished: {
if(accepted) {
gameNetwork.api.acceptChallenge(lastCreatedChallengeId)
}
textInputState = ""
challengeList.visible = false
}
}
function createChallengeCallback() {
console.debug("createChallengeCallback()")
}
Connections {
target: gameNetwork
onChallengeCreated: {
lastCreatedChallengeId = challengeData.id
console.debug("onChallengeCreated:", JSON.stringify(challengeData))
}
onChallengeWithScoreSubmitted: {
console.debug("onChallengeWithScoreSubmitted:", JSON.stringify(challengeData))
}
onChallengeAccepted: {
console.debug("onChallengeAccepted:", JSON.stringify(challengeData))
}
onChallengeCompleted: {
console.debug("onChallengeCompleted:", JSON.stringify(challengeData))
}
onLastQueriedChallengeListChanged: {
console.debug("QueriedChallengeListChanged changed to:", JSON.stringify(gameNetwork.lastQueriedChallengeList) )
challengeList.visible = true
}