Better feedback
This commit is contained in:
parent
b8d8127bc0
commit
fff70cc8b3
6 changed files with 41 additions and 10 deletions
|
@ -53,7 +53,7 @@ func CreateCommandRegistry(commandDefinitions ...CommandDefinition) *CommandRegi
|
||||||
func (comReg *CommandRegistry) Match(tokens []Token) (comDef *CommandDefinition) {
|
func (comReg *CommandRegistry) Match(tokens []Token) (comDef *CommandDefinition) {
|
||||||
for _, v := range comReg.commandDefinitions {
|
for _, v := range comReg.commandDefinitions {
|
||||||
if v.Match(tokens) {
|
if v.Match(tokens) {
|
||||||
logging.Debug("Found match", v.Name())
|
logging.Debug("Found match ", v.Name())
|
||||||
return &v
|
return &v
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -106,7 +106,7 @@ func CreateTokenizer() *Tokenizer {
|
||||||
{tokenType: TokenSelf, pattern: `\bself\b`},
|
{tokenType: TokenSelf, pattern: `\bself\b`},
|
||||||
{tokenType: TokenIdentifier, pattern: `\b[a-zA-Z'-][a-zA-Z0-9'-]*\b`},
|
{tokenType: TokenIdentifier, pattern: `\b[a-zA-Z'-][a-zA-Z0-9'-]*\b`},
|
||||||
{tokenType: TokenWhitespace, pattern: `\s+`},
|
{tokenType: TokenWhitespace, pattern: `\s+`},
|
||||||
{tokenType: TokenUnknown, pattern: `.`},
|
{tokenType: TokenUnknown, pattern: `[^ \t\n\r\f\v]+`},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -141,6 +141,11 @@ func (t *Tokenizer) Tokenize(commandString string) (tokens []Token, err error) {
|
||||||
pos += loc[1]
|
pos += loc[1]
|
||||||
matched = true
|
matched = true
|
||||||
|
|
||||||
|
// Skip whitespace
|
||||||
|
if pattern.tokenType == TokenWhitespace {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
tokens = append(tokens, CreateToken(pattern.tokenType, lexeme, pos))
|
tokens = append(tokens, CreateToken(pattern.tokenType, lexeme, pos))
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,10 @@
|
||||||
package game
|
package game
|
||||||
|
|
||||||
import "code.haedhutner.dev/mvv/LastMUD/internal/game/command"
|
import (
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"code.haedhutner.dev/mvv/LastMUD/internal/game/command"
|
||||||
|
)
|
||||||
|
|
||||||
type CommandType = string
|
type CommandType = string
|
||||||
|
|
||||||
|
@ -16,14 +20,14 @@ func (game *LastMUDGame) CreateGameCommandRegistry() *command.CommandRegistry {
|
||||||
return len(tokens) > 1 && tokens[0].Lexeme() == "say"
|
return len(tokens) > 1 && tokens[0].Lexeme() == "say"
|
||||||
},
|
},
|
||||||
func(tokens []command.Token) []command.Parameter {
|
func(tokens []command.Token) []command.Parameter {
|
||||||
saying := ""
|
lexemes := []string{}
|
||||||
|
|
||||||
for _, t := range tokens[1:] {
|
for _, t := range tokens[1:] {
|
||||||
saying += t.Lexeme()
|
lexemes = append(lexemes, t.Lexeme())
|
||||||
}
|
}
|
||||||
|
|
||||||
return []command.Parameter{
|
return []command.Parameter{
|
||||||
command.CreateParameter(saying),
|
command.CreateParameter(strings.Join(lexemes, " ")),
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package game
|
package game
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"code.haedhutner.dev/mvv/LastMUD/internal/logging"
|
"code.haedhutner.dev/mvv/LastMUD/internal/logging"
|
||||||
|
@ -16,11 +17,30 @@ const (
|
||||||
PlayerSpeak
|
PlayerSpeak
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func (et EventType) String() string {
|
||||||
|
switch et {
|
||||||
|
case PlayerCommand:
|
||||||
|
return "PlayerCommand"
|
||||||
|
case PlayerJoin:
|
||||||
|
return "PlayerJoin"
|
||||||
|
case PlayerLeave:
|
||||||
|
return "PlayerLeave"
|
||||||
|
case PlayerSpeak:
|
||||||
|
return "PlayerSpeak"
|
||||||
|
default:
|
||||||
|
return "Unknown"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
type GameEvent interface {
|
type GameEvent interface {
|
||||||
Type() EventType
|
Type() EventType
|
||||||
Handle(game *LastMUDGame, delta time.Duration)
|
Handle(game *LastMUDGame, delta time.Duration)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func stringifyEvent(ev GameEvent) string {
|
||||||
|
return ev.Type().String() + fmt.Sprintf(`%+v`, ev)
|
||||||
|
}
|
||||||
|
|
||||||
type EventBus struct {
|
type EventBus struct {
|
||||||
events chan GameEvent
|
events chan GameEvent
|
||||||
}
|
}
|
||||||
|
@ -38,7 +58,7 @@ func (eb *EventBus) HasNext() bool {
|
||||||
func (eb *EventBus) Pop() (event GameEvent) {
|
func (eb *EventBus) Pop() (event GameEvent) {
|
||||||
select {
|
select {
|
||||||
case event := <-eb.events:
|
case event := <-eb.events:
|
||||||
logging.Info("Popped event of type ", event.Type(), ":", event)
|
logging.Debug("Popped event ", stringifyEvent(event))
|
||||||
return event
|
return event
|
||||||
default:
|
default:
|
||||||
return nil
|
return nil
|
||||||
|
@ -47,7 +67,7 @@ func (eb *EventBus) Pop() (event GameEvent) {
|
||||||
|
|
||||||
func (eb *EventBus) Push(event GameEvent) {
|
func (eb *EventBus) Push(event GameEvent) {
|
||||||
eb.events <- event
|
eb.events <- event
|
||||||
logging.Info("Enqueued event of type ", event.Type(), ":", event)
|
logging.Debug("Enqueued event ", stringifyEvent(event))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (eb *EventBus) close() {
|
func (eb *EventBus) close() {
|
||||||
|
|
|
@ -24,7 +24,7 @@ func (pje *PlayerJoinEvent) Type() EventType {
|
||||||
|
|
||||||
func (pje *PlayerJoinEvent) Handle(game *LastMUDGame, delta time.Duration) {
|
func (pje *PlayerJoinEvent) Handle(game *LastMUDGame, delta time.Duration) {
|
||||||
game.world.AddPlayerToDefaultRoom(CreatePlayer(pje.connectionId, nil))
|
game.world.AddPlayerToDefaultRoom(CreatePlayer(pje.connectionId, nil))
|
||||||
game.enqeueOutput(game.CreateOutput(pje.connectionId, []byte("Welcome to LastMUD\n")))
|
game.enqeueOutput(game.CreateOutput(pje.connectionId, []byte("Welcome to LastMUD!")))
|
||||||
}
|
}
|
||||||
|
|
||||||
type PlayerLeaveEvent struct {
|
type PlayerLeaveEvent struct {
|
||||||
|
|
|
@ -71,7 +71,7 @@ func (c *Connection) listen() {
|
||||||
event, err := c.server.game().CreatePlayerCommandEvent(c.Id(), message)
|
event, err := c.server.game().CreatePlayerCommandEvent(c.Id(), message)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.conn.Write([]byte(err.Error() + "\n"))
|
c.Write([]byte(err.Error()))
|
||||||
} else {
|
} else {
|
||||||
c.server.game().EnqueueEvent(event)
|
c.server.game().EnqueueEvent(event)
|
||||||
}
|
}
|
||||||
|
@ -121,6 +121,8 @@ func (c *Connection) closeConnection() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Connection) Write(output []byte) (err error) {
|
func (c *Connection) Write(output []byte) (err error) {
|
||||||
|
output = append([]byte("< "), output...)
|
||||||
|
output = append(output, []byte("\n> ")...)
|
||||||
_, err = c.conn.Write(output)
|
_, err = c.conn.Write(output)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue