-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathambience.ts
More file actions
46 lines (41 loc) · 1.62 KB
/
ambience.ts
File metadata and controls
46 lines (41 loc) · 1.62 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
/**
* This script shows some ambient room descriptions at random time intervals
* between the `minDelay` and `maxDelay` constants.
*/
const minDelay = 5 * 60 * 1000 // 5 minutes in milliseconds
const maxDelay = 30 * 60 * 1000 // 30 minutes in milliseconds
const messages = [
"A bird chirps from a nearby tree.",
"A gentle breeze makes some leaves twirl across the path.",
"The leaves of the trees rustle soothingly.",
]
export function onActivate(): void {
// Start the script by scheduling the first message. On script update
// or deactivation, any previous scheduled posts are deleted.
scheduleNext()
}
export function onMessage(addr: string, topic: string, data: string | null, sender: string): void {
// The topic will always be "showMessage" since we listen to no other
// script. But we still check it for good measure.
if (topic == "showMessage") {
// Randomly select one of the messages.
const msg = messages[i32(Math.floor(Math.random() * messages.length))]
// Show the message to the room.
Room.describe(msg)
// Schedule a post for next time to show a message.
scheduleNext()
}
}
/**
* Schedules a message to be posted in a duration between `minDelay` and
* `maxDelay`.
*/
function scheduleNext(): void {
// Randomize a delay value between between minDelay and maxDelay.
const delay = i64(Math.floor(Math.random() * (maxDelay - minDelay))) + minDelay
// Create a post to self ("#" is a shortcut for the script instance's own
// address), using the calculated delay.
// Self-posts does not require a call to Script.listen - a script can always
// post to itself.
Script.post("#", "showMessage", null, delay)
}