]> git.r.bdr.sh - rbdr/mobius/blobdiff - client.go
FIx icon ID
[rbdr/mobius] / client.go
index 272be9463879aabea136669e5d6a5e7efeed5f61..2f79552885ef87cd95f607ae29562d7a200d8a8c 100644 (file)
--- a/client.go
+++ b/client.go
@@ -44,6 +44,12 @@ type ClientPrefs struct {
        Tracker   string     `yaml:"Tracker"`
 }
 
        Tracker   string     `yaml:"Tracker"`
 }
 
+func (cp *ClientPrefs) IconBytes() []byte {
+       iconBytes := make([]byte, 2)
+       binary.BigEndian.PutUint16(iconBytes, uint16(cp.IconID))
+       return iconBytes
+}
+
 func readConfig(cfgPath string) (*ClientPrefs, error) {
        fh, err := os.Open(cfgPath)
        if err != nil {
 func readConfig(cfgPath string) (*ClientPrefs, error) {
        fh, err := os.Open(cfgPath)
        if err != nil {
@@ -101,7 +107,6 @@ func NewUI(c *Client) *UI {
        app := tview.NewApplication()
        chatBox := tview.NewTextView().
                SetScrollable(true).
        app := tview.NewApplication()
        chatBox := tview.NewTextView().
                SetScrollable(true).
-               SetText("").
                SetDynamicColors(true).
                SetWordWrap(true).
                SetChangedFunc(func() {
                SetDynamicColors(true).
                SetWordWrap(true).
                SetChangedFunc(func() {
@@ -113,7 +118,6 @@ func NewUI(c *Client) *UI {
        chatInput.
                SetLabel("> ").
                SetFieldBackgroundColor(tcell.ColorDimGray).
        chatInput.
                SetLabel("> ").
                SetFieldBackgroundColor(tcell.ColorDimGray).
-               //SetFieldTextColor(tcell.ColorWhite).
                SetDoneFunc(func(key tcell.Key) {
                        // skip send if user hit enter with no other text
                        if len(chatInput.GetText()) == 0 {
                SetDoneFunc(func(key tcell.Key) {
                        // skip send if user hit enter with no other text
                        if len(chatInput.GetText()) == 0 {
@@ -130,10 +134,12 @@ func NewUI(c *Client) *UI {
 
        chatInput.Box.SetBorder(true).SetTitle("Send")
 
 
        chatInput.Box.SetBorder(true).SetTitle("Send")
 
-       userList := tview.NewTextView().SetDynamicColors(true)
-       userList.SetChangedFunc(func() {
-               app.Draw() // TODO: docs say this is bad but it's the only way to show content during initial render??
-       })
+       userList := tview.
+               NewTextView().
+               SetDynamicColors(true).
+               SetChangedFunc(func() {
+                       app.Draw() // TODO: docs say this is bad but it's the only way to show content during initial render??
+               })
        userList.Box.SetBorder(true).SetTitle("Users")
 
        return &UI{
        userList.Box.SetBorder(true).SetTitle("Users")
 
        return &UI{
@@ -148,12 +154,6 @@ func NewUI(c *Client) *UI {
        }
 }
 
        }
 }
 
-const defaultUsername = "unnamed"
-
-const (
-       trackerListPage = "trackerList"
-)
-
 func (ui *UI) showBookmarks() *tview.List {
        list := tview.NewList()
        list.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
 func (ui *UI) showBookmarks() *tview.List {
        list := tview.NewList()
        list.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
@@ -216,7 +216,7 @@ func (ui *UI) renderSettingsForm() *tview.Flex {
        iconStr := strconv.Itoa(ui.HLClient.pref.IconID)
        settingsForm := tview.NewForm()
        settingsForm.AddInputField("Your Name", ui.HLClient.pref.Username, 0, nil, nil)
        iconStr := strconv.Itoa(ui.HLClient.pref.IconID)
        settingsForm := tview.NewForm()
        settingsForm.AddInputField("Your Name", ui.HLClient.pref.Username, 0, nil, nil)
-       settingsForm.AddInputField("IconID",iconStr, 0, func(idStr string, _ rune) bool {
+       settingsForm.AddInputField("IconID", iconStr, 0, func(idStr string, _ rune) bool {
                _, err := strconv.Atoi(idStr)
                return err == nil
        }, nil)
                _, err := strconv.Atoi(idStr)
                return err == nil
        }, nil)
@@ -255,12 +255,6 @@ func (ui *UI) renderSettingsForm() *tview.Flex {
        return centerFlex
 }
 
        return centerFlex
 }
 
-var (
-       srvIP    string
-       srvLogin string
-       srvPass  string
-)
-
 // DebugBuffer wraps a *tview.TextView and adds a Sync() method to make it available as a Zap logger
 type DebugBuffer struct {
        TextView *tview.TextView
 // DebugBuffer wraps a *tview.TextView and adds a Sync() method to make it available as a Zap logger
 type DebugBuffer struct {
        TextView *tview.TextView
@@ -290,19 +284,13 @@ func (ui *UI) joinServer(addr, login, password string) error {
 }
 
 func (ui *UI) renderJoinServerForm(server, login, password, backPage string, save, defaultConnect bool) *tview.Flex {
 }
 
 func (ui *UI) renderJoinServerForm(server, login, password, backPage string, save, defaultConnect bool) *tview.Flex {
-       srvIP = server
        joinServerForm := tview.NewForm()
        joinServerForm.
        joinServerForm := tview.NewForm()
        joinServerForm.
-               AddInputField("Server", server, 20, nil, func(text string) {
-                       srvIP = text
-               }).
-               AddInputField("Login", login, 20, nil, func(text string) {
-                       l := []byte(text)
-                       ui.HLClient.Login = &l
-               }).
-               AddPasswordField("Password", password, 20, '*', nil).
+               AddInputField("Server", server, 0, nil, nil).
+               AddInputField("Login", login, 0, nil, nil).
+               AddPasswordField("Password", password, 0, '*', nil).
                AddCheckbox("Save", save, func(checked bool) {
                AddCheckbox("Save", save, func(checked bool) {
-                       // TODO
+                       // TODO: Implement bookmark saving
                }).
                AddButton("Cancel", func() {
                        ui.Pages.SwitchToPage(backPage)
                }).
                AddButton("Cancel", func() {
                        ui.Pages.SwitchToPage(backPage)
@@ -429,9 +417,8 @@ func (ui *UI) Start() {
                0, 1, true,
        )
 
                0, 1, true,
        )
 
-       joinServerPage := ui.renderJoinServerForm("", GuestAccount, "", "home", false, false)
-
        mainMenu.AddItem("Join Server", "", 'j', func() {
        mainMenu.AddItem("Join Server", "", 'j', func() {
+               joinServerPage := ui.renderJoinServerForm("", GuestAccount, "", "home", false, false)
                ui.Pages.AddPage("joinServer", joinServerPage, true, true)
        }).
                AddItem("Bookmarks", "", 'b', func() {
                ui.Pages.AddPage("joinServer", joinServerPage, true, true)
        }).
                AddItem("Bookmarks", "", 'b', func() {
@@ -442,8 +429,6 @@ func (ui *UI) Start() {
                        ui.Pages.AddAndSwitchToPage("trackerList", ui.trackerList, true)
                }).
                AddItem("Settings", "", 's', func() {
                        ui.Pages.AddAndSwitchToPage("trackerList", ui.trackerList, true)
                }).
                AddItem("Settings", "", 's', func() {
-                       //ui.Pages.AddPage("settings", ui.renderSettingsForm(), true, false)
-
                        ui.Pages.AddPage("settings", ui.renderSettingsForm(), true, true)
                }).
                AddItem("Quit", "", 'q', func() {
                        ui.Pages.AddPage("settings", ui.renderSettingsForm(), true, true)
                }).
                AddItem("Quit", "", 'q', func() {
@@ -461,12 +446,10 @@ func (ui *UI) Start() {
                }
                // Show Logs
                if event.Key() == tcell.KeyCtrlL {
                }
                // Show Logs
                if event.Key() == tcell.KeyCtrlL {
-                       //curPage, _ := ui.Pages.GetFrontPage()
                        ui.HLClient.DebugBuf.TextView.ScrollToEnd()
                        ui.HLClient.DebugBuf.TextView.SetBorder(true).SetTitle("Logs")
                        ui.HLClient.DebugBuf.TextView.SetDoneFunc(func(key tcell.Key) {
                                if key == tcell.KeyEscape {
                        ui.HLClient.DebugBuf.TextView.ScrollToEnd()
                        ui.HLClient.DebugBuf.TextView.SetBorder(true).SetTitle("Logs")
                        ui.HLClient.DebugBuf.TextView.SetDoneFunc(func(key tcell.Key) {
                                if key == tcell.KeyEscape {
-                                       //ui.Pages.SwitchToPage("serverUI")
                                        ui.Pages.RemovePage("logs")
                                }
                        })
                                        ui.Pages.RemovePage("logs")
                                }
                        })
@@ -477,7 +460,8 @@ func (ui *UI) Start() {
        })
 
        if err := ui.App.SetRoot(ui.Pages, true).SetFocus(ui.Pages).Run(); err != nil {
        })
 
        if err := ui.App.SetRoot(ui.Pages, true).SetFocus(ui.Pages).Run(); err != nil {
-               panic(err)
+               ui.App.Stop()
+               os.Exit(1)
        }
 }
 
        }
 }
 
@@ -739,7 +723,7 @@ func handleClientTranShowAgreement(c *Client, t *Transaction) (res []Transaction
                                        *NewTransaction(
                                                tranAgreed, nil,
                                                NewField(fieldUserName, []byte(c.pref.Username)),
                                        *NewTransaction(
                                                tranAgreed, nil,
                                                NewField(fieldUserName, []byte(c.pref.Username)),
-                                               NewField(fieldUserIconID, *c.Icon),
+                                               NewField(fieldUserIconID, c.pref.IconBytes()),
                                                NewField(fieldUserFlags, []byte{0x00, 0x00}),
                                                NewField(fieldOptions, []byte{0x00, 0x00}),
                                        ),
                                                NewField(fieldUserFlags, []byte{0x00, 0x00}),
                                                NewField(fieldOptions, []byte{0x00, 0x00}),
                                        ),
@@ -748,7 +732,7 @@ func handleClientTranShowAgreement(c *Client, t *Transaction) (res []Transaction
                                c.UI.Pages.HidePage("agreement")
                                c.UI.App.SetFocus(c.UI.chatInput)
                        } else {
                                c.UI.Pages.HidePage("agreement")
                                c.UI.App.SetFocus(c.UI.chatInput)
                        } else {
-                               c.Disconnect()
+                               _ = c.Disconnect()
                                c.UI.Pages.SwitchToPage("home")
                        }
                },
                                c.UI.Pages.SwitchToPage("home")
                        }
                },
@@ -860,7 +844,7 @@ func (c *Client) LogIn(login string, password string) error {
                *NewTransaction(
                        tranLogin, nil,
                        NewField(fieldUserName, []byte(c.pref.Username)),
                *NewTransaction(
                        tranLogin, nil,
                        NewField(fieldUserName, []byte(c.pref.Username)),
-                       NewField(fieldUserIconID, []byte{0x07, 0xd1}),
+                       NewField(fieldUserIconID, c.pref.IconBytes()),
                        NewField(fieldUserLogin, []byte(NegatedUserString([]byte(login)))),
                        NewField(fieldUserPassword, []byte(NegatedUserString([]byte(password)))),
                        NewField(fieldVersion, []byte{0, 2}),
                        NewField(fieldUserLogin, []byte(NegatedUserString([]byte(login)))),
                        NewField(fieldUserPassword, []byte(NegatedUserString([]byte(password)))),
                        NewField(fieldVersion, []byte{0, 2}),
@@ -868,61 +852,6 @@ func (c *Client) LogIn(login string, password string) error {
        )
 }
 
        )
 }
 
-//// Agree agrees to the server agreement and sends user info, completing the login sequence
-//func (c *Client) Agree() {
-//     c.Send(
-//             NewTransaction(
-//                     tranAgreed, 3,
-//                     []Field{
-//                             NewField(fieldUserName, []byte("test")),
-//                             NewField(fieldUserIconID, *c.Icon),
-//                             NewField(fieldUserFlags, []byte{0x00, 0x00}),
-//                     },
-//             ),
-//     )
-//     //
-//     //// Block until we receive the agreement reply from the server
-//     //_ = c.WaitForTransaction(tranAgreed)
-//}
-
-//func (c *Client) WaitForTransaction(id uint16) Transaction {
-//     var trans Transaction
-//     for {
-//             buf := make([]byte, 1400)
-//             readLen, err := c.Connection.Read(buf)
-//             if err != nil {
-//                     panic(err)
-//             }
-//
-//             transactions := ReadTransactions(buf[:readLen])
-//             tran, err := FindTransactions(id, transactions)
-//             if err == nil {
-//                     fmt.Println("returning")
-//                     return tran
-//             }
-//     }
-//
-//     return trans
-//}
-
-//func (c *Client) Read() error {
-//     // Main loop where we wait for and take action on client requests
-//     for {
-//             buf := make([]byte, 1400)
-//             readLen, err := c.Connection.Read(buf)
-//             if err != nil {
-//                     panic(err)
-//             }
-//             transactions, _, _ := readTransactions(buf[:readLen])
-//
-//             for _, t := range transactions {
-//                     c.HandleTransaction(&t)
-//             }
-//     }
-//
-//     return nil
-//}
-
 func (c *Client) Send(t Transaction) error {
        requestNum := binary.BigEndian.Uint16(t.Type)
        tID := binary.BigEndian.Uint32(t.ID)
 func (c *Client) Send(t Transaction) error {
        requestNum := binary.BigEndian.Uint16(t.Type)
        tID := binary.BigEndian.Uint32(t.ID)