Skip to content

Commit

Permalink
Merge pull request #2 from C4T-BuT-S4D/mask-input
Browse files Browse the repository at this point in the history
Mask user input
  • Loading branch information
jnovikov authored Dec 8, 2023
2 parents fca8c4a + 08fd71d commit 95314ba
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 13 deletions.
2 changes: 1 addition & 1 deletion cmd/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func main() {

fntmng := fonts.NewManager()
smng := sprites.NewManager()
dialogProvider := &dialog.StandardProvider{}
dialogProvider := dialog.NewStandardProvider(false)

game := server.NewGame(*snapshotsDir, fntmng)

Expand Down
13 changes: 9 additions & 4 deletions internal/dialog/llm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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"
Expand Down
30 changes: 24 additions & 6 deletions internal/dialog/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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":
Expand All @@ -46,3 +60,7 @@ type ClientProvider struct {
func (cp *ClientProvider) Get(_ string) (Dialog, error) {
return NewClientDialog(), nil
}

func (cp *ClientProvider) DisplayInput() bool {
return true
}
10 changes: 9 additions & 1 deletion internal/engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ type Config struct {
type dialogControl struct {
inputBuffer []rune
scroll int
maskInput bool
}

type Engine struct {
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
}
}
Expand Down

0 comments on commit 95314ba

Please sign in to comment.