fixed server

This commit is contained in:
zongor 2024-01-04 08:45:54 -05:00
parent 29a333323a
commit d648f49c68
1 changed files with 28 additions and 12 deletions

View File

@ -1,29 +1,45 @@
(load "~/quicklisp/setup.lisp") (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 (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) (in-package :mmo-server)
(defvar *connections* (make-hash-table)) (defvar *connections* (make-hash-table))
(defun handle-new-connection (con) (defun handle-new-connection (con)
(setq *user* (format nil "{\"cmd\":\"login\", \"uid\":\"~a\"}" (random 100000))) (let ((user (make-hash-table)))
(setf (gethash con *connections*) (setf (gethash "uid" user) (random 100000))
*user*) (setf (gethash "cmd" user) "login")
(websocket-driver:send con *user*)) (websocket-driver:send con (com.inuoe.jzon:stringify user))
(remhash "cmd" user)
(setf (gethash con *connections*) user)))
(defun broadcast-to-room (connection json) (defun broadcast-to-room (connection message)
(setq message (com.inuoe.jzon:parse json)) (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 (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) (defun handle-close-connection (connection)
(let ((message (format nil "{\"cmd\":\"logout\", \"user\":\"~a\"}" (let ((message (format nil "{\"cmd\":\"logout\", \"uid\":\"~a\"}"
(gethash connection *connections*)))) (gethash "uid" (gethash connection *connections*)))))
(remhash connection *connections*) (remhash connection *connections*)
(loop :for con :being :the :hash-key :of *connections* :do (loop :for con :being :the :hash-key :of *connections* :do
(websocket-driver:send con message)))) (websocket-driver:send con message))))