-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmeasureSpeakingTimes.js
44 lines (40 loc) · 1.52 KB
/
measureSpeakingTimes.js
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
;(() => {
//setup duration humanizer lib
const script = document.createElement('script')
script.src =
'https://cdnjs.cloudflare.com/ajax/libs/humanize-duration/3.28.0/humanize-duration.js'
document.body.appendChild(script)
})()
const speakingTimes = {}
game.subscribeToEvent(
'playerActivelySpeaks',
({ playerActivelySpeaks: { activelySpeaking } }, { player, playerId }) => {
const date = Date.now()
if (!speakingTimes[playerId])
speakingTimes[playerId] = { totalDuration: 0 }
const playerData = speakingTimes[playerId]
if (activelySpeaking) {
console.log(
`🍏 ${
player.name
} (${playerId}) started speaking. At ${new Date(
date
).toLocaleString()}`
)
//if several activelySpeaking events trigger without any stop, skip update and consider the earliest
if (playerData.lastStarted) return
playerData.lastStarted = date
} else {
if (!playerData.lastStarted) return
playerData.totalDuration += date - playerData.lastStarted
delete playerData.lastStarted
const readableDuration = humanizeDuration(
playerData.totalDuration,
{ maxDecimalPoints: 2, round: false }
)
console.log(
`🍎 ${player.name} (${playerId}) stopped speaking. Total Speaking Time: ${readableDuration}.`
)
}
}
)