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

59 lines
1.1 KiB
Go
Raw Permalink Normal View History

2022-08-12 19:08:47 +00:00
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
2022-08-12 19:08:47 +00:00
}
func (serv *Server) CanAttraction(attraction string) bool {
serv.Mux.RLock()
defer serv.Mux.RUnlock()
2022-08-12 19:08:47 +00:00
_, b := serv.Attraction[attraction]
2022-08-12 19:08:47 +00:00
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
}
2022-08-12 19:08:47 +00:00
serv.Sessions[session] = io
serv.Attraction[attraction] = session
return true
2022-08-12 19:08:47 +00:00
}
func (serv *Server) HasSession(session string) (bool, socketio.Socket) {
serv.Mux.RLock()
defer serv.Mux.RUnlock()
2022-08-12 19:08:47 +00:00
io, b := serv.Sessions[session]
return b, io
}
func (serv *Server) RemoveSession(session, attraction string) {
serv.Mux.Lock()
2022-08-12 19:08:47 +00:00
delete(serv.Sessions, session)
delete(serv.Attraction, attraction)
serv.Mux.Unlock()
}