Skip to content

Commit 0f43724

Browse files
committed
Add double check along with hash to see if share game is valid
1 parent 2bea8a5 commit 0f43724

File tree

2 files changed

+35
-11
lines changed

2 files changed

+35
-11
lines changed

app/controllers/MathSwipeController.coffee

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,23 @@ class MathSwipeController
3232
else
3333
TrackingService.desktopView()
3434
@cursorToPointer()
35-
@initialize window.location.hash
35+
while not @initialize window.location.hash
36+
continue
3637

3738
# # Uncomment the following line to perform general tests
3839
# GeneralTests.tests @board
3940

4041
initialize: (hash) ->
41-
goals = []
42-
solutionPlacements = []
43-
42+
hasCompleteBoard = false
4443
if hash? and hash isnt ''
44+
solutionPlacements = []
45+
goals = []
4546
boardValues = []
46-
ShareGameService.decode boardValues, goals, solutionPlacements
47-
else
47+
hasCompleteBoard = ShareGameService.decode boardValues, goals, solutionPlacements
48+
unless hasCompleteBoard
4849
length = 3
50+
goals = []
51+
solutionPlacements = []
4952
inputs = []
5053
inputLengths = RandomizedFitLength.generate length * length
5154
@generateInputs inputLengths, inputs, goals
@@ -59,7 +62,10 @@ class MathSwipeController
5962
Colors, ClickHandler, SolutionService,
6063
BoardSolvedService, RunningSum
6164
ResetButton.bindClick @board
62-
ShareGameService.reloadPageWithHash @board, solutionPlacements
65+
unless ShareGameService.reloadPageWithHash @board, solutionPlacements
66+
@goalContainer.clearGoals()
67+
return false
68+
true
6369

6470
isMobile: () ->
6571
Android: () ->

app/services/ShareGameService.coffee

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ SolutionService = require './SolutionService'
44
class ShareGameService
55

66
@reloadPageWithHash: (board, solutionPlacements) ->
7-
console.log @checkSolutionPlacements board, solutionPlacements
7+
unless @checkSolutionPlacements board, solutionPlacements
8+
window.location.hash = ''
9+
console.log window.location.hash
10+
return false
811
hash = @encode board.initialValues, board.goals, solutionPlacements
912
window.location.hash = hash
1013

@@ -19,9 +22,13 @@ class ShareGameService
1922
btoa(JSON.stringify {b: boardValues, g: goals, p: slnPlacements})
2023

2124
@decode: (boardValues, goals, slnPlacements) ->
22-
decoded = JSON.parse atob window.location.hash
23-
return false if not decoded?
24-
decoded.substr(1, window.location.hash.length)
25+
try
26+
decoded = atob window.location.hash.substr(1, window.location.hash.length)
27+
catch e
28+
return false
29+
return false unless decoded? and @isValidDecode decoded
30+
decoded = JSON.parse decoded
31+
return false unless decoded.b? and decoded.g? and decoded.p?
2532
length = Math.sqrt decoded.b.length
2633
index = 0
2734

@@ -40,6 +47,17 @@ class ShareGameService
4047
expression.push [(Math.floor decoded.p[placement][coord] / length), (decoded.p[placement][coord] % length)]
4148
slnPlacements.push expression
4249

50+
true
51+
52+
@isValidDecode: (decoded) ->
53+
alphabet = ['"', '{', '}', '[', ']', ',', ':',
54+
'b', 'g', 'p', '1', '2', '3', '4',
55+
'5', '6', '7', '8', '9', '0',
56+
'+', '-', '*']
57+
for char in decoded
58+
return false if alphabet.indexOf(char) is -1
59+
true
60+
4361
@checkSolutionPlacements: (board, solutionPlacements) ->
4462
@tempBoard = {}
4563
@tempBoard.boardValues = []

0 commit comments

Comments
 (0)