Skip to content

Commit

Permalink
feat(jsbattle): store current progress when registering
Browse files Browse the repository at this point in the history
  • Loading branch information
jamro committed Apr 16, 2020
1 parent fa1c5ac commit 594b548
Show file tree
Hide file tree
Showing 7 changed files with 185 additions and 16 deletions.
12 changes: 12 additions & 0 deletions packages/jsbattle-server/app/services/UserStore.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,18 @@ class UserStoreService extends Service {
displayName: displayName,
registered: true
});

let initCalls = [];
let initChallenges = ctx.params.challenges || [];
let initScripts = ctx.params.scripts || [];
for(let challenge of initChallenges) {
initCalls.push(ctx.call('challenges.updateUserChallange', challenge));
}
for(let script of initScripts) {
initCalls.push(ctx.call('scriptStore.createUserScript', script));
}
await Promise.all(initCalls);

return ctx.call('auth.whoami', {});
}

Expand Down
93 changes: 91 additions & 2 deletions packages/jsbattle-server/test/unit/UserStore.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ const ConfigBroker = require("../../app/lib/ConfigBroker.js");
const { ValidationError } = require("moleculer").Errors;
const { MoleculerClientError } = require("moleculer").Errors;

const updateUserChallange = jest.fn();
const createUserScript = jest.fn();

const createTestToken = (user) => ({
id: (user ? user.id : '') || "123456",
username: (user ? user.username : '') || "amy",
Expand All @@ -14,13 +17,25 @@ describe("Test 'UserStore' service", () => {
let config = { auth: { admins: [{provider: 'google', username: 'monica83' }] } };
let broker = new ConfigBroker({ logger: false }, config, false);
broker.createService({
name: 'auth',
actions: {
name: 'auth',
actions: {
whoami: () => ({

})
}
})
broker.createService({
name: 'challenges',
actions: {
updateUserChallange: updateUserChallange
}
})
broker.createService({
name: 'scriptStore',
actions: {
createUserScript: createUserScript
}
})
broker.loadService(__dirname + "../../../app/services/UserStore.service.js");

beforeAll(() => broker.start());
Expand Down Expand Up @@ -297,7 +312,81 @@ describe("Test 'UserStore' service", () => {
broker.call("userStore.register", { username: 'monic_888724' }, {meta: {user: createTestToken(user2)}})
).rejects.toThrow(ValidationError);

});

it('submit challenge data when registering', async () => {
updateUserChallange.mockReset();
let user = await broker.call("userStore.findOrCreate", {user: {
extUserId: 'google_306464422',
username: 'alfred9854',
provider: 'google',
displayName: "Alfred Allegro",
email: "[email protected]",
registered: false,
role: 'user'
}});
let result = await broker.call(
"userStore.register",
{
challenges: [
{
challengeId: "challenge-11123423",
completed: true,
code: "// code 12345"
},
{
challengeId: "challenge-222425",
completed: false,
code: "// code 2345452"
}
]
},
{meta: {user: createTestToken(user)}}
);

expect(updateUserChallange.mock.calls).toHaveLength(2);
expect(updateUserChallange.mock.calls[0][0].params).toHaveProperty('challengeId', "challenge-11123423");
expect(updateUserChallange.mock.calls[0][0].params).toHaveProperty('completed', true);
expect(updateUserChallange.mock.calls[0][0].params).toHaveProperty('code', "// code 12345");
expect(updateUserChallange.mock.calls[1][0].params).toHaveProperty('challengeId', "challenge-222425");
expect(updateUserChallange.mock.calls[1][0].params).toHaveProperty('completed', false);
expect(updateUserChallange.mock.calls[1][0].params).toHaveProperty('code', "// code 2345452");
});

it('submit scripts data when registering', async () => {
createUserScript.mockReset();
let user = await broker.call("userStore.findOrCreate", {user: {
extUserId: 'google_67345',
username: 'book2495',
provider: 'google',
displayName: "Book Mack",
email: "[email protected]",
registered: false,
role: 'user'
}});
let result = await broker.call(
"userStore.register",
{
scripts: [
{
scriptName: 'pink',
code: "// pink code"
},
{
scriptName: 'blue',
code: "// blue code"
}
]
},
{meta: {user: createTestToken(user)}}
);

expect(createUserScript.mock.calls).toHaveLength(2);
expect(createUserScript.mock.calls[0][0].params).toHaveProperty('scriptName', "pink");
expect(createUserScript.mock.calls[0][0].params).toHaveProperty('code', "// pink code");
expect(createUserScript.mock.calls[1][0].params).toHaveProperty('scriptName', "blue");
expect(createUserScript.mock.calls[1][0].params).toHaveProperty('code', "// blue code");
});

})
});
34 changes: 34 additions & 0 deletions packages/jsbattle-webpage/features/auth.feature
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,37 @@ Feature: Auth
And Click "guest" auth method
Then "Challenges" section is selected in the navigation bar
And challenge [1] are unlocked

@integration_only
Scenario: submit chalenges when registering
Given JsBattle open in the browser
And "Challenges" section open
And battle quality set to "0"
And battle speed set to "50"
When open challenge 1
And close challenge info
And type "<<jamro code>>" in AI Script editor
And battle is completed
And the challenge is won
And Click Sign in button
And Click "mock" auth method
And register form is shown
And type "mock-registred" in "#register-form #username" input
And type "Registered Mock User" in "#register-form #displayname" input
And click "#register-form button"
And "Challenges" section open
Then challenge [1, 2] are unlocked

@integration_only
Scenario: submit scripts when registering
Given JsBattle open in the browser
And "Sandbox" section open
When click create tank button
And Click Sign in button
And Click "mock" auth method
And register form is shown
And type "mock-registred" in "#register-form #username" input
And type "Registered Mock User" in "#register-form #displayname" input
And click "#register-form button"
And "Sandbox" section open
Then list of AI scripts contains 1 items
2 changes: 1 addition & 1 deletion packages/jsbattle-webpage/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"build:bundle": "webpack --config ./webpack.config.js --mode=production",
"test": "npm run test:dom && npm run test:browser",
"test:dom": "jest -b=3",
"test:browser": "cucumber-js -f ./features/support/SimpleFormatter.js --retry 3 --fail-fast --world-parameters '{\"mockserver\": true}'",
"test:browser": "cucumber-js --tags \"not @integration_only\" -f ./features/support/SimpleFormatter.js --retry 3 --fail-fast --world-parameters '{\"mockserver\": true}'",
"test:browser-smoke": "cucumber-js -t @smoke -f ./features/support/SimpleFormatter.js --retry 3 --fail-fast --world-parameters '{\"mockserver\": true}'",
"analyse": "webpack --profile --json --config ./webpack.config.js > bundle_stats.json && webpack-bundle-analyzer bundle_stats.json",
"watch": "npm-watch",
Expand Down
47 changes: 38 additions & 9 deletions packages/jsbattle-webpage/src/actions/coreAction.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import profileService from '../services/profileService.js';
import challengeService from "../services/challengeService.js";
import aiRepoService from "../services/aiRepoService.js";

import {
SET_SIM_QUALITY_REQUEST,
Expand Down Expand Up @@ -67,15 +69,42 @@ export const getUserProfile = () => {
};

export const registerProfile = (username, displayName) => {
return fetchFromApi("/api/user/initData", "PROFILE_REGISTER", {
method: 'PATCH',
body: JSON.stringify({
username,
displayName
}),
headers: {
'Content-Type': 'application/json'
},
return fetchFromApi("/api/user/initData", "PROFILE_REGISTER", async () => {

let challenges = await challengeService.getChallengeList(challengeService.getCompletedChallenges());
let scripts = await aiRepoService.getScriptNameList();

let i;
for(i=0; i < challenges.length; i++) {
let challengeDef = await challengeService.getChallengeDefinition(challenges[i].id);
challenges[i] = {
challengeId: challenges[i].id,
completed: challenges[i].completed,
code: challengeDef.code
};
}

for(i=0; i < scripts.length; i++) {
let scriptData = await aiRepoService.getScript(scripts[i].id);
scripts[i] = {
scriptName: scripts[i].scriptName,
code: scriptData.code
};
}


return {
method: 'PATCH',
body: JSON.stringify({
username,
displayName,
challenges,
scripts
}),
headers: {
'Content-Type': 'application/json'
}
};
});

};
9 changes: 7 additions & 2 deletions packages/jsbattle-webpage/src/lib/fetchFromApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,18 @@ function sequenceFetch(actionList) {
};
}


function fetchFromApi(url, type, opts, localFetch) {
return async (dispatch) => {
localFetch = localFetch || fetch;
dispatch({type: type + "_REQUEST"});
try {
let response = await localFetch(url, opts);
let opsData;
if(typeof opts == 'function') {
opsData = await opts();
} else {
opsData = opts;
}
let response = await localFetch(url, opsData);
if (!response.ok) {
let responseText = await response.text();
return dispatch({type: type + "_FAILURE", payload: new Error(`Error ${response.status}: ${responseText || response.statusText}`), error: true});
Expand Down
4 changes: 2 additions & 2 deletions packages/jsbattle/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
"test:copy-webpage": "cpx './node_modules/jsbattle-webpage/features/**/*' ./test/features-webpage && cpx './test/pm2_steps.js' './test/features-webpage/step_definitions'",
"test:copy-admin": "cpx './node_modules/jsbattle-admin/features/**/*' ./test/features-admin && cpx './test/pm2_steps.js' './test/features-admin/step_definitions'",
"test:run-all": "npm run test:run-webpage && npm run test:run-admin",
"test:run-webpage": "cucumber-js --fail-fast -t @integration --retry 3 -f ./test/features-webpage/support/SimpleFormatter.js --world-parameters '{\"port\": 8100}' ./test/features-webpage",
"test:run-admin": "cucumber-js --fail-fast -t @integration --retry 3 -f ./test/features-admin/support/SimpleFormatter.js --world-parameters '{\"port\": 8100}' ./test/features-admin",
"test:run-webpage": "cucumber-js --fail-fast -t \"@integration or @integration_only\" --retry 3 -f ./test/features-webpage/support/SimpleFormatter.js --world-parameters '{\"port\": 8100}' ./test/features-webpage",
"test:run-admin": "cucumber-js --fail-fast -t \"@integration or @integration_only\" --retry 3 -f ./test/features-admin/support/SimpleFormatter.js --world-parameters '{\"port\": 8100}' ./test/features-admin",
"test:prep": "npm run test:copy-webpage && npm run test:copy-admin"
},
"repository": {
Expand Down

0 comments on commit 594b548

Please sign in to comment.