Added better randomization and better test data

This commit is contained in:
diamondburned (Forefront) 2020-07-03 21:32:10 -07:00
parent 861177dd4a
commit 8ba6640d64
5 changed files with 65 additions and 29 deletions

View File

@ -21,7 +21,7 @@ const FetchBacklog = 35
const maxBacklog = FetchBacklog * 2
// max number to add to before the next author, with rand.Intn(limit) + incr.
const sameAuthorLimit = 12
const sameAuthorLimit = 6
type Channel struct {
id uint32
@ -125,7 +125,7 @@ func (ch *Channel) JoinServer(ctx context.Context, ct cchat.MessagesContainer) (
for {
select {
case msg := <-ch.send:
ch.addMessage(echoMessage(msg, ch.nextID(), ch.username), ct)
ch.addMessage(echoMessage(msg, ch.nextID(), newAuthor(ch.username)), ct)
case msg := <-ch.edit:
ct.UpdateMessage(msg)
@ -166,7 +166,7 @@ func (ch *Channel) MessageEditable(id string) bool {
m, ok := ch.messages[i]
if ok {
// Editable if same author.
return m.author.Content == ch.username.Content
return m.author.name.Content == ch.username.Content
}
return false
@ -295,16 +295,16 @@ func (ch *Channel) randomMsg() (msg Message) {
// Add a random number into incrAuthor and determine if that should be
// enough to generate a new author.
ch.incrAuthor += uint8(rand.Intn(5)) // 2~4 appearances
ch.incrAuthor += uint8(rand.Intn(sameAuthorLimit)) // 1~6 appearances
var lastID = ch.messageids[len(ch.messageids)-1]
var last = ch.messages[lastID]
var lastAu = ch.messages[lastID].author
// If the last author is not the current user, then we can use it.
// Should we generate a new author for the new message? No if we're not over
// the limits.
if last.author.Content != ch.username.Content && ch.incrAuthor < sameAuthorLimit {
msg = randomMessageWithAuthor(ch.nextID(), last.author)
if lastAu.name.Content != ch.username.Content && ch.incrAuthor < sameAuthorLimit {
msg = randomMessageWithAuthor(ch.nextID(), lastAu)
} else {
msg = randomMessage(ch.nextID())
ch.incrAuthor = 0 // reset
@ -335,7 +335,7 @@ func (ch *Channel) SendMessage(msg cchat.SendableMessage) error {
const (
DeleteAction = "Delete"
NoopAction = "No-op"
BestTrapAction = "What's the best trap?"
BestTrapAction = "Who's the best trap?"
TriggerTypingAction = "Trigger Typing"
)
@ -365,8 +365,7 @@ func (ch *Channel) DoMessageAction(action, messageID string) error {
case DeleteAction:
ch.del <- MessageHeader{uint32(i), time.Now()}
case TriggerTypingAction:
// Find the message.
ch.typ <- Author{name: ch.messages[uint32(i)].author}
ch.typ <- ch.messages[uint32(i)].author
}
case NoopAction:
@ -417,16 +416,16 @@ func (ch *Channel) CompleteMessage(words []string, i int) (entries []cchat.Compl
// Look for members.
for _, id := range ch.messageids {
if msg := ch.messages[id]; strings.HasPrefix(msg.author.Content, words[i]) {
if _, ok := found[msg.author.Content]; ok {
if msg := ch.messages[id]; strings.HasPrefix(msg.author.name.Content, words[i]) {
if _, ok := found[msg.author.name.Content]; ok {
continue
}
found[msg.author.Content] = struct{}{}
found[msg.author.name.Content] = struct{}{}
entries = append(entries, cchat.CompletionEntry{
Raw: msg.author.Content,
Text: msg.author,
Raw: msg.author.name.Content,
Text: msg.author.name,
IconURL: avatarURL,
})
}

2
go.mod
View File

@ -4,6 +4,8 @@ go 1.14
require (
github.com/Pallinder/go-randomdata v1.2.0
github.com/diamondburned/aqs v0.0.0-20200704043012-98e184f74adf
github.com/diamondburned/cchat v0.0.40
github.com/lucasb-eyer/go-colorful v1.0.3
github.com/pkg/errors v0.9.1
)

16
go.sum
View File

@ -1,5 +1,15 @@
github.com/Pallinder/go-randomdata v1.2.0 h1:DZ41wBchNRb/0GfsePLiSwb0PHZmT67XY00lCDlaYPg=
github.com/Pallinder/go-randomdata v1.2.0/go.mod h1:yHmJgulpD2Nfrm0cR9tI/+oAgRqCQQixsA8HyRZfV9Y=
github.com/diamondburned/aqs v0.0.0-20200704020052-0184c4aa7fcd h1:7+tZmK30eejFaoF7yEo7OeEs91b3IdZDOaw0aZL+0MU=
github.com/diamondburned/aqs v0.0.0-20200704020052-0184c4aa7fcd/go.mod h1:qgHV+kTgLp7PAhs6ZcTQeXeNXjGdN1wGWa9/c8Z82YI=
github.com/diamondburned/aqs v0.0.0-20200704040306-5ee84e3df30b h1:aecM4nKK6gFNApcA1pkV+gabAiRezv3aqgN2OF/AlMs=
github.com/diamondburned/aqs v0.0.0-20200704040306-5ee84e3df30b/go.mod h1:qgHV+kTgLp7PAhs6ZcTQeXeNXjGdN1wGWa9/c8Z82YI=
github.com/diamondburned/aqs v0.0.0-20200704041843-5d8bbcba538b h1:hH/PQr4xTRwRj3sjDMwCHm6cYFxxlQ0SUzZobY1ByUs=
github.com/diamondburned/aqs v0.0.0-20200704041843-5d8bbcba538b/go.mod h1:qgHV+kTgLp7PAhs6ZcTQeXeNXjGdN1wGWa9/c8Z82YI=
github.com/diamondburned/aqs v0.0.0-20200704042704-ac1f513c67dc h1:Xv8q5Z6Ue2cdHkzat+wjGjDEa/Q31H791FYgdCZoyqo=
github.com/diamondburned/aqs v0.0.0-20200704042704-ac1f513c67dc/go.mod h1:qgHV+kTgLp7PAhs6ZcTQeXeNXjGdN1wGWa9/c8Z82YI=
github.com/diamondburned/aqs v0.0.0-20200704043012-98e184f74adf h1:sQZbIMXUEIxNQkGP8RBOFaWgkgKocD6yMOZXZVLtFNY=
github.com/diamondburned/aqs v0.0.0-20200704043012-98e184f74adf/go.mod h1:qgHV+kTgLp7PAhs6ZcTQeXeNXjGdN1wGWa9/c8Z82YI=
github.com/diamondburned/cchat v0.0.1 h1:ABoYPQ+uhsUopdDDw62jFsEjvsz6dlOttL/xIeU4wy4=
github.com/diamondburned/cchat v0.0.1/go.mod h1:2MdhWABRer4WhwcuLR0b2VY5S22Y1zDTpFqriAFrC08=
github.com/diamondburned/cchat v0.0.2 h1:WfynDllfkUeTWKGwNWz5AWEXL88rl+6FO/uSfMdNiss=
@ -68,7 +78,13 @@ github.com/diamondburned/cchat v0.0.39 h1:Hxd7swmAIECm0MBd5wb1IFvreChwDFwnAshqgA
github.com/diamondburned/cchat v0.0.39/go.mod h1:+zXktogE45A0om4fT6B/z6Ii7FXNafjxsNspI0rlhbU=
github.com/diamondburned/cchat v0.0.40 h1:38gPyJnnDoNDHrXcV8Qchfv3y6jlS3Fzz/6FY0BPH6I=
github.com/diamondburned/cchat v0.0.40/go.mod h1:+zXktogE45A0om4fT6B/z6Ii7FXNafjxsNspI0rlhbU=
github.com/k0kubun/pp v3.0.1+incompatible/go.mod h1:GWse8YhT0p8pT4ir3ZgBbfZild3tgzSScAn6HmfYukg=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/lucasb-eyer/go-colorful v1.0.3 h1:QIbQXiugsb+q10B+MI+7DI1oQLdmnep86tWFlaaUAac=
github.com/lucasb-eyer/go-colorful v1.0.3/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/mattn/go-colorable v0.1.7/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

View File

@ -5,10 +5,13 @@ import (
"strings"
"time"
"github.com/Pallinder/go-randomdata"
"github.com/diamondburned/aqs"
"github.com/diamondburned/cchat"
"github.com/diamondburned/cchat-mock/segments"
"github.com/diamondburned/cchat/text"
_ "github.com/diamondburned/aqs/data"
"github.com/diamondburned/aqs/incr"
)
const avatarURL = "https://gist.github.com/diamondburned/945744c2b5ce0aa0581c9267a4e5cf24/raw/598069da673093aaca4cd4aa0ede1a0e324e9a3a/astolfo_selfie.png"
@ -38,7 +41,7 @@ func (m MessageHeader) Time() time.Time {
type Message struct {
MessageHeader
author text.Rich
author Author
content string
nonce string
}
@ -51,22 +54,22 @@ var (
_ cchat.MessageMentioned = (*Message)(nil)
)
func newEmptyMessage(id uint32, author text.Rich) Message {
func newEmptyMessage(id uint32, author Author) Message {
return Message{
MessageHeader: MessageHeader{id: id},
author: author,
}
}
func newRandomMessage(id uint32, author text.Rich) Message {
func newRandomMessage(id uint32, author Author) Message {
return Message{
MessageHeader: MessageHeader{id: id, time: time.Now()},
author: author,
content: randomdata.Paragraph(),
content: incr.RandomQuote(author.char),
}
}
func echoMessage(sendable cchat.SendableMessage, id uint32, author text.Rich) Message {
func echoMessage(sendable cchat.SendableMessage, id uint32, author Author) Message {
var echo = Message{
MessageHeader: MessageHeader{id: id, time: time.Now()},
author: author,
@ -79,19 +82,19 @@ func echoMessage(sendable cchat.SendableMessage, id uint32, author text.Rich) Me
}
func randomMessage(id uint32) Message {
return randomMessageWithAuthor(id, randomAuthor().name)
return randomMessageWithAuthor(id, randomAuthor())
}
func randomMessageWithAuthor(id uint32, author text.Rich) Message {
func randomMessageWithAuthor(id uint32, author Author) Message {
return Message{
MessageHeader: MessageHeader{id: id, time: time.Now()},
author: author,
content: randomdata.Paragraph(),
content: incr.RandomQuote(author.char),
}
}
func (m Message) Author() cchat.MessageAuthor {
return Author{name: m.author}
return m.author
}
func (m Message) Content() text.Rich {
@ -105,11 +108,12 @@ func (m Message) Nonce() string {
// Mentioned is true when the message content contains the author's name.
func (m Message) Mentioned() bool {
// hack
return strings.Contains(m.content, m.author.Content)
return strings.Contains(m.content, m.author.name.Content)
}
type Author struct {
name text.Rich
char aqs.Character
}
var (
@ -117,12 +121,17 @@ var (
_ cchat.MessageAuthorAvatar = (*Author)(nil)
)
func newAuthor(name text.Rich) Author {
return Author{name: name}
}
func randomAuthor() Author {
var author = randomdata.SillyName()
var char = aqs.RandomCharacter()
return Author{
char: char,
name: text.Rich{
Content: author,
Segments: []text.Segment{segments.NewRandomColored(author)},
Content: char.Name,
Segments: []text.Segment{segments.NewColorful(char.Name, char.NameColor())},
},
}
}
@ -136,5 +145,8 @@ func (a Author) Name() text.Rich {
}
func (a Author) Avatar() string {
if a.char.ImageURL != "" {
return a.char.ImageURL
}
return avatarURL
}

View File

@ -6,6 +6,7 @@ import (
"time"
"github.com/diamondburned/cchat/text"
"github.com/lucasb-eyer/go-colorful"
)
func init() {
@ -30,6 +31,12 @@ func NewRandomColored(str string) Colored {
return Colored{len(str), RandomColor()}
}
func NewColorful(str string, color colorful.Color) Colored {
r, g, b := color.RGB255()
h := (uint32(r) << 16) + (uint32(g) << 8) + (uint32(b))
return NewColored(str, h)
}
func (color Colored) Bounds() (start, end int) {
return 0, color.strlen
}