/
gametime.go
65 lines (58 loc) · 1.66 KB
/
gametime.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
package main
import (
"fmt"
"net"
"net/http"
"net/http/fcgi"
"time"
)
type FastCGIServer struct{}
func isGametime (t time.Time) bool {
var start, end time.Time
year := t.Year()
month := t.Month()
day := t.Day()
hour := t.Hour()
loc := t.Location()
switch t.Weekday() {
case time.Monday,time.Tuesday,time.Wednesday,time.Thursday:
start = time.Date(year, month, day, 20, 0, 0, 0, loc)
end = time.Date(year, month, day, 23, 59, 59, 999999999, loc)
case time.Friday:
start = time.Date(year, month, day, 19, 0, 0, 0, loc)
end = time.Date(year, month, day+1, 2, 59, 59, 999999999, loc)
case time.Saturday:
if hour < 3 {
start = time.Date(year, month, day-1, 23, 59, 59, 999999999, loc)
end = time.Date(year, month, day, 2, 59, 59, 999999999, loc)
} else {
start = time.Date(year, month, day, 15, 0, 0, 0, loc)
end = time.Date(year, month, day+1, 2, 59, 59, 999999999, loc)
}
case time.Sunday:
if hour < 3 {
start = time.Date(year, month, day-1, 23, 59, 59, 999999999, loc)
end = time.Date(year, month, day, 2, 59, 59, 999999999, loc)
} else {
start = time.Date(year, month, day, 17, 0, 0, 0, loc)
end = time.Date(year, month, day, 23, 59, 59, 999999999, loc)
}
}
return (t.After(start) && t.Before(end))
}
func (s FastCGIServer) ServeHTTP(w http.ResponseWriter, req *http.Request) {
now := time.Now()
w.Header().Set("Content-Length", "1")
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
if isGametime(now) {
w.Write([]byte("1"))
} else {
w.Write([]byte("0"))
}
}
func main() {
fmt.Printf("Starting gametime server\n")
l, _ := net.Listen("tcp", "127.0.0.1:9000")
b := new(FastCGIServer)
fcgi.Serve(l, b)
}