Support player registration

This commit is contained in:
Trevor Slocum 2021-07-09 00:47:36 -07:00
parent 334628fd82
commit 182b022dc1
2 changed files with 52 additions and 6 deletions

21
app.go
View file

@ -71,6 +71,14 @@ func logIn(c *Client) {
go c.connect()
}
func register(c *Client) {
app.SetRoot(uiGrid, true)
app.SetFocus(inputField)
c.registerAccount = true
go c.connect()
}
func RunApp(c *Client) error {
app = cview.NewApplication()
app.EnableMouse(true)
@ -150,6 +158,9 @@ func RunApp(c *Client) error {
usernameField := cview.NewInputField()
usernameField.SetLabel("Username")
usernameField.SetFieldWidth(16)
usernameField.SetAcceptanceFunc(func(textToCheck string, lastChar rune) bool {
return (lastChar >= 'a' && lastChar <= 'z') || (lastChar >= 'A' && lastChar <= 'Z') || lastChar == '_'
})
passwordField := cview.NewInputField()
passwordField.SetLabel("Password")
passwordField.SetFieldWidth(16)
@ -164,6 +175,13 @@ func RunApp(c *Client) error {
logIn(c)
}
registerFunc := func() {
c.username = usernameField.GetText()
c.password = passwordField.GetText()
register(c)
}
app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
if !c.loggedin {
if event.Key() == tcell.KeyEnter {
@ -188,7 +206,8 @@ func RunApp(c *Client) error {
return event
})
form.AddButton("Connect", connectFunc)
form.AddButton("Log in", connectFunc)
form.AddButton("Register", registerFunc)
form.SetPadding(1, 0, 2, 0)
logInHeader := cview.NewTextView()

View file

@ -64,6 +64,8 @@ type Client struct {
username string
password string
registerAccount bool
loggedin bool
motd []byte
rawMode bool
@ -152,7 +154,22 @@ func (c *Client) CallTELNET(ctx telnet.Context, w telnet.Writer, r telnet.Reader
bt := strings.TrimSpace(b.String())
if bt == "login:" {
b.Reset()
c.out <- []byte(fmt.Sprintf("login bgammon 1008 %s %s", c.username, c.password))
if c.registerAccount {
time.Sleep(5 * time.Second)
c.out <- []byte("guest")
time.Sleep(5 * time.Second)
c.out <- []byte(fmt.Sprintf("name %s", c.username))
time.Sleep(2 * time.Second)
c.out <- []byte(c.password)
time.Sleep(2 * time.Second)
c.out <- []byte(c.password)
time.Sleep(5 * time.Second)
c.registerAccount = false
c.reconnect()
continue
} else {
c.out <- []byte(fmt.Sprintf("login bgammon 1008 %s %s", c.username, c.password))
}
c.loggedin = true
go func() {
@ -289,15 +306,25 @@ func (c *Client) watchRandomGame() {
c.out <- []byte("watch " + option)
}
func (c *Client) dial() {
err := telnet.DialToAndCall(serverAddress, c)
if err != nil {
lf("disconnected: %s", err)
}
}
func (c *Client) connect() {
l(fmt.Sprintf("Connecting to %s...", serverAddress))
go c.keepAlive()
err := telnet.DialToAndCall(serverAddress, c)
if err != nil {
lf("disconnected: %s", err)
}
c.dial()
}
func (c *Client) reconnect() {
c.out <- []byte("bye")
time.Sleep(time.Second)
c.dial()
}
func (c *Client) eventLoop() {