-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
83 lines (71 loc) · 1.59 KB
/
main.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
package main
import (
"encoding/json"
"fmt"
"net/http"
"scrum-poker-api/app/api"
"scrum-poker-api/pkg/setting"
"scrum-poker-api/routers"
"strconv"
"time"
)
func isOverNot(key string) (is_over bool) {
// 玩家个数
sec, _ := setting.Cfg.GetSection("app")
palyer_num, _ := strconv.Atoi(sec.Key("PLAYER_NUM").String())
fmt.Println(palyer_num)
len_wspokers := len(api.Wspokers[key])
if len_wspokers == 0 {
return false
}
for _, v := range api.Wspokers[key] {
if (len(v.Poker) == 0) || len_wspokers == 1 {
return false
}
}
return true
}
func needleTrack() {
ticker := time.NewTicker(time.Second * 2)
go func() {
for _ = range ticker.C {
for k, _ := range api.Wspokers {
// 游戏是否结束
if isOverNot(k) {
res := make(map[string]int)
for _, v := range api.Wspokers[k] {
if _, ok := res[v.Poker]; ok {
res[v.Poker] += 1
} else {
res[v.Poker] = 1
}
}
mjson, _ := json.Marshal(res)
// 写入ws数据
for _, v := range api.Wspokers[k] {
err := v.Ws.WriteMessage(v.Mt, mjson)
if err != nil {
fmt.Println("[" + strconv.Itoa(v.Mt) + "]:推送结果失败")
}
}
//断开链接
// delete(api.Wspokers, k)
}
}
}
}()
time.Sleep(time.Minute)
}
func main() {
router := routers.InitRouter()
s := &http.Server{
Addr: fmt.Sprintf(":%d", setting.HTTPPort),
Handler: router,
ReadTimeout: setting.ReadTimeout,
WriteTimeout: setting.WriteTimeout,
MaxHeaderBytes: 1 << 20,
}
// 探针监测游戏是否结束
go needleTrack()
s.ListenAndServe()
}