Added more IO latency for realism, session now has icon
This commit is contained in:
parent
31a53c555e
commit
407aa1a13d
17
channel.go
17
channel.go
|
@ -49,7 +49,11 @@ func (ch *Channel) Nickname(labeler cchat.LabelContainer) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ch *Channel) JoinServer(container cchat.MessagesContainer) error {
|
func (ch *Channel) JoinServer(container cchat.MessagesContainer) error {
|
||||||
|
// Emulate IO.
|
||||||
|
emulateAustralianInternet()
|
||||||
|
|
||||||
var lastAuthor text.Rich
|
var lastAuthor text.Rich
|
||||||
|
var lastCounter int
|
||||||
|
|
||||||
var nextID = func() uint32 {
|
var nextID = func() uint32 {
|
||||||
id := ch.lastID
|
id := ch.lastID
|
||||||
|
@ -57,8 +61,15 @@ func (ch *Channel) JoinServer(container cchat.MessagesContainer) error {
|
||||||
return id
|
return id
|
||||||
}
|
}
|
||||||
var randomMsg = func() Message {
|
var randomMsg = func() Message {
|
||||||
|
// Try and reuse the author multiple times.
|
||||||
|
if lastCounter++; lastCounter < randClamp(2, 5) {
|
||||||
|
return randomMessageWithAuthor(nextID(), lastAuthor)
|
||||||
|
}
|
||||||
|
|
||||||
msg := randomMessage(nextID())
|
msg := randomMessage(nextID())
|
||||||
lastAuthor = msg.author
|
lastAuthor = msg.author
|
||||||
|
lastCounter = 0
|
||||||
|
|
||||||
return msg
|
return msg
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -148,9 +159,13 @@ func generateChannels(s *Session, amount int) []cchat.Server {
|
||||||
return channels
|
return channels
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func randClamp(min, max int) int {
|
||||||
|
return rand.Intn(max-min) + min
|
||||||
|
}
|
||||||
|
|
||||||
// emulate network latency
|
// emulate network latency
|
||||||
func emulateAustralianInternet() (lost bool) {
|
func emulateAustralianInternet() (lost bool) {
|
||||||
var ms = rand.Intn(internetMaxLatency-internetMinLatency) + internetMinLatency
|
var ms = randClamp(internetMinLatency, internetMaxLatency)
|
||||||
<-time.After(time.Duration(ms) * time.Millisecond)
|
<-time.After(time.Duration(ms) * time.Millisecond)
|
||||||
|
|
||||||
// because australia, drop packet 20% of the time if internetCanFail is
|
// because australia, drop packet 20% of the time if internetCanFail is
|
||||||
|
|
23
message.go
23
message.go
|
@ -11,6 +11,8 @@ import (
|
||||||
"github.com/diamondburned/cchat/text"
|
"github.com/diamondburned/cchat/text"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const avatarURL = "https://gist.github.com/diamondburned/945744c2b5ce0aa0581c9267a4e5cf24/raw/598069da673093aaca4cd4aa0ede1a0e324e9a3a/astolfo_selfie.png"
|
||||||
|
|
||||||
type Message struct {
|
type Message struct {
|
||||||
id uint32
|
id uint32
|
||||||
time time.Time
|
time time.Time
|
||||||
|
@ -57,16 +59,21 @@ func echoMessage(sendable cchat.SendableMessage, id uint32, author text.Rich) Me
|
||||||
}
|
}
|
||||||
|
|
||||||
func randomMessage(id uint32) Message {
|
func randomMessage(id uint32) Message {
|
||||||
var now = time.Now()
|
|
||||||
var author = randomdata.SillyName()
|
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{
|
return Message{
|
||||||
id: id,
|
id: id,
|
||||||
time: now,
|
time: now,
|
||||||
author: text.Rich{
|
author: author,
|
||||||
Content: author,
|
|
||||||
Segments: []text.Segment{segments.NewRandomColored(author)},
|
|
||||||
},
|
|
||||||
content: randomdata.Paragraph(),
|
content: randomdata.Paragraph(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -115,5 +122,5 @@ func (a Author) Name() text.Rich {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a Author) Avatar() string {
|
func (a Author) Avatar() string {
|
||||||
return "https://gist.github.com/diamondburned/945744c2b5ce0aa0581c9267a4e5cf24/raw/598069da673093aaca4cd4aa0ede1a0e324e9a3a/astolfo_selfie.png"
|
return avatarURL
|
||||||
}
|
}
|
||||||
|
|
|
@ -37,7 +37,7 @@ func (sv *Server) Servers(container cchat.ServersContainer) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func GenerateServers(s *Session) []cchat.Server {
|
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 {
|
func generateServers(s *Session, amount int) []cchat.Server {
|
||||||
|
@ -47,7 +47,7 @@ func generateServers(s *Session, amount int) []cchat.Server {
|
||||||
session: s,
|
session: s,
|
||||||
id: atomic.AddUint32(&s.lastid, 1),
|
id: atomic.AddUint32(&s.lastid, 1),
|
||||||
name: randomdata.Noun(),
|
name: randomdata.Noun(),
|
||||||
children: generateChannels(s, rand.Intn(12)),
|
children: generateChannels(s, rand.Intn(12)+2),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return servers
|
return servers
|
||||||
|
|
15
service.go
15
service.go
|
@ -31,6 +31,10 @@ func (s Service) Name() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s Service) RestoreSession(storage map[string]string) (cchat.Session, error) {
|
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"]
|
username, ok := storage["username"]
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, ErrInvalidSession
|
return nil, ErrInvalidSession
|
||||||
|
@ -64,6 +68,11 @@ func (Authenticator) AuthenticateForm() []cchat.AuthenticateEntry {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (Authenticator) Authenticate(form []string) (cchat.Session, error) {
|
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
|
return newSession(form[0]), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -115,6 +124,7 @@ type Session struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
_ cchat.Icon = (*Session)(nil)
|
||||||
_ cchat.Session = (*Session)(nil)
|
_ cchat.Session = (*Session)(nil)
|
||||||
_ cchat.SessionSaver = (*Session)(nil)
|
_ cchat.SessionSaver = (*Session)(nil)
|
||||||
)
|
)
|
||||||
|
@ -139,6 +149,11 @@ func (s *Session) Servers(container cchat.ServersContainer) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Session) Icon(iconer cchat.IconContainer) error {
|
||||||
|
iconer.SetIcon(avatarURL)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (s *Session) Save() (map[string]string, error) {
|
func (s *Session) Save() (map[string]string, error) {
|
||||||
return map[string]string{
|
return map[string]string{
|
||||||
"username": s.username,
|
"username": s.username,
|
||||||
|
|
Loading…
Reference in New Issue