Skip to content

Commit ecb9580

Browse files
committed
Refactor for readability
1 parent 01c2fc2 commit ecb9580

File tree

2 files changed

+61
-50
lines changed

2 files changed

+61
-50
lines changed

app/controllers/MathSwipeController.coffee

+13-13
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,17 @@ class MathSwipeController
3232
else
3333
TrackingService.desktopView()
3434
@cursorToPointer()
35-
while not @initialize window.location.hash
36-
continue
35+
@initialize window.location.hash
3736

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

4140
initialize: (hash) ->
41+
solutionPlacements = []
42+
goals = []
43+
boardValues = []
4244
hasCompleteBoard = false
4345
if hash? and hash isnt ''
44-
solutionPlacements = []
45-
goals = []
46-
boardValues = []
4746
hasCompleteBoard = ShareGameService.decode boardValues, goals, solutionPlacements
4847
unless hasCompleteBoard
4948
length = 3
@@ -62,10 +61,8 @@ class MathSwipeController
6261
Colors, ClickHandler, SolutionService,
6362
BoardSolvedService, RunningSum
6463
ResetButton.bindClick @board
65-
unless ShareGameService.reloadPageWithHash @board, solutionPlacements
66-
@goalContainer.clearGoals()
67-
return false
68-
true
64+
@createNewGame() unless ShareGameService.reloadPageWithHash(@board,
65+
solutionPlacements, SolutionService)
6966

7067
isMobile: () ->
7168
Android: () ->
@@ -86,10 +83,13 @@ class MathSwipeController
8683
bindNewGameButton: ->
8784
$('#new-game-button').click (e) =>
8885
TrackingService.boardEvent 'new game'
89-
@gameScene.clear()
90-
@goalContainer.clearGoals()
91-
ResetButton.unbindClick()
92-
@initialize (window.location.hash = '')
86+
@createNewGame()
87+
88+
createNewGame: ->
89+
@gameScene.clear()
90+
@goalContainer.clearGoals()
91+
ResetButton.unbindClick()
92+
@initialize (window.location.hash = '')
9393

9494
createGameScene: ->
9595
gameDom = document.getElementById('game')

app/services/ShareGameService.coffee

+48-37
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,38 @@
11
$ = require 'jquery'
2-
SolutionService = require './SolutionService'
32

43
class ShareGameService
54

6-
@reloadPageWithHash: (board, solutionPlacements) ->
7-
unless @checkSolutionPlacements board, solutionPlacements
5+
@reloadPageWithHash: (board, solutionPlacements, SolutionService) ->
6+
unless @checkSolutionPlacements board, solutionPlacements, SolutionService
87
window.location.hash = ''
9-
console.log window.location.hash
108
return false
119
hash = @encode board.initialValues, board.goals, solutionPlacements
1210
window.location.hash = hash
1311

14-
@encode: (boardValues, goals, slnPlacements) ->
12+
@encode: (boardValues, goals, solutionPlacements) ->
1513
boardValues = (JSON.stringify boardValues).replace(/(\[|\]|"|,|{|})*/g, '')
1614

1715
length = Math.sqrt boardValues.length
18-
for placement in [0...slnPlacements.length]
19-
for coord in [0...slnPlacements[placement].length]
20-
slnPlacements[placement][coord] = slnPlacements[placement][coord][0] * length + slnPlacements[placement][coord][1]
16+
for list in [0...solutionPlacements.length]
17+
for pos in [0...solutionPlacements[list].length]
18+
solutionPlacements[list][pos] = solutionPlacements[list][pos][0] * length +
19+
solutionPlacements[list][pos][1]
2120

22-
btoa(JSON.stringify {b: boardValues, g: goals, p: slnPlacements})
21+
btoa(JSON.stringify {b: boardValues, g: goals, p: solutionPlacements})
2322

24-
@decode: (boardValues, goals, slnPlacements) ->
23+
@decode: (boardValues, goals, solutionPlacements) ->
2524
try
2625
decoded = atob window.location.hash.substr(1, window.location.hash.length)
2726
decoded = JSON.parse decoded
2827
catch e
29-
return false
30-
return false unless decoded? and @isValidDecode decoded
31-
return false unless decoded.b? and decoded.g? and decoded.p?
32-
length = Math.sqrt decoded.b.length
33-
index = 0
34-
35-
for i in [0...length]
36-
row = []
37-
for j in [0...length]
38-
row.push decoded.b[index++]
39-
boardValues.push row
40-
41-
for goal in decoded.g
42-
goals.push goal
43-
44-
for placement in [0...decoded.p.length]
45-
expression = []
46-
for coord in [0...decoded.p[placement].length]
47-
expression.push [(Math.floor decoded.p[placement][coord] / length), (decoded.p[placement][coord] % length)]
48-
slnPlacements.push expression
28+
decoded = null
29+
return false unless decoded? and decoded.b? and decoded.g? and
30+
decoded.p? and @isValidDecode decoded
4931

32+
length = Math.sqrt decoded.b.length
33+
@decodeBoardValues decoded.b, boardValues, length
34+
@decodeGoals decoded.g, goals
35+
@decodeSolutionPlacements decoded.p, solutionPlacements, length
5036
true
5137

5238
@isValidDecode: (decoded) ->
@@ -58,28 +44,53 @@ class ShareGameService
5844
return false if alphabet.indexOf(char) is -1
5945
true
6046

61-
@checkSolutionPlacements: (board, solutionPlacements) ->
62-
@tempBoard = {}
63-
@tempBoard.boardValues = []
64-
for row, i in board.initialValues
65-
@tempBoard.boardValues.push []
66-
for col in row
67-
@tempBoard.boardValues[i].push col
47+
@decodeBoardValues: (copy, boardValues, length) ->
48+
index = 0
49+
for i in [0...length]
50+
row = []
51+
for j in [0...length]
52+
row.push copy[index++]
53+
boardValues.push row
54+
55+
@decodeGoals: (copy, goals) ->
56+
goals.push goal for goal in copy
57+
58+
@decodeSolutionPlacements: (copy, solutionPlacements, length) ->
59+
for list in [0...copy.length]
60+
expression = []
61+
for coord in [0...copy[list].length]
62+
expression.push [(Math.floor copy[list][coord] / length),
63+
(copy[list][coord] % length)]
64+
solutionPlacements.push expression
65+
66+
@checkSolutionPlacements: (board, solutionPlacements, SolutionService) ->
67+
@initializeTempBoard board
6868
@solutionService = new SolutionService @tempBoard, board.goals
69+
6970
for expression in solutionPlacements
7071
clickedCells = []
7172
for index in [0...expression.length]
7273
cell = expression[index]
7374
clickedCells.push {row: cell[0], col: cell[1]}
7475
@solutionService.initialize clickedCells
76+
7577
for cell in clickedCells
7678
@tempBoard.boardValues[cell.row][cell.col] = ' '
7779
@pushDownTempBoard()
7880

7981
unless @solutionService.isSolution()
8082
return false
83+
8184
true
8285

86+
@initializeTempBoard: (board) ->
87+
@tempBoard = {}
88+
@tempBoard.boardValues = []
89+
for row, i in board.initialValues
90+
@tempBoard.boardValues.push []
91+
for col in row
92+
@tempBoard.boardValues[i].push col
93+
8394
@pushDownTempBoard: ->
8495
for row in [@tempBoard.boardValues.length-1..1]
8596
for col in [@tempBoard.boardValues.length-1..0]

0 commit comments

Comments
 (0)