Skip to content

Commit c2b3b07

Browse files
authored
DayNightCycle events
# Changes * Added `DayNightCycle` as event type * Handling in discord integration * Miscellaneous cleanup/typo fixes # Examples Discord integration: <img width="673" alt="image" src="https://github.com/user-attachments/assets/b188c750-046d-4ac7-9abd-82699a0cb4f9" />
1 parent ade784f commit c2b3b07

File tree

12 files changed

+90
-30
lines changed

12 files changed

+90
-30
lines changed

internal/events/eventtypes.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,3 +211,13 @@ type PlayerDeath struct {
211211
}
212212

213213
func (l PlayerDeath) Type() string { return `PlayerDeath` }
214+
215+
type DayNightCycle struct {
216+
IsSunrise bool
217+
Day int
218+
Month int
219+
Year int
220+
Time string
221+
}
222+
223+
func (l DayNightCycle) Type() string { return `DayNightCycle` }
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package hooks
2+
3+
import (
4+
"github.com/volte6/gomud/internal/events"
5+
"github.com/volte6/gomud/internal/gametime"
6+
"github.com/volte6/gomud/internal/templates"
7+
)
8+
9+
//
10+
// Watches the rounds go by
11+
// Spawns the loot goblin every so often
12+
//
13+
14+
func NotifySunriseSunset(e events.Event) bool {
15+
evt, typeOk := e.(events.DayNightCycle)
16+
if !typeOk {
17+
return false
18+
}
19+
20+
if evt.IsSunrise {
21+
sunriseTxt, _ := templates.Process("generic/sunrise", gametime.GetDate())
22+
events.AddToQueue(events.Broadcast{
23+
Text: sunriseTxt,
24+
})
25+
return true
26+
}
27+
28+
sunsetTxt, _ := templates.Process("generic/sunset", nil)
29+
events.AddToQueue(events.Broadcast{
30+
Text: sunsetTxt,
31+
})
32+
33+
return true
34+
}

internal/hooks/NewRound_DoCombat.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,6 @@ import (
2020
"github.com/volte6/gomud/internal/util"
2121
)
2222

23-
//
24-
// Watches the rounds go by
25-
// Applies autohealing where appropriate
26-
//
27-
2823
func DoCombat(e events.Event) bool {
2924

3025
evt := e.(events.NewRound)

internal/hooks/NewRound_MobRoundTick.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,6 @@ import (
99
"github.com/volte6/gomud/internal/users"
1010
)
1111

12-
//
13-
// Watches the rounds go by
14-
// Applies autohealing where appropriate
15-
//
16-
1712
func MobRoundTick(e events.Event) bool {
1813

1914
//

internal/hooks/NewRound_SpawnLootGoblin.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,6 @@ import (
66
"github.com/volte6/gomud/internal/rooms"
77
)
88

9-
//
10-
// Watches the rounds go by
11-
// Applies autohealing where appropriate
12-
//
13-
149
func SpawnLootGoblin(e events.Event) bool {
1510

1611
evt := e.(events.NewRound)

internal/hooks/NewRound_SunriseSunset.go

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package hooks
33
import (
44
"github.com/volte6/gomud/internal/events"
55
"github.com/volte6/gomud/internal/gametime"
6-
"github.com/volte6/gomud/internal/templates"
76
)
87

98
//
@@ -20,20 +19,15 @@ func SunriseSunset(e events.Event) bool {
2019
gdNow := gametime.GetDate()
2120

2221
if gdBefore.Night != gdNow.Night {
23-
if gdNow.Night {
24-
sunsetTxt, _ := templates.Process("generic/sunset", nil)
2522

26-
events.AddToQueue(events.Broadcast{
27-
Text: sunsetTxt,
28-
})
23+
events.AddToQueue(events.DayNightCycle{
24+
IsSunrise: !gdNow.Night,
25+
Day: gdNow.Day,
26+
Month: gdNow.Month,
27+
Year: gdNow.Year,
28+
Time: gdNow.String(),
29+
})
2930

30-
} else {
31-
sunriseTxt, _ := templates.Process("generic/sunrise", gdNow)
32-
events.AddToQueue(events.Broadcast{
33-
Text: sunriseTxt,
34-
})
35-
36-
}
3731
}
3832

3933
return true

internal/hooks/hooks.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ func RegisterListeners() {
5555
events.RegisterListener(events.LevelUp{}, SendLevelNotifications)
5656
events.RegisterListener(events.LevelUp{}, CheckGuide)
5757

58+
// Day/Night cycle
59+
events.RegisterListener(events.DayNightCycle{}, NotifySunriseSunset)
60+
5861
// Listener for debugging some stuff (catches all events)
5962
/*
6063
events.RegisterListener(nil, func(e events.Event) bool {

internal/integrations/discord/client.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ func registerListeners() {
5353
events.RegisterListener(events.Log{}, HandleLogs)
5454
events.RegisterListener(events.LevelUp{}, HandleLevelup)
5555
events.RegisterListener(events.PlayerDeath{}, HandleDeath)
56+
events.RegisterListener(events.DayNightCycle{}, HandleNewDay)
5657
}
5758

5859
// Sends an embed message to discord which includes a colored bar to the left

internal/integrations/discord/listeners.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,15 @@ import (
66

77
"github.com/volte6/gomud/internal/connections"
88
"github.com/volte6/gomud/internal/events"
9+
"github.com/volte6/gomud/internal/gametime"
910
"github.com/volte6/gomud/internal/users"
1011
"github.com/volte6/gomud/internal/util"
1112
)
1213

14+
var (
15+
lastMonth = -1
16+
)
17+
1318
// Player enters the world event
1419
func HandlePlayerSpawn(e events.Event) bool {
1520
evt, typeOk := e.(events.PlayerSpawn)
@@ -100,3 +105,22 @@ func HandleDeath(e events.Event) bool {
100105

101106
return true
102107
}
108+
109+
func HandleNewDay(e events.Event) bool {
110+
evt, typeOk := e.(events.DayNightCycle)
111+
if !typeOk {
112+
return false
113+
}
114+
115+
if evt.IsSunrise {
116+
message := fmt.Sprintf(`:sunny: **Day %d begins.**`, evt.Day)
117+
if lastMonth != evt.Month {
118+
message += fmt.Sprintf(` *It is the month **%s** in the year of the **%s**.*`, gametime.MonthName(evt.Month), gametime.GetZodiac(evt.Year))
119+
lastMonth = evt.Month
120+
}
121+
122+
SendMessage(message)
123+
}
124+
125+
return true
126+
}

internal/templates/templatesfunctions.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,8 @@ var (
208208
"permadeath": func() bool {
209209
return bool(configs.GetConfig().PermaDeath)
210210
},
211-
"zodiac": func(month int) string {
212-
return gametime.GetZodiac(month)
211+
"zodiac": func(year int) string {
212+
return gametime.GetZodiac(year)
213213
},
214214
"month": func(month int) string {
215215
return gametime.MonthName(month)

0 commit comments

Comments
 (0)