From 8e31de079306b288ff159c4e364a1716d8e47d25 Mon Sep 17 00:00:00 2001 From: Loic Guegan Date: Sun, 12 May 2019 14:01:05 +0200 Subject: [PATCH] Add admin interface --- server/api/api.lisp | 12 ++++++++++++ server/game/game.lisp | 6 ++++++ server/packages.lisp | 8 +++++--- server/server.lisp | 11 +++++++++++ 4 files changed, 34 insertions(+), 3 deletions(-) diff --git a/server/api/api.lisp b/server/api/api.lisp index a87cf6c..cea5d66 100644 --- a/server/api/api.lisp +++ b/server/api/api.lisp @@ -33,6 +33,7 @@ ((equal dir "down") (setf (getf p-request :direction) :down)) ((equal dir "left") (setf (getf p-request :direction) :left)) ((equal dir "right") (setf (getf p-request :direction) :right)))))) + ((equal type "admin")) ((not (equal type "new-game")) (error 'bad-request :msg "Unknown request type"))) p-request))) @@ -59,6 +60,16 @@ (to-json (append (list :type "state") (dump gm game-id))))))) +;;; TODO: debug this function +(defmethod handle-admin ((api api) data) + (with-slots (gm) api + (let* ((game-id (getf data :game-id)) + (cmd (getf data :cmd)) + (arg (getf data :arg)) + (game (get-game gm game-id))) + (cond + ((equal cmd "move") (admin game :move arg))))) + "Command executed!") (defmethod handle-request ((api api) request) ;; Catch request error and send it to the client @@ -68,6 +79,7 @@ (cond ((equal type "new-game") (handle-new-game api data)) ((equal type "update") (handle-update api data)) + ((equal type "admin") (handle-admin api data)) (t (format t "Unknow type")))) (error (condition) ; Send reason to the client (let ((reason (make-array 0 diff --git a/server/game/game.lisp b/server/game/game.lisp index 9ea2029..4aff595 100644 --- a/server/game/game.lisp +++ b/server/game/game.lisp @@ -21,6 +21,12 @@ :initform nil :accessor game-over))) +;;; Admin function +(defmethod admin ((g game) &key (move nil move-supplied-p)) + (with-slots (snake) g + (when move-supplied-p (setf (nth 0 snake) move)))) + + ;;; Class constructor to initialize the snake (defmethod initialize-instance :after ((g game) &key) (with-slots (snake initial-size initial-position) g diff --git a/server/packages.lisp b/server/packages.lisp index d512691..10a17b3 100644 --- a/server/packages.lisp +++ b/server/packages.lisp @@ -4,7 +4,8 @@ (:export #:game #:dump - #:refresh)) + #:refresh + #:admin)) (defpackage :remote-snake-server-api (:nicknames :rsapi) @@ -16,6 +17,7 @@ (defpackage :remote-snake-server (:nicknames :rss) - (:use :common-lisp :usocket :remote-snake-server-api :cl-strings) + (:use :common-lisp :usocket :remote-snake-server-api :cl-strings :jonathan) (:export - #:start)) + #:start + #:send-cmd)) diff --git a/server/server.lisp b/server/server.lisp index 3676121..618f91e 100644 --- a/server/server.lisp +++ b/server/server.lisp @@ -6,6 +6,17 @@ (defparameter *server-buffer* (make-array 10000 :element-type '(unsigned-byte 8) :initial-element 0)) +;;; TODO: debug this function +(defun send-cmd (host port game-id command arg) + (let ((socket (usocket:socket-connect host port :protocol :datagram)) + (request (list :type "admin" :cmd command :game-id game-id :arg arg)) + (buffer (make-array 500 :element-type '(unsigned-byte 8) :initial-element 0))) + (usocket:socket-send socket (string-downcase (to-json request)) 300) + (format t (babel:octets-to-string (usocket:socket-receive socket buffer 300))) + (force-output t) + (usocket:socket-close socket))) + + (defun handle-client (buffer) ; echo (declare (type (simple-array (unsigned-byte 8) *) buffer)) ; Seems to be to tell lisp which type is buffer