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() }