Skip to content

Commit

Permalink
more progress on puzzles
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar committed Feb 5, 2014
1 parent 43216ac commit 7e3f0a3
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 24 deletions.
1 change: 0 additions & 1 deletion app/views/puzzle/layout.scala.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
}

@moreJs = {
@jsTagCompiled("chessboard.js")
@evenMoreJs
}

Expand Down
2 changes: 2 additions & 0 deletions app/views/puzzle/show.scala.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

@moreJs = {
@jsTag("vendor/chess.min.js")
@jsTagCompiled("chessboard.js")
@jsAt("compiled/puzzle.js")
}

Expand All @@ -21,6 +22,7 @@ <h1>Puzzle #@puzzle.id</h1>

<div id="chessboard"
data-fen="@puzzle.fen"
data-color="@puzzle.color"
data-move="@puzzle.initialMove"
data-lines="@lila.puzzle.Line.toJsonString(puzzle.lines)"></div>

Expand Down
7 changes: 7 additions & 0 deletions bin/clojurescript_prod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/sh

mkdir -p public/compiled
rm public/compiled/puzzle.js
cd cljs/puzzle
lein cljsbuild once prod
cd -
1 change: 0 additions & 1 deletion bin/closure
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#!/bin/sh
. bin/lilarc


mkdir -p public/compiled
for file in big.js chart2.js user.js boardEditor.js pgn4hacks.js chessboard.js; do
orig=public/javascripts/$file
Expand Down
1 change: 1 addition & 0 deletions bin/prod/deploy
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ if [ $mode == "main" ]; then
fi

if [ $mode == "main" ]; then
bin/clojurescript_prod
bin/closure
fi

Expand Down
6 changes: 6 additions & 0 deletions cljs/puzzle/externs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
var ChessBoard = {
position: function() {}
};
var Chess = {
fen: function() {}
};
1 change: 1 addition & 0 deletions cljs/puzzle/project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@
:prod {
:source-paths ["src"]
:compiler {:output-to "../../public/compiled/puzzle.js"
:externs ["externs.js"]
:optimizations :advanced
:pretty-print false}}}})
51 changes: 29 additions & 22 deletions cljs/puzzle/src/puzzle.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@
(def drop-chan (chan))
(def animation-delay 300)
(def chess (new js/Chess initial-fen))
(def started-at (new js/Date))

(defn playing [] (dommy/has-class? puzzle-elem "playing"))
(defn seconds-since-started [] (.round js/Math (/ 1000 (- (.getTime (new js/Date)) (.getTime started-at)))))

(defn playing? [] (dommy/has-class? puzzle-elem "playing"))

(defn apply-move
([orig, dest] (.move chess (clj->js {:from orig :to dest})))
Expand All @@ -32,13 +35,12 @@
(defn await-in [ch value duration] (js/setTimeout #(put! ch value) duration) ch)

(defn on-drop! [orig, dest]
(if (and (playing) (apply-move orig dest))
(put! drop-chan (str orig dest)) "snapback"))
(if (and (playing?) (apply-move orig dest)) (put! drop-chan (str orig dest)) "snapback"))

(def chessboard
(new js/ChessBoard "chessboard"
(clj->js {:position initial-fen
:orientation (if (= "b" (.turn chess)) "white" "black")
:orientation (dommy/attr chessboard-elem "data-color")
:draggable true
:dropOffBoard "snapback"
:sparePieces false
Expand Down Expand Up @@ -70,13 +72,18 @@
(defn end! [status]
(set-status! status))

(defn fail! []
(set-status! "playing fail"))

(go
(<! (timeout 1000))
(apply-move initial-move)
(set-position! (.fen chess))
(color-move! initial-move)
(set-status! "playing")
(loop [progress [] fen (.fen chess)]
(loop [progress []
fen (.fen chess)
failed false]
(let [move (<! drop-chan)
new-progress (conj progress move)
new-lines (get-in lines new-progress)]
Expand All @@ -86,21 +93,21 @@
(<! (timeout animation-delay))
(.load chess fen)
(set-position! fen)
(recur progress fen))
(recur progress fen failed))
nil (do
(set-status! "playing fail")
(<! (timeout animation-delay))
(.load chess fen)
(set-position! fen)
(recur progress fen))
(do
(set-status! "playing")
(color-move! move)
(set-position! (.fen chess))
(<! (timeout (+ animation-delay 50)))
(if (= new-lines "win")
(end! "win")
(let [aim (<! (ai-play! new-lines))]
(if (= (get new-lines aim) "win")
(end! "win")
(recur (conj new-progress aim) (.fen chess))))))))))
(when (not failed) (fail!)
(<! (timeout animation-delay))
(.load chess fen)
(set-position! fen)
(recur progress fen true))
(do
(set-status! "playing")
(color-move! move)
(set-position! (.fen chess))
(<! (timeout (+ animation-delay 50)))
(if (= new-lines "win")
(end! "win")
(let [aim (<! (ai-play! new-lines))]
(if (= (get new-lines aim) "win")
(end! "win")
(recur (conj new-progress aim) (.fen chess) failed)))))))))

0 comments on commit 7e3f0a3

Please sign in to comment.