It looks great!
Thank for your great work.
There is the definitions of Request API:
https://github.com/johnsoncodehk/gamesparks-request-api-typings
How would you use them with Cloud Code?
Have them as modules and require them in relevant scripts?
I write Cloud Code with CommonJS + typescript,
and build the CommonJS code to Cloud Code format.
https://gist.github.com/johnsoncodehk/517bbed85ddd08713c0023195464ad69
This is my build script, but not rigorous, just do some string replace.
>> So you use this with an external editor with TS
Yes. I use the external editor is VS Code.
>> build it (outputs a CS-parsed version)
Yes. In fact, to build twice, the first time TypeScript build to JavaScript(command: tsc), the second time JavaScript build to Cloud Code(command: node gscc).
>> then push to github
You can try, but I just upload with zip file.
>> and it reflects in GS as normal cloud code?
Yes.
>> With this, can I also use ES6 and it'll parse to CS?
No. Just can use ES5, this is my tsconfig settings:
"compilerOptions": {
"target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
"lib": ["es5", "es2015"], /* Specify library files to be included in the compilation: */
"outDir": "./dist/", /* Redirect output structure to the directory. */
"removeComments": true, /* Do not emit comments to output. */
"strict": true, /* Enable all strict type-checking options. */
"baseUrl": "./modules/" /* Base directory to resolve non-absolute module names. */
}
private createMatchRequest(skill : number) : SparkRequests.MatchmakingRequest { const request = new SparkRequests.MatchmakingRequest(); request.skill = skill; return request; }
It has no compilation errors but when I run test I get error :
SparkRequests is not defined.
Mislav
@Mislav Zlatar
1. Do you have to use this command to compile?
tsc; node gscc
2. Can you show your compiled js code?
Actually it compiles OK with that command but error happens when I run Mocha unit tests with npm run test
Hi Mislav,
Did you include the real javascript sdk to your Mocha configuration?
It's possible to run unit tests against a real GameSparks instance, but I would advise you to create a mock implementation.
Best regards
Yep I am successfully mocked whole GameSparks until this but Spark and SparkPlayerMock typings are defined as interfaces while this SparkRequests are namespaces..
My workaround for now is that I have created my own class like this:
// tslint:disable-next-line:no-namespace export namespace SparkRequests { export class MatchmakingRequest { public matchShortCode : string; public skill : number; // tslint:disable-next-line:no-empty public Send() : void {} // tslint:disable-next-line:no-empty public SendAs(playerId : string) : void {} } }
And when I run my build Grunt.js script before ts compilation I am commenting out line
import { SparkRequests } from "../spark-requests/MatchMakingRequest";
and after build I am uncommenting it again so I can unit test it.
It's ugly, I am searching for bettrer way.
Mislav
@Mislav Zlatar
This may seem like a problem with my definition file, when I confirm the problem, I will update to gamesparks-request-api-typings.
Thanks, please let me know when you finish to test it out,
Mislav
@Mislav Zlatar
It seems that this problem can not be resolved from the definition file (.d.ts files) because the definition file is valid only at compile time, and the definition file does not produce any real content in the runtime environment
Creating fake run-time files (.ts files) can pass the test, but the test results are not always predictable. I suggest if the code uses the GameSparks API, should uploaded to the cloud code to test
Jordan Ephron
I've generated a set of TypeScript type definitions for the Cloud Code API.
https://github.com/JEphron/gamesparks-typings
Kudos to the GameSparks team for having such consistent and parseable documentation.
1 person has this question