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

 // required so the ChallengeView is known, but only during development
 import "."

 /*

  */
 TestBase {

   // the challengeView is set as the challengeDelegate for FelgoGameNetwork
   property alias challengeView: challengeView

   property alias friendsView: challengeView.friendsView

   property variant gameNetworkItem: gameNetwork

   property int lastChallengeId: -1

   ChallengeView {
     id: challengeView
     anchors.fill: parent

     // this is only set to true in this test, because we want to display the back button and background of ListView
     // a real game should add own back buttons, and thus this is set to false by default
     debugMode: true
   }

   // NOTE: this is just for quick testing - a real game initiates a call to submit the scores in a different place probably
   // but use the lastChallengeId as shown with the Connections element below!

   Column {
     visible: challengeView.state === "challengeOverview"

     anchors.bottom: parent.bottom

     Text {
       color: "white"
       text: "lastChallengeId: " + lastChallengeId
     }

     SimpleButton {
       text: "Submit score to lastChallengeId"
       onClicked: {

         // if no challengeId was created, set it to empty
         if(lastChallengeId === -1)
           return;

         // 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

         // instead, use the lastChallengeId property, which is also used when a challenge is accepted, not only created
         //gameNetwork.reportScore(Math.round(scoreSlider.value), "challenges", [gameNetworkItem.lastCreatedChallengeData.id])
         gameNetwork.reportScore(Math.round(scoreSlider.value), "challenges", [lastChallengeId])
       }
     }
     Row {
       spacing: 20

       GameSlider {
         id: scoreSlider
         minimumValue: -1000
         maximumValue: 1000
         value: 10
         width: 200
         height: 20
       }

       Text {
         text: Math.round(scoreSlider.value)
         color: "white"
       }
     }

     // this is only for debugging, to see all challenges with all states in the log output
     SimpleButton {
       text: "Receive all user challenges (see log output)"
       onClicked: {
         gameNetwork.api.getUserChallenges()
       }
     }

   }

   // is created in ChallengeView!
 //  FriendsView {
 //    id: friendsView
 //    anchors.fill: parent
 //  }

   Connections {
     target: gameNetworkItem

     onChallengeCreated: {

       console.debug("onChallengeCreated:", JSON.stringify(challengeData))

       // use the gameNetwork.lastCreatedChallengeData.id to submit the score to it
       nativeUtils.displayMessageBox("Challenge created", "You can now submit a score to the challenge")
       challengeView.state = "challengeOverview"

       lastChallengeId = challengeData.id
     }

     onChallengeWithScoreSubmitted: {
       console.debug("onChallengeWithScoreSubmitted:", JSON.stringify(challengeData))
       nativeUtils.displayMessageBox("Challenge score submitted", "Waiting for the friend to complete the challenge")

       lastChallengeId = -1
     }

     onChallengeAccepted: {
       console.debug("onChallengeAccepted:", JSON.stringify(challengeData))
       nativeUtils.displayMessageBox("Challenge was accepted", "Submit a score to the challenge to complete it")

       lastChallengeId = challengeData.id
     }

     onChallengeCompleted: {
       console.debug("onChallengeCompleted:", JSON.stringify(challengeData))
       nativeUtils.displayMessageBox("Challenge completed", "Winner is: " + challengeData.winner.name + ", with score ")

       lastChallengeId = -1
     }

     onLastQueriedUserChallengeListChanged: {

       console.debug("onLastQueriedUserChallengeListChanged to:", JSON.stringify(gameNetwork.lastQueriedUserChallengeList) )
       //challengeList.visible = true
     }
Qt_Technology_Partner_RGB_475 Qt_Service_Partner_RGB_475_padded