From 2bea8a589ccc78408e0f5bd0822e9d8d22c31829 Mon Sep 17 00:00:00 2001 From: Brandon Woo Date: Thu, 3 Sep 2015 11:42:59 -0700 Subject: [PATCH] Implement sharing of game --- app/controllers/MathSwipeController.coffee | 21 ++++----------------- app/services/ShareGameService.coffee | 16 ++++++++++------ 2 files changed, 14 insertions(+), 23 deletions(-) diff --git a/app/controllers/MathSwipeController.coffee b/app/controllers/MathSwipeController.coffee index ba5361a..9bd2b99 100644 --- a/app/controllers/MathSwipeController.coffee +++ b/app/controllers/MathSwipeController.coffee @@ -43,11 +43,7 @@ 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 = [] @@ -55,24 +51,15 @@ class MathSwipeController @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: () -> diff --git a/app/services/ShareGameService.coffee b/app/services/ShareGameService.coffee index d76edcb..a66798e 100644 --- a/app/services/ShareGameService.coffee +++ b/app/services/ShareGameService.coffee @@ -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) -> @@ -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 @@ -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 = {}