Compare commits

...

5 Commits

Author SHA1 Message Date
Sebastian Crane 6cce43f137 Move processing out of clojure.data.json/read call
This commit removes from the call to clojure.data.json/read the
conversion of the serialised list of players into a Clojure set, and
adds an independent function to do this task instead.

Since the structure of the serialised data is known in advance, the
recursive characteristics of clojure.data.json are not needed.

Resolves issue LibreGaming/matchbot#4

Signed-off-by: Sebastian Crane <seabass-labrax@gmx.com>
2022-03-28 21:52:52 +01:00
Sebastian Crane 91cbdb69da Simplify the system start and restart functions
This commit changes the binding name of system/start's argument to
_ (since it is currently not used within the function), and simplifies
the system/restart function by not swapping the system Var's content
with nil before filling it with the new-created system state.

Eventually, these changes will be reverted when the initialisation of
the system is separated from the starting of that system, which will
mean that system/start will read its argument and that system/restart
will have to reinitialise the system state before restarting it.

Signed-off-by: Sebastian Crane <seabass-labrax@gmx.com>
2022-03-24 21:51:06 +00:00
Sebastian Crane fecd09c656 Remove unnecessary system/system function
A nil value acts as an empty map wherever a map value is expected. For
this reason, this commit removes the system/system function (which
returns an empty map), since a call to it can safely be replaced with a
nil value.

Signed-off-by: Sebastian Crane <seabass-labrax@gmx.com>
2022-03-22 22:51:59 +00:00
Sebastian Crane 0abd793fe7 Use a Var instead of an Atom for system state
The Atom's swap! function may run its provided function multiple
times. Because of this, using an Atom for the system state can
occasionally result in duplicate TCP connections being opened when the
system is started. These connections remain open until they
timeout (this is typically 5 minutes for IRC).

This commit changes the container for the system state from an Atom to a
Var, whose alter-var-root function (analogous to swap!) does not have
this issue.

Signed-off-by: Sebastian Crane <seabass-labrax@gmx.com>
2022-03-22 22:47:00 +00:00
Sebastian Crane 56094478db Remove use of state container in main function
Since the main function will only ever start and stop one system, this
commit simplifies the main function to use a single lexical binding for
the system state rather than creating an Atom.

Signed-off-by: Sebastian Crane <seabass-labrax@gmx.com>
2022-03-22 22:38:30 +00:00
2 changed files with 25 additions and 32 deletions

View File

@ -66,18 +66,18 @@ Since `matchbot` uses Clojure's [tools.deps library](https://clojure.org/guides/
You can create, start and stop an instance of the chatbot process with the functions in the `system` namespace:
``` clojure
;; creating a new instance
(def my-instance (atom (system/system)))
;; creating a new instance - an empty Var
(def my-instance nil)
;; starting the instance
(swap! my-instance system/start)
(alter-var-root #'my-instance system/start)
;; restarting the instance
(system/restart my-instance)
(system/restart #'my-instance)
;; stopping and resetting the instance
(swap! my-instance stop)
(reset! my-instance (system/system))
(alter-var-root #'my-instance system/stop)
(alter-var-root #'my-instance (constantly nil))
```
Once you are familiar with nREPL, you can additionally use [tools.namespace.repl](https://github.com/clojure/tools.namespace) to make reevaluating (reloading) your changes easier:

View File

@ -6,21 +6,21 @@
[clojure.data.json :as json]
[clj-yaml.core :as yaml]))
(defn json-data-reader [key value]
(if (= key :games)
(into (empty value)
(map #(hash-map (first %)
(set (second %)))
value))
value))
(defn setify-vals [x]
(reduce #(assoc %1
(first %2)
(set (second %2)))
{} x))
(defn process-json [x]
(update x :games setify-vals))
(defn load-state [f]
(try
(with-open [datafile (clojure.java.io/reader f)]
(json/read datafile
:value-fn json-data-reader
:key-fn keyword))
(catch Exception e nil)))
(process-json
(try
(with-open [datafile (clojure.java.io/reader f)]
(json/read datafile :key-fn keyword))
(catch Exception e nil))))
(defn save-state [f data]
(try
@ -34,12 +34,7 @@
(yaml/parse-stream datafile))
(catch Exception e nil)))
(defn system []
{:config nil
:state nil
:irc nil})
(defn start [system]
(defn start [_]
(let [config (load-config "config.yaml")
state (atom (load-state (:data-file config)))
irc (irc/new-irc-connection state config)]
@ -55,14 +50,12 @@
(deref (:state system)))
(irclj.core/quit (system :irc))))
(defn restart [system-atom]
(defn restart [system-var]
(do
(swap! system-atom stop)
(reset! system-atom (system))
(swap! system-atom start)))
(stop (deref system-var))
(alter-var-root system-var start)))
(defn -main [& args]
(let [main-system (atom (system/system))]
(swap! main-system system/start)
(let [main-system (system/start nil)]
(.addShutdownHook (Runtime/getRuntime)
(Thread. (partial swap! main-system stop)))))
(Thread. #(stop main-system)))))