(defn spawn-words-button
"Starts the game with a preset set of words."
[]
(let [game-started (rf/subscribe [::game-started])]
(when-not @game-started
[:button
{:on-click #(rf/dispatch [::set-words-and-letters words/word-collection])
:style button-style}
"Get Letters!"])))
(defn text-input
"Field for the user to input a word of their choosing."
[]
(let [;letters (rf/subscribe [::letters])
;common-letter (rf/subscribe [::common-letter])
input-value (rf/subscribe [::current-input])]
[:input {:type "text"
:placeholder "Type here!"
:value @input-value
:on-change #(rf/dispatch [::set-current-input (-> % .-target .-value)])
:style input-style}]))
(defn shuffle-order-button!
"Shuffles the order of the letters displayed."
[display-letters]
[:button {:on-click #(rf/dispatch [::shuffle-letter-order display-letters])
:style button-style}
"Shuffle letters"])
(defn init []
(install-global-key-listeners) ; listen for keypress events
(rf/dispatch-sync [::initialize-db]) ; get re-frame atom initialized
(stylefy/init {:dom (stylefy-reagent/init)}) ; set up css
(dev-setup)
(mount-root))
(defn find-common-letter [word-set]
(reduce
set/intersection
(map set (seq word-set))))
(defn validate-word
"Checks the given word against the current word list and letter set to see if it is valid. Gives the following keywords as a result.
:submit-ok :too-short :invalid :no-common :not-in-list :other"
[word word-list letters common-letter]
(cond
(contains? word-list word) :submit-ok ; first check if the word is in the word collection
(> 4 (count (seq word))) :too-short ; check length, notify if less than 3 letters
(not (every? letters (set word))) :invalid ; check if every letter in the word is in letters set
(not (contains? (set word) (first common-letter))) :no-common ; if it does not contain the common letter
(contains? (set word) (first common-letter)) :not-in-list ; then check if the word at least contains common letter
:else :other)) ; generic if it somehow manages to not match one of the above
(rf/reg-event-db ::submit-word
(fn [db [_ word]]
(let [letters (:letters db)
common-letter (:common-letter db)
words (:words db)
current-word (:current-word db)
point-val (calculate-points word letters)
;get-message #(assoc db :message)
]
(case (validate-word word words letters common-letter)
:submit-ok (if (contains? (:found-words db) word)
(assoc db :message "You've already found that word!")
(-> db
(update :found-words conj word)
(update :score + point-val)
(assoc :message (str "Great job! You found " word ", worth a score of " point-val "!")))) ; add the valid word to found words
:too-short (assoc db :message "Only words with 4 letters or more count.")
:not-in-list (assoc db :message (str "Sorry, " word " isn't in the word list today."))
:no-common (assoc db :message "Nice try, but the word needs to contain the common letter.")
:invalid (assoc db :message "All letters in the word must be from the given letter set.")
:other (assoc db :message "Try again.")))))
; use reg-event-fx to dispatch further event to clear input
It was working before I moved the events and words into their own namespaces.
When you refactor a part of your ClojureScript code that involves moving functions and state into separate namespaces, it's important to make sure all the references are updated accordingly. Here are some steps to assure that splitting the events and words into their own namespaces is seamless:
Check Namespace Declarations: Make sure that the spelling-bee.events and spelling-bee.words namespaces have correct ns declarations. In your code snippet, the spelling-bee.events namespace is missing an ns declaration, so be sure to include that at the top of the file.
Proper Requiring: Ensure that you've updated all the requires in the files where you use spelling-bee.events or spelling-bee.words. For example, every event keyword like ::append-current-input should now be events/append-current-input unless you have aliased the spelling-bee.events namespace in which case you can use that alias.
Update Event Keywords: Convert the namespace-qualified keywords according to their new namespace. An example: replace ::append-current-input with :spelling-bee.events/append-current-input or with events/append-current-input if you've aliased the namespace as events.
Validate Execution Order: Check the execution order of your ClojureScript code. Make sure that all related subscriptions and event handlers are defined after you've required the namespaces that contain them.
Check the Events Namespace: Make sure that you're using reg-event-db and reg-event-fx correctly in your spelling-bee.events namespace.
Check for Missed References: Go through the code and look for any place where you might be using keywords that need to be updated with the new namespace. This might include dispatching events, referencing subscriptions, or any other use of the keywords you've moved.
Test the Application: After refactoring, test each feature of your application to make sure that there are no issues. Look out for any warnings or errors in your browser's JavaScript console, as they can provide useful information about what might be going wrong.
Update Event Handlers: Verify that all event handling functions have been correctly moved and that there are no lingering copies in the original namespace causing confusion.
Ensure Database Keys Match: If your event handlers and subscriptions are working with the app database (db), make sure that all keys used match the ones defined in the new namespaces.
A simple debugging step after a codebase restructuring could involve adding println statements at key points to ensure the code's execution is as expected.
Lastly, it's essential that you ensure that your loading order in shadow-cljs is correct to avoid issues due to namespaces or symbols not being defined when they're needed. Make sure that the 'spelling-bee.events' and 'spelling-bee.words' namespaces do not have code that runs at the top level which could be executed before they are supposed to. Avoid side-effects at the top-level of namespaces.