| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- // package win_bot_view -- просмотр состояния бота
- package win_bot_view
- import (
- _ "embed"
- "fmt"
- "log"
- "net/url"
- "runtime"
- "time"
- "github.com/zserge/lorca"
- "wartank/pkg/types"
- )
- //go:embed win_bot_view.html
- var strWinHtml string
- // WinBotView -- окно просмотра бота
- type WinBotView struct {
- desktop types.IDesktop
- store types.IStore
- win lorca.UI
- ws types.IWebSocket
- name string
- }
- // NewWinBotView -- возвращает новое окно просмотра бота
- func NewWinBotView(desktop types.IDesktop, name string) (*WinBotView, error) {
- { // Предусловия
- if desktop == nil {
- return nil, fmt.Errorf("NewWinBotView(): IDesktop == nil")
- }
- if name == "" {
- return nil, fmt.Errorf("NewWinBotView(): name шы уьзен")
- }
- }
- sf := &WinBotView{
- desktop: desktop,
- store: desktop.Store(),
- ws: desktop.Ws(),
- name: name,
- }
- args := []string{}
- if runtime.GOOS == "linux" {
- args = append(args, "--class=Lorca")
- }
- var err error
- sf.win, err = lorca.New("data:text/html,"+url.PathEscape(strWinHtml), "", 640, 480, args...)
- if err != nil {
- return nil, fmt.Errorf("NewWinBotView(): in create win, err=\n\t%w", err)
- }
- go sf.close()
- return sf, nil
- }
- // Работает в отдельном потоке, главный цикл окна
- func (sf *WinBotView) Run() {
- log.Println("WinBotView.Run()")
- sf.win.Bind("close_win", sf.onClose)
- for {
- select {
- case <-sf.win.Done(): // Ожидание закрытия окна
- sf.close()
- return
- default: // Дежурный вывод информации
- go sf.update()
- time.Sleep(time.Millisecond * 500)
- }
- }
- }
- // Обновляет информацию в окне
- func (sf *WinBotView) update() {
- dictReq := make(map[string]string)
- dictReq["name"] = sf.name
- dictResp, err := sf.ws.Call("/bot/status", dictReq)
- if err != nil {
- log.Printf("WinBotView.update(): in read bot(%q),err=\n\t%v\n", sf.name, err)
- return
- }
- // log.Printf("WinBotView.update(): dictRes=%#v\n", dictResp)
- { // Имя
- name := dictResp["/bot/name"]
- js := fmt.Sprintf(
- `function UpdateName(){
- var _el=document.getElementById("/bot/name");
- _el.innerText=%q
- }
- UpdateName()`, name)
- sf.win.Eval(js)
- }
- { // Если онлайн
- isOnline := dictResp["/bot/online"]
- js := fmt.Sprintf(
- `function UpdateIsOnlime(){
- var _el=document.getElementById("/bot/online");
- _el.innerText=%q
- }
- UpdateIsOnlime()`, isOnline)
- sf.win.Eval(js)
- }
- { // Топливо
- fuel := dictResp["/bot/fuel"]
- js := fmt.Sprintf(
- `function UpdateFuel(){
- var _el=document.getElementById("/bot/fuel");
- _el.innerText=%q
- }
- UpdateFuel()`, fuel)
- sf.win.Eval(js)
- }
- { // Золото
- gold := dictResp["/bot/gold"]
- js := fmt.Sprintf(
- `function UpdateGold(){
- var _el=document.getElementById("/bot/gold");
- _el.innerText=%q
- }
- UpdateGold()`, gold)
- sf.win.Eval(js)
- }
- { // Серебро
- silver := dictResp["/bot/silver"]
- js := fmt.Sprintf(
- `function UpdateSilver(){
- var _el=document.getElementById("/bot/silver");
- _el.innerText=%q
- }
- UpdateSilver()`, silver)
- sf.win.Eval(js)
- }
- { // Серебро-время
- silverTime := dictResp["/bank/silver-time"]
- js := fmt.Sprintf(
- `function UpdateSilverTime(){
- var _el=document.getElementById("/bank/silver-time");
- _el.innerText=%q
- }
- UpdateSilverTime()`, silverTime)
- sf.win.Eval(js)
- }
- { // Серебро-режим
- silverMode := dictResp["/bank/silver-mode"]
- js := fmt.Sprintf(
- `function UpdateSilverMode(){
- var _el=document.getElementById("/bank/silver-mode");
- _el.innerText=%q
- }
- UpdateSilverMode()`, silverMode)
- sf.win.Eval(js)
- }
- { // Серебро-всего
- silverAll := dictResp["/angar/silver-all"]
- js := fmt.Sprintf(
- `function UpdateSilverAll(){
- var _el=document.getElementById("/angar/silver-all");
- _el.innerText=%q
- }
- UpdateSilverAll()`, silverAll)
- sf.win.Eval(js)
- }
- { // Шахта-время
- mineTime := dictResp["/bot/mine-time"]
- js := fmt.Sprintf(
- `function UpdateMineTime(){
- var _el=document.getElementById("/bot/mine-time");
- _el.innerText=%q
- }
- UpdateMineTime()`, mineTime)
- sf.win.Eval(js)
- }
- { // Шахта-режим
- mineMode := dictResp["/bot/mine-mode"]
- js := fmt.Sprintf(
- `function UpdateMineMode(){
- var _el=document.getElementById("/bot/mine-mode");
- _el.innerText=%q
- }
- UpdateMineMode()`, mineMode)
- sf.win.Eval(js)
- }
- { // Шахта-руда
- mineRuda := dictResp["/mine/ruda"]
- js := fmt.Sprintf(
- `function UpdateMineRuda(){
- var _el=document.getElementById("/mine/ruda");
- _el.innerText=%q
- }
- UpdateMineRuda()`, mineRuda)
- sf.win.Eval(js)
- }
- }
- // Закрывает окно
- func (sf *WinBotView) onClose() {
- log.Println("WinBotView.onClose()")
- sf.win.Close()
- }
- // close -- ожидает отмены глобального контекста
- func (sf *WinBotView) close() {
- <-sf.desktop.CtxApp().Done()
- log.Println("WinBotView.close()")
- sf.win.Close()
- }
|