1
0
Fork 0
mirror of https://github.com/diamondburned/cchat-mock.git synced 2024-12-27 13:46:44 +00:00
cchat-mock/server.go

53 lines
998 B
Go
Raw Normal View History

2020-05-20 07:13:12 +00:00
package mock
import (
"math/rand"
2020-05-20 21:23:44 +00:00
"strconv"
"sync/atomic"
2020-05-20 07:13:12 +00:00
"github.com/Pallinder/go-randomdata"
"github.com/diamondburned/cchat"
)
type Server struct {
2020-05-23 02:44:50 +00:00
session *Session
2020-05-20 21:23:44 +00:00
id uint32
2020-05-20 07:13:12 +00:00
name string
children []cchat.Server
}
var (
_ cchat.Server = (*Server)(nil)
_ cchat.ServerList = (*Server)(nil)
)
2020-05-20 21:23:44 +00:00
func (sv *Server) ID() string {
return strconv.Itoa(int(sv.id))
}
2020-05-20 07:13:12 +00:00
func (sv *Server) Name() (string, error) {
return sv.name, nil
}
func (sv *Server) Servers(container cchat.ServersContainer) error {
container.SetServers(sv.children)
return nil
}
2020-05-23 02:44:50 +00:00
func GenerateServers(s *Session) []cchat.Server {
2020-05-20 07:13:12 +00:00
return generateServers(s, rand.Intn(45))
}
2020-05-23 02:44:50 +00:00
func generateServers(s *Session, amount int) []cchat.Server {
2020-05-20 07:13:12 +00:00
var channels = make([]cchat.Server, amount)
for i := range channels {
channels[i] = &Server{
session: s,
2020-05-20 21:23:44 +00:00
id: atomic.AddUint32(&s.lastid, 1),
2020-05-20 07:13:12 +00:00
name: randomdata.Noun(),
children: generateChannels(s, rand.Intn(12)),
}
}
return channels
}