players can send each other chat messages

master
Jordan Orelli 4 years ago
parent c3c07c97cf
commit 7a6172e511

@ -5,6 +5,7 @@ import (
"github.com/gdamore/tcell/v2" "github.com/gdamore/tcell/v2"
"github.com/jordanorelli/astro-domu/internal/math" "github.com/jordanorelli/astro-domu/internal/math"
"github.com/jordanorelli/astro-domu/internal/sim"
"github.com/jordanorelli/blammo" "github.com/jordanorelli/blammo"
) )
@ -12,6 +13,7 @@ type chatView struct {
*blammo.Log *blammo.Log
composing string composing string
inFocus bool inFocus bool
history []sim.ChatMessage
} }
func (c *chatView) handleEvent(ui *UI, e tcell.Event) bool { func (c *chatView) handleEvent(ui *UI, e tcell.Event) bool {
@ -28,6 +30,13 @@ func (c *chatView) handleEvent(ui *UI, e tcell.Event) bool {
break break
} }
if key == tcell.KeyEnter {
// ugh lol
go ui.client.Send(sim.SendChatMessage{Text: c.composing})
c.composing = ""
break
}
if key == tcell.KeyRune { if key == tcell.KeyRune {
c.composing = fmt.Sprintf("%s%c", c.composing, t.Rune()) c.composing = fmt.Sprintf("%s%c", c.composing, t.Rune())
c.Info("composing: %v", c.composing) c.Info("composing: %v", c.composing)
@ -40,6 +49,13 @@ func (c *chatView) handleEvent(ui *UI, e tcell.Event) bool {
} }
func (c *chatView) draw(b *buffer) { func (c *chatView) draw(b *buffer) {
chatHeight := b.height - 1
for i := 0; i < math.Min(chatHeight, len(c.history)); i++ {
msg := c.history[len(c.history)-1-i]
s := fmt.Sprintf("%12s: %s", msg.From, msg.Text)
b.writeString(s, math.Vec{0, b.height - 2 - i}, tcell.StyleDefault)
}
b.writeString(c.composing, math.Vec{0, b.height - 1}, tcell.StyleDefault) b.writeString(c.composing, math.Vec{0, b.height - 1}, tcell.StyleDefault)
if c.inFocus { if c.inFocus {

@ -6,6 +6,7 @@ import (
"github.com/gdamore/tcell/v2" "github.com/gdamore/tcell/v2"
"github.com/jordanorelli/astro-domu/internal/exit" "github.com/jordanorelli/astro-domu/internal/exit"
"github.com/jordanorelli/astro-domu/internal/math" "github.com/jordanorelli/astro-domu/internal/math"
"github.com/jordanorelli/astro-domu/internal/sim"
"github.com/jordanorelli/astro-domu/internal/wire" "github.com/jordanorelli/astro-domu/internal/wire"
"github.com/jordanorelli/blammo" "github.com/jordanorelli/blammo"
) )
@ -17,8 +18,8 @@ type UI struct {
room *wire.Room room *wire.Room
client *wire.Client client *wire.Client
gameView view gameView *gameView
chatView view chatView *chatView
focussed view focussed view
} }
@ -57,7 +58,8 @@ func (ui *UI) Run() {
}, },
} }
ui.chatView = &chatView{ ui.chatView = &chatView{
Log: ui.Child("chat-view"), Log: ui.Child("chat-view"),
history: make([]sim.ChatMessage, 0, 32),
} }
ui.focussed = ui.gameView ui.focussed = ui.gameView
@ -140,6 +142,10 @@ func (ui *UI) handleNotification(v wire.Value) bool {
ui.room.Entities = n.Entities ui.room.Entities = n.Entities
return true return true
case *sim.ChatMessage:
ui.chatView.history = append(ui.chatView.history, *n)
return true
default: default:
ui.Info("ignoring notification: %v", n) ui.Info("ignoring notification: %v", n)
return false return false

@ -21,3 +21,18 @@ func Max(a, b int, more ...int) int {
} }
return v return v
} }
func Min(a, b int, more ...int) int {
var v int
if a < b {
v = a
} else {
v = b
}
for _, next := range more {
if next < v {
v = next
}
}
return v
}

@ -0,0 +1,30 @@
package sim
import (
"github.com/jordanorelli/astro-domu/internal/wire"
)
type SendChatMessage struct {
Text string `json:"text"`
}
func (SendChatMessage) NetTag() string { return "chat/send-msg" }
func (m *SendChatMessage) exec(w *world, r *room, p *player, seq int) result {
for _, p2 := range r.players {
p2.outbox <- wire.Response{Body: ChatMessage{From: p.name, Text: m.Text}}
}
return result{reply: wire.OK{}}
}
type ChatMessage struct {
From string `json:"from"`
Text string `json:"text"`
}
func (ChatMessage) NetTag() string { return "chat/msg" }
func init() {
wire.Register(func() wire.Value { return new(SendChatMessage) })
wire.Register(func() wire.Value { return new(ChatMessage) })
}
Loading…
Cancel
Save