Skip to content

Commit

Permalink
cmd/argument.go: Don't read multiple arguments for player names (quot…
Browse files Browse the repository at this point in the history
…es take care of names with spaces).
  • Loading branch information
Sandertv committed Dec 28, 2024
1 parent 918a904 commit a7874df
Showing 1 changed file with 7 additions and 14 deletions.
21 changes: 7 additions & 14 deletions server/cmd/argument.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,27 +301,20 @@ func (p parser) parseTargets(line *Line, tx *world.Tx) ([]Target, error) {
}
return []Target{players[rand.Intn(len(players))]}, nil
default:
target, err := p.parsePlayer(line, players)
target, err := p.parsePlayer(first, players)
if err != nil {
return nil, err
}
return []Target{target}, nil
}
}

// parsePlayer parses one Player from the Line, consuming multiple arguments
// from Line if necessary.
func (p parser) parsePlayer(line *Line, players []NamedTarget) (Target, error) {
name := ""
for i := 0; i < line.Len(); i++ {
name += line.args[0]
if ind := slices.IndexFunc(players, func(target NamedTarget) bool {
return strings.EqualFold(target.Name(), name)
}); ind != -1 {
return players[ind], nil
}
name += " "
line.RemoveNext()
// parsePlayer attempts to find a target whose name matches the name passed.
func (p parser) parsePlayer(name string, players []NamedTarget) (Target, error) {
if ind := slices.IndexFunc(players, func(target NamedTarget) bool {
return strings.EqualFold(target.Name(), name)
}); ind != -1 {
return players[ind], nil
}
return nil, MessagePlayerNotFound.F()
}

0 comments on commit a7874df

Please sign in to comment.