To get the word-collection into your re-frame database, you'll need to follow a series of steps to properly define your re-frame events, define the initial database structure, and dispatch an event to populate it with the word collection.
Here's a breakdown of how you might proceed:
Step 1: Define the default-db
In spelling-bee.db namespace, define the default database schema. This would typically look something like:
Step 3: Define functions to manipulate the word set
The provision for get-letter-collection and find-common-letter functions is appropriate except that they might not do what their names suggest. For instance, the get-letter-collection would return a set of all unique letters from the concatenated words, which might not be the intention. Here's an improved version to get a set of unique characters from the collection:
Based on the additional code you've provided, it looks like you are aiming to have a re-frame database with words, and you've already set up a subscription to access those words in your application. Assuming you want the words from the word-collection set to be part of your :words db state, let's put all the pieces together.
Here's what you should do to ensure that your word-collection is stored in your re-frame application's database:
Step 1: Provide an event to initialize the database with default data
In your events namespace (spelling-bee.events), make sure you have an event to initialize your database with the default data:
Note: You don't have to define ::name in the subscriptions, because it's not present in the database structure you've defined.
Step 5: Initialize the database when the app starts
Make sure that the app dispatches the ::initialize-db event when the application loads to set up the default state:
clojure
User
Here is my code so far. I am trying to reimplement a spelling bee game with re-frame where valid answers are combined from any number of 7 letters. Please assist me in adding the core features.
;; Later this can be substituted with a database call to pull a list of words.
(def word-collection #{“babyproof” “afar” “affray”
“afro” “arbor” “array”
“arroyo” “barb” “barf”
“boar” “boor” “bray”
“farro” “fora” “foray”
“fray” “parry” “poor”
“pray” “proof” “prop”
“pyro” “roar” “roof”
“ropy”})
(defn main-panel []
(let [name (rf/subscribe [::name])
words (rf/subscribe [::words])
database (rf/subscribe [::dbdb])]
[:div
[:h1
"Hello from " @name]
[spawn-words-button]
[:h2
“Here are the words you have found:”]
[:p "test: here are the answer words: " @words]
[:p "debug: db: " @database]]))
To continue building on your Spelling Bee game with re-frame, let's make sure to define core functionality, such as checking valid words, selecting the central letter, and filtering selectable words based on the passed collection of letters.
Here is the guidance on how to add these features:
Update get-unique-letter-collection
The function get-unique-letter-collection should return all unique letters in the word collection, not just a simple set of all letters from a concatenated string, to ensure it's picking unique letters from individual words:
find-common-letter is intended to find a common letter that must appear in every valid answer. For Spelling Bee, one letter is typically central and must appear in every word. Since find-common-letter uses set/intersection, it would only work if all words share common letters. Spelling Bee rules typically select one letter from the pool to be the "mandatory" letter, but if you really want to find common letters, you'll need all words to share them. Here's a simplified example of selecting a mandatory letter at random to be part of every word:
You also need functionality to check if a word is valid, i.e., it can be formed using a combination of the 7 letters, with at least one occurrence of the common letter:
;; Later this can be substituted with a database call to pull a list of words.
(def word-collection #{"babyproof" "afar" "affray"
"afro" "arbor" "array"
"arroyo" "barb" "barf"
"boar" "boor" "bray"
"farro" "fora" "foray"
"fray" "parry" "poor"
"pray" "proof" "prop"
"pyro" "roar" "roof"
"ropy"})
(defn find-common-letter [word-set]
(reduce
set/intersection
(map set (seq word-set))))
(defn word-validity-case [word letters common-letter]
(cond
(contains? word-collection 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
To improve the visual appeal of your Spelling Bee app, let’s incorporate some basic CSS styling. We'll assume you have a basic working knowledge of CSS and can embed these styles directly into your spelling-bee.core namespace or attach an external stylesheet. Below is an example of how to add inline styles using Reagent's hiccup syntax:
Add CSS styles
Add a map of CSS styles that you'll apply to elements in your Reagent components:
Apply the styles to your Reagent components. For example, let’s update the main-panel to include these styles:
clojure
User
How do I set a background image, and what folder do I put it in in this kind of shadowcljs re-frame project?
Assistant
To set a background image in a shadow-cljs re-frame project, you'll want to follow these general steps:
Place the Image in the Public Directory:
Typically, you would create a directory called public at the root level of your project where shadow-cljs.edn is located. Within that, you could have an images directory to organize your image files.
Place your background image, let's say background.jpg, inside the public/images directory. The public directory is usually set to be served statically by the web server, meaning every file in public is accessible via URLs relative to the project's root URL.
Reference the Image in CSS:
Use a CSS rule that references the image using a path relative to the project's root URL. In your CSS (or inline styles if you use inline styles), you would set it like this:
css
body {
background-image: url('/images/background.jpg');
background-size: cover; /* or `contain`, depending on what you want */
background-position: center center;
background-repeat: no-repeat;
}
If you are inserting the styles directly into your Reagent component using inline styles, it would look like this:
It looks like you're receiving a 404 Not Found error when the browser tries to load your background image. This typically means that the web server can't find the image at the specified URL.
The cause of this issue could be one or more of the following:
Incorrect Path in the Public Directory:
Double-check that the public/images folder actually contains the tokimemobgscroll.gif file. Verify the file name and extension are correct and match the URL's case sensitivity.
Incorrect Web Server Configuration:
Ensure that shadow-cljs is configured to correctly serve files from the public directory. Your shadow-cljs.edn should have a :dev-http key as follows:
clojure
{:source-paths ["src"]
:dependencies [...]
:dev-http {8280 "public"} ; Make sure that this port matches the one you are using (8280 in your case)
...}
Port Mismatch:
Confirm that you're using the correct port number. In your console log, it shows http://localhost:8280/images/tokimemobgscroll.gif. Make sure the port number 8280 is intended for shadow-cljs and that it matches the configuration in the shadow-cljs.edn.
Caching Issues:
Sometimes browsers cache assets and don't reflect changes. To avoid this, you can perform a hard reload of the page (Ctrl + F5 on most browsers) or open a new incognito/private window to bypass the cache.
Inline Styles with Reagent:
If you are using Reagent, you need to ensure that styles are applied properly as a map of styles, not using a use-style function, unless you have defined this. The hiccup syntax should be:
clojure
[:div {:style body-background}
[:div {:style main-style} ; assuming main-style is another defined map of styles
[:h1 "Hello, " @name]
...]]
Public Folder Serving in Production:
If you're experiencing this issue in production, make sure that the production web server (like Nginx, Apache, etc.) is configured to serve static files from the public directory just as does in development.
Not yet included in a published release.
Step 5: Dispatch the event to update the database
To trigger the update and add word-collection to the re-frame database, dispatch the ::set-words-and-collections event, passing in the word-collection:
Make sure to put this dispatch call somewhere where it will be executed properly, such as in your application's initialization logic.
Note
Please verify the logic in both get-letter-collection and find-common-letter functions to ensure they meet your requirements, as the logic provided is based on the names given to those functions, not necessarily on what you might want them to do.
Also, ensure that you are subscribing to the correct keys when you use them in your application views or elsewhere. The keys must match those that you're using in the assoc function of your event handler.
(rf/dispatch [::initialize-db])
Step 6: Dispatch the event to update the :words
Once you have initialized the default database and set up your event, dispatch the ::set-words event with the word-collection:
clojure
(def word-collection #{"babyproof" "afar" "affray" ;;... and so on})
;; When ready to populate the words in your re-frame app
(rf/dispatch [::set-words word-collection])
Put the dispatch call in your application's initialization logic after the ::initialize-db dispatch to ensure the database has been set up before trying to update the :words.
After these steps are followed, your word-collection will be stored in the :words key of the re-frame application database, and you can use the subscription ::words to access this data in your application UI.
Remember that it's important to dispatch events in the correct sequence and to ensure the event handlers correctly update the database. Your application components can then use the subscriptions you've set up to reactively display this data in your UI.
Finally, your main-panel might present the UI elements to interact with the game:
clojure
(defn main-panel []
(let [name (rf/subscribe [::name])
common-letter (rf/subscribe [::common-letter])
letters (rf/subscribe [::letters])
words (rf/subscribe [::words])]
[:div
[:h1 "Hello from " @name]
[spawn-words-button]
[:h2 "Use these letters to form words"]
[:div "Common Letter: " @common-letter]
[:div "Available Letters: " (clojure.string/join ", " @letters)]
[:div [input-word]]
[:h2 "Here are the words you have found:"]
[:ul (for [word @words] [:li word])]]))
Make sure once you've got the core functionality working, you focus on refining the user experience with styling, interaction feedback, and additional game features like score tracking, timer, etc.
(rf/reg-event-db ::submit-word
(fn [db [_ word]]
(let [letters (:letters db)
common-letter (:common-letter db)
point-val (point-formula word letters)]
(case (word-validity-case word 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.")))))
;---------- main page elements ----------
(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 word-collection])}
"Get Letters!"])))
(defn text-input
"Field for the user to input a word of their choosing."
[]
(let [input-value (rf/subscribe [::current-input])]
[:input {:type "text"
:placeholder "Type here!"
:value @input-value
:on-change #(rf/dispatch [::update-current-input (-> % .-target .-value)])}]))
(defn shuffle-order-button
"Shuffles the order of the letters displayed."
[display-letters]
[:button {:on-click #(rf/dispatch [::shuffle-letter-order display-letters])}
"Shuffle letters"])
(defn found-words-display []
(let [found-words (rf/subscribe [::found-words])]
[:div {:style (:found-words styles)}
[:h3 "Here are the words you have found:"]
[:p (str/join ", " (sort @found-words))]]))
This is a starting point that can be improved further by incorporating more advanced styles, animations, or even using a CSS preprocessor such as Sass or Less. You might also want to consider using a CSS framework like Bootstrap or Tailwind CSS for a quicker and more uniform design.
Remember that inline styles can become cumbersome to maintain for larger applications, so you may want to extract the styles into a separate CSS file or use more sophisticated styling solutions like CSS-in-JS libraries if required.
Configure shadow-cljs to Serve Static Files:
Assuming you are using shadow-cljs for development and not a custom server, shadow-cljs should serve files from the public directory by default. If it is not configured to do so, you might need to configure :dev-http in your shadow-cljs.edn file to specify where static files should be served from. For example:
The above configuration tells shadow-cljs to serve static files from the public folder at http://localhost:8080/ when in development mode.
By following these steps, your app will have a background image applied on the main-panel or any other component, depending on where you apply the :body-background style. Remember that path handling can vary based on the server and deployment setup, so adjust your project's directory structure and paths as needed for production builds.
shadow-cljs
CSS Content Security Policy:
Ensure there's no Content Security Policy (CSP) that is blocking the loading of background images. You can check this in the browser's console for any CSP-related errors.
After you've checked these possibilities, try to access the image directly via the browser by entering the expected URL to see if it can be loaded successfully. This can help pinpoint whether the issue is with the file path, the server configuration, or possibly a problem with the image file itself.