Added more IO latency for realism, session now has icon

This commit is contained in:
diamondburned (Forefront) 2020-06-07 10:45:14 -07:00
parent 31a53c555e
commit 407aa1a13d
4 changed files with 48 additions and 11 deletions

View File

@ -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

View File

@ -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
}

View File

@ -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

View File

@ -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,