Add txt to chess position function.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Oly 2025-01-24 13:40:30 +00:00
parent 0d97469f64
commit e1f13b6b1a
3 changed files with 73 additions and 37 deletions

View File

@ -3,6 +3,10 @@
(defn get-square [pos game-state]
(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?
"Return true if x and are inside the grid"
[[distance x y]]

View File

@ -170,3 +170,13 @@
:type :rook
: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))

View File

@ -1,17 +1,18 @@
(ns com.oly.static-sites.do-blog.pages.chess-board
(:require
[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
: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.ui-hiccup.interface :refer [board]]
[reagent.core :as r]))
;;/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 {}))
(defn send-to-parent
@ -21,16 +22,30 @@
js/window.parent
#js {:api "fromWidget",
:v "1.0"
:widgetId @widget
:action action}, "*"))
([action data]
(prn (str "sending " action))
(prn (str "sending d " action))
(.postMessage
js/window.parent
#js {:api "fromWidget",
:v "1.0"
:action action
:data data}, "*")))
:widgetId @widget
:data data}, "*"))
([action hm-key data]
(prn (str "sending kv " action))
(.postMessage
js/window.parent
(clj->js {:api "fromWidget",
:v "1.0"
:action action
:widgetId @widget
(keyword hm-key) data
}), "*"))
)
(defn site-startup []
(prn "site start up")
@ -43,46 +58,33 @@
(prn (aget event "data" "action"))
(prn (aget event "data" "widgetId"))
(when (= (aget event "data" "api") "toWidget")
(prn (str "msg type " (.-type (.-data event))))
(prn (str "msg type " (-> event .-data .-action )))
(case (aget event "data" "action")
(prn (str "msg type " (aget event "data" "action") ))
(case (aget event "data" "action")
"m.room.message" (prn "type msg")
"m.room.member" (prn "type member")
"capabilities"
(do
(swap! widget assoc :widget-id (-> event .-data .-widgetId ))
(.postMessage
js/window.parent
#js {:api "fromWidget",
:v "1.0"
:action "capabilities"
:capabilities #js ["m.text" "m.image"]
#_#_:action "supported_api_versions"}, "*")
(prn "type capa"))
(swap! widget assoc :widget-id (aget event "data" "widgetId"))
(send-to-parent "capabilities" :capabilities #js ["m.capability.screenshot"]))
"notify_capabilities"
(do
(.postMessage
js/window.parent
#js {:api "fromWidget",
:v "1.0"
:action "capabilities"
:capabilities #js ["m.text" "m.image"]
#_#_:action "supported_api_versions"}, "*")
(prn "type capa"))
(prn (.-type event)))
(send-to-parent "capabilities" :capabilities #js ["m.capability.screenshot"]))
(do
(prn "unhandled action " (str (aget event "data" "action")))
(prn (aget event "data"))))
(prn (.-origin event))
(prn (.-data event)))))
#_(prn (.-origin event))
#_(prn (.-data event)))))
(prn "send ready message")
(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",
:v "1.0"
:action "ready"
#_#_:action "supported_api_versions"}, "*")
(prn "send content loaded message")
#_(.postMessage js/window.parent #js {:api "fromWidget",
:v "1.0"
@ -99,17 +101,28 @@
)
#_(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 []
(prn "register listener")
(.addEventListener js/window "load"
(fn [event]
(prn "on load")
(prn (.-location js/window))
(prn (-> js/window .-location .-hash))
(site-startup)
))
)
(site-startup))))
(defonce test-event (setup))
@ -250,7 +263,7 @@
(enhance-board 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
{:player-one {:pieces []
:colour :white
@ -263,7 +276,16 @@
(enhance-board 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}]
[:div
(let [[pos piece] (get-selected-square)]