Skip to content

Commit

Permalink
Update event.go
Browse files Browse the repository at this point in the history
  • Loading branch information
Edouard127 committed Oct 7, 2023
1 parent 6696367 commit 45be12b
Showing 1 changed file with 29 additions and 52 deletions.
81 changes: 29 additions & 52 deletions bot/provider/event.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package provider

import (
"container/heap"
"context"
pk "github.com/Edouard127/go-mc/net/packet"
"sort"
)

type Events[T any] struct {
handlers map[int32]*handlerHeap[T] // for specific packet id only
tickers *tickerHeap[T]
handlers map[int32][]PacketHandler[T] // for packets
tickers []TickHandler[T] // for tickers
}

// AddListener adds a listener to the event.
Expand All @@ -17,22 +17,22 @@ type Events[T any] struct {
// The listeners cannot have multiple same ID.
func (e *Events[T]) AddListener(listeners ...PacketHandler[T]) {
for _, l := range listeners {
var s *handlerHeap[T]
var ok bool
if s, ok = e.handlers[l.ID]; !ok {
s = &handlerHeap[T]{l}
e.handlers[l.ID] = s
if e.handlers == nil {
e.handlers = make(map[int32][]PacketHandler[T])
}
if e.handlers[l.ID] == nil {
e.handlers[l.ID] = []PacketHandler[T]{l}
} else {
s.Push(l)
e.handlers[l.ID] = append(e.handlers[l.ID], l)
sortPacket[T](e.handlers[l.ID])
}
heap.Fix(s, s.Len()-1)
}
}

func (e *Events[T]) HandlePacket(cl *T, p pk.Packet) error {
if h := e.handlers[p.ID]; h != nil {
ctx, cancel := context.WithCancel(context.TODO())
for _, handler := range *h {
for _, handler := range h {
if err := handler.F(cl, p, cancel); err != nil {
return err
}
Expand All @@ -46,20 +46,12 @@ func (e *Events[T]) HandlePacket(cl *T, p pk.Packet) error {
return nil
}

type TickHandler[T any] struct {
Priority int
F func(*T, context.CancelFunc) error
}

func (e *Events[T]) AddTicker(tickers ...TickHandler[T]) {
for _, t := range tickers {
if e.tickers == nil {
e.tickers = &tickerHeap[T]{t}
} else {
e.tickers.Push(t)
}
if e.tickers == nil {
e.tickers = []TickHandler[T]{}
}
heap.Fix(e.tickers, e.tickers.Len()-1)
e.tickers = append(e.tickers, tickers...)

}

func (e *Events[T]) Tick(cl *T) error {
Expand All @@ -68,8 +60,7 @@ func (e *Events[T]) Tick(cl *T) error {
}

ctx, cancel := context.WithCancel(context.Background())

for _, t := range *e.tickers {
for _, t := range e.tickers {
if err := t.F(cl, cancel); err != nil {
return err
}
Expand All @@ -78,6 +69,7 @@ func (e *Events[T]) Tick(cl *T) error {
break
}
}
sortTick[T](e.tickers)
cancel()
return nil
}
Expand All @@ -88,34 +80,19 @@ type PacketHandler[T any] struct {
F func(*T, pk.Packet, context.CancelFunc) error
}

// handlerHeap is PriorityQueue<PacketHandlerFunc>
type handlerHeap[T any] []PacketHandler[T]

func (h handlerHeap[T]) Len() int { return len(h) }
func (h handlerHeap[T]) Less(i, j int) bool { return h[i].Priority < h[j].Priority }
func (h handlerHeap[T]) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h *handlerHeap[T]) Push(x interface{}) { *h = append(*h, x.(PacketHandler[T])) }
func (h *handlerHeap[T]) Pop() interface{} {
old := *h
n := len(old)
*h = old[0 : n-1]
return old[n-1]
type TickHandler[T any] struct {
Priority int
F func(*T, context.CancelFunc) error
}

// tickerHeap is PriorityQueue<TickHandlerFunc>
type tickerHeap[T any] []TickHandler[T]

func (h tickerHeap[T]) Len() int { return len(h) }
func (h tickerHeap[T]) Less(i, j int) bool {
return h[i].Priority < h[j].Priority
func sortPacket[T any](slice []PacketHandler[T]) {
sort.SliceStable(slice, func(i, j int) bool {
return slice[i].Priority > slice[j].Priority
})
}
func (h tickerHeap[T]) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h *tickerHeap[T]) Push(x interface{}) {
*h = append(*h, x.(TickHandler[T]))
}
func (h *tickerHeap[T]) Pop() interface{} {
old := *h
n := len(old)
*h = old[0 : n-1]
return old[n-1]

func sortTick[T any](slice []TickHandler[T]) {
sort.SliceStable(slice, func(i, j int) bool {
return slice[i].Priority > slice[j].Priority
})
}

0 comments on commit 45be12b

Please sign in to comment.