zone.go 1.4 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.IBot // Ссылка на бота зоны
  12. ctx context.Context
  13. fnCancel func()
  14. name string // Имя игровой зоны
  15. }
  16. // NewZone -- возвращает новую игровую зону
  17. func NewZone(bot types.IBot, 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.Name(), zoneName)
  27. ctx, fnCancel := context.WithCancel(bot.Ctx())
  28. sf := &Zone{
  29. bot: bot,
  30. name: zoneName,
  31. ctx: ctx,
  32. fnCancel: fnCancel,
  33. }
  34. _ = types.IZone(sf)
  35. return sf, nil
  36. }
  37. // Ctx -- возвращает контекст игровой зоны
  38. func (sf *Zone) Ctx() context.Context {
  39. return sf.ctx
  40. }
  41. // CancelZone -- отменяет контекст игровой зоны
  42. func (sf *Zone) CancelZone() {
  43. sf.fnCancel()
  44. }
  45. // Bot -- возвращает бота игровой зоны
  46. func (sf *Zone) Bot() types.IBot {
  47. return sf.bot
  48. }
  49. // Name -- возвращает имя зоны
  50. func (sf *Zone) Name() string {
  51. return sf.name
  52. }