Add txt to chess position function.
	
		
			
	
		
	
	
		
			
				
	
				continuous-integration/drone/push Build is failing
				
					Details
				
			
		
	
				
					
				
			
				
	
				continuous-integration/drone/push Build is failing
				
					Details
				
			
		
	This commit is contained in:
		
							parent
							
								
									0d97469f64
								
							
						
					
					
						commit
						b267ae6cbb
					
				| 
						 | 
					@ -3,6 +3,10 @@
 | 
				
			||||||
(defn get-square [pos game-state]
 | 
					(defn get-square [pos game-state]
 | 
				
			||||||
  (get game-state pos {}))
 | 
					  (get game-state pos {}))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					(defn chess-pos->coords [chars]
 | 
				
			||||||
 | 
					  [( + 1 (first (keep-indexed #(when (= (first chars) %2) %1) [\A \B "C" \D \E \F \G \H])))
 | 
				
			||||||
 | 
					  (js/parseInt (second chars))])
 | 
				
			||||||
 | 
					
 | 
				
			||||||
(defn edge?
 | 
					(defn edge?
 | 
				
			||||||
  "Return true if x and are inside the grid"
 | 
					  "Return true if x and are inside the grid"
 | 
				
			||||||
  [[distance x y]]
 | 
					  [[distance x y]]
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -170,3 +170,13 @@
 | 
				
			||||||
    :type :rook
 | 
					    :type :rook
 | 
				
			||||||
    :start-pos [8 8]}])
 | 
					    :start-pos [8 8]}])
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					(defn find-piece [colour piece]
 | 
				
			||||||
 | 
					  (first (filter #(and (= colour (:colour %1)) (= piece (:type %1))) start-state)))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					(defn translate-char->piece [char]
 | 
				
			||||||
 | 
					  (get {"K" :king
 | 
				
			||||||
 | 
					        "Q" :queen
 | 
				
			||||||
 | 
					        "B" :bishop
 | 
				
			||||||
 | 
					        "N" :knight
 | 
				
			||||||
 | 
					        "R" :rook
 | 
				
			||||||
 | 
					        "P" :pawn} char))
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,17 +1,18 @@
 | 
				
			||||||
(ns com.oly.static-sites.do-blog.pages.chess-board
 | 
					(ns com.oly.static-sites.do-blog.pages.chess-board
 | 
				
			||||||
  (:require
 | 
					  (:require
 | 
				
			||||||
   [com.oly.static-sites.do-blog.pages.chess.movement
 | 
					   [com.oly.static-sites.do-blog.pages.chess.movement
 | 
				
			||||||
    :refer [fetch-selected-piece-moves valid-move?]]
 | 
					    :refer [fetch-selected-piece-moves valid-move? chess-pos->coords ]]
 | 
				
			||||||
   [com.oly.static-sites.do-blog.pages.chess.render
 | 
					   [com.oly.static-sites.do-blog.pages.chess.render
 | 
				
			||||||
    :refer [calculate-black-white create-board enhance-board place-pieces-on-board]]
 | 
					    :refer [calculate-black-white create-board enhance-board place-pieces-on-board]]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
   [com.oly.static-sites.do-blog.pages.chess.state :refer [game-state start-state]]
 | 
					   [com.oly.static-sites.do-blog.pages.chess.state :refer [game-state start-state translate-char->piece find-piece]]
 | 
				
			||||||
   [com.oly.static-sites.do-blog.pages.chess.vstate :as v :refer [viking-start-state]]
 | 
					   [com.oly.static-sites.do-blog.pages.chess.vstate :as v :refer [viking-start-state]]
 | 
				
			||||||
   [com.oly.static-sites.ui-hiccup.interface :refer [board]]
 | 
					   [com.oly.static-sites.ui-hiccup.interface :refer [board]]
 | 
				
			||||||
   [reagent.core :as r]))
 | 
					   [reagent.core :as r]))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
;;/addwidget https://blog-test.digitaloctave.com/chess?widgetId=dochess
 | 
					;;/addwidget https://blog-test.digitaloctave.com/chess?widgetId=dochess
 | 
				
			||||||
 | 
					;;/addwidget https://blog-test.digitaloctave.com/chess?roomI=!YOyzcCyoomnjylcOFP:matrix.org&widgetId=dochess
 | 
				
			||||||
(def widget (atom {}))
 | 
					(def widget (atom {}))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
(defn send-to-parent
 | 
					(defn send-to-parent
 | 
				
			||||||
| 
						 | 
					@ -21,16 +22,36 @@
 | 
				
			||||||
    js/window.parent
 | 
					    js/window.parent
 | 
				
			||||||
    #js {:api "fromWidget",
 | 
					    #js {:api "fromWidget",
 | 
				
			||||||
         :v "1.0"
 | 
					         :v "1.0"
 | 
				
			||||||
 | 
					         :widgetId (:widget-id @widget)
 | 
				
			||||||
         :action action}, "*"))
 | 
					         :action action}, "*"))
 | 
				
			||||||
  ([action data]
 | 
					  ([action data]
 | 
				
			||||||
   (prn (str "sending " action))
 | 
					   (prn (str "sending d " action))
 | 
				
			||||||
   (.postMessage
 | 
					   (.postMessage
 | 
				
			||||||
    js/window.parent
 | 
					    js/window.parent
 | 
				
			||||||
    #js {:api "fromWidget",
 | 
					    #js {:api "fromWidget",
 | 
				
			||||||
         :v "1.0"
 | 
					         :v "1.0"
 | 
				
			||||||
         :action action
 | 
					         :action action
 | 
				
			||||||
         :data data}, "*")))
 | 
					         :widgetId (:widget-id @widget)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					         :data data}, "*"))
 | 
				
			||||||
 | 
					  ([action hm-key data]
 | 
				
			||||||
 | 
					   (prn (str "sending kv " action))
 | 
				
			||||||
 | 
					   (prn (clj->js  {:api "fromWidget",
 | 
				
			||||||
 | 
					               :v "1.0"
 | 
				
			||||||
 | 
					               :action action
 | 
				
			||||||
 | 
					               :widgetId (:widget-id @widget)
 | 
				
			||||||
 | 
					               (keyword hm-key) data
 | 
				
			||||||
 | 
					               }))
 | 
				
			||||||
 | 
					   (.postMessage
 | 
				
			||||||
 | 
					    js/window.parent
 | 
				
			||||||
 | 
					    (clj->js  {:api "fromWidget",
 | 
				
			||||||
 | 
					               :v "1.0"
 | 
				
			||||||
 | 
					               :action action
 | 
				
			||||||
 | 
					               :widgetId @widget
 | 
				
			||||||
 | 
					               (keyword hm-key) data
 | 
				
			||||||
 | 
					               }), "*"))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
(defn site-startup []
 | 
					(defn site-startup []
 | 
				
			||||||
  (prn "site start up")
 | 
					  (prn "site start up")
 | 
				
			||||||
| 
						 | 
					@ -43,46 +64,34 @@
 | 
				
			||||||
                       (prn (aget event "data" "action"))
 | 
					                       (prn (aget event "data" "action"))
 | 
				
			||||||
                       (prn (aget event "data" "widgetId"))
 | 
					                       (prn (aget event "data" "widgetId"))
 | 
				
			||||||
                       (when (= (aget event "data" "api") "toWidget")
 | 
					                       (when (= (aget event "data" "api") "toWidget")
 | 
				
			||||||
                         (prn (str "msg type " (.-type (.-data event))))
 | 
					                         (prn (str "msg type " (aget event "data" "action") ))
 | 
				
			||||||
                         (prn (str "msg type " (-> event .-data .-action )))
 | 
					 | 
				
			||||||
                         (case (aget event "data" "action")
 | 
					                         (case (aget event "data" "action")
 | 
				
			||||||
                           "m.room.message" (prn "type msg")
 | 
					                           "m.room.message" (prn "type msg")
 | 
				
			||||||
                           "m.room.member" (prn "type member")
 | 
					                           "m.room.member" (prn "type member")
 | 
				
			||||||
                           "capabilities" 
 | 
					                           "capabilities" 
 | 
				
			||||||
                           (do
 | 
					                           (do
 | 
				
			||||||
                             (swap! widget assoc :widget-id (-> event .-data .-widgetId ))
 | 
					                             (prn "sending capabilities")
 | 
				
			||||||
                             (.postMessage
 | 
					                             (swap! widget assoc :widget-id (aget event "data" "widgetId"))
 | 
				
			||||||
                              js/window.parent
 | 
					                             (send-to-parent "capabilities" :capabilities #js ["m.capability.screenshot"]))
 | 
				
			||||||
                              #js {:api "fromWidget",
 | 
					 | 
				
			||||||
                                   :v "1.0"
 | 
					 | 
				
			||||||
                                   :action "capabilities"
 | 
					 | 
				
			||||||
                                   :capabilities #js ["m.text" "m.image"]
 | 
					 | 
				
			||||||
                                   #_#_:action "supported_api_versions"}, "*")
 | 
					 | 
				
			||||||
                                                   (prn "type capa"))
 | 
					 | 
				
			||||||
                           "notify_capabilities"
 | 
					                           "notify_capabilities"
 | 
				
			||||||
                           (do
 | 
					                           (do
 | 
				
			||||||
                             (.postMessage
 | 
					                             (send-to-parent "capabilities" :capabilities #js ["m.capability.screenshot"]))
 | 
				
			||||||
                              js/window.parent
 | 
					                           (do
 | 
				
			||||||
                              #js {:api "fromWidget",
 | 
					                             (prn "unhandled action " (str (aget event "data" "action")))
 | 
				
			||||||
                                   :v "1.0"
 | 
					                             (prn (aget event "data"))))
 | 
				
			||||||
                                   :action "capabilities"
 | 
					 | 
				
			||||||
                                   :capabilities #js ["m.text" "m.image"]
 | 
					 | 
				
			||||||
                                   #_#_:action "supported_api_versions"}, "*")
 | 
					 | 
				
			||||||
                                                   (prn "type capa"))
 | 
					 | 
				
			||||||
                           (prn (.-type event)))
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
                         (prn (.-origin event))
 | 
					                         #_(prn (.-origin event))
 | 
				
			||||||
                         (prn (.-data event)))))
 | 
					                         #_(prn (.-data event)))))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  (prn "send ready message")
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
  (send-to-parent "ready")
 | 
					  (send-to-parent "ready")
 | 
				
			||||||
 | 
					  (send-to-parent "widget_ready")
 | 
				
			||||||
 | 
					  (send-to-parent "m.room.message" #js {:body "testing from widget" :msgtype "m.text"})
 | 
				
			||||||
  #_(.postMessage js/window.parent #js {:api "fromWidget",
 | 
					  #_(.postMessage js/window.parent #js {:api "fromWidget",
 | 
				
			||||||
                                      :v "1.0"
 | 
					                                      :v "1.0"
 | 
				
			||||||
                                      :action "ready"
 | 
					                                      :action "ready"
 | 
				
			||||||
                                    #_#_:action "supported_api_versions"}, "*")
 | 
					                                    #_#_:action "supported_api_versions"}, "*")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  (prn "send content loaded message")
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
  #_(.postMessage js/window.parent #js {:api "fromWidget",
 | 
					  #_(.postMessage js/window.parent #js {:api "fromWidget",
 | 
				
			||||||
                                      :v "1.0"
 | 
					                                      :v "1.0"
 | 
				
			||||||
| 
						 | 
					@ -99,17 +108,28 @@
 | 
				
			||||||
  )
 | 
					  )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#_(def game-state-viking (r/atom {:board viking-start-state}))
 | 
					#_(def game-state-viking (r/atom {:board viking-start-state}))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					(defn chars->map [text]
 | 
				
			||||||
 | 
					  (when (= 0 (mod (count text) 4))
 | 
				
			||||||
 | 
					    (loop [positions text
 | 
				
			||||||
 | 
					           results []]
 | 
				
			||||||
 | 
					      (if (seq positions)
 | 
				
			||||||
 | 
					        (recur
 | 
				
			||||||
 | 
					         (drop 4 positions)
 | 
				
			||||||
 | 
					         (conj results
 | 
				
			||||||
 | 
					               (merge (find-piece :white (translate-char->piece (second positions)))
 | 
				
			||||||
 | 
					                      {:start-pos (chess-pos->coords (str (nth positions 2) (nth positions 3)))
 | 
				
			||||||
 | 
					                       :pos (chess-pos->coords (str (nth positions 2) (nth positions 3)))})))
 | 
				
			||||||
 | 
					        results))))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
(defn setup []
 | 
					(defn setup []
 | 
				
			||||||
  (prn "register listener")
 | 
					  (prn "register listener")
 | 
				
			||||||
  (.addEventListener js/window "load"
 | 
					  (.addEventListener js/window "load"
 | 
				
			||||||
                     (fn [event]
 | 
					                     (fn [event]
 | 
				
			||||||
                       (prn "on load")
 | 
					                       (prn "on load")
 | 
				
			||||||
                       (prn (.-location js/window))
 | 
					                       (site-startup))))
 | 
				
			||||||
                       (prn (-> js/window .-location .-hash))
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                       (site-startup)
 | 
					 | 
				
			||||||
                       ))
 | 
					 | 
				
			||||||
  )
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
(defonce test-event (setup))
 | 
					(defonce test-event (setup))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -250,7 +270,7 @@
 | 
				
			||||||
               (enhance-board click-square)
 | 
					               (enhance-board click-square)
 | 
				
			||||||
               (place-pieces-on-board v/viking-start-state click-square))}))
 | 
					               (place-pieces-on-board v/viking-start-state click-square))}))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
(defonce chess-board-setup
 | 
					(defn chess-board-setup [start-state]
 | 
				
			||||||
  (reset! game-state
 | 
					  (reset! game-state
 | 
				
			||||||
          {:player-one {:pieces []
 | 
					          {:player-one {:pieces []
 | 
				
			||||||
                        :colour :white
 | 
					                        :colour :white
 | 
				
			||||||
| 
						 | 
					@ -263,7 +283,16 @@
 | 
				
			||||||
               (enhance-board click-square)
 | 
					               (enhance-board click-square)
 | 
				
			||||||
               (place-pieces-on-board start-state click-square))}))
 | 
					               (place-pieces-on-board start-state click-square))}))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
(defn chess-page [{:keys [path-params] :as route}]
 | 
					(defn chess-page [{:keys [path-params query-params] :as route}]
 | 
				
			||||||
 | 
					  (prn route)
 | 
				
			||||||
 | 
					  (prn (:pieces query-params))
 | 
				
			||||||
 | 
					  (prn start-state)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  (prn (chars->map (:pieces query-params)))
 | 
				
			||||||
 | 
					  (if (:pieces query-params)
 | 
				
			||||||
 | 
					    (chess-board-setup (chars->map (:pieces query-params)))
 | 
				
			||||||
 | 
					    (chess-board-setup start-state))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  (fn [{:keys [path-params] :as route}]
 | 
					  (fn [{:keys [path-params] :as route}]
 | 
				
			||||||
    [:div
 | 
					    [:div
 | 
				
			||||||
     (let [[pos piece] (get-selected-square)]
 | 
					     (let [[pos piece] (get-selected-square)]
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue