win_bot_view.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. // package win_bot_view -- просмотр состояния бота
  2. package win_bot_view
  3. import (
  4. _ "embed"
  5. "fmt"
  6. "log"
  7. "net/url"
  8. "runtime"
  9. "time"
  10. "github.com/zserge/lorca"
  11. "wartank/pkg/types"
  12. )
  13. //go:embed win_bot_view.html
  14. var strWinHtml string
  15. // WinBotView -- окно просмотра бота
  16. type WinBotView struct {
  17. desktop types.IDesktop
  18. store types.IStore
  19. win lorca.UI
  20. ws types.IWebSocket
  21. name string
  22. }
  23. // NewWinBotView -- возвращает новое окно просмотра бота
  24. func NewWinBotView(desktop types.IDesktop, name string) (*WinBotView, error) {
  25. { // Предусловия
  26. if desktop == nil {
  27. return nil, fmt.Errorf("NewWinBotView(): IDesktop == nil")
  28. }
  29. if name == "" {
  30. return nil, fmt.Errorf("NewWinBotView(): name шы уьзен")
  31. }
  32. }
  33. sf := &WinBotView{
  34. desktop: desktop,
  35. store: desktop.Store(),
  36. ws: desktop.Ws(),
  37. name: name,
  38. }
  39. args := []string{}
  40. if runtime.GOOS == "linux" {
  41. args = append(args, "--class=Lorca")
  42. }
  43. var err error
  44. sf.win, err = lorca.New("data:text/html,"+url.PathEscape(strWinHtml), "", 640, 480, args...)
  45. if err != nil {
  46. return nil, fmt.Errorf("NewWinBotView(): in create win, err=\n\t%w", err)
  47. }
  48. go sf.close()
  49. return sf, nil
  50. }
  51. // Работает в отдельном потоке, главный цикл окна
  52. func (sf *WinBotView) Run() {
  53. log.Println("WinBotView.Run()")
  54. sf.win.Bind("close_win", sf.onClose)
  55. for {
  56. select {
  57. case <-sf.win.Done(): // Ожидание закрытия окна
  58. sf.close()
  59. return
  60. default: // Дежурный вывод информации
  61. go sf.update()
  62. time.Sleep(time.Millisecond * 500)
  63. }
  64. }
  65. }
  66. // Обновляет информацию в окне
  67. func (sf *WinBotView) update() {
  68. dictReq := make(map[string]string)
  69. dictReq["name"] = sf.name
  70. dictResp, err := sf.ws.Call("/bot/status", dictReq)
  71. if err != nil {
  72. log.Printf("WinBotView.update(): in read bot(%q),err=\n\t%v\n", sf.name, err)
  73. return
  74. }
  75. // log.Printf("WinBotView.update(): dictRes=%#v\n", dictResp)
  76. { // Имя
  77. name := dictResp["/bot/name"]
  78. js := fmt.Sprintf(
  79. `function UpdateName(){
  80. var _el=document.getElementById("/bot/name");
  81. _el.innerText=%q
  82. }
  83. UpdateName()`, name)
  84. sf.win.Eval(js)
  85. }
  86. { // Если онлайн
  87. isOnline := dictResp["/bot/online"]
  88. js := fmt.Sprintf(
  89. `function UpdateIsOnlime(){
  90. var _el=document.getElementById("/bot/online");
  91. _el.innerText=%q
  92. }
  93. UpdateIsOnlime()`, isOnline)
  94. sf.win.Eval(js)
  95. }
  96. { // Топливо
  97. fuel := dictResp["/bot/fuel"]
  98. js := fmt.Sprintf(
  99. `function UpdateFuel(){
  100. var _el=document.getElementById("/bot/fuel");
  101. _el.innerText=%q
  102. }
  103. UpdateFuel()`, fuel)
  104. sf.win.Eval(js)
  105. }
  106. { // Золото
  107. gold := dictResp["/bot/gold"]
  108. js := fmt.Sprintf(
  109. `function UpdateGold(){
  110. var _el=document.getElementById("/bot/gold");
  111. _el.innerText=%q
  112. }
  113. UpdateGold()`, gold)
  114. sf.win.Eval(js)
  115. }
  116. { // Серебро
  117. silver := dictResp["/bot/silver"]
  118. js := fmt.Sprintf(
  119. `function UpdateSilver(){
  120. var _el=document.getElementById("/bot/silver");
  121. _el.innerText=%q
  122. }
  123. UpdateSilver()`, silver)
  124. sf.win.Eval(js)
  125. }
  126. { // Серебро-время
  127. silverTime := dictResp["/bank/silver-time"]
  128. js := fmt.Sprintf(
  129. `function UpdateSilverTime(){
  130. var _el=document.getElementById("/bank/silver-time");
  131. _el.innerText=%q
  132. }
  133. UpdateSilverTime()`, silverTime)
  134. sf.win.Eval(js)
  135. }
  136. { // Серебро-режим
  137. silverMode := dictResp["/bank/silver-mode"]
  138. js := fmt.Sprintf(
  139. `function UpdateSilverMode(){
  140. var _el=document.getElementById("/bank/silver-mode");
  141. _el.innerText=%q
  142. }
  143. UpdateSilverMode()`, silverMode)
  144. sf.win.Eval(js)
  145. }
  146. { // Серебро-всего
  147. silverAll := dictResp["/angar/silver-all"]
  148. js := fmt.Sprintf(
  149. `function UpdateSilverAll(){
  150. var _el=document.getElementById("/angar/silver-all");
  151. _el.innerText=%q
  152. }
  153. UpdateSilverAll()`, silverAll)
  154. sf.win.Eval(js)
  155. }
  156. { // Шахта-время
  157. mineTime := dictResp["/bot/mine-time"]
  158. js := fmt.Sprintf(
  159. `function UpdateMineTime(){
  160. var _el=document.getElementById("/bot/mine-time");
  161. _el.innerText=%q
  162. }
  163. UpdateMineTime()`, mineTime)
  164. sf.win.Eval(js)
  165. }
  166. { // Шахта-режим
  167. mineMode := dictResp["/bot/mine-mode"]
  168. js := fmt.Sprintf(
  169. `function UpdateMineMode(){
  170. var _el=document.getElementById("/bot/mine-mode");
  171. _el.innerText=%q
  172. }
  173. UpdateMineMode()`, mineMode)
  174. sf.win.Eval(js)
  175. }
  176. { // Шахта-руда
  177. mineRuda := dictResp["/mine/ruda"]
  178. js := fmt.Sprintf(
  179. `function UpdateMineRuda(){
  180. var _el=document.getElementById("/mine/ruda");
  181. _el.innerText=%q
  182. }
  183. UpdateMineRuda()`, mineRuda)
  184. sf.win.Eval(js)
  185. }
  186. }
  187. // Закрывает окно
  188. func (sf *WinBotView) onClose() {
  189. log.Println("WinBotView.onClose()")
  190. sf.win.Close()
  191. }
  192. // close -- ожидает отмены глобального контекста
  193. func (sf *WinBotView) close() {
  194. <-sf.desktop.CtxApp().Done()
  195. log.Println("WinBotView.close()")
  196. sf.win.Close()
  197. }