Require password when creating a private match

This commit is contained in:
Trevor Slocum 2023-09-11 20:01:05 -07:00
parent 7df3279bc4
commit 0dabbef6fb
4 changed files with 55 additions and 28 deletions

2
.gitignore vendored
View file

@ -2,4 +2,4 @@
dist/
vendor/
*.sh
bgammon
bgammon-cli

69
app.go
View file

@ -31,8 +31,9 @@ var (
inputField *cview.InputField
loginForm *cview.Form
createGameForm *cview.Form
createGameGrid *cview.Grid
createGameForm *cview.Form
createGamePasswordField *cview.InputField
createGameGrid *cview.Grid
statusWriter *bufferWriter
gameWriter *bufferWriter
@ -115,7 +116,7 @@ func beforeFocus(p cview.Primitive) bool {
if viewScreen == ScreenLobby {
if showCreateGameDialog {
return primitiveInForm(p, createGameForm)
return p != gameList && p != gameBuffer && p != statusBuffer && p != inputField
} else if gameInProgress {
return p == inputField
} else {
@ -214,9 +215,12 @@ func buildLayout() {
func resetCreateGameDialog() {
nameInput := createGameForm.GetFormItem(0).(*cview.InputField)
typeDropDown := createGameForm.GetFormItem(1).(*cview.DropDown)
passwordInput := createGameForm.GetFormItem(2).(*cview.InputField)
nameInput.SetText("")
typeDropDown.SetCurrentOption(0)
passwordInput.SetText("")
passwordInput.SetVisible(false)
}
func acceptCreateGameDialog() {
@ -226,17 +230,21 @@ func acceptCreateGameDialog() {
nameInput := createGameForm.GetFormItem(0).(*cview.InputField)
typeDropDown := createGameForm.GetFormItem(1).(*cview.DropDown)
passwordInput := createGameForm.GetFormItem(2).(*cview.InputField)
_ = nameInput // TODO
gameType := "public"
typeAndPassword := "public"
index, _ := typeDropDown.GetCurrentOption()
if index == 1 {
gameType = "private"
// TODO prompt password
if strings.TrimSpace(passwordInput.GetText()) == "" {
createGameForm.SetFocus(2)
app.SetFocus(createGameForm)
l("Please enter a password to create a private match.")
return
}
typeAndPassword = fmt.Sprintf("private %s", passwordInput.GetText())
}
board.client.Out <- []byte(fmt.Sprintf("c %s", gameType))
board.client.Out <- []byte(fmt.Sprintf("c %s %s", typeAndPassword, nameInput.GetText()))
}
func RunApp(c *Client, b *GameBoard) error {
@ -298,16 +306,16 @@ func RunApp(c *Client, b *GameBoard) error {
usernameField.SetAcceptanceFunc(func(textToCheck string, lastChar rune) bool {
return !unicode.IsSpace(lastChar)
})
passwordField := cview.NewInputField()
passwordField.SetLabel("Password")
passwordField.SetFieldWidth(16)
passwordField.SetMaskCharacter('*')
loginPasswordField := cview.NewInputField()
loginPasswordField.SetLabel("Password")
loginPasswordField.SetFieldWidth(16)
loginPasswordField.SetMaskCharacter('*')
loginForm.AddFormItem(usernameField)
loginForm.AddFormItem(passwordField)
loginForm.AddFormItem(loginPasswordField)
connectFunc := func() {
c.Username = usernameField.GetText()
c.Password = passwordField.GetText()
c.Password = loginPasswordField.GetText()
logIn(c)
}
@ -481,9 +489,26 @@ func RunApp(c *Client, b *GameBoard) error {
createGameLabel.SetText("[" + colorYellow + "]Create match[-]")
createGameLabel.SetDynamicColors(true)
publicOption := cview.NewDropDownOption("Public")
publicOption.SetSelectedFunc(func(index int, option *cview.DropDownOption) {
if createGamePasswordField == nil {
return
}
createGamePasswordField.SetVisible(false)
})
privateOption := cview.NewDropDownOption("Private")
privateOption.SetSelectedFunc(func(index int, option *cview.DropDownOption) {
if createGamePasswordField == nil {
return
}
createGamePasswordField.SetVisible(true)
})
createGameForm = cview.NewForm()
createGameForm.AddInputField("Name", "", 10, nil, nil)
createGameForm.AddDropDown("Type", 0, nil, []*cview.DropDownOption{cview.NewDropDownOption("Public"), cview.NewDropDownOption("Private")})
createGameForm.AddDropDown("Type", 0, nil, []*cview.DropDownOption{publicOption, privateOption})
createGameForm.AddPasswordField("Password", "", 10, '*', nil)
createGameForm.AddButton("Cancel", func() {
showCreateGameDialog = false
buildLayout()
@ -491,6 +516,8 @@ func RunApp(c *Client, b *GameBoard) error {
createGameForm.AddButton("Create", func() {
acceptCreateGameDialog()
})
createGamePasswordField = createGameForm.GetFormItem(2).(*cview.InputField)
resetCreateGameDialog()
createGameGrid = cview.NewGrid()
createGameGrid.SetRows(1, -1)
@ -606,14 +633,14 @@ func HandleEvents(c *Client, b *GameBoard) {
showCreateGameDialog = false
setScreen(ScreenGame)
} else {
lg(fmt.Sprintf("%s joined the game.", ev.Player))
lg(fmt.Sprintf("%s joined the match.", ev.Player))
}
case *bgammon.EventFailedJoin:
l(fmt.Sprintf("*** Failed to join game: %s", ev.Reason))
l(fmt.Sprintf("*** Failed to join match: %s", ev.Reason))
case *bgammon.EventLeft:
if ev.PlayerNumber == 1 {
if b.Board.Player1.Name == ev.Player {
b.Board.Player1.Name = ""
} else if ev.PlayerNumber == 2 {
} else if b.Board.Player2.Name == ev.Player {
b.Board.Player2.Name = ""
}
b.Update()
@ -621,7 +648,7 @@ func HandleEvents(c *Client, b *GameBoard) {
gameInProgress = false
setScreen(ScreenLobby)
} else {
lg(fmt.Sprintf("%s left the game.", ev.Player))
lg(fmt.Sprintf("%s left the match.", ev.Player))
}
case *bgammon.EventBoard:
b.Board = &ev.GameState

4
go.mod
View file

@ -3,8 +3,8 @@ module code.rocket9labs.com/tslocum/bgammon-cli
go 1.16
require (
code.rocket9labs.com/tslocum/bgammon v0.0.0-20230910220109-d735899304ee
code.rocketnine.space/tslocum/cview v1.5.8
code.rocket9labs.com/tslocum/bgammon v0.0.0-20230912025931-f63bd90964af
code.rocketnine.space/tslocum/cview v1.5.9-0.20230912025026-d5de9a7b0a6c
github.com/gdamore/tcell/v2 v2.6.0
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/rivo/uniseg v0.4.4 // indirect

8
go.sum
View file

@ -1,9 +1,9 @@
code.rocket9labs.com/tslocum/bgammon v0.0.0-20230910220109-d735899304ee h1:NCURJ36ztPbyYD1+85XmL1O3mbOo/8mNuKxBNFZTj/s=
code.rocket9labs.com/tslocum/bgammon v0.0.0-20230910220109-d735899304ee/go.mod h1:zacnxtEUuMgpvs0DOJRU5TGWSB9Tpp2LdYgiQdfVNYc=
code.rocket9labs.com/tslocum/bgammon v0.0.0-20230912025931-f63bd90964af h1:vxuzn61owiy3wbloUopqYaKgH2dCymld32bG1vUVpMs=
code.rocket9labs.com/tslocum/bgammon v0.0.0-20230912025931-f63bd90964af/go.mod h1:zacnxtEUuMgpvs0DOJRU5TGWSB9Tpp2LdYgiQdfVNYc=
code.rocketnine.space/tslocum/cbind v0.1.5 h1:i6NkeLLNPNMS4NWNi3302Ay3zSU6MrqOT+yJskiodxE=
code.rocketnine.space/tslocum/cbind v0.1.5/go.mod h1:LtfqJTzM7qhg88nAvNhx+VnTjZ0SXBJtxBObbfBWo/M=
code.rocketnine.space/tslocum/cview v1.5.8 h1:G90dP78WmZ8obrLNggx9z4kPXgJDT/MhsKpEAoqqto8=
code.rocketnine.space/tslocum/cview v1.5.8/go.mod h1:+MjJYGHPuMPgVgZEir/JZNIDFvsL87kOTzwIba6au2A=
code.rocketnine.space/tslocum/cview v1.5.9-0.20230912025026-d5de9a7b0a6c h1:6PkEnjl48DmwEgGibnOTi7tZGT66HTijiLSitu4eCh8=
code.rocketnine.space/tslocum/cview v1.5.9-0.20230912025026-d5de9a7b0a6c/go.mod h1:+MjJYGHPuMPgVgZEir/JZNIDFvsL87kOTzwIba6au2A=
github.com/gdamore/encoding v1.0.0 h1:+7OoQ1Bc6eTm5niUzBa0Ctsh6JbMW6Ra+YNuAtDBdko=
github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo5dl+VrEg=
github.com/gdamore/tcell/v2 v2.2.0/go.mod h1:cTTuF84Dlj/RqmaCIV5p4w8uG1zWdk0SF6oBpwHp4fU=