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

 TestBase {

   property int currentLevelId: gameNetwork.currentLevelId
   property string currentLevelName

   // can either be "newLevelName", "changeLevelName" or ""
   property string textInputState

   // can either be "created_at", "average_quality", "average_difficulty", "times_favored" or "times_played"
   // is used for load levels
   property string ordering: "created_at"

   Column {
     width: parent.width
     spacing: 3

     Text {
       text: "currentLevelName: " + currentLevelName + ", currentLevelId:" + currentLevelId
       color: "white"
     }

     Flow {
       width: parent.width
       spacing: 3

       SimpleButton {
         text: "Create a new level"
         onClicked: {
           textInputState = "newLevelName"
           nativeUtils.displayTextInput("Enter New Level Name", "", "", currentLevelName )
         }
       }

       SimpleButton {
         text: "Change levelName"
         onClicked: {
           textInputState = "changeLevelName"
           nativeUtils.displayTextInput("Enter New Level Name", "", "", currentLevelName )
         }
       }

       SimpleButton {
         text: "Load Levels"
         onClicked: {
           gameNetwork.getLevels( {order: ordering} )
         }
       }

       SimpleButton {
         text: "Load User Levels"
         onClicked: {
           gameNetwork.getUserLevels( {order: ordering} )
         }
       }

       SimpleButton {
         text: "Show All Levels"
         onClicked: {
           levelDelegate.levels = gameNetwork.allLevels
           levelDelegate.visible = true
         }
       }

       SimpleButton {
         text: "Show User Levels"
         onClicked: {
           levelDelegate.levels = gameNetwork.userLevels.levels
           levelDelegate.visible = true
         }
       }

       SimpleButton {
         font.pixelSize: 12
         text: "Order: " + ordering
         onClicked: {
           // toggle between the options
           // can either be "created_at", "average_quality", "average_difficulty", "times_favored" or "times_played"
           if(ordering === "created_at")
             ordering = "average_quality"
           else if(ordering === "average_quality")
             ordering = "average_difficulty"
           else if(ordering === "average_difficulty")
             ordering = "times_favored"
           else if(ordering === "times_favored")
             ordering = "times_played"
           else if(ordering === "times_played")
             ordering = "times_downloaded"
           else if(ordering === "times_downloaded")
             ordering = "created_at"
         }
       }

       SimpleButton {
         text: "Load Level"
         onClicked: {
           if(currentLevelId === 0) {
             nativeUtils.displayMessageBox("No Level Selected", "No level was selected, please first load levels and then select one from the 'Show All Levels' menu")
             return
           }

           gameNetwork.getLevel(currentLevelId)
         }
       }
       SimpleButton {
         text: "Play"
         onClicked: {
           gameNetwork.playLevel(currentLevelId)
         }
       }

       SimpleButton {
         text: "Download"
         onClicked: {
           gameNetwork.downloadLevel(currentLevelId)
         }
       }

       SimpleButton {
         text: "Delete"
         color: "red"
         onClicked: {
           gameNetwork.deleteLevel(currentLevelId)
         }
       }
     }// Flow

     Flow {
       spacing: 5
       width: parent.width

       Row {
         spacing: 10
         SimpleButton {
           text: gameNetwork.currentLevelDataVerbose && gameNetwork.currentLevelDataVerbose["rating"] && gameNetwork.currentLevelDataVerbose["rating"]["favored"] ? "Unfavor level" : "Favor level"
           //text: "Favor level"

           onClicked: {

             if(text === "Favor level")
               gameNetwork.rateLevel({levelId: currentLevelId, favored: true} )
             else
               gameNetwork.rateLevel({levelId: currentLevelId, favored: false} )
           }
         }
       }

       Row {
         spacing: 3

         Text {
           text: "Rating:"
           color: "white"
         }

         SimpleButton {
           text: "*"
           color: gameNetwork.currentLevelDataVerbose && gameNetwork.currentLevelDataVerbose["rating"] && gameNetwork.currentLevelDataVerbose["rating"]["quality"] === 1 ? "green" : "darkblue"

           onClicked: {

             gameNetwork.rateLevel({levelId: currentLevelId, quality: 1} )
           }
         }
         SimpleButton {
           text: "**"
           color: gameNetwork.currentLevelDataVerbose && gameNetwork.currentLevelDataVerbose["rating"] && gameNetwork.currentLevelDataVerbose["rating"]["quality"] === 2 ? "green" : "darkblue"

           onClicked: {

             gameNetwork.rateLevel({levelId: currentLevelId, quality: 2} )
           }
         }
         SimpleButton {
           text: "***"
           color: gameNetwork.currentLevelDataVerbose && gameNetwork.currentLevelDataVerbose["rating"] && gameNetwork.currentLevelDataVerbose["rating"]["quality"] === 3 ? "green" : "darkblue"

           onClicked: {

             gameNetwork.rateLevel({levelId: currentLevelId, quality: 3} )
           }
         }
         SimpleButton {
           text: "****"
           color: gameNetwork.currentLevelDataVerbose && gameNetwork.currentLevelDataVerbose["rating"] && gameNetwork.currentLevelDataVerbose["rating"]["quality"] === 4 ? "green" : "darkblue"

           onClicked: {

             gameNetwork.rateLevel({levelId: currentLevelId, quality: 4} )
           }
         }
         SimpleButton {
           text: "*****"
           color: gameNetwork.currentLevelDataVerbose && gameNetwork.currentLevelDataVerbose["rating"] && gameNetwork.currentLevelDataVerbose["rating"]["quality"] === 5 ? "green" : "darkblue"

           onClicked: {

             gameNetwork.rateLevel({levelId: currentLevelId, quality: 5} )
           }
         }

       }

       Row {
         spacing: 3

         Text {
           text: "Difficulty:"
           color: "white"
         }

         SimpleButton {
           text: "*"
           color: gameNetwork.currentLevelDataVerbose && gameNetwork.currentLevelDataVerbose["rating"] && gameNetwork.currentLevelDataVerbose["rating"]["difficulty"] === 1 ? "green" : "darkblue"

           onClicked: {

             gameNetwork.rateLevel({levelId: currentLevelId, difficulty: 1} )
           }
         }
         SimpleButton {
           text: "**"
           color: gameNetwork.currentLevelDataVerbose && gameNetwork.currentLevelDataVerbose["rating"] && gameNetwork.currentLevelDataVerbose["rating"]["difficulty"] === 2 ? "green" : "darkblue"

           onClicked: {

             gameNetwork.rateLevel({levelId: currentLevelId, difficulty: 2} )
           }
         }
         SimpleButton {
           text: "***"
           color: gameNetwork.currentLevelDataVerbose && gameNetwork.currentLevelDataVerbose["rating"] && gameNetwork.currentLevelDataVerbose["rating"]["difficulty"] === 3 ? "green" : "darkblue"

           onClicked: {

             gameNetwork.rateLevel({levelId: currentLevelId, difficulty: 3} )
           }
         }
       }// Difficulty Row

     }//Rating Flow

     Text {
       id: levelDebugData
       width: parent.width*0.9
       color: "white"
       font.pixelSize: 8
       wrapMode: Text.Wrap

       onTextChanged: console.debug("levelDebugData text changed to", text)
     }

   }// Column

   LevelDelegate {
     id: levelDelegate

     // make invisible in the beginning, only gets visible if show is pressed
     visible: false

     onLevelClicked: {
       // the currentLevelId is updated at the response handling of getLevel; currentLevelName is updated in onLevelLoaded
       gameNetwork.getLevel(levelId)
       levelDelegate.visible = false
     }
   }

   Connections {
     target: nativeUtils
     onTextInputFinished: {

       if((textInputState === "newLevelName" || textInputState === "changeLevelName")  && accepted) {
         currentLevelName = enteredText

         var saveParams = {
           name: currentLevelName,
           // data must be a string; if we store custom JS objects, calling JSON.stringify() is required!
           data: "testData",
           description: "test description"
         }

         if(textInputState === "changeLevelName") {
           if(currentLevelId !== 0) {
             saveParams.levelId = currentLevelId
           } else {
             console.debug("ERROR: to change the level name, first load a level so a valid levelId is available")
           }
         }

         gameNetwork.saveLevel(saveParams)
       }

       textInputState = ""
     }
   }

   Connections {
     target: gameNetwork

     onLevelSaved: {
       console.debug("level saved")

       levelDebugData.text = JSON.stringify(gameNetwork.currentLevelDataVerbose)
     }

     onLevelLoaded: {
       console.debug("single level loaded")

       currentLevelName = gameNetwork.currentLevelName
       levelDebugData.text = JSON.stringify(gameNetwork.currentLevelDataVerbose)
     }
     onLevelDeleted: {
       console.debug("level deleted")

       currentLevelName = gameNetwork.currentLevelName
       levelDebugData.text = JSON.stringify(gameNetwork.currentLevelDataVerbose)
     }

     onLevelsLoaded: {
       console.debug("levels loaded")

       levelDebugData.text = JSON.stringify(gameNetwork.allLevels)
     }

     onUserLevelsLoaded: {
       console.debug("userLevels loaded")

       levelDebugData.text = JSON.stringify(gameNetwork.userLevels)
     }

Qt_Technology_Partner_RGB_475 Qt_Service_Partner_RGB_475_padded