From 407aa1a13dd61b0f2798f7d34beb3532fce9e4e4 Mon Sep 17 00:00:00 2001 From: "diamondburned (Forefront)" Date: Sun, 7 Jun 2020 10:45:14 -0700 Subject: [PATCH] Added more IO latency for realism, session now has icon --- channel.go | 17 ++++++++++++++++- message.go | 23 +++++++++++++++-------- server.go | 4 ++-- service.go | 15 +++++++++++++++ 4 files changed, 48 insertions(+), 11 deletions(-) diff --git a/channel.go b/channel.go index 8b7760a..181e57f 100644 --- a/channel.go +++ b/channel.go @@ -49,7 +49,11 @@ func (ch *Channel) Nickname(labeler cchat.LabelContainer) error { } func (ch *Channel) JoinServer(container cchat.MessagesContainer) error { + // Emulate IO. + emulateAustralianInternet() + var lastAuthor text.Rich + var lastCounter int var nextID = func() uint32 { id := ch.lastID @@ -57,8 +61,15 @@ func (ch *Channel) JoinServer(container cchat.MessagesContainer) error { return id } var randomMsg = func() Message { + // Try and reuse the author multiple times. + if lastCounter++; lastCounter < randClamp(2, 5) { + return randomMessageWithAuthor(nextID(), lastAuthor) + } + msg := randomMessage(nextID()) lastAuthor = msg.author + lastCounter = 0 + return msg } @@ -148,9 +159,13 @@ func generateChannels(s *Session, amount int) []cchat.Server { return channels } +func randClamp(min, max int) int { + return rand.Intn(max-min) + min +} + // emulate network latency func emulateAustralianInternet() (lost bool) { - var ms = rand.Intn(internetMaxLatency-internetMinLatency) + internetMinLatency + var ms = randClamp(internetMinLatency, internetMaxLatency) <-time.After(time.Duration(ms) * time.Millisecond) // because australia, drop packet 20% of the time if internetCanFail is diff --git a/message.go b/message.go index 171621e..a8ec1d7 100644 --- a/message.go +++ b/message.go @@ -11,6 +11,8 @@ import ( "github.com/diamondburned/cchat/text" ) +const avatarURL = "https://gist.github.com/diamondburned/945744c2b5ce0aa0581c9267a4e5cf24/raw/598069da673093aaca4cd4aa0ede1a0e324e9a3a/astolfo_selfie.png" + type Message struct { id uint32 time time.Time @@ -57,16 +59,21 @@ func echoMessage(sendable cchat.SendableMessage, id uint32, author text.Rich) Me } func randomMessage(id uint32) Message { - var now = time.Now() var author = randomdata.SillyName() + return randomMessageWithAuthor(id, text.Rich{ + Content: author, + Segments: []text.Segment{segments.NewRandomColored(author)}, + }) +} + +func randomMessageWithAuthor(id uint32, author text.Rich) Message { + var now = time.Now() + return Message{ - id: id, - time: now, - author: text.Rich{ - Content: author, - Segments: []text.Segment{segments.NewRandomColored(author)}, - }, + id: id, + time: now, + author: author, content: randomdata.Paragraph(), } } @@ -115,5 +122,5 @@ func (a Author) Name() text.Rich { } func (a Author) Avatar() string { - return "https://gist.github.com/diamondburned/945744c2b5ce0aa0581c9267a4e5cf24/raw/598069da673093aaca4cd4aa0ede1a0e324e9a3a/astolfo_selfie.png" + return avatarURL } diff --git a/server.go b/server.go index 00c3027..ab31903 100644 --- a/server.go +++ b/server.go @@ -37,7 +37,7 @@ func (sv *Server) Servers(container cchat.ServersContainer) error { } func GenerateServers(s *Session) []cchat.Server { - return generateServers(s, rand.Intn(45)) + return generateServers(s, rand.Intn(45)+2) } func generateServers(s *Session, amount int) []cchat.Server { @@ -47,7 +47,7 @@ func generateServers(s *Session, amount int) []cchat.Server { session: s, id: atomic.AddUint32(&s.lastid, 1), name: randomdata.Noun(), - children: generateChannels(s, rand.Intn(12)), + children: generateChannels(s, rand.Intn(12)+2), } } return servers diff --git a/service.go b/service.go index 3e0458e..d1e17f3 100644 --- a/service.go +++ b/service.go @@ -31,6 +31,10 @@ func (s Service) Name() string { } func (s Service) RestoreSession(storage map[string]string) (cchat.Session, error) { + if emulateAustralianInternet() { + return nil, errors.New("Restore failed: server machine broke") + } + username, ok := storage["username"] if !ok { return nil, ErrInvalidSession @@ -64,6 +68,11 @@ func (Authenticator) AuthenticateForm() []cchat.AuthenticateEntry { } func (Authenticator) Authenticate(form []string) (cchat.Session, error) { + // SLOW IO TIME. + if emulateAustralianInternet() { + return nil, errors.New("Authentication timed out.") + } + return newSession(form[0]), nil } @@ -115,6 +124,7 @@ type Session struct { } var ( + _ cchat.Icon = (*Session)(nil) _ cchat.Session = (*Session)(nil) _ cchat.SessionSaver = (*Session)(nil) ) @@ -139,6 +149,11 @@ func (s *Session) Servers(container cchat.ServersContainer) error { return nil } +func (s *Session) Icon(iconer cchat.IconContainer) error { + iconer.SetIcon(avatarURL) + return nil +} + func (s *Session) Save() (map[string]string, error) { return map[string]string{ "username": s.username,