-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
240 lines (199 loc) · 4.94 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package main
import (
"context"
"math"
"math/rand"
"net/http"
"sort"
"sync"
"time"
"github.com/DeanThompson/ginpprof"
"github.com/gin-gonic/gin"
"github.com/liumingmin/goutils/log"
"github.com/liumingmin/goutils/utils/safego"
"github.com/liumingmin/goutils/ws"
"github.com/liumingmin/stacktiger/constant"
"github.com/liumingmin/stacktiger/packet"
)
var tigerRank sync.Map
func main() {
ws.InitServer()
e := gin.Default()
e.Static("/static", "./static")
e.GET("/join", JoinGame)
e.GET("/rank", GameRank)
e.GET("/", func(ctx *gin.Context) {
ctx.Redirect(http.StatusFound, "/static/game")
})
ginpprof.WrapGroup(&e.RouterGroup)
e.Run(":8004")
}
func JoinGame(ctx *gin.Context) {
uid := ctx.Query("uid")
if uid == "" {
ctx.JSON(http.StatusOK, gin.H{"msg": "需要登录"})
return
}
connMeta := ws.ConnectionMeta{
UserId: uid,
Typed: 0,
DeviceId: "",
Version: 0,
Charset: 0,
}
_, err := ws.AcceptGin(ctx, connMeta,
ws.ConnEstablishHandlerOption(ConnFinished),
ws.ConnClosedHandlerOption(DisconnFinished),
)
if err != nil {
log.Error(ctx, "Accept client connection failed. error: %v", err)
return
}
}
type tigerScale struct {
LastMts int64
Rand int64
Done chan struct{}
Pause chan struct{}
Resume chan struct{}
CurrScale int
LastScale int
StackScales []int
}
func ConnFinished(ctx context.Context, connection ws.IConnection) {
doneCh := make(chan struct{}, 1)
pauseCh := make(chan struct{}, 1)
resumeCh := make(chan struct{}, 1)
params := &tigerScale{
LastMts: mts(),
Rand: int64(randInt()),
Done: doneCh,
Pause: pauseCh,
Resume: resumeCh,
}
connection.SetCommDataValue(constant.CONN_PARAMS, params)
safego.Go(func() {
ticker := time.NewTicker(time.Millisecond * 250)
defer ticker.Stop()
for {
select {
case <-ticker.C:
scale := math.Abs(math.Sin(float64(mts()-params.LastMts)/600))*2.8 + 0.2
message := ws.GetPoolMessage(constant.WS_S2C_SCALING)
dataMsg := message.DataMsg().(*packet.SCALING)
dataMsg.Scale = float32(scale)
connection.SendMsg(context.Background(), message, nil)
params.CurrScale = int(scale * 10)
case <-doneCh:
return
case <-pauseCh:
if (mts() - params.LastMts) < 300 {
continue
}
ticker.Stop()
dropTiger(connection)
select {
case <-resumeCh:
params.LastMts = mts()
ticker.Reset(time.Millisecond * 250)
}
}
}
})
}
func mts() int64 {
return time.Now().UnixNano() / int64(time.Millisecond)
}
func randInt() int {
rand.Seed(time.Now().UnixNano())
randN := rand.Intn(100)
if randN == 0 {
randN = 10
}
return randN
}
func DisconnFinished(ctx context.Context, connection ws.IConnection) {
obj, ok := connection.GetCommDataValue(constant.CONN_PARAMS)
if !ok {
return
}
scale := obj.(*tigerScale)
select {
case scale.Done <- struct{}{}:
default:
}
}
func DropTiger(ctx context.Context, connection ws.IConnection, msg ws.IMessage) error {
log.Info(ctx, "recv drop msg")
obj, ok := connection.GetCommDataValue(constant.CONN_PARAMS)
if !ok {
return nil
}
scale := obj.(*tigerScale)
select {
case scale.Pause <- struct{}{}:
default:
}
return nil
}
func ResumeTiger(ctx context.Context, connection ws.IConnection, msg ws.IMessage) error {
log.Info(ctx, "recv resume msg")
obj, ok := connection.GetCommDataValue(constant.CONN_PARAMS)
if !ok {
return nil
}
scale := obj.(*tigerScale)
select {
case scale.Resume <- struct{}{}:
default:
}
return nil
}
func dropTiger(connection ws.IConnection) {
obj, ok := connection.GetCommDataValue(constant.CONN_PARAMS)
if !ok {
return
}
success := int32(0)
scale := obj.(*tigerScale)
if scale.LastScale == 0 || scale.LastScale >= scale.CurrScale {
scale.LastScale = scale.CurrScale
scale.StackScales = append(scale.StackScales, scale.CurrScale)
success = 1
} else { //failed
select {
case scale.Done <- struct{}{}:
default:
}
tigerRank.Store(connection.UserId(), len(scale.StackScales))
}
message := ws.GetPoolMessage(constant.WS_S2C_DROP_TIGER)
dataMsg := message.DataMsg().(*packet.STACK_STATUS)
dataMsg.Status = success
dataMsg.Count = int32(len(scale.StackScales))
connection.SendMsg(context.Background(), message, nil)
}
type PlayerScore struct {
Uid string `json:"uid"`
Score int `json:"score"`
}
func GameRank(ctx *gin.Context) {
playerScores := make([]*PlayerScore, 0)
tigerRank.Range(func(key, value interface{}) bool {
playerScores = append(playerScores, &PlayerScore{
Uid: key.(string),
Score: value.(int),
})
return true
})
sort.Slice(playerScores, func(i, j int) bool {
return playerScores[i].Score > playerScores[j].Score
})
ctx.JSON(http.StatusOK, gin.H{"code": 0, "data": playerScores})
}
func init() {
ws.RegisterHandler(constant.WS_C2S_DROP, DropTiger)
ws.RegisterHandler(constant.WS_C2S_RESUME, ResumeTiger)
ws.RegisterDataMsgType(constant.WS_S2C_SCALING, &packet.SCALING{})
ws.RegisterDataMsgType(constant.WS_S2C_DROP_TIGER, &packet.STACK_STATUS{})
}