-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmakevote.go
185 lines (159 loc) · 5.09 KB
/
makevote.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
package main
import (
"fmt"
"net"
"regexp"
"strconv"
"strings"
"time"
"github.com/gin-gonic/gin"
)
type TopgVoter struct {
UserID int `db:"userid"`
Count int `db:"count"`
Time int `db:"time"`
}
type topgVotePageStruct struct {
baseTemplateData
CanUserSubmit bool
NextTimeSubmit int64
Balance int
}
func SubmiterVotes(c *gin.Context) {
addr, err := net.LookupIP("monitor.topg.org")
if err != nil {
resp403(c)
return
}
remoteAddr := strings.Split(c.Request.Header.Get("X-FORWARDED-FOR"), ",")
if remoteAddr[0] != addr[0].String() {
fmt.Println(remoteAddr)
fmt.Println(addr[0].String())
fmt.Println("ip not correct?")
resp403(c)
return
}
var reResp = regexp.MustCompile(`/[^A-Za-z0-9\_\-]+/`)
PResp := reResp.ReplaceAllString(c.Query("p_resp"), ``)
userID, err := strconv.Atoi(PResp)
if err != nil {
resp403(c)
return
}
data := new(topgVotePageStruct)
data.KyutGrill = "donate.jpg"
data.TitleBar = T(c, "Voting")
defer resp(c, 200, "topgvoter.html", data)
currentTime := time.Now().Unix()
voter := TopgVoter{}
err = db.Get(&voter, "SELECT userid, count, time FROM topg_votes WHERE userid = ?", userID)
if err != nil {
db.Exec("INSERT INTO topg_votes (userid, count, time) VALUES (?, ?, ?)", userID, 0, int(currentTime))
err = db.Get(&voter, "SELECT userid, count, time FROM topg_votes WHERE userid = ?", userID)
if err != nil {
data.Messages = append(data.Messages, errorMessage{T(c, "Sorry, but we can't register you(")})
return
}
}
if voter.Time > int(currentTime) {
data.Messages = append(data.Messages, errorMessage{T(c, "Not now, try later!")})
data.NextTimeSubmit = int64(voter.Time)
} else {
voter.Time = int(currentTime + 172800)
voter.Count++
db.Exec("UPDATE topg_votes SET time = ?, count = ? WHERE userid = ?", voter.Time, voter.Count, voter.UserID)
}
// if getContext(c).User.ID == 0 {
// }
// data := new(topgVotePageStruct)
// defer resp(c, 200, "topgvoter.html", data)
// currentTime := time.Now().Unix()
// isNewbie := false
// voter := TopgVoter{}
// err := db.Get(&voter, "SELECT userid, count, time FROM topg_votes WHERE id = ?", getContext(c).User.ID)
// if err != nil {
// db.Exec("INSERT INTO topg_votes (userid, count, time) VALUES (?, ?, ?)", getContext(c).User.ID, 0, int(currentTime))
// err = db.Get(&voter, "SELECT userid, count, time FROM topg_votes WHERE id = ?", getContext(c).User.ID)
// if err != nil {
// data.Messages = append(data.Messages, errorMessage{T(c, "Sorry, but we can't register you(")})
// return
// }
// isNewbie = true
// }
// data.Balance = voter.Count
// data.CanUserSubmit = false
// // 604800 - 1 week!
// if isNewbie {
// // submit him vote
// // TODO: Topg Publisher!
// voter.Time += 604800
// voter.Count++
// db.Exec("UPDATE topg_votes SET time = ?, count = ? WHERE userid = ?", voter.Time, voter.Count, voter.UserID)
// data.NextTimeSubmit = int64(voter.Time)
// data.Messages = append(data.Messages, successMessage{T(c, "You successfully voted for us. You have +1 vote")})
// data.Balance = voter.Count
// } else {
// if voter.Time > int(currentTime) {
// data.Messages = append(data.Messages, errorMessage{T(c, "Not now, try later!")})
// data.NextTimeSubmit = int64(voter.Time)
// } else {
// voter.Time = int(currentTime + 604800)
// voter.Count++
// db.Exec("UPDATE topg_votes SET time = ?, count = ? WHERE userid = ?", voter.Time, voter.Count, voter.UserID)
// data.NextTimeSubmit = int64(voter.Time)
// data.Messages = append(data.Messages, successMessage{T(c, "You successfully voted for us. You have +1 vote")})
// data.Balance = voter.Count
// }
// }
return
}
func GetVoter(c *gin.Context) {
if getContext(c).User.ID == 0 {
resp403(c)
return
}
data := new(topgVotePageStruct)
data.KyutGrill = "donate.jpg"
data.TitleBar = T(c, "Voting")
defer resp(c, 200, "topgvoter.html", data)
voter := TopgVoter{}
err := db.Get(&voter, "SELECT userid, count, time FROM topg_votes WHERE userid = ?", getContext(c).User.ID)
if err != nil {
voter.Count = 0
voter.Time = 0
data.CanUserSubmit = true
data.NextTimeSubmit = 0
return
}
data.Balance = voter.Count
data.CanUserSubmit = false
currentTime := time.Now().Unix()
if voter.Time > int(currentTime) {
data.CanUserSubmit = false
data.NextTimeSubmit = int64(voter.Time)
} else {
data.CanUserSubmit = true
}
return
}
func exchangeVotes(c *gin.Context) {
if getContext(c).User.ID == 0 {
resp403(c)
return
}
voter := TopgVoter{}
err := db.Get(&voter, "SELECT userid, count, time FROM topg_votes WHERE userid = ?", getContext(c).User.ID)
if err != nil {
respEmpty(c, "Forbidden", warningMessage{T(c, "You haven't enough votes. Min votes to exchange = 15. 15 votes = 150 rub")})
return
}
if voter.Count < 15 {
respEmpty(c, "Forbidden", warningMessage{T(c, "You haven't enough votes. Min votes to exchange = 15. 15 votes = 150 rub")})
return
}
voter.Count -= 15
db.Exec("UPDATE topg_votes SET count = ? WHERE userid = ?", voter.Count, voter.UserID)
db.Exec(`UPDATE users SET balance = balance+150 WHERE id = ?`, voter.UserID)
c.Redirect(302, "/vote")
return
}