From 029678b4fd40a2e5117cfafe8a0648c53ff17b7b Mon Sep 17 00:00:00 2001 From: wh_zcy Date: Thu, 9 Mar 2023 14:03:48 +0800 Subject: [PATCH] =?UTF-8?q?=E8=87=AA=E5=AE=9A=E4=B9=89=E5=BE=AA=E7=8E=AF?= =?UTF-8?q?=E6=8A=BD=E5=8D=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/v2/FyneApp.toml | 4 +- cmd/v2/ui/app_monitor.go | 119 +++++++-------------- cmd/v2/ui/mainwindow.go | 35 +++--- cmd/v2/ui/views/hero_zhaomu.go | 188 +++++++++++++++++++++++++++++++-- 4 files changed, 241 insertions(+), 105 deletions(-) diff --git a/cmd/v2/FyneApp.toml b/cmd/v2/FyneApp.toml index 3059a2a98..fce5c6979 100644 --- a/cmd/v2/FyneApp.toml +++ b/cmd/v2/FyneApp.toml @@ -4,5 +4,5 @@ Website = "http://legu.cc" Icon = "app.png" Name = "RobotGUI" ID = "cc.legu.app" - Version = "1.2.11" - Build = 41 + Version = "1.2.13" + Build = 43 diff --git a/cmd/v2/ui/app_monitor.go b/cmd/v2/ui/app_monitor.go index 73d73a91b..0543f2246 100644 --- a/cmd/v2/ui/app_monitor.go +++ b/cmd/v2/ui/app_monitor.go @@ -9,7 +9,6 @@ import ( "fyne.io/fyne/v2" "fyne.io/fyne/v2/container" - "fyne.io/fyne/v2/data/binding" "fyne.io/fyne/v2/driver/desktop" "fyne.io/fyne/v2/layout" "fyne.io/fyne/v2/theme" @@ -21,12 +20,10 @@ import ( type appMonitor struct { appAdapter - obs observer.Observer - logPanel *widget.Entry - monitorHeader *widget.List - monitorList *widget.List - monitorBinding binding.UntypedList - monitorData *model.PushModelList + obs observer.Observer + logPanel *widget.Entry + monitorList *common.ItemList + id int64 } func (app *appMonitor) LazyInit(service service.PttService, obs observer.Observer) error { @@ -42,21 +39,35 @@ func (app *appMonitor) LazyInit(service service.PttService, obs observer.Observe //clear button clearBtn := widget.NewButtonWithIcon(common.APP_TESTCASE_BTN_CLEARLOG, theme.DeleteIcon(), func() { app.logPanel.SetText("") - app.monitorBinding.Set([]interface{}{}) + app.monitorList.Reset() }) resPanel := container.NewBorder(container.NewHBox(clearBtn, layout.NewSpacer()), nil, nil, nil, app.logPanel) - app.monitorBinding = binding.NewUntypedList() - app.monitorData = model.NewPushModelList() - app.createMonitorList() + app.monitorList = common.NewItemList() + + app.monitorList.ItemList = app.monitorList.CreateList() + app.monitorList.ItemList.OnSelected = func(id widget.ListItemID) { + o := app.monitorList.CachedList.Items[id] + pd, ok := o.Data.(*model.PushModel) + if !ok { + return + } + if res, err := anypb.New(pd.Msg.Data); err == nil { + app.logPanel.Text = res.String() + app.logPanel.Refresh() + } else { + logrus.WithField("err", err).Error("view detail") + } + } // layout - panel := container.NewVSplit(container.NewBorder(app.monitorHeader, nil, nil, nil, app.monitorList), resPanel) + panel := container.NewVSplit(container.NewBorder(nil, nil, nil, nil, + app.monitorList.ItemList), resPanel) panel.Offset = 0.8 content.Objects = append(content.Objects, panel) app.tabItem.Content = content - app.Run() + app.monitorlistener() return nil } @@ -73,78 +84,30 @@ func (a appMonitor) OnClose() bool { } // monitor list data -func (app *appMonitor) Run() { +func (app *appMonitor) monitorlistener() { app.obs.AddListener(observer.EVENT_APP_MONI, observer.Listener{ OnNotify: func(d interface{}, args ...interface{}) { + app.id++ data := d.(*model.PushModel) - app.monitorData.DataList = append(app.monitorData.DataList, data) - app.reloadMonitorData() - common.ShowCanvasTip("收到新的数据推送,请打开[推送]页面") + + //大于50条时清除一次 + // if len(app.monitorList.CachedList.Items) >= 50 { + // app.monitorList.Reset() + // } + + protocol := fmt.Sprintf("【%s.%s】", data.Msg.MainType, data.Msg.SubType) + app.monitorList.AddItem(common.Item{ + Text: fmt.Sprintf("%d - %-50v %-50v %-50v", + app.id, protocol, data.MethodName, data.DataTime), + Data: data, + }) + + app.monitorList.ItemList.ScrollToBottom() + // common.ShowCanvasTip("收到新的数据推送,请打开[推送]页面")//非常多调式时会导致系统崩溃 }, }) } -func (this *appMonitor) reloadMonitorData() { - if this.monitorData != nil { - d := this.monitorData.AsInterfaceArray() - this.monitorBinding.Set(d) - } -} - -func (this *appMonitor) createMonitorList() { - // header - this.monitorHeader = widget.NewList( - func() int { - return 1 - }, - func() fyne.CanvasObject { - return container.NewGridWithColumns(3, - widget.NewLabelWithStyle("", fyne.TextAlignLeading, fyne.TextStyle{}), - widget.NewLabelWithStyle("", fyne.TextAlignCenter, fyne.TextStyle{}), - widget.NewLabelWithStyle("", fyne.TextAlignCenter, fyne.TextStyle{})) - }, - func(id widget.ListItemID, item fyne.CanvasObject) { - item.(*fyne.Container).Objects[0].(*widget.Label).SetText(common.APP_MONITOR_TITLE_ID) - item.(*fyne.Container).Objects[1].(*widget.Label).SetText(common.APP_MONITOR_TITLE_DATA) - item.(*fyne.Container).Objects[2].(*widget.Label).SetText(common.APP_MONITOR_TITLE_DATE) - }, - ) - - // data - this.monitorList = widget.NewListWithData(this.monitorBinding, - func() fyne.CanvasObject { - return container.NewGridWithColumns(3, - widget.NewLabelWithStyle("", fyne.TextAlignLeading, fyne.TextStyle{}), - widget.NewLabelWithStyle("", fyne.TextAlignCenter, fyne.TextStyle{}), - widget.NewLabelWithStyle("", fyne.TextAlignCenter, fyne.TextStyle{}), - ) - }, - func(data binding.DataItem, item fyne.CanvasObject) { - o, _ := data.(binding.Untyped).Get() - pd := o.(*model.PushModel) - item.(*fyne.Container).Objects[0].(*widget.Label).SetText(fmt.Sprintf("%s.%s", pd.Msg.MainType, pd.Msg.SubType)) - item.(*fyne.Container).Objects[1].(*widget.Label).SetText(pd.MethodName) - item.(*fyne.Container).Objects[2].(*widget.Label).SetText(pd.DataTime) - }, - ) - - this.monitorList.OnSelected = func(id widget.ListItemID) { - if di, err := this.monitorBinding.GetItem(id); err != nil { - logrus.Error(err) - } else { - o, _ := di.(binding.Untyped).Get() - pd := o.(*model.PushModel) - if res, err := anypb.New(pd.Msg.Data); err == nil { - this.logPanel.Text = res.String() - this.logPanel.Refresh() - } else { - logrus.WithField("err", err).Error("view detail") - } - } - - } -} - func (this *appMonitor) ShortCut() fyne.Shortcut { return &desktop.CustomShortcut{KeyName: fyne.Key3, Modifier: desktop.AltModifier} } diff --git a/cmd/v2/ui/mainwindow.go b/cmd/v2/ui/mainwindow.go index aa5d2df25..dab4138a4 100644 --- a/cmd/v2/ui/mainwindow.go +++ b/cmd/v2/ui/mainwindow.go @@ -10,6 +10,7 @@ import ( "go_dreamfactory/modules/user" "go_dreamfactory/pb" "strings" + "time" "fyne.io/fyne/v2" "fyne.io/fyne/v2/container" @@ -312,23 +313,23 @@ func (ui *MainWindowImpl) createLoginWin(sid, sname string) { return } - // go func() { - // timer := time.NewTimer(10 * time.Second) - // for { - // if !timer.Stop() { - // select { - // case <-timer.C: - // default: - // } - // } - // timer.Reset(10 * time.Second) - // select { - // case <-timer.C: - // ui.pttService.Ping(sid, account.Text) - // continue - // } - // } - // }() + go func() { + timer := time.NewTimer(10 * time.Second) + for { + if !timer.Stop() { + select { + case <-timer.C: + default: + } + } + timer.Reset(10 * time.Second) + select { + case <-timer.C: + ui.pttService.Ping(sid, account.Text) + continue + } + } + }() // reset main window title subTitle := fmt.Sprintf("%s[%s]", sname, sid) ui.w.SetTitle(fmt.Sprintf(common.APP_WIN_TITLE, subTitle, ui.app.Metadata().Version, ui.app.Metadata().Build, common.APP_NAME)) diff --git a/cmd/v2/ui/views/hero_zhaomu.go b/cmd/v2/ui/views/hero_zhaomu.go index cb48b2e41..03b5e628d 100644 --- a/cmd/v2/ui/views/hero_zhaomu.go +++ b/cmd/v2/ui/views/hero_zhaomu.go @@ -1,37 +1,118 @@ package formview import ( + "bufio" + "go_dreamfactory/cmd/v2/lib/common" "go_dreamfactory/cmd/v2/model" "go_dreamfactory/cmd/v2/service" + "go_dreamfactory/cmd/v2/service/observer" + "go_dreamfactory/comm" + "go_dreamfactory/modules/hero" "go_dreamfactory/pb" + "os" + "os/exec" + "path/filepath" + "runtime" "fyne.io/fyne/v2" + "fyne.io/fyne/v2/container" + "fyne.io/fyne/v2/dialog" + "fyne.io/fyne/v2/layout" + fyne_storage "fyne.io/fyne/v2/storage" + "fyne.io/fyne/v2/theme" "fyne.io/fyne/v2/widget" + "github.com/pkg/errors" "github.com/sirupsen/logrus" "github.com/spf13/cast" ) type HeroZhaomuView struct { BaseformView + flag bool + exportFolder *widget.Entry + resultCountLbl *widget.Label + resultCount int +} + +// 打开目录 +func openFolder(entry *widget.Entry, w fyne.Window) { + dConf := dialog.NewFolderOpen(func(lu fyne.ListableURI, err error) { + if lu == nil { + return + } + entry.Text = lu.Path() + entry.Refresh() + }, w) + luri, _ := fyne_storage.ListerForURI(fyne_storage.NewFileURI(".")) + dConf.SetLocation(luri) + dConf.SetConfirmText("打开") + dConf.SetDismissText("取消") + dConf.Resize(fyne.NewSize(750, 500)) + dConf.Show() } func (this *HeroZhaomuView) CreateView(t *model.TestCase) fyne.CanvasObject { var ckType, ckCount string // 抽卡类型 0 1 2 3 4 - ckTypeSelect := widget.NewSelect([]string{"0", "1", "2", "3", "4"}, func(s string) { - ckType = s + ckTypeSelect := widget.NewSelect([]string{"普通", "魔法", "功夫", "科技", "月能"}, func(s string) { + switch s { + case "普通": + ckType = "0" + case "魔法": + ckType = "1" + case "功夫": + ckType = "2" + case "科技": + ckType = "3" + case "月能": + ckType = "4" + default: + ckType = "0" + } + }) + ckTypeSelect.Selected = "选择" //数量 - ckCountSelect := widget.NewSelect([]string{"1", "10"}, func(s string) { - ckCount = s + ckCountSelect := widget.NewSelect([]string{"单抽", "十连抽"}, func(s string) { + if s == "单抽" { + ckCount = "1" + } else if s == "十连抽" { + ckCount = "10" + } }) + ckCountSelect.Selected = "选择" - this.form.AppendItem(widget.NewFormItem("抽卡类型", ckTypeSelect)) - this.form.AppendItem(widget.NewFormItem("数量", ckCountSelect)) + //循环次数 + loopCount := widget.NewEntry() + loopCount.PlaceHolder = "循环抽卡次数" - this.form.OnSubmit = func() { + //结果导出目录 + this.exportFolder = widget.NewEntry() + this.exportFolder.PlaceHolder = "请选择保存结果的目录" + + form := widget.NewForm( + widget.NewFormItem("抽卡类型", ckTypeSelect), + widget.NewFormItem("单抽/十连", ckCountSelect), + widget.NewFormItem("循环次数", loopCount), + widget.NewFormItem("导出", container.NewBorder(nil, nil, nil, + widget.NewButtonWithIcon("", theme.FolderIcon(), func() { + openFolder(this.exportFolder, this.w) + }), this.exportFolder)), + ) + form.Items[3].HintText = "文件名默认result.txt" + + choukaBtnFunc := func() { + if ckTypeSelect.Selected == "选择" { + common.ShowTip("请选择抽卡类型") + return + } + + if ckCountSelect.Selected == "选择" { + common.ShowTip("请选择单抽/十连") + return + } if err := service.GetPttService().SendToClient(t.MainType, t.SubType, &pb.HeroDrawCardReq{ DrawType: cast.ToInt32(ckType), @@ -40,5 +121,96 @@ func (this *HeroZhaomuView) CreateView(t *model.TestCase) fyne.CanvasObject { logrus.Error(err) } } - return this.form + choukaBtn := widget.NewButton("抽卡", choukaBtnFunc) + choukaBtn.Importance = widget.HighImportance + + this.resultCountLbl = widget.NewLabel("共抽卡:") + //抽卡并导出结果 + choukaAndExport := func() { + this.resultCount = 0 + if loopCount.Text == "" || cast.ToInt(loopCount.Text) <= 0 { + common.ShowTip("请设置正确的循环次数") + return + } + if this.exportFolder.Text == "" { + common.ShowTip("请设置导出目录") + return + } + + for i := 0; i < cast.ToInt(loopCount.Text); i++ { + choukaBtnFunc() + } + } + + choukaAndExportBtn := widget.NewButton("抽卡并导出", choukaAndExport) + choukaAndExportBtn.Importance = widget.MediumImportance + + openExplor := func(dir string) { + if dir == "" { + common.ShowTip("资源管理器路径错误") + return + } + if runtime.GOOS == "windows" { + if err := exec.Command("explorer", filepath.Join(dir)).Start(); err != nil { + dialog.ShowError(errors.WithMessage(err, "请确认Json目录是否填写正确"), this.w) + return + } + } + } + //资源目录 + explorBtn := widget.NewButtonWithIcon("打开目录", theme.FolderIcon(), func() { + openExplor(this.exportFolder.Text) + }) + explorBtn.Importance = widget.LowImportance + + btns := container.NewHBox(this.resultCountLbl, layout.NewSpacer(), explorBtn, choukaAndExportBtn, choukaBtn) + c := container.NewBorder(nil, btns, nil, nil, form) + this.resListener() + return c +} + +// 监听抽卡结果 +func (this *HeroZhaomuView) resListener() { + if this.flag { + return + } + + this.obs.AddListener(observer.EVENT_REQ_RSP, observer.Listener{ + OnNotify: func(d interface{}, args ...interface{}) { + data := d.(*pb.UserMessage) + if !(data.MainType == string(comm.ModuleHero) && + data.SubType == hero.DrawCard) { + return + } + + rsp := &pb.HeroDrawCardResp{} + + if !comm.ProtoUnmarshal(data, rsp) { + logrus.Error("unmarshal err") + return + } + filePath := filepath.Join(this.exportFolder.Text, "result.txt") + f, err := os.OpenFile(filePath, os.O_CREATE|os.O_APPEND|os.O_RDWR, os.ModeAppend|os.ModePerm) + if err != nil { + logrus.Error(err.Error()) + return + } + defer f.Close() + + write := bufio.NewWriter(f) + for _, v := range rsp.Data { + for _, o := range v.Atno { + if o.A == "hero" && o.O != "" && o.T != "" { + write.WriteString(o.T + "\r\n") + } + } + this.resultCount++ + this.resultCountLbl.SetText("共抽卡:" + cast.ToString(this.resultCount)) + this.resultCountLbl.Refresh() + } + write.Flush() + }, + }) + + this.flag = true }