3
0
Fork 0
This repository has been archived on 2024-11-14. You can view files and clone it, but cannot push or open issues or pull requests.
ThemeParkPlus-Socket/objects/server.go
Thomas van Weert 9edcb83fc4 CHG: refactor
CHG: use concurrent map instead of regular map with lock
DEL: global session control, moved to per server only
DEL: unused command system
2023-02-16 20:04:17 +01:00

58 lines
1.1 KiB
Go
Executable file

package objects
import (
"github.com/Mindgamesnl/socketio"
"sync"
)
type Server struct {
Socket socketio.Socket
ID string
Sessions map[string]socketio.Socket
Attraction map[string]string
Mux *sync.RWMutex
}
func (serv *Server) CanAttraction(attraction string) bool {
serv.Mux.RLock()
defer serv.Mux.RUnlock()
_, b := serv.Attraction[attraction]
return !b
}
func (serv *Server) AddSession(session, attraction string, io socketio.Socket) bool {
serv.Mux.Lock()
defer serv.Mux.Unlock()
// Check if session in use
if _, b := serv.Sessions[session]; b {
return false
}
// Check if attraction is already controlled
if _, b := serv.Attraction[attraction]; b {
return false
}
serv.Sessions[session] = io
serv.Attraction[attraction] = session
return true
}
func (serv *Server) HasSession(session string) (bool, socketio.Socket) {
serv.Mux.RLock()
defer serv.Mux.RUnlock()
io, b := serv.Sessions[session]
return b, io
}
func (serv *Server) RemoveSession(session, attraction string) {
serv.Mux.Lock()
delete(serv.Sessions, session)
delete(serv.Attraction, attraction)
serv.Mux.Unlock()
}