Skip to content

Commit

Permalink
Implement sharing of game
Browse files Browse the repository at this point in the history
  • Loading branch information
bmwwoo committed Sep 3, 2015
1 parent b04e4fd commit 2bea8a5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 23 deletions.
21 changes: 4 additions & 17 deletions app/controllers/MathSwipeController.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -43,36 +43,23 @@ class MathSwipeController

if hash? and hash isnt ''
boardValues = []
goals = []
placements = []
ShareGameService.decode boardValues, goals, placements
console.log goals
gameModel = @createSharedGrid boardValues, length
ShareGameService.decode boardValues, goals, solutionPlacements
else
length = 3
inputs = []
inputLengths = RandomizedFitLength.generate length * length
@generateInputs inputLengths, inputs, goals
console.log expression for expression in inputs
console.log '\n'
gameModel = @generateBoard inputs, length, solutionPlacements
boardValues = @generateBoard inputs, length, solutionPlacements

@goalContainer = new GoalContainer goals, Colors
@board = new Board gameModel, @gameScene, goals, @symbols,
@board = new Board boardValues, @gameScene, goals, @symbols,
@goalContainer, @isMobile().any()?, Cell,
Colors, ClickHandler, SolutionService,
BoardSolvedService, RunningSum
ResetButton.bindClick @board
# ShareGameService.reloadPageWithHash @board, solutionPlacements

createSharedGrid: (boardValues, length) ->
grid = []
index = 0
for row in [0...length]
grid.push []
for col in [0...length]
grid[row].push boardValues[index++]
grid
ShareGameService.reloadPageWithHash @board, solutionPlacements

isMobile: () ->
Android: () ->
Expand Down
16 changes: 10 additions & 6 deletions app/services/ShareGameService.coffee
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
$ = require 'jquery'
SolutionService = require './SolutionService'

# btoa('{"a":"9677*1+8*","b":[135,102],"c":[[2,2,1,1,0],[7,8,9,4]]}');
# "eyJhIjoiOTY3NyoxKzgqIiwiYiI6WzEzNSwxMDJdLCJjIjpbWzIsMiwxLDEsMF0sWzcsOCw5LDRdXX0="

class ShareGameService

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

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

Expand All @@ -32,9 +31,14 @@ class ShareGameService
row.push decoded.b[index++]
boardValues.push row

goals = decoded.g
for goal in decoded.g
goals.push goal

console.log decoded.p
for placement in [0...decoded.p.length]
expression = []
for coord in [0...decoded.p[placement].length]
expression.push [(Math.floor decoded.p[placement][coord] / length), (decoded.p[placement][coord] % length)]
slnPlacements.push expression

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

0 comments on commit 2bea8a5

Please sign in to comment.