From d648f49c68c2f37121450c39438f4bc0b60bd192 Mon Sep 17 00:00:00 2001 From: zongor Date: Thu, 4 Jan 2024 08:45:54 -0500 Subject: [PATCH] fixed server --- lisp/server/server.lisp | 40 ++++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/lisp/server/server.lisp b/lisp/server/server.lisp index 1b2e6a1..8155be2 100644 --- a/lisp/server/server.lisp +++ b/lisp/server/server.lisp @@ -1,29 +1,45 @@ (load "~/quicklisp/setup.lisp") -(ql:quickload '(:clack :websocket-driver :cl-bcrypt :datafly :sxql :alexandria :com.inuoe.jzon)) +(ql:quickload '(:clack :websocket-driver :cl-bcrypt :datafly + :sxql :alexandria :com.inuoe.jzon)) (defpackage :mmo-server - (:use :common-lisp :clack :websocket-driver :cl-bcrypt :datafly :sxql :alexandria :com.inuoe.jzon)) + (:use :common-lisp :clack :websocket-driver :cl-bcrypt :datafly + :sxql :alexandria :com.inuoe.jzon)) (in-package :mmo-server) - (defvar *connections* (make-hash-table)) (defun handle-new-connection (con) - (setq *user* (format nil "{\"cmd\":\"login\", \"uid\":\"~a\"}" (random 100000))) - (setf (gethash con *connections*) - *user*) - (websocket-driver:send con *user*)) + (let ((user (make-hash-table))) + (setf (gethash "uid" user) (random 100000)) + (setf (gethash "cmd" user) "login") + (websocket-driver:send con (com.inuoe.jzon:stringify user)) + (remhash "cmd" user) + (setf (gethash con *connections*) user))) -(defun broadcast-to-room (connection json) - (setq message (com.inuoe.jzon:parse json)) +(defun broadcast-to-room (connection message) + (let* ((json (com.inuoe.jzon:parse message)) + (user (gethash connection *connections*)) + (cmd (gethash "cmd" json))) + (cond + ((equalp cmd "user-login") + (let ((copy (alexandria:copy-hash-table *connections*))) + (remhash connection copy) + (websocket-driver:send connection (com.inuoe.jzon:stringify copy)))) + ((equalp cmd "move") + (setf (gethash "x" user) (gethash "x" json)) + (setf (gethash "y" user) (gethash "y" json)) + (setf (gethash connection *connections*) user)) + (t + (format t "Unknown message: ~A~%" message))) (loop :for con :being :the :hash-key :of *connections* :do - (websocket-driver:send con json))) + (websocket-driver:send con message)))) (defun handle-close-connection (connection) - (let ((message (format nil "{\"cmd\":\"logout\", \"user\":\"~a\"}" - (gethash connection *connections*)))) + (let ((message (format nil "{\"cmd\":\"logout\", \"uid\":\"~a\"}" + (gethash "uid" (gethash connection *connections*))))) (remhash connection *connections*) (loop :for con :being :the :hash-key :of *connections* :do (websocket-driver:send con message))))