-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
294 lines (258 loc) · 7.71 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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package main
import (
"Lottery/common"
"Lottery/middleware"
"Lottery/modules/model"
"Lottery/modules/transport"
"crypto/rand"
"errors"
"fmt"
"github.com/gin-gonic/gin"
"gorm.io/driver/mysql"
"gorm.io/gorm"
"log"
"math/big"
"net/http"
"strconv"
"sync"
"time"
)
type Bets struct {
common.SQLModel
PlayerId int `json:"player_id" gorm:"column:player_id;"`
BetNumber int `json:"bet_number" gorm:"column:bet_number;"`
BetTime string `json:"bet_time" gorm:"column:bet_time;"`
ResultNumber int `json:"result_number" gorm:"column:result_number;"`
Player model.Player `gorm:"foreignKey:PlayerId"`
}
func (Bets) TableName() string { return "bets" }
var betsWithUsers []struct {
Bets
Player model.Player
}
type BetsCreation struct {
common.SQLModel
PlayerId int `json:"player_id" gorm:"column:player_id;"`
BetNumber int `json:"bet_number" gorm:"column:bet_number;"`
BetTime time.Time `json:"bet_time" gorm:"column:bet_time;"`
ResultNumber int `json:"result_number" gorm:"column:result_number;"`
}
func (BetsCreation) TableName() string { return Bets{}.TableName() }
var userBetTimes = make(map[int]time.Time)
var mutex sync.Mutex
func main() {
dsn := "root:lotto@tcp(db:3306)/vietlott?charset=utf8mb4&parseTime=True&loc=Local"
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
if err != nil {
log.Fatalln("Cannot connect to MySQL:", err)
}
log.Println("Connected:", db)
router := gin.Default()
router.Use(middleware.Recovery())
done := make(chan struct{})
go spinAndSave(db, done)
v1 := router.Group("/v1")
{
v1.POST("/register", transport.CreateUser(db)) // register user
v1.POST("/login", transport.Login(db)) // login user
v1.GET("/player/:id", transport.GetItem(db)) // get user by id
v1.POST("/bets", createBets(db))
v1.GET("/history", getListOfBets(db))
v1.GET("/history/:id", getListOfBetsById(db))
}
router.Run(":3030")
}
func validateBetNumber(betnumber int) error {
if betnumber < 0 || betnumber > 9 {
return errors.New("Chỉ được nhập 1 số từ 0 đến 9")
}
return nil
}
func spinRandomInt(max int) (int, error) {
n, err := rand.Int(rand.Reader, big.NewInt(int64(max)))
if err != nil {
return 0, err
}
return int(n.Int64()), nil
}
func spinAndSave(db *gorm.DB, finish chan struct{}) {
var dbLock sync.Mutex
for {
select {
case <-finish:
return
default:
result, err := spinRandomInt(10)
if err != nil {
fmt.Println("Lỗi quay số:", err)
continue
}
var data BetsCreation
currentTime := time.Now()
truncatedTime := roundUpToNextHour(currentTime)
data.BetTime = truncatedTime
data.ResultNumber = result
dbLock.Lock()
db.Model(&BetsCreation{}).Where("result_number = 0").Update("result_number", result)
if db.RowsAffected == 0 {
var data BetsCreation
currentTime := time.Now()
truncatedTime := roundUpToNextHour(currentTime)
data.BetTime = truncatedTime
data.ResultNumber = result
db.Create(&data)
}
dbLock.Unlock()
time.Sleep(60 * time.Second)
}
}
}
func roundUpToNextHour(t time.Time) time.Time {
return t.Add(time.Hour - time.Duration(t.Minute())*time.Minute - time.Duration(t.Second())*time.Second -
time.Duration(t.Nanosecond())*time.Nanosecond)
}
func createBets(db *gorm.DB) gin.HandlerFunc {
return func(c *gin.Context) {
var dataItem BetsCreation
if err := c.ShouldBind(&dataItem); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
if dataItem.BetNumber < 1 || dataItem.BetNumber > 9 {
errors.New("Chỉ được nhập 1 số từ 1 đến 9")
return
}
// Lock the mutex to ensure thread safety when accessing the global state
mutex.Lock()
defer mutex.Unlock()
var count int64
db.Model(&BetsCreation{}).
Where("user_id = ? AND HOUR(bet_time) = HOUR(?)", dataItem.PlayerId, time.Now()).
Count(&count)
if count > 0 {
c.JSON(http.StatusBadRequest, gin.H{"error": "Bạn chỉ được phép đặt cược trong cùng một khung giờ"})
return
}
// Check if the user has placed a bet before
lastBetTime, exists := userBetTimes[dataItem.PlayerId]
// If the user has placed a bet before, check if it's in a different hour
if exists && lastBetTime.Hour() != time.Now().Hour() {
c.JSON(http.StatusBadRequest, gin.H{"error": "Bạn chỉ được phép đặt cược trong cùng một khung giờ"})
return
}
// Update the last bet time for the user
userBetTimes[dataItem.PlayerId] = roundUpToNextHour(time.Now())
// Your other bet creation logic here...
// For demonstration purposes, let's print the user's last bet time
fmt.Println("Last bet time for user", dataItem.PlayerId, "is", lastBetTime)
currentTime := time.Now()
truncatedTime := roundUpToNextHour(currentTime)
dataItem.BetTime = truncatedTime
/*var betTime = currentTime*/
if err := db.Create(&dataItem).Error; err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"bạn đã đặt số thành công": dataItem.BetNumber})
}
}
func getListOfBets(db *gorm.DB) gin.HandlerFunc {
return func(c *gin.Context) {
type DataPaging struct {
Page int `json:"page" form:"page"`
Limit int `json:"limit" form:"limit"`
Total int64 `json:"total" form:"-"`
}
var paging DataPaging
if err := c.ShouldBind(&paging); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
if paging.Page <= 0 {
paging.Page = 1
}
if paging.Limit <= 0 {
paging.Limit = 10
}
offset := (paging.Page - 1) * paging.Limit
var result []Bets
if err := db.Table(Bets{}.TableName()).Model(&Bets{}).Preload("Player").
Count(&paging.Total).
Offset(offset).
Order("id desc").
Find(&result).Error; err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
/* if err := db.Table("bets").
Model(&Bets{}).
Preload("Player").
Where("bets.id = ?", id).
Count(&paging.Total).
Offset(offset).
Order("id desc").
Find(&data).Error; err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return*/
c.JSON(http.StatusOK, gin.H{"data": result})
}
}
func getListOfBetsById(db *gorm.DB) gin.HandlerFunc {
return func(c *gin.Context) {
type DataPaging struct {
Page int `json:"page" form:"page"`
Limit int `json:"limit" form:"limit"`
Total int64 `json:"total" form:"-"`
}
var paging DataPaging
if err := c.ShouldBind(&paging); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
if paging.Page <= 0 {
paging.Page = 1
}
if paging.Limit <= 0 {
paging.Limit = 10
}
offset := (paging.Page - 1) * paging.Limit
/*var result []Bets*/
var data []Bets
id, err := strconv.Atoi(c.Param("id"))
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"error": err.Error(),
})
return
}
/* if err := db.Table("bets").Where("id = ?", id).
Count(&paging.Total).
Offset(offset).
Order("id desc").
First(&data).Error; err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
return
}*/
/* if err := db.Table("bets").Joins("JOIN players ON players.id = bets.player_id").
Where("bets.id = ?", id).
Count(&paging.Total).
Offset(offset).
Order("id desc").
Find(&data).Error; err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}*/
if err := db.Table("bets").
Model(&Bets{}).
Preload("Player").
Where("bets.id = ?", id).
Count(&paging.Total).
Offset(offset).
Order("id desc").
Find(&data).Error; err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"data": data})
}
}