forked from NicoNex/echotron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer.go
96 lines (84 loc) · 2.39 KB
/
timer.go
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
/*
* Echotron
* Copyright (C) 2019 Nicolò Santamaria
*
* Echotron is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Echotron is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package echotron
import (
"fmt"
"time"
)
type timer struct {
timestamp int64
lapseTime int64
callback func()
}
var timerMap map[int64]map[string]*timer
// AddTimer creates a timer associated with a name to the current
// bot session that calls a callback once any given amounth of time.
func AddTimer(chatId int64, name string, callback func(), lapse int64) {
if timerMap[chatId] == nil {
timerMap[chatId] = make(map[string]*timer)
}
timerMap[chatId][name] = &timer{
time.Now().Unix(),
lapse,
callback,
}
}
// SetTimerLapse sets the lapse time between each call to the callback.
func SetTimerLapse(chatId int64, lapse int64, name string) error {
timer, ok := timerMap[chatId][name]
if !ok {
return fmt.Errorf("Error: cannot find timer %s for instance %d", name, chatId)
}
timer.lapseTime = lapse
return nil
}
// ResetTimer sets the current timestamp associated with the last call
// to the current epoch time in Unix format.
func ResetTimer(chatId int64, name string) error {
timer, ok := timerMap[chatId][name]
if !ok {
return fmt.Errorf("Error: cannot find timer %s for instance %d", name, chatId)
}
timer.timestamp = time.Now().Unix()
return nil
}
// DelTimer deletes the specified timer from the routine.
func DelTimer(chatId int64, name string) {
if timer, ok := timerMap[chatId]; ok {
delete(timer, name)
}
}
func timerRoutine() {
for {
for _, m := range timerMap {
go func(m map[string]*timer) {
for _, t := range m {
if time.Now().Unix()-t.timestamp >= t.lapseTime {
t.timestamp = time.Now().Unix()
go t.callback()
}
}
}(m)
}
time.Sleep(time.Second)
}
}
func init() {
timerMap = make(map[int64]map[string]*timer)
go timerRoutine()
}