Skip to content

Commit

Permalink
Add profiles
Browse files Browse the repository at this point in the history
  • Loading branch information
nenadalm committed Aug 10, 2023
1 parent a3e6b68 commit 57e8c2e
Show file tree
Hide file tree
Showing 5 changed files with 259 additions and 83 deletions.
22 changes: 21 additions & 1 deletion resources/public/css/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ label {
font-size: 1.2rem;
}

input, select {
input, select, fieldset {
box-sizing: border-box;
width: 100%;
font-size: 2rem;
Expand All @@ -26,6 +26,9 @@ input, select {
border: none;
color: var(--text-color);
}
input[disabled], select[disabled] {
color: var(--text-medium-color);
}

option {
background-color: var(--background-color);
Expand All @@ -36,6 +39,11 @@ a {
color: var(--text-medium-color);
}

hr {
width: 100%;
margin: 0 0 2rem 0;
}

.issue-link {
text-align: center;
margin-bottom: 1rem;
Expand Down Expand Up @@ -140,6 +148,17 @@ a {
pointer-events: auto;
}

.title-panel {
position: absolute;
display: flex;
justify-content: space-evenly;
align-items: flex-end;
width: 100%;
height: 100%;
pointer-events: none;
writing-mode: vertical-rl;
}

.menu-button, .amount-toggle {
width: 5rem;
height: 5rem;
Expand Down Expand Up @@ -190,6 +209,7 @@ a {
.menu--header {
display: flex;
justify-content: flex-end;
margin-bottom: 2rem;
}

.menu--footer {
Expand Down
98 changes: 73 additions & 25 deletions src/app/events.cljs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
(ns app.events
(:require
[clojure.edn :as edn]
[re-frame.core :as re-frame]))
[re-frame.core :as re-frame]
[app.util :as u]))

(re-frame/reg-cofx
:time
Expand Down Expand Up @@ -33,40 +34,70 @@
(fn [settings]
(js/window.localStorage.setItem "nenadalm.life-counter/settings" (pr-str settings))))

(re-frame/reg-cofx
:profiles
(fn [coeffects _]
(assoc coeffects
:profiles
(or
(edn/read-string
(js/window.localStorage.getItem "nenadalm.life-counter/profiles"))
{}))))

(re-frame/reg-fx
:profiles
(fn [profiles]
(js/window.localStorage.setItem "nenadalm.life-counter/profiles" (pr-str profiles))))

(def ^:private default-profiles
(u/index-by
:profile
[{:profile "Cribbage"
:hp 0
:end-hp 121
:type :up}
{:profile "Star Realms"
:hp 50
:end-hp 0
:type :down}]))

(def ^:private default-settings
{:hp 50
:end-hp 0
:merge-events-threshold 1000
:type :down
{:merge-events-threshold 1000
:players [{:id "0" :color "#cf6666" :text-color "rgba(0, 0, 0, 0.87)"}
{:id "1" :color "#3797fa" :text-color "rgba(0, 0, 0, 0.87)"}]})

(defn- create-game [settings]
{:players (reduce
(fn [res player]
(assoc
res
(:id player)
(assoc player
:initial-amount (:hp settings)
:amount (:hp settings))))
{}
(:players settings))
:change-type :by-1 ;; :by-1 | :by-n
:events []})
{:id "1" :color "#3797fa" :text-color "rgba(0, 0, 0, 0.87)"}]
:profile "Star Realms"})

(defn- create-game [settings profiles]
(let [profile (profiles (:profile settings))]
{:players (reduce
(fn [res player]
(assoc
res
(:id player)
(assoc player
:initial-amount (:hp profile)
:amount (:hp profile))))
{}
(:players settings))
:change-type :by-1 ;; :by-1 | :by-n
:events []}))

(defn- reset-game [db]
(assoc db
:game (create-game (:settings db))
:game (create-game (:settings db) (:profiles db))
:page :game))

(re-frame/reg-event-fx
::init
[(re-frame/inject-cofx :app-version)
(re-frame/inject-cofx :settings)]
(fn [{:keys [app-version settings]} _]
{:db (reset-game {:settings (merge default-settings settings)
:app-info {:version app-version}})}))
(re-frame/inject-cofx :settings)
(re-frame/inject-cofx :profiles)]
(fn [{:keys [app-version settings profiles]} _]
(let [db (reset-game {:settings (merge default-settings settings)
:profiles (if (seq profiles) profiles default-profiles)
:app-info {:version app-version}})]
(cond-> {:db db}
(not= profiles (:profiles db)) (assoc :profiles (:profiles db))))))

(re-frame/reg-event-db
::reset
Expand All @@ -81,6 +112,23 @@
reset-game)
:settings settings}))

(re-frame/reg-event-fx
::save-profile
(fn [{:keys [db]} [_ profile]]
(let [db (-> db
(update :profiles assoc (:profile profile) profile)
(assoc :page :menu))]
{:db db
:profiles (:profiles db)})))

(re-frame/reg-event-fx
::delete-profile
(fn [{:keys [db]} [_ profile]]
(let [db (-> db
(update :profiles dissoc profile))]
{:db db
:profiles (:profiles db)})))

(re-frame/reg-event-fx
::increase-amount
[(re-frame/inject-cofx :time)]
Expand Down
23 changes: 20 additions & 3 deletions src/app/subs.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
(let [players (get-in db [:game :players])
player (get players id)
opponent-player (get players (opponent id))
{:keys [end-hp type]} (:settings db)]
{:keys [profile]} (:settings db)
{:keys [end-hp type]} (get-in db [:profiles profile])]
(assoc player :winner (case type
:down (<= (:amount opponent-player) end-hp)
:up (<= end-hp (:amount player)))))))
:up (<= end-hp (:amount player))
false)))))

(defn- events-close? [threshold e1 e2]
(and
Expand Down Expand Up @@ -60,13 +62,28 @@
(re-frame/reg-sub
::page
(fn [db _]
(:page db)))
(let [page (:page db)]
(if (= :game page)
(if (get-in db [:profiles (get-in db [:settings :profile])])
page
:menu)
page))))

(re-frame/reg-sub
::settings
(fn [db _]
(:settings db)))

(re-frame/reg-sub
::profiles
(fn [db _]
(sort-by :profile u/compare-ci (vals (:profiles db)))))

(re-frame/reg-sub
::profile
(fn [db [_ profile]]
(get-in db [:profiles profile] {})))

(re-frame/reg-sub
::app-info
(fn [db _]
Expand Down
17 changes: 16 additions & 1 deletion src/app/util.cljs
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
(ns app.util)
(ns app.util
(:require [clojure.string :as str]))

(defn index-by
[f coll]
(persistent!
(reduce
(fn [ret x]
(let [k (f x)]
(assoc! ret k x)))
(transient {}) coll)))

(defn compare-ci [x y]
(compare
(str/lower-case x)
(str/lower-case y)))

(defn merge-close
"Merges close inputs next to each other
Expand Down
Loading

0 comments on commit 57e8c2e

Please sign in to comment.