X-Git-Url: https://git.r.bdr.sh/rbdr/mobius/blobdiff_plain/8b53c840617ef7c2b14b197759d3c1722fe478a1..5dd573088995edec81ac34eeed20bdb5819d9136:/client.go?ds=sidebyside diff --git a/client.go b/client.go index 12a9355..79f623a 100644 --- a/client.go +++ b/client.go @@ -17,11 +17,15 @@ import ( "math/rand" "net" "os" + "strconv" "strings" "time" ) const clientConfigPath = "/usr/local/etc/mobius-client-config.yaml" +const ( + trackerListPage = "trackerList" +) //go:embed client/banners/*.txt var bannerDir embed.FS @@ -37,6 +41,13 @@ type ClientPrefs struct { Username string `yaml:"Username"` IconID int `yaml:"IconID"` Bookmarks []Bookmark `yaml:"Bookmarks"` + 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) { @@ -57,10 +68,8 @@ func readConfig(cfgPath string) (*ClientPrefs, error) { type Client struct { DebugBuf *DebugBuffer Connection net.Conn - UserName []byte Login *[]byte Password *[]byte - Icon *[]byte Flags *[]byte ID *[]byte Version []byte @@ -96,7 +105,6 @@ func NewUI(c *Client) *UI { app := tview.NewApplication() chatBox := tview.NewTextView(). SetScrollable(true). - SetText(""). SetDynamicColors(true). SetWordWrap(true). SetChangedFunc(func() { @@ -108,7 +116,6 @@ func NewUI(c *Client) *UI { 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 { @@ -125,10 +132,12 @@ func NewUI(c *Client) *UI { 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{ @@ -143,12 +152,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 { @@ -177,7 +180,7 @@ func (ui *UI) showBookmarks() *tview.List { } func (ui *UI) getTrackerList() *tview.List { - listing, err := GetListing("hltracker.com:5498") + listing, err := GetListing(ui.HLClient.pref.Tracker) if err != nil { spew.Dump(err) } @@ -208,10 +211,20 @@ func (ui *UI) getTrackerList() *tview.List { } func (ui *UI) renderSettingsForm() *tview.Flex { + iconStr := strconv.Itoa(ui.HLClient.pref.IconID) settingsForm := tview.NewForm() - settingsForm.AddInputField("Your Name", ui.HLClient.pref.Username, 20, nil, nil) + settingsForm.AddInputField("Your Name", ui.HLClient.pref.Username, 0, nil, nil) + settingsForm.AddInputField("IconID", iconStr, 0, func(idStr string, _ rune) bool { + _, err := strconv.Atoi(idStr) + return err == nil + }, nil) + settingsForm.AddInputField("Tracker", ui.HLClient.pref.Tracker, 0, nil, nil) settingsForm.AddButton("Save", func() { ui.HLClient.pref.Username = settingsForm.GetFormItem(0).(*tview.InputField).GetText() + iconStr = settingsForm.GetFormItem(1).(*tview.InputField).GetText() + ui.HLClient.pref.IconID, _ = strconv.Atoi(iconStr) + ui.HLClient.pref.Tracker = settingsForm.GetFormItem(2).(*tview.InputField).GetText() + out, err := yaml.Marshal(&ui.HLClient.pref) if err != nil { // TODO: handle err @@ -240,12 +253,6 @@ func (ui *UI) renderSettingsForm() *tview.Flex { 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 @@ -275,19 +282,13 @@ func (ui *UI) joinServer(addr, login, password string) error { } func (ui *UI) renderJoinServerForm(server, login, password, backPage string, save, defaultConnect bool) *tview.Flex { - srvIP = server 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) { - // TODO + // TODO: Implement bookmark saving }). AddButton("Cancel", func() { ui.Pages.SwitchToPage(backPage) @@ -414,9 +415,8 @@ func (ui *UI) Start() { 0, 1, true, ) - joinServerPage := ui.renderJoinServerForm("", GuestAccount, "", "home", false, false) - mainMenu.AddItem("Join Server", "", 'j', func() { + joinServerPage := ui.renderJoinServerForm("", GuestAccount, "", "home", false, false) ui.Pages.AddPage("joinServer", joinServerPage, true, true) }). AddItem("Bookmarks", "", 'b', func() { @@ -427,8 +427,6 @@ func (ui *UI) Start() { 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() { @@ -446,12 +444,10 @@ func (ui *UI) Start() { } // 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.Pages.SwitchToPage("serverUI") ui.Pages.RemovePage("logs") } }) @@ -462,13 +458,13 @@ func (ui *UI) Start() { }) if err := ui.App.SetRoot(ui.Pages, true).SetFocus(ui.Pages).Run(); err != nil { - panic(err) + ui.App.Stop() + os.Exit(1) } } func NewClient(username string, logger *zap.SugaredLogger) *Client { c := &Client{ - Icon: &[]byte{0x07, 0xd7}, Logger: logger, activeTasks: make(map[uint32]*Transaction), Handlers: clientHandlers, @@ -692,15 +688,15 @@ func (c *Client) renderUserList() { for _, u := range c.UserList { flagBitmap := big.NewInt(int64(binary.BigEndian.Uint16(u.Flags))) if flagBitmap.Bit(userFlagAdmin) == 1 { - fmt.Fprintf(c.UI.userList, "[red::b]%s[-:-:-]\n", u.Name) + _, _ = fmt.Fprintf(c.UI.userList, "[red::b]%s[-:-:-]\n", u.Name) } else { - fmt.Fprintf(c.UI.userList, "%s\n", u.Name) + _, _ = fmt.Fprintf(c.UI.userList, "%s\n", u.Name) } } } func handleClientChatMsg(c *Client, t *Transaction) (res []Transaction, err error) { - fmt.Fprintf(c.UI.chatBox, "%s \n", t.GetField(fieldData).Data) + _, _ = fmt.Fprintf(c.UI.chatBox, "%s \n", t.GetField(fieldData).Data) return res, err } @@ -724,7 +720,7 @@ func handleClientTranShowAgreement(c *Client, t *Transaction) (res []Transaction *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}), ), @@ -733,7 +729,7 @@ func handleClientTranShowAgreement(c *Client, t *Transaction) (res []Transaction c.UI.Pages.HidePage("agreement") c.UI.App.SetFocus(c.UI.chatInput) } else { - c.Disconnect() + _ = c.Disconnect() c.UI.Pages.SwitchToPage("home") } }, @@ -845,7 +841,7 @@ func (c *Client) LogIn(login string, password string) error { *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}), @@ -853,61 +849,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)