From 08fd71d498db754a0955c18849453ee1c161a878 Mon Sep 17 00:00:00 2001 From: jnovikov Date: Fri, 8 Dec 2023 18:59:04 +0000 Subject: [PATCH] Mask user input --- cmd/client/main.go | 2 +- cmd/server/main.go | 2 +- internal/dialog/llm.go | 13 +++++++++---- internal/dialog/provider.go | 30 ++++++++++++++++++++++++------ internal/engine/engine.go | 10 +++++++++- 5 files changed, 44 insertions(+), 13 deletions(-) diff --git a/cmd/client/main.go b/cmd/client/main.go index 3ad6505..c1cb228 100644 --- a/cmd/client/main.go +++ b/cmd/client/main.go @@ -86,7 +86,7 @@ func NewGame(ctx context.Context, client gameserverpb.GameServerServiceClient, l } }() } else { - e, err := engine.New(engineConfig, smng, fntmng, mscmng, &dialog.StandardProvider{}) + e, err := engine.New(engineConfig, smng, fntmng, mscmng, dialog.NewStandardProvider(true)) if err != nil { return nil, fmt.Errorf("initializing engine: %w", err) } diff --git a/cmd/server/main.go b/cmd/server/main.go index 10e5095..348229b 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -41,7 +41,7 @@ func main() { fntmng := fonts.NewManager() smng := sprites.NewManager() - dialogProvider := &dialog.StandardProvider{} + dialogProvider := dialog.NewStandardProvider(false) game := server.NewGame(*snapshotsDir, fntmng) diff --git a/internal/dialog/llm.go b/internal/dialog/llm.go index 0e49af6..d96f635 100644 --- a/internal/dialog/llm.go +++ b/internal/dialog/llm.go @@ -6,16 +6,18 @@ import ( "encoding/json" "fmt" "net/http" + "strings" "time" ) const timeout time.Duration = 30 * time.Second type LLM struct { - Intro string - URL string - Token string - state State + Intro string + URL string + Token string + MaskInput bool + state State } func (l *LLM) Greeting() { @@ -63,6 +65,9 @@ func (l *LLM) checkIsFlag(text string) bool { func (l *LLM) Feed(text string) { l.state.Text = fmt.Sprintf("> %s\n", text) + if l.MaskInput { + l.state.Text = fmt.Sprintf("> %s\n", strings.Repeat("*", len(text))) + } if l.checkIsFlag(text) { l.state.Text += "You defeated me!!!\n" diff --git a/internal/dialog/provider.go b/internal/dialog/provider.go index 7045e57..0da011c 100644 --- a/internal/dialog/provider.go +++ b/internal/dialog/provider.go @@ -7,9 +7,21 @@ import ( type Provider interface { Get(id string) (Dialog, error) + DisplayInput() bool } type StandardProvider struct { + ShowInput bool +} + +func NewStandardProvider(showInput bool) *StandardProvider { + return &StandardProvider{ + ShowInput: showInput, + } +} + +func (sp *StandardProvider) DisplayInput() bool { + return sp.ShowInput } func (sp *StandardProvider) Get(id string) (Dialog, error) { @@ -22,15 +34,17 @@ func (sp *StandardProvider) Get(id string) (Dialog, error) { return NewWiseTree(), nil case "llm-boss-1": return &LLM{ - Intro: "I'm the boss of Arbalest of Siberia. Don't try to defeat me!", - Token: os.Getenv("AUTH_TOKEN"), - URL: "http://localhost:8081", + Intro: "I'm the boss of Arbalest of Siberia. Don't try to defeat me!", + Token: os.Getenv("AUTH_TOKEN"), + URL: "http://localhost:8081", + MaskInput: !sp.DisplayInput(), }, nil case "ceo-boss": return &LLM{ - Intro: "I'm the REAL boss of Arbalest of Siberia. It's impossible to defeat me!", - Token: os.Getenv("AUTH_TOKEN"), - URL: "http://localhost:8082", + Intro: "I'm the REAL boss of Arbalest of Siberia. It's impossible to defeat me!", + Token: os.Getenv("AUTH_TOKEN"), + URL: "http://localhost:8082", + MaskInput: !sp.DisplayInput(), }, nil case "capytoshka": @@ -46,3 +60,7 @@ type ClientProvider struct { func (cp *ClientProvider) Get(_ string) (Dialog, error) { return NewClientDialog(), nil } + +func (cp *ClientProvider) DisplayInput() bool { + return true +} diff --git a/internal/engine/engine.go b/internal/engine/engine.go index b7ef865..dbf9111 100644 --- a/internal/engine/engine.go +++ b/internal/engine/engine.go @@ -60,6 +60,7 @@ type Config struct { type dialogControl struct { inputBuffer []rune scroll int + maskInput bool } type Engine struct { @@ -444,6 +445,9 @@ func New(config Config, spriteManager *sprites.Manager, fontsManager *fonts.Mana playerSpawn: playerPos, Level: config.Level, TeamName: strings.Split(os.Getenv("AUTH_TOKEN"), ":")[0], + dialogControl: dialogControl{ + maskInput: !dialogProvider.DisplayInput(), + }, }, nil } @@ -595,7 +599,11 @@ func (e *Engine) drawNPCDialog(screen *ebiten.Image) { if len(e.dialogControl.inputBuffer) > 0 { dtbx, dtby := dtx, dty+float64((len(visibleLines)-1)*face.Metrics().Height.Floor())+1.0*float64(face.Metrics().Height.Floor()) c := color.RGBA{R: 0x00, G: 0xff, B: 0xff, A: 0xff} - x := input.AutoWrap(string(e.dialogControl.inputBuffer), face, ibw-camera.WIDTH/32) + ibuf := string(e.dialogControl.inputBuffer) + if e.dialogControl.maskInput { + ibuf = strings.Repeat("*", len(ibuf)) + } + x := input.AutoWrap(ibuf, face, ibw-camera.WIDTH/32) text.Draw(screen, strings.Join(x, "\n"), face, int(dtbx), int(dtby), c) } }