Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cammie messages #18

Merged
merged 2 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions api/cammie.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func cammiePostMessage(app *screen.ScreenApp, c *gin.Context) {
// Get structs
header := &cammieHeader{}
message := &cammieMessage{}
cammieMessage := &screen.CammieMessage{}

// Check Header
if err := c.ShouldBindHeader(header); err != nil {
Expand All @@ -57,27 +58,26 @@ func cammiePostMessage(app *screen.ScreenApp, c *gin.Context) {
}

// Check if sender is blocked and construct message
var newMessage string
if header.Name != "" {
if slices.Contains(cammieBlockedNames, header.Name) {
c.JSON(http.StatusOK, gin.H{"message": "Message received"})
return
}
newMessage = fmt.Sprintf("[%s[] %s", header.Name, message.Message)
cammieMessage.Sender = fmt.Sprintf("[%s[]", header.Name)
} else if header.IP != "" {
if slices.Contains(cammieBlockedIps, header.IP) {
c.JSON(http.StatusOK, gin.H{"message": "Message received"})
return
}
newMessage = fmt.Sprintf("<%s> %s", header.IP, message.Message)
} else {
newMessage = message.Message
cammieMessage.Sender = fmt.Sprintf("<%s>", header.IP)
}

cammieMessage.Message = message.Message

// Increment messages
cammieMessages++

app.Cammie.Update(newMessage)
app.Cammie.Update(cammieMessage)
go buzzer.PlayBuzzer()

c.JSON(http.StatusOK, gin.H{"message": "Message received"})
Expand Down
45 changes: 25 additions & 20 deletions screen/cammie.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,27 @@ package screen

import (
"fmt"
"scc/utils"
"hash/fnv"
"time"

"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)

type CammieMessage struct {
Sender string
Message string
}

// Component that displays messages received from the website aka cammie chat
type Cammie struct {
screenApp *ScreenApp
view *tview.TextView

text string
buffer string
}

// Initial value, gets adjusted once it's known how much space is available
var maxMessages = 20

Expand All @@ -26,16 +40,6 @@ var colors = [...]tcell.Color{
tcell.ColorYellow,
tcell.ColorSalmon,
}
var lastColorIndex = 0

// Component that displays messages received from the website aka cammie chat
type Cammie struct {
screenApp *ScreenApp
view *tview.TextView

text string
buffer string
}

// Create a new cammie struct
func NewCammie(screenApp *ScreenApp) *Cammie {
Expand Down Expand Up @@ -63,20 +67,21 @@ func (cammie *Cammie) Run() {

// Updates the cammie chat
// Gets called when a new message is received from the website
func (cammie *Cammie) Update(message string) {
var colorIndex int
for {
colorIndex = utils.RandRange(0, len(colors))
if colorIndex != lastColorIndex {
break
}
}
func (cammie *Cammie) Update(message *CammieMessage) {
colorIndex := hashColor(message.Sender)

color := colors[colorIndex].String()

cammie.screenApp.execute(func() {
fmt.Fprintf(cammie.view, "\n[%s]%s", color, message)
fmt.Fprintf(cammie.view, "\n[%s]%s %s[-:-:-:-]", color, message.Sender, message.Message)

cammie.view.ScrollToEnd()
})
}

func hashColor(s string) int {
h := fnv.New32a()
h.Write([]byte(s))
hashNumber := h.Sum32()
return int(hashNumber) % len(colors)
}
Loading