@@ -4,7 +4,10 @@ SolutionService = require './SolutionService'
4
4
class ShareGameService
5
5
6
6
@ 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
8
11
hash = @ encode board .initialValues , board .goals , solutionPlacements
9
12
window .location .hash = hash
10
13
@@ -19,9 +22,13 @@ class ShareGameService
19
22
btoa (JSON .stringify {b : boardValues, g : goals, p : slnPlacements})
20
23
21
24
@ 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 ?
25
32
length = Math .sqrt decoded .b .length
26
33
index = 0
27
34
@@ -40,6 +47,17 @@ class ShareGameService
40
47
expression .push [(Math .floor decoded .p [placement][coord] / length), (decoded .p [placement][coord] % length)]
41
48
slnPlacements .push expression
42
49
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
+
43
61
@ checkSolutionPlacements: (board , solutionPlacements ) ->
44
62
@tempBoard = {}
45
63
@tempBoard .boardValues = []
0 commit comments