Skip to content

Commit

Permalink
Add double check along with hash to see if share game is valid
Browse files Browse the repository at this point in the history
  • Loading branch information
bmwwoo committed Sep 3, 2015
1 parent 2bea8a5 commit 01c2fc2
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 11 deletions.
20 changes: 13 additions & 7 deletions app/controllers/MathSwipeController.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,23 @@ class MathSwipeController
else
TrackingService.desktopView()
@cursorToPointer()
@initialize window.location.hash
while not @initialize window.location.hash
continue

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

initialize: (hash) ->
goals = []
solutionPlacements = []

hasCompleteBoard = false
if hash? and hash isnt ''
solutionPlacements = []
goals = []
boardValues = []
ShareGameService.decode boardValues, goals, solutionPlacements
else
hasCompleteBoard = ShareGameService.decode boardValues, goals, solutionPlacements
unless hasCompleteBoard
length = 3
goals = []
solutionPlacements = []
inputs = []
inputLengths = RandomizedFitLength.generate length * length
@generateInputs inputLengths, inputs, goals
Expand All @@ -59,7 +62,10 @@ class MathSwipeController
Colors, ClickHandler, SolutionService,
BoardSolvedService, RunningSum
ResetButton.bindClick @board
ShareGameService.reloadPageWithHash @board, solutionPlacements
unless ShareGameService.reloadPageWithHash @board, solutionPlacements
@goalContainer.clearGoals()
return false
true

isMobile: () ->
Android: () ->
Expand Down
26 changes: 22 additions & 4 deletions app/services/ShareGameService.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ SolutionService = require './SolutionService'
class ShareGameService

@reloadPageWithHash: (board, solutionPlacements) ->
console.log @checkSolutionPlacements board, solutionPlacements
unless @checkSolutionPlacements board, solutionPlacements
window.location.hash = ''
console.log window.location.hash
return false
hash = @encode board.initialValues, board.goals, solutionPlacements
window.location.hash = hash

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

@decode: (boardValues, goals, slnPlacements) ->
decoded = JSON.parse atob window.location.hash
return false if not decoded?
decoded.substr(1, window.location.hash.length)
try
decoded = atob window.location.hash.substr(1, window.location.hash.length)
decoded = JSON.parse decoded
catch e
return false
return false unless decoded? and @isValidDecode decoded
return false unless decoded.b? and decoded.g? and decoded.p?
length = Math.sqrt decoded.b.length
index = 0

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

true

@isValidDecode: (decoded) ->
alphabet = ['"', '{', '}', '[', ']', ',', ':',
'b', 'g', 'p', '1', '2', '3', '4',
'5', '6', '7', '8', '9', '0',
'+', '-', '*']
for char in decoded
return false if alphabet.indexOf(char) is -1
true

@checkSolutionPlacements: (board, solutionPlacements) ->
@tempBoard = {}
@tempBoard.boardValues = []
Expand Down

0 comments on commit 01c2fc2

Please sign in to comment.