diff --git a/lisp/client/common-lisp/client.lisp b/lisp/client/common-lisp/client.lisp index b248d21..eb2ec19 100644 --- a/lisp/client/common-lisp/client.lisp +++ b/lisp/client/common-lisp/client.lisp @@ -7,11 +7,37 @@ (in-package :mmo-client) (defvar *client* (wsd:make-client "ws://localhost:12345")) +(defvar *current-user* (make-hash-table)) +(defvar *logged-in-users* (make-hash-table)) +;; +; Game loop info +; +; when start connection happens the server will generate a random id +; this is called the `login` request, this should only be seen by this user +; this user will then associate the id that is returned from the server with +; this users username/password and send out a `user-login` request +; the server should then associate that user get their info from the database +; and broadcast the updated list to all logged in users to all logged in users +; +; the `logged-in-users` key:generated user id, value: hash of user info +; user info is username, color, and position +; +; in the game loop it will loop over all the logged in users in the hash, +; if the user in the loop is the current user then ignore the info and only +; use local user info, otherwise it will draw all of the other cubes +;; (wsd:start-connection *client*) (wsd:on :message *client* (lambda (message) - (format t "~A~%" message))) + (setq json (com.inuoe.jzon:parse message)) + (setq cmd (gethash "cmd" json)) + (case + ((= cmd "login") (setf (gethash "uid" *user*) (gethash "uid" json))) ; handles self login + ((= cmd "user-login") (setf (gethash (gethash "uid" json) *logged-in-users*) (make-hash-table))) ; handles a new user logging on (not the user using this client!) + ((= cmd "logout") ()) ; log out another user + ((= cmd "move") ()) ; some user moved + (t (format t "Unknown message: ~A~%" message))))) ; some unknown thing happened (let* ((screen-width 800) (screen-height 450) @@ -24,24 +50,33 @@ :projection :camera-perspective)) (cube-screen-pos (vec 0.0 0.0))) + (defparameter *login* (make-hash-table)) + (setf (gethash "cmd" *login*) "user-login") + (setf (gethash "user" *login*) username) + (with-window (screen-width screen-height title) ;; (disable-cursor) (set-target-fps 60) ; Set our game to run at 60 FPS - (wsd:send *client* (com.inuoe.jzon:stringify (list :login username))) + (wsd:send *client* (com.inuoe.jzon:stringify *login*)) (loop until (window-should-close) ; detect window close button or ESC key do (update-camera camera :camera-third-person) + (defparameter *move* (make-hash-table)) + (setf (gethash "cmd" *move*) "move") + (setf (gethash "user" *move*) username) + (setf (gethash "pos" *move*) (camera3d-target camera)) + (when (is-key-down 87) - (wsd:send *client* (com.inuoe.jzon:stringify (list :move (camera3d-target camera))))) + (wsd:send *client* (com.inuoe.jzon:stringify *move*))) (when (is-key-down 65) - (wsd:send *client* (com.inuoe.jzon:stringify (list :move (camera3d-target camera))))) + (wsd:send *client* (com.inuoe.jzon:stringify *move*))) (when (is-key-down 83) - (wsd:send *client* (com.inuoe.jzon:stringify (list :move (camera3d-target camera))))) + (wsd:send *client* (com.inuoe.jzon:stringify *move*))) (when (is-key-down 68) - (wsd:send *client* (com.inuoe.jzon:stringify (list :move (camera3d-target camera))))) + (wsd:send *client* (com.inuoe.jzon:stringify *move*))) (setf cube-screen-pos (get-world-to-screen (v+ (camera3d-target camera) (vec 0 1.0 0)) camera)) (with-drawing @@ -51,7 +86,6 @@ (draw-cube-wires (camera3d-target camera) 1.0 1.0 1.0 :maroon) (draw-grid 20 1.0)) (draw-text username (- (floor (vx cube-screen-pos)) (floor (measure-text username 20) 2)) - (floor (vy cube-screen-pos) ) 20 :black)))) - (wsd:send *client* (com.inuoe.jzon:stringify (list :logout username)))) + (floor (vy cube-screen-pos) ) 20 :black))))) (wsd:close-connection *client*) diff --git a/lisp/server/server.lisp b/lisp/server/server.lisp index 71d82bd..1b2e6a1 100644 --- a/lisp/server/server.lisp +++ b/lisp/server/server.lisp @@ -10,21 +10,19 @@ (defvar *connections* (make-hash-table)) (defun handle-new-connection (con) + (setq *user* (format nil "{\"cmd\":\"login\", \"uid\":\"~a\"}" (random 100000))) (setf (gethash con *connections*) - (format nil "user-~a" (random 100000)))) + *user*) + (websocket-driver:send con *user*)) -(defun broadcast-to-room (connection message) - (let ((message (format nil "~a" - ;(gethash connection *connections*) - message))) - - (format t "~A~%" (com.inuoe.jzon:parse message)) +(defun broadcast-to-room (connection json) + (setq message (com.inuoe.jzon:parse json)) (loop :for con :being :the :hash-key :of *connections* :do - (websocket-driver:send con message)))) + (websocket-driver:send con json))) (defun handle-close-connection (connection) - (let ((message (format nil " .... ~a has left." + (let ((message (format nil "{\"cmd\":\"logout\", \"user\":\"~a\"}" (gethash connection *connections*)))) (remhash connection *connections*) (loop :for con :being :the :hash-key :of *connections* :do