package user import ( "fmt" "github.com/Mindgamesnl/socketio" "github.com/ParadoxPixel/ThemePark-Websocket/data" "github.com/ParadoxPixel/ThemePark-Websocket/objects" "log" ) var debug bool func Load(namespace socketio.Namespace, b bool) { debug = b namespace.OnConnect(func(so socketio.Socket) { clientType := so.GetQuery().Get("type") if clientType == "server" { serverLogin(so) return } else if clientType == "client" { clientLogin(so) return } else { _ = so.Close() } }) namespace.OnDisconnect(func(so socketio.Socket) { clientType := so.GetQuery().Get("type") var session string if clientType == "server" { session = so.GetQuery().Get("id") obj := data.GetServer(session) if obj == nil { return } data.RemoveServer(session) if debug { log.Println( "Server with id:", so.GetQuery().Get("id"), "disconnected", ) } for _, user := range obj.Sessions { _ = user.Emit("close", "SERVER_ERROR") _ = user.Close() } } else if clientType == "client" { session = so.GetQuery().Get("token") obj := data.GetSession(session) if obj == nil { return } ok, server := data.HasServer(obj.ID) if !ok { return } if debug { log.Println( "Client disconnected to id:", so.GetQuery().Get("control_id"), "with token:", so.GetQuery().Get("token"), "to attraction:", so.GetQuery().Get("attraction"), ) } data.RemoveSession(session) server.RemoveSession(session, so.GetQuery().Get("attraction")) _ = server.Socket.Emit("data", "{\"channel\":\"SERVER_IN_UNREGISTER_CLIENT\",\"data\":{\"payload\":{\"uuid\":\""+obj.UUID+"\"},\"type\":\"ClientDisconnectPayload\"}}") } }) namespace.OnError(func(so socketio.Socket, err ...interface{}) { fmt.Println(err) }) } func serverLogin(so socketio.Socket) { id := so.GetQuery().Get("id") if !data.CanServer(id) { if debug { log.Println( "Server with id:", so.GetQuery().Get("id"), "already connected", ) } _ = so.Close() return } data.AddServer(&objects.Server{ ID: id, Sessions: make(map[string]socketio.Socket), Attraction: make(map[string]string), Socket: so, }) if debug { log.Println( "Server with id:", so.GetQuery().Get("id"), "connected", ) } } func clientLogin(so socketio.Socket) { if !data.CanSession(so.GetQuery().Get("token")) { _ = so.Close() return } ok, server := data.HasServer(so.GetQuery().Get("control_id")) if !ok { if debug { log.Println( "No server with id:", so.GetQuery().Get("control_id"), "for token:", so.GetQuery().Get("token"), ) } _ = so.Emit("close", "SERVER_ERROR") _ = so.Close() return } ok = server.CanAttraction(so.GetQuery().Get("attraction")) if !ok { if debug { log.Println( "Server with id:", so.GetQuery().Get("control_id"), "already has operator for attraction:", so.GetQuery().Get("attraction"), ) } _ = so.Emit("close", "AUTHENTICATION_ERROR") _ = so.Close() return } data.AddSession(&objects.Session{ Token: so.GetQuery().Get("token"), UUID: so.GetQuery().Get("uuid"), ID: so.GetQuery().Get("control_id"), Attraction: so.GetQuery().Get("attraction"), }) server.AddSession(so.GetQuery().Get("token"), so.GetQuery().Get("attraction"), so) if debug { log.Println( "Client connected to id:", so.GetQuery().Get("control_id"), "with token:", so.GetQuery().Get("token"), "to attraction:", so.GetQuery().Get("attraction"), ) } err := server.Socket.Emit("data", "{\"channel\":\"SERVER_IN_REGISTER_CLIENT\",\"data\":{\"payload\":{\"uuid\":\""+so.GetQuery().Get("uuid")+"\",\"token\":\""+so.GetQuery().Get("token")+"\",\"attraction_id\":\""+so.GetQuery().Get("attraction")+"\"},\"type\":\"ClientConnectPayload\"}}") if err != nil { log.Println(err) } }