-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
139 lines (121 loc) · 3.63 KB
/
Copy pathmain.go
File metadata and controls
139 lines (121 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Package main demonstrates context propagation and error handling patterns.
package main
import (
"context"
"fmt"
"log"
"sync/atomic"
"time"
"github.com/ompgo-dev/ompgo/pkg/omp"
"github.com/ompgo-dev/ompgo/pkg/omp/core"
"github.com/ompgo-dev/ompgo/pkg/omp/players"
"github.com/ompgo-dev/ompgo/pkg/runtime"
)
var eventSeq atomic.Uint64
type ContextGamemode struct {
omp.BaseEventHandler
}
func (gm *ContextGamemode) OnLoad(ctx context.Context) error {
_ = core.Log("[ContextDemo] Loaded")
return nil
}
func (gm *ContextGamemode) OnPlayerConnect(ctx context.Context, event *omp.PlayerConnectEvent) error {
if event.Player == nil || !event.Player.Valid() {
return nil
}
name, _ := players.GetName(event.Player)
reqID := readRequestID(ctx)
_ = players.SendClientMessage(
event.Player,
uint32(omp.ColorGreen),
fmt.Sprintf("Welcome %s! request_id=%s", name, reqID),
)
_ = players.SendClientMessage(
event.Player,
uint32(omp.ColorYellow),
"Try: /ctx, /cancel, /fail",
)
return nil
}
func (gm *ContextGamemode) OnPlayerCommandText(ctx context.Context, event *omp.PlayerCommandTextEvent) (bool, error) {
if event.Player == nil || !event.Player.Valid() {
return false, nil
}
command := event.Command.Clone()
switch command {
case "/ctx":
reqID := readRequestID(ctx)
age := time.Since(readStartedAt(ctx)).Truncate(time.Millisecond)
eventName, _ := runtime.EventNameFromContext(ctx)
_ = players.SendClientMessage(
event.Player,
uint32(omp.ColorWhite),
fmt.Sprintf("ctx event=%s request_id=%s age=%s", eventName, reqID, age),
)
return true, nil
case "/cancel":
child, cancel := context.WithCancel(ctx)
cancel()
select {
case <-child.Done():
_ = players.SendClientMessage(event.Player, uint32(omp.ColorOrange), "child context cancelled")
default:
_ = players.SendClientMessage(event.Player, uint32(omp.ColorRed), "child context still active")
}
return true, nil
case "/fail":
// Intentional error for demonstrating WithEventErrorHandler.
return true, fmt.Errorf("intentional failure from /fail")
}
return false, nil
}
func NewGamemode() runtime.Gamemode {
return &ContextGamemode{}
}
func init() {
runtime.Bootstrap(
runtime.WithComponentName("ompgo_context_demo"),
runtime.WithGamemode(NewGamemode),
runtime.WithSetup(func(ctx context.Context) error {
log.Printf("[ContextDemo] setup complete")
return nil
}),
runtime.WithOnReady(func(ctx context.Context) error {
log.Printf("[ContextDemo] component ready")
return nil
}),
runtime.WithOnFree(func(ctx context.Context) error {
log.Printf("[ContextDemo] component free")
return nil
}),
runtime.WithEventErrorPolicy(runtime.ErrorPolicyBlockOnError),
runtime.WithLifecycleErrorPolicy(runtime.ErrorPolicyContinue),
runtime.WithEventContextDecorator(func(ctx context.Context, eventName string, event any) context.Context {
_ = event
seq := eventSeq.Add(1)
return runtime.WithRequestID(ctx, fmt.Sprintf("%s-%d", eventName, seq))
}),
runtime.WithEventErrorHandler(func(ctx context.Context, eventName string, event any, err error) {
_ = ctx
_ = event
log.Printf("[ContextDemo] event error in %s: %v", eventName, err)
}),
runtime.WithLifecycleErrorHandler(func(ctx context.Context, stage string, err error) {
_ = ctx
log.Printf("[ContextDemo] lifecycle error in %s: %v", stage, err)
}),
)
}
func readRequestID(ctx context.Context) string {
if v, ok := runtime.RequestIDFromContext(ctx); ok && v != "" {
return v
}
return "unknown"
}
func readStartedAt(ctx context.Context) time.Time {
if v, ok := runtime.EventStartedAtFromContext(ctx); ok {
return v
}
return time.Now()
}
func main() {}