zone.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // package zone -- игровая зона (ангар, база, бан, битва и т.п.)
  2. package zone
  3. import (
  4. "context"
  5. "fmt"
  6. "log"
  7. "wartank/pkg/types"
  8. )
  9. // Zone -- игровая зона (ангар, база, бан, битва и т.п.)
  10. type Zone struct {
  11. bot types.ИБот // Ссылка на бота зоны
  12. ctx context.Context
  13. fnCancel func()
  14. name string // Имя игровой зоны
  15. }
  16. // NewZone -- возвращает новую игровую зону
  17. func NewZone(bot types.ИБот, zoneName string) (*Zone, error) {
  18. { // Предусловия
  19. if bot == nil {
  20. return nil, fmt.Errorf("NewZone(): IBot==nil")
  21. }
  22. if zoneName == "" {
  23. return nil, fmt.Errorf("NewZone(): zoneName is empty")
  24. }
  25. }
  26. log.Printf("NewZone(): name=%q\tzone=%q\n", bot.Имя(), zoneName)
  27. ctx, fnCancel := context.WithCancel(bot.Кнт())
  28. sf := &Zone{
  29. bot: bot,
  30. name: zoneName,
  31. ctx: ctx,
  32. fnCancel: fnCancel,
  33. }
  34. _ = types.ИСцена(sf)
  35. return sf, nil
  36. }
  37. // Кнт -- возвращает контекст игровой зоны
  38. func (sf *Zone) Кнт() context.Context {
  39. return sf.ctx
  40. }
  41. // Закончить -- отменяет контекст игровой зоны
  42. func (sf *Zone) Закончить() {
  43. sf.fnCancel()
  44. }
  45. // Бот -- возвращает бота игровой зоны
  46. func (sf *Zone) Бот() types.ИБот {
  47. return sf.bot
  48. }
  49. // Имя -- возвращает имя зоны
  50. func (sf *Zone) Имя() string {
  51. return sf.name
  52. }