diff --git a/server/api/api.lisp b/server/api/api.lisp new file mode 100644 index 0000000..186076c --- /dev/null +++ b/server/api/api.lisp @@ -0,0 +1,9 @@ +(in-package :remote-snake-server-api) + + + +(defclass api () + ((gm + :initform (make-instance 'game-manager)))) + + diff --git a/server/api/game-manager.lisp b/server/api/game-manager.lisp new file mode 100644 index 0000000..e5355d1 --- /dev/null +++ b/server/api/game-manager.lisp @@ -0,0 +1,41 @@ +(in-package :remote-snake-server-api) + +(defclass game-manager () + ((games + :initform '()) + (next-id + :initform 0))) + +(defgeneric create-game (gm) + (:documentation "Create a game in the Game Manager.")) + +(defmethod print-object :around ((gm game-manager) stream) + (with-slots (games) gm + (format stream "Games: ~a" games))) + +(defgeneric delete-game (gm game-id) + (:documentation "Delete a game from Game Manager.")) + +(defgeneric get-game (gm game-id) + (:documentation "Fetch a game from Game Manager.")) + +(defmethod delete-game ((gm game-manager) game-id) + (get-game gm game-id) ; First try to get the game (if not found, an error will be raised) + (with-slots (games) gm + (setf games (remove-if #'(lambda (entry) (eql game-id (getf entry :id))) games)))) + +(defmethod get-game ((gm game-manager) game-id) + (with-slots (games) gm + (let ((game (remove-if-not #'(lambda (entry) (eql game-id (getf entry :id))) games))) + (when (< (length game) 1) (error "Game ~a not found !" game-id)) + (getf (first game) :game)))) + +;;; Create a new game in gm and return its id +(defmethod create-game ((gm game-manager)) + (let ((new-game-id nil)) + (with-slots (games next-id) gm + (push (list :id next-id :game (make-instance 'game)) games) + (setf new-game-id next-id) + (incf next-id)) + new-game-id)) + diff --git a/server/packages.lisp b/server/packages.lisp index 107f3e6..788083d 100644 --- a/server/packages.lisp +++ b/server/packages.lisp @@ -3,3 +3,9 @@ (:use :common-lisp) (:export #:game)) + +(defpackage :remote-snake-server-api + (:nicknames :rsapi) + (:use :common-lisp :jonathan) + (:export + #:api)) diff --git a/server/remote-snake-server.asd b/server/remote-snake-server.asd index 3a4d813..0dc53fc 100644 --- a/server/remote-snake-server.asd +++ b/server/remote-snake-server.asd @@ -9,4 +9,9 @@ :depends-on ("packages") :components ((:file "utils") (:file "game" - :depends-on ("utils")))))) + :depends-on ("utils")))) + (:module "api" + :depends-on ("game") + :components ((:file "game-manager") + (:file "api" + :depends-on ("game-manager"))))))