echoing commands works

trunk
vilmibm 2022-07-22 16:57:15 -05:00
parent e1169d63cd
commit 0075bc8836
2 changed files with 42 additions and 27 deletions

View File

@ -24,7 +24,17 @@ var (
serverHostOverride = flag.String("server_host_override", "x.test.example.com", "The server name used to verify the hostname returned by the TLS handshake") serverHostOverride = flag.String("server_host_override", "x.test.example.com", "The server name used to verify the hostname returned by the TLS handshake")
) )
func messages(cs *ClientState) error { type ClientState struct {
App *tview.Application
Client proto.GameWorldClient
SessionInfo *proto.SessionInfo
MaxMessages int
messagesView *tview.TextView
messages []*proto.ClientMessage
cmdStream proto.GameWorld_CommandsClient
}
func (cs *ClientState) Messages() error {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
stream, err := cs.Client.Messages(ctx, cs.SessionInfo) stream, err := cs.Client.Messages(ctx, cs.SessionInfo)
@ -46,16 +56,6 @@ func messages(cs *ClientState) error {
return nil return nil
} }
type ClientState struct {
App *tview.Application
Client proto.GameWorldClient
SessionInfo *proto.SessionInfo
MaxMessages int
messagesView *tview.TextView
messages []*proto.ClientMessage
cmdStream proto.GameWorld_CommandsClient
}
func (cs *ClientState) HandleInput(input string) error { func (cs *ClientState) HandleInput(input string) error {
var verb string var verb string
rest := input rest := input
@ -213,6 +213,8 @@ func _main() error {
pages.SwitchToPage("game") pages.SwitchToPage("game")
app.SetFocus(commandInput) app.SetFocus(commandInput)
// TODO error handle
go cs.Messages()
} }
// TODO login and register pages should refuse blank entries // TODO login and register pages should refuse blank entries
@ -278,8 +280,6 @@ func _main() error {
pages.AddPage("game", gamePage, true, false) pages.AddPage("game", gamePage, true, false)
go messages(cs)
return app.SetRoot(pages, true).SetFocus(pages).Run() return app.SetRoot(pages, true).SetFocus(pages).Run()
} }

View File

@ -7,7 +7,7 @@ import (
"io" "io"
"log" "log"
"net" "net"
"time" "sync"
"github.com/vilmibm/hermeticum/proto" "github.com/vilmibm/hermeticum/proto"
"github.com/vilmibm/hermeticum/server/db" "github.com/vilmibm/hermeticum/server/db"
@ -56,10 +56,15 @@ func _main() (err error) {
type gameWorldServer struct { type gameWorldServer struct {
proto.UnimplementedGameWorldServer proto.UnimplementedGameWorldServer
mu sync.Mutex // for msgRouter
msgRouter map[string]func(*proto.ClientMessage) error
} }
func newServer() *gameWorldServer { func newServer() *gameWorldServer {
s := &gameWorldServer{} s := &gameWorldServer{
msgRouter: make(map[string]func(*proto.ClientMessage) error),
}
return s return s
} }
@ -73,15 +78,28 @@ func (s *gameWorldServer) Commands(stream proto.GameWorld_CommandsServer) error
if err != nil { if err != nil {
return err return err
} }
fmt.Printf("DBG %#v\n", cmd)
// TODO FOR NOW, just find the session's associated message stream and do an echo (which requires doing a session storage solution first) sid := cmd.SessionInfo.SessionID
send := s.msgRouter[sid]
msg := &proto.ClientMessage{
Type: proto.ClientMessage_OVERHEARD,
Text: fmt.Sprintf("%s sent command %s with args %s",
sid, cmd.Verb, cmd.Rest),
}
speaker := "ECHO"
msg.Speaker = &speaker
err = send(msg)
if err != nil {
log.Printf("failed to send %v to %s: %s", msg, sid, err)
}
// TODO find the user who ran action via SessionInfo // TODO find the user who ran action via SessionInfo
// TODO get area of effect, which should include the sender // TODO get area of effect, which should include the sender
// TODO dispatch the command to each affected object // TODO dispatch the command to each affected object
} }
return nil
} }
func (s *gameWorldServer) Ping(ctx context.Context, _ *proto.SessionInfo) (*proto.Pong, error) { func (s *gameWorldServer) Ping(ctx context.Context, _ *proto.SessionInfo) (*proto.Pong, error) {
@ -93,16 +111,13 @@ func (s *gameWorldServer) Ping(ctx context.Context, _ *proto.SessionInfo) (*prot
} }
func (s *gameWorldServer) Messages(si *proto.SessionInfo, stream proto.GameWorld_MessagesServer) error { func (s *gameWorldServer) Messages(si *proto.SessionInfo, stream proto.GameWorld_MessagesServer) error {
for x := 0; x < 100; x++ { s.mu.Lock()
msg := &proto.ClientMessage{} s.msgRouter[si.SessionID] = stream.Send
speaker := "snoozy" s.mu.Unlock()
msg.Speaker = &speaker
msg.Type = proto.ClientMessage_WHISPER // TODO this is clearly bad but it works. I should refactor this so that messages are received on a channel.
msg.Text = fmt.Sprintf("hi this is message %d. by the way i am a horse. neigh neigh neigh neigh neigh neigh neigh neigh neigh neigh neigh neigh", x) for {
stream.Send(msg)
time.Sleep(500 * time.Millisecond)
} }
return nil
} }
func (s *gameWorldServer) Register(ctx context.Context, auth *proto.AuthInfo) (si *proto.SessionInfo, err error) { func (s *gameWorldServer) Register(ctx context.Context, auth *proto.AuthInfo) (si *proto.SessionInfo, err error) {