Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix badges disappearing randomly #1671

Merged
merged 12 commits into from
Jun 16, 2022
234 changes: 116 additions & 118 deletions Pipfile.lock

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ describe('Avatar worker', () => {
.invoke('getState')
.its('consoleLog.logs')
.then(logs => {
const log = logs.entries().next().value[1]
const log = logs.entries().next().value[0]
expect(log).to.deep.equal('SyntaxError: invalid syntax\n')
})
})
Expand All @@ -65,7 +65,7 @@ return MoveAction(direction.NORTH)`
.invoke('getState')
.its('consoleLog.logs')
.then(logs => {
const log = logs.entries().next().value[1]
const log = logs.entries().next().value[0]
expect(log).to.deep.equal('IndentationError: expected an indented block after function definition on line 1\n')
})
})
Expand Down
23 changes: 6 additions & 17 deletions game_frontend/src/pyodide/badges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ export function checkIfBadgeEarned(
badges: string,
result: ComputedTurnResult,
userCode: string,
gameState: any,
playerAvatarId: number
gameState: any
): string {
const userPythonCode = userCode.replace(/\s*#.*/gm, '') // Remove all comment lines from the user's code
const badgesPerWorksheet = [
Expand All @@ -28,7 +27,7 @@ export function checkIfBadgeEarned(
{
id: 3,
worksheetID: 1,
trigger: badge3Trigger(result, userPythonCode, gameState, playerAvatarId),
trigger: badge3Trigger(result, userPythonCode),
},
]

Expand All @@ -40,7 +39,7 @@ export function checkIfBadgeEarned(
badge.trigger
) {
// Here is when a new badge is earned
// TODO on worksshet 2: This might have to order the badges, in case user does not do the worksheet in order
// TODO on worksheet 2: This might have to order the badges, in case user does not do the worksheet in order
badges += `${badgeWorksheetPair},`
}
}
Expand Down Expand Up @@ -71,21 +70,11 @@ function badge2Trigger(userPythonCode: string): boolean {
return substrings.every((substring) => userPythonCode.includes(substring))
}

function badge3Trigger(
result: any,
userPythonCode: string,
gameState: any,
playerAvatarId: number
): boolean {
function badge3Trigger(result: any, userPythonCode: string): boolean {
// Check the code contains certain keywords about moving to a cell
const substrings = ['world_state.can_move_to(', 'print(', 'if ']
const codeContainsKeywords = substrings.every((substring) => userPythonCode.includes(substring))

if (!codeContainsKeywords) return false

// Check action is move action
const isMoveAction = result.action.action_type === 'move'
if (!isMoveAction) return false

return true
// And check it returns a move action
return codeContainsKeywords && result.action.action_type === 'move';
}
5 changes: 2 additions & 3 deletions game_frontend/src/pyodide/pyodideRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@ export async function checkIfBadgeEarned(
badges: string,
result: ComputedTurnResult,
userCode: string,
gameState: any,
playerAvatarId: number
gameState: any
): Promise<string> {
return worker.checkIfBadgeEarned(badges, result, userCode, gameState, playerAvatarId)
return worker.checkIfBadgeEarned(badges, result, userCode, gameState)
}

export async function updateAvatarCode(
Expand Down
8 changes: 8 additions & 0 deletions game_frontend/src/redux/features/AvatarWorker/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ const pyodideInitialized = () => ({
type: types.PYODIDE_INITIALIZED,
})

const initialAvatarCodeUpdated = (computedTurnResult) => ({
type: types.INIT_AVATAR_CODE_UPDATED,
payload: {
...computedTurnResult,
},
})

const avatarCodeUpdated = (computedTurnResult) => ({
type: types.AVATAR_CODE_UPDATED,
payload: {
Expand Down Expand Up @@ -59,6 +66,7 @@ export default {
initializePyodide,
pyodideInitialized,
avatarCodeUpdated,
initialAvatarCodeUpdated,
avatarsNextActionComputed,
badgesEarned,
filterBadges,
Expand Down
13 changes: 6 additions & 7 deletions game_frontend/src/redux/features/AvatarWorker/epics.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const initialUpdateAvatarCodeEpic = (action$, state$, { pyodideRunner: { updateA
action$.pipe(ofType(editorTypes.GET_CODE_SUCCESS), take(1))
),
switchMap(() => updateAvatarCode(state$.value.editor.code.codeOnServer)),
map(actions.avatarCodeUpdated)
map(actions.initialAvatarCodeUpdated)
)

const updateAvatarCodeEpic = (
Expand Down Expand Up @@ -157,18 +157,17 @@ const checkBadgesEpic = (action$, state$, { api }) =>
*/
const checkBadgesEarnedEpic = (action$, state$, { pyodideRunner: { checkIfBadgeEarned } }) =>
action$.pipe(
ofType(types.BADGES_CHECKED_SUCCESS),
switchMap(({ payload: badges }) =>
ofType(types.AVATAR_CODE_UPDATED),
switchMap(({ payload: computedTurnResult }) =>
action$.pipe(
ofType(types.AVATAR_CODE_UPDATED),
switchMap(({ payload: computedTurnResult }) =>
ofType(types.BADGES_CHECKED_SUCCESS),
switchMap(({ payload: badges }) =>
from(
checkIfBadgeEarned(
badges,
computedTurnResult,
state$.value.editor.code.codeOnServer,
state$.value.game.gameState,
state$.value.game.connectionParameters.currentAvatarID
state$.value.game.gameState
)
)
),
Expand Down
4 changes: 2 additions & 2 deletions game_frontend/src/redux/features/AvatarWorker/epics.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ describe('avatarWorkerEpic', () => {
const output$2 = epics.initialUpdateAvatarCodeEpic(action$2, state$, dependencies)

expectObservable(output$1).toBe('-----(u|)', {
u: actions.avatarCodeUpdated()
u: actions.initialAvatarCodeUpdated()
})

expectObservable(output$2).toBe('-----(u|)', {
u: actions.avatarCodeUpdated()
u: actions.initialAvatarCodeUpdated()
})
})
})
Expand Down
2 changes: 2 additions & 0 deletions game_frontend/src/redux/features/AvatarWorker/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const INITIALIZE_PYODIDE = 'features/AvatarWorker/INITIALIZE_PYODIDE'
const PYODIDE_INITIALIZED = 'features/AvatarWorker/PYODIDE_INITIALIZED'

const AVATAR_CODE_UPDATED = 'features/AvatarWorker/AVATAR_CODE_UPDATED'
const INIT_AVATAR_CODE_UPDATED = 'features/AvatarWorker/INIT_AVATAR_CODE_UPDATED'

const AVATARS_NEXT_ACTION_COMPUTED = 'features/AvatarWorker/AVATARS_NEXT_ACTION_COMPUTED'

Expand All @@ -18,6 +19,7 @@ const POST_BADGES_FAILURE = 'features/AvatarWorker/POST_CODE_FAILURE'

export default {
AVATAR_CODE_UPDATED,
INIT_AVATAR_CODE_UPDATED,
PYODIDE_INITIALIZED,
AVATARS_NEXT_ACTION_COMPUTED,
INITIALIZE_PYODIDE,
Expand Down