dm.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package model
  2. import (
  3. "database/sql/driver"
  4. "strconv"
  5. "time"
  6. )
  7. // all const variable used in job
  8. const (
  9. AttrNo = int32(0)
  10. AttrYes = int32(1)
  11. // platform
  12. PlatUnknow = int32(0)
  13. PlatWeb = int32(1)
  14. PlatAndroid = int32(2)
  15. PlatIPhone = int32(3)
  16. PlatWpM = int32(4) // wp mobile
  17. PlatIPad = int32(5)
  18. PlatPadHd = int32(6) // ipad hd
  19. PlatWpPc = int32(7) // win10
  20. // dm state
  21. StateNormal = int32(0) // 普通状态
  22. StateDelete = int32(1) // 删除状态
  23. StateHide = int32(2) // 隐藏状态
  24. StateBlock = int32(3) // 屏蔽状态
  25. StateFilter = int32(4) // 过滤状态
  26. StateMonitorBefore = int32(5) // 先审后发
  27. StateMonitorAfter = int32(6) // 先发后审
  28. // dm attribute
  29. AttrProtect = uint(0) // 保护弹幕
  30. // dm pool
  31. PoolNormal = int32(0) // 普通弹幕池
  32. PoolSubtitle = int32(1) // 字幕弹幕池
  33. PoolSpecial = int32(2) // 特殊弹幕池
  34. // dm mode
  35. ModeNormal = int32(1) // 正常滚动弹幕
  36. ModeBottom = int32(4) // 底部弹幕
  37. ModeTop = int32(5) // 顶部弹幕
  38. ModeReverse = int32(6) // 逆向滚动弹幕
  39. ModeAdvance = int32(7) // 高级弹幕
  40. ModeCode = int32(8) // 代码弹幕
  41. NotFound = int64(-1)
  42. )
  43. // AttrVal return val of index'attr
  44. func (d *DM) AttrVal(bit uint) int32 {
  45. return (d.Attr >> bit) & int32(1)
  46. }
  47. // AttrSet set val of index'attr
  48. func (d *DM) AttrSet(v int32, bit uint) {
  49. d.Attr = d.Attr&(^(1 << bit)) | (v << bit)
  50. }
  51. // NeedDisplay 判断该条弹幕是否需要展示
  52. func (d *DM) NeedDisplay() bool {
  53. return d.State == StateNormal || d.State == StateMonitorAfter
  54. }
  55. // NeedStateNormal 判断是否更新状态
  56. // pool0 变为 pool1 状态正常
  57. // 变为保护弹幕 状态正常
  58. func (d *DM) NeedStateNormal(old *DM) bool {
  59. if (d.Pool != old.Pool) && d.Pool == PoolSubtitle {
  60. return !(d.State == StateNormal)
  61. }
  62. if d.AttrVal(AttrProtect) == AttrYes && old.AttrVal(AttrProtect) == AttrNo {
  63. return !(d.State == StateNormal)
  64. }
  65. return false
  66. }
  67. // Trim dmid and it's progress time will be trimed.
  68. type Trim struct {
  69. ID int64 `json:"id"`
  70. Attr int32 `json:"-"`
  71. }
  72. type stime int64
  73. // Scan scan time.
  74. func (st *stime) Scan(src interface{}) (err error) {
  75. switch sc := src.(type) {
  76. case time.Time:
  77. *st = stime(sc.Unix())
  78. case string:
  79. var i int64
  80. i, err = strconv.ParseInt(sc, 10, 64)
  81. *st = stime(i)
  82. }
  83. return
  84. }
  85. // Value get time value.
  86. func (st stime) Value() (driver.Value, error) {
  87. return time.Unix(int64(st), 0), nil
  88. }
  89. // UnmarshalJSON implements the json.Unmarshaler interface.
  90. func (st *stime) UnmarshalJSON(data []byte) error {
  91. timestamp, err := strconv.ParseInt(string(data), 10, 64)
  92. if err == nil {
  93. *st = stime(timestamp)
  94. return nil
  95. }
  96. t, err := time.ParseInLocation(`"2006-01-02 15:04:05"`, string(data), time.Local)
  97. *st = stime(t.Unix())
  98. return err
  99. }