You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
1.9 KiB

  1. package handlerfactory
  2. import (
  3. "git.aiterp.net/lucifer/new-server/models"
  4. )
  5. func optimizeLists(
  6. add []models.EventHandler,
  7. del []models.EventHandler,
  8. ) ([]models.EventHandler, []models.EventHandler) {
  9. newAdd := make([]models.EventHandler, 0, len(add))
  10. newDel := make([]models.EventHandler, 0, len(del))
  11. OptimizeLoop1:
  12. for _, addItem := range add {
  13. for _, delItem := range del {
  14. if checkDuplicate(&addItem, &delItem) {
  15. continue OptimizeLoop1
  16. }
  17. }
  18. newAdd = append(newAdd, addItem)
  19. }
  20. OptimizeLoop2:
  21. for _, delItem := range del {
  22. for _, addItem := range add {
  23. if checkDuplicate(&addItem, &delItem) {
  24. continue OptimizeLoop2
  25. }
  26. }
  27. newDel = append(newDel, delItem)
  28. }
  29. return newAdd, newDel
  30. }
  31. func checkDuplicate(a *models.EventHandler, b *models.EventHandler) bool {
  32. check := true
  33. check = check && a.EventName == b.EventName
  34. check = check && len(a.Conditions) == len(b.Conditions)
  35. for k := range a.Conditions {
  36. check = check && a.Conditions[k] == b.Conditions[k]
  37. }
  38. check = check && a.OneShot == b.OneShot
  39. check = check && a.Priority == b.Priority
  40. check = check && a.TargetKind == b.TargetKind
  41. check = check && a.TargetValue == b.TargetValue
  42. check = check && a.From == b.From
  43. check = check && a.To == b.To
  44. aPower := a.Actions.SetPower
  45. bPower := b.Actions.SetPower
  46. powerEq := (aPower == nil && bPower == nil) || (aPower != nil && bPower != nil && *aPower == *bPower)
  47. aColor := a.Actions.SetColor
  48. bColor := b.Actions.SetColor
  49. colorEq := (aColor == nil && bColor == nil) || (aColor != nil && bColor != nil && *aColor == *bColor)
  50. aInt := a.Actions.SetIntensity
  51. bInt := b.Actions.SetIntensity
  52. intEq := (aInt == nil && bInt == nil) || (aInt != nil && bInt != nil && *aInt == *bInt)
  53. aTemp := a.Actions.SetTemperature
  54. bTemp := b.Actions.SetTemperature
  55. tempEq := (aTemp == nil && bTemp == nil) || (aTemp != nil && bTemp != nil && *aTemp == *bTemp)
  56. check = check && powerEq && colorEq && intEq && tempEq
  57. return check
  58. }