Learn what Felgo offers to help your business succeed. Start your free evaluation today! Felgo for Your Business

GameNetworkExample

 import Felgo 3.0
 import QtQuick 2.0

 /*
   This test is only for testing the raw API, and should not be used as a reference implementation!
   Instead, use ChallengeTest.qml!!!
  */
 TestBase {

   // can either be deviceId or fbId
   property string textInputState

   // must be an int!
   property int toChallengeUserId

   property int lastCreatedChallengeId: -1

   // this is not needed here, because the list gets set to visible when new data is available
   //property alias challengeView: challengeList

  Flow {
    anchors.fill: parent
    spacing: 5

   Text {
     color: "white"
     text: "To challenge userId:" + toChallengeUserId + "\nLast created challengeId: " + lastCreatedChallengeId
   }

   SimpleButton {
     text: "Create Open Challenge"
     onClicked: {
       // this challenge will change to state "public" once a score was submitted to it
       gameNetwork.api.createChallenge(/*createChallengeCallback*/)
     }
   }

   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: {
       // submit it to the current leaderboard (or rather set it to the "challenges" leaderboard to avoid modifying the leaderboard scores!?)
       // the challengeIds is an array property, so only send it to one

       gameNetwork.reportScore(Math.round(scoreSlider.value), undefined, [lastCreatedChallengeId])
     }
   }

   /* this is useless, as the end user will never need to show a list of created challenges
     to select the challenge to submit to, use the "Show all User challenges" button below
   SimpleButton {
     text: "Submit score to created challenge"
     onClicked: {
       // TODO: show the list of created challenges

       //gameNetwork.api.getUserChallenges("as_challenger", "created")

       // submit it to the current leaderboard
       // the challengeIds is an array property, so only send it to one
       //gameNetwork.reportScore(Math.round(scoreSlider.value), undefined, [toChallengeUserId])
     }
   }
   */

   SimpleButton {
     text: "List pending direct challenges from other users"
     onClicked: {

       // also display the accepted challenges? another call would be needed!
       gameNetwork.api.getUserChallenges("as_challenged", "pending")
     }
   }

   SimpleButton {
     text: "List public challenges"
     onClicked: {

       // only show the public challenges not initiated by the logged in user
       gameNetwork.api.getChallenges("public", gameNetwork.user.userId)

       // is as_challenged needed here for state?
       // otherwise we would also show the ones that were initiated by the player
       // NOTE: the following call does not work, because only the public challenges I initiated are displayed, not the one from others!
       //gameNetwork.api.getChallenges("as_challenged", "public")

     }
   }

   /* accepting is done by clicking on a challenge in the list
   SimpleButton {
     text: "Accept challenge"
     onClicked: {
     }
   }*/

   SimpleButton {
     text: "List Challenge History"
     onClicked: {

       // state must not be set here, because also the ones should be shown that were initiated by the player
       gameNetwork.api.getUserChallenges(undefined, "completed")
     }
   }

   SimpleButton {
     text: "List All User Challenges"
     onClicked: {

       gameNetwork.api.getUserChallenges()
     }
   }

  }//Column

   VPListView {
     id: challengeList
     width: 400
     height: parent.height
     clip: true
     anchors.horizontalCenter: parent.horizontalCenter

     // start invisible
     visible: false

     model: gameNetwork.lastQueriedChallengeList

     delegate: SimpleButton {
       width: challengeList.width/2
       text: textForChallengeDelegate(modelData)
       font.pixelSize: 10

       // TODO: how to anchor in the challengeList?
       //anchors.horizontalCenter: parent.horizontalCenter

       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

       // this doesnt work - the ? syntax in a string concatenation doesnt work?
 //      "State:" + modelData.state + "\nChallenger: " +
 //                modelData["challenger"] ? modelData["challenger"].name : "" +
 //                "(" + modelData["challenger_score"] ? modelData["challenger_score"].value : "-"
     }

     function handleClickedChallenge(modelData) {

       console.debug("handleClickedChallenge called for challengeData:", JSON.stringify(modelData))

       // only do not set it if in state completed
       if(modelData.state === "completed")
         return

       //if(modelData.state === "created" || modelData.state === "accepted") {
         // set the id of this challenge, so we can submit a score to it or accept it
         lastCreatedChallengeId = modelData.id
       //}

       if(modelData.state === "pending" || modelData.state === "public") {

         // this check is also performed on the server side
         // for testing if the server-side check works, do not add this check on the client side here
         // in the final API, only the challenges which are not sent by the user are listed anyway
         if(modelData.state === "public" && modelData.challenger.id === gameNetwork.user.userId) {
           // do not set the textInputState here, as just the messageBox is shown with a single cancel button
           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 {
       //Note: Due to an implementation detail, items placed inside a Flickable cannot anchor to it by id. Use parent instead.
       // so anchors.fill: challengeList does not work!
       anchors.fill: parent
       z: -1 // otherwise it would be on top of the delegate items
       color: "lightgrey"
     }

     SimpleButton {
       text: "Close"
       onClicked: challengeList.visible = false
       anchors.right: challengeList.right
       z: 1 // otherwise it would be on top of the delegate items
     }
   }// VPListView

   Connections {
     target: nativeUtils
     onTextInputFinished: {

       if(textInputState === "toChallengeUserId" && accepted) {

         // the userId must be an integer
         toChallengeUserId = enteredText

         // this challenge will change to state "pending" once a score was submitted to it
         gameNetwork.api.createChallenge(toChallengeUserId /*createChallengeCallback*/)
       }

       textInputState = ""
     }

     onMessageBoxFinished: {
       if(accepted) {
         gameNetwork.api.acceptChallenge(lastCreatedChallengeId)
       }

       textInputState = ""

       challengeList.visible = false
     }
   }

   // callbacks are not supported by the api yet - use a signal in the api at the moment!
   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
     }
Qt_Technology_Partner_RGB_475 Qt_Service_Partner_RGB_475_padded