P.s.: The added data should be and will be frequently searched/filtered and optionally will be returned with the basic responses.
So after many trial and errors there is no solution to the problem without very ugly hacks.
Finally what I've settled with is saving the extra data and create a new CreateTeamRequest using the id from the extra data entry. To cancel the request so it won't try to create the team twice, I have set a scriptError with the original response and copied that into the scriptData in the CreateTeamResponse.
// ==================================================================================================== // // Cloud Code for CreateTeamRequest, write your code here to customize the GameSparks platform. // // For details of the GameSparks Cloud Code API see https://docs.gamesparks.com/ // // ==================================================================================================== var data = Spark.getData(); // TODO: eval scriptData var scriptData = data.scriptData; // save data var extra_data = Spark.runtimeCollection("teamExtraData"); extra_data.save(scriptData); // give id to team and that's it data.teamId = scriptData._id["$oid"]; // another request as we need to know if the team can be created var response = Spark.sendRequest({ "@class": ".CreateTeamRequest", "teamId": data.teamId, "teamName": data.teamName, "teamType": data.teamType, "scriptData": {"ez":"valami"} }) if( response.hasOwnProperty("error") ) { // remove extra data extra_data = Spark.runtimeCollection("teamExtraData"); extra_data.findAndRemove({"_id": {"$oid": data.teamId }}); // couldn't create team for (var property in response.error) { if (response.error.hasOwnProperty(property)) { Spark.setScriptError(property, response.error[property]); } } } else { Spark.setScriptError("skip_request", response); } Spark.exit();
// ==================================================================================================== // // Cloud Code for CreateTeamResponse, write your code here to customize the GameSparks platform. // // For details of the GameSparks Cloud Code API see https://docs.gamesparks.com/ // // ==================================================================================================== var data = Spark.getData(); // save original response var orig_response = Spark.getScriptError("skip_request"); // remove fake error with original response Spark.removeScriptError("skip_request"); if(Object.keys(data.error).length === 0 && data.error.constructor === Object) { Spark.removeAllScriptErrors(); } if( orig_response !== null ) { Spark.setScriptData("response", orig_response); } Spark.exit();
This is working but very ugly and error prone. Also it renders your framework absolutely unnecessary 'cause this could be a custom request.
The preferred way would be something like:
// // request script var scriptData = Spark.getData().scriptData; var extra_data = Spark.runtimeCollection("teamExtraData"); extra_data.save(scriptData); Spark.setData("teamId", scriptData._id["$oid"]); // // GS creates team with the set id and gives normal response // // // response script with teamId and scriptData still set if( Spark.hasScriptErrors() ) { var data = Spark.getData(); var extra_data = Spark.runtimeCollection("teamExtraData"); extra_data.findAndRemove({"_id": {"$oid": data.teamId }}); }
Much cleaner and response can be processed normally by GS API and our code as well.
Hi RwB,
When you send a CreateTeamRequest you can leave the teamID field out completely and a mongo oid will be used. This will make sure the id is unique. This id will then be in the CreateTeamResponse which you can then use as the id of the document you are going to insert into your custom collection. It's always best to use a playerId or any unique id that ties the extra data back to the object that it was created from. You can pass extra data into the request via scriptData and pass it to the response at which point you can insert it into the custom collection using the new teams ID. For players you can set scriptData or privateData on the player object itself. If you plan to store lot of data on the player a runtime collection may suit your needs better. Challenges and Matches can have data set to them also. Let me know if you have any further questions.
Regards,
Liam
bump - I'd like to be able to set a public/private flag on a team when it's created and can't come up with a solution other than what RwB World has suggested above.
RwB World
1 person has this question