diff --git a/channel.go b/channel.go index 212044b..a9c6837 100644 --- a/channel.go +++ b/channel.go @@ -13,7 +13,7 @@ import ( ) type Channel struct { - session *Service + session *Session id uint32 name string done chan struct{} @@ -90,7 +90,7 @@ func (ch *Channel) CompleteMessage(words []string, i int) []string { return words } -func generateChannels(s *Service, amount int) []cchat.Server { +func generateChannels(s *Session, amount int) []cchat.Server { var channels = make([]cchat.Server, amount) for i := range channels { channels[i] = &Channel{ diff --git a/go.mod b/go.mod index 056bd38..22bdeaa 100644 --- a/go.mod +++ b/go.mod @@ -4,5 +4,5 @@ go 1.14 require ( github.com/Pallinder/go-randomdata v1.2.0 - github.com/diamondburned/cchat v0.0.7 + github.com/diamondburned/cchat v0.0.8 ) diff --git a/go.sum b/go.sum index 46fb0cb..9c8a8b5 100644 --- a/go.sum +++ b/go.sum @@ -14,4 +14,6 @@ github.com/diamondburned/cchat v0.0.6 h1:lSIgbxT7C8uSka0wqLl/5kc/8rgsYDfZGxaU346 github.com/diamondburned/cchat v0.0.6/go.mod h1:+zXktogE45A0om4fT6B/z6Ii7FXNafjxsNspI0rlhbU= github.com/diamondburned/cchat v0.0.7 h1:y5zbEoD6L8YHuN/yclqjokF0lPkRj5tYWOuJe0bBldI= github.com/diamondburned/cchat v0.0.7/go.mod h1:+zXktogE45A0om4fT6B/z6Ii7FXNafjxsNspI0rlhbU= +github.com/diamondburned/cchat v0.0.8 h1:/PmI23SFHJcjYBWNBwQbp36n7fDvDu+NMnQuhM5FM2E= +github.com/diamondburned/cchat v0.0.8/go.mod h1:+zXktogE45A0om4fT6B/z6Ii7FXNafjxsNspI0rlhbU= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= diff --git a/server.go b/server.go index e7acbd6..2e36722 100644 --- a/server.go +++ b/server.go @@ -10,7 +10,7 @@ import ( ) type Server struct { - session *Service + session *Session id uint32 name string children []cchat.Server @@ -34,11 +34,11 @@ func (sv *Server) Servers(container cchat.ServersContainer) error { return nil } -func GenerateServers(s *Service) []cchat.Server { +func GenerateServers(s *Session) []cchat.Server { return generateServers(s, rand.Intn(45)) } -func generateServers(s *Service, amount int) []cchat.Server { +func generateServers(s *Session, amount int) []cchat.Server { var channels = make([]cchat.Server, amount) for i := range channels { channels[i] = &Server{ diff --git a/service.go b/service.go index 0f809fc..617311e 100644 --- a/service.go +++ b/service.go @@ -6,44 +6,34 @@ import ( "strconv" "github.com/diamondburned/cchat" + "github.com/diamondburned/cchat/services" ) -type Service struct { - username string - servers []cchat.Server - lastid uint32 +func init() { + services.RegisterService(&Service{}) } +type Service struct{} + var ( - _ cchat.Service = (*Service)(nil) - _ cchat.Authenticator = (*Service)(nil) - _ cchat.Configurator = (*Service)(nil) + _ cchat.Service = (*Service)(nil) + _ cchat.Configurator = (*Service)(nil) ) -func NewService() cchat.Service { - return &Service{} +func (s Service) Name() string { + return "Mock" } -func (s *Service) AuthenticateForm() []cchat.AuthenticateEntry { +func (s Service) AuthenticateForm() []cchat.AuthenticateEntry { return []cchat.AuthenticateEntry{{ Name: "Username", }} } -func (s *Service) Authenticate(form []string) error { - s.username = form[0] - s.servers = GenerateServers(s) - - return nil -} - -func (s *Service) Name() string { - return "Mock backend" -} - -func (s *Service) Servers(container cchat.ServersContainer) error { - container.SetServers(s.servers) - return nil +func (s Service) Authenticate(form []string) (cchat.Session, error) { + ses := &Session{username: form[0]} + ses.servers = GenerateServers(ses) + return ses, nil } var ( @@ -54,7 +44,7 @@ var ( internetMaxLatency = 2500 ) -func (s *Service) Configuration() (map[string]string, error) { +func (s Service) Configuration() (map[string]string, error) { return map[string]string{ "internet.canFail": strconv.FormatBool(internetCanFail), "internet.minLatency": strconv.Itoa(internetMinLatency), @@ -62,7 +52,7 @@ func (s *Service) Configuration() (map[string]string, error) { }, nil } -func (s *Service) SetConfiguration(config map[string]string) error { +func (s Service) SetConfiguration(config map[string]string) error { for _, err := range []error{ // shit code, would not recommend. It's only an ok-ish idea here because // unmarshalConfig() returns ErrInvalidConfigAtField. @@ -85,3 +75,20 @@ func unmarshalConfig(config map[string]string, key string, value interface{}) er } return nil } + +type Session struct { + username string + servers []cchat.Server + lastid uint32 +} + +var _ cchat.Session = (*Session)(nil) + +func (s *Session) Name() (string, error) { + return s.username, nil +} + +func (s *Session) Servers(container cchat.ServersContainer) error { + container.SetServers(s.servers) + return nil +}