~subsetpark/whist

b0874042e4abd9fb9e633879a1e4a6a5f0296a43 — Zach Smith 11 months ago e2233ac
Added joy
4 files changed, 57 insertions(+), 11 deletions(-)

M init.janet
A main.janet
M project.janet
M test/whist.janet
M init.janet => init.janet +4 -4
@@ 9,16 9,16 @@
(defn-
  make-player
  [id team]
  {:id id :team team :score 0 :meta {} :hand []})
  {:id id :team team :score 0})

(defn init
  "Create an initial game state."
  [[fst snd] [thd fth]]
  [fst snd thd fth]
  (let [rng (math/rng (os/time))
	players [fst snd thd fth]
	forehand (players (math/rng-int rng 4))]
      {:players [(make-player fst 1)
		 (make-player thd 2)
		 (make-player snd 1)
		 (make-player snd 2)
		 (make-player thd 1)
		 (make-player fth 2)]
       :state {:phase "deal"}}))

A main.janet => main.janet +30 -0
@@ 0,0 1,30 @@
(import json)
(import init)
(import whist)

(use joy)


(route :get "/api/v1/whist/init" :init)
(route :post "/api/v1/whist/next" :next)

(defn next [request]
  (let [state (get-in request [:body :state])
	players (get-in request [:body :players])
	[new-state events] (whist/next {:state state :players players})]
    (pp state)
    (pp new-state)
    (pp events)
    (application/json {:state new-state :events []})))

(defn init [request]
  (pp request)
  (let [players (get-in request [:query-string :players])
	[p1 p2 p3 p4] (string/split "," players)
	resp (init/init p1 p2 p3 p4)]
  
    (application/json resp)))

(def app (app {:layout layout :csrf-token false}))
(defn main [& args]
  (server app 9001))

M project.janet => project.janet +18 -2
@@ 1,5 1,21 @@
(declare-project
 :name "whist-app"
 :description ""
 :dependencies ["https://github.com/janet-lang/json.git"
		"https://github.com/pyrmont/testament"
		"https://github.com/joy-framework/joy"]
 :author ""
 :license ""
 :url ""
 :repo "")

(phony "server" []
  (os/shell "janet main.janet"))

(phony "watch" []
  (os/shell "find . -name '*.janet' | entr -r -d janet main.janet"))

(declare-project
  :name "bidwhist" # required
  # Optional urls to git repositories that contain required artifacts.
  :dependencies ["https://github.com/janet-lang/json.git"
		 "https://github.com/pyrmont/testament"])
  :dependencies )

M test/whist.janet => test/whist.janet +5 -5
@@ 7,12 7,12 @@

(cards/intern-cards!)

(def- players [["North" "South"] ["East" "West"]])
(def- players ["North" "East" "South" "West"])

(def- player-state [{:hand [] :score 0 :team 1 :meta {} :id "North"}
		    {:hand [] :score 0 :team 2 :meta {} :id "East"}
		    {:hand [] :score 0 :team 1 :meta {} :id "South"}
		    {:hand [] :score 0 :team 2 :meta {} :id "West"}])
(def- player-state [{:score 0 :team 1 :id "North"}
		    {:score 0 :team 2 :id "East"}
		    {:score 0 :team 1 :id "South"}
		    {:score 0 :team 2 :id "West"}])

(defn all-not-passed [] @{"North" true "East" true "South" true "West" true})