forked from osuripple/hanayo
-
Notifications
You must be signed in to change notification settings - Fork 3
/
clan.go
295 lines (256 loc) · 8.21 KB
/
clan.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
295
package main
import (
"database/sql"
"fmt"
"math/rand"
"strconv"
"time"
"github.com/gin-gonic/gin"
)
// TODO: replace with simple ResponseInfo containing userid
type clanData struct {
baseTemplateData
ClanID int
}
func leaveClan(c *gin.Context) {
i := c.Param("cid")
// login check
if getContext(c).User.ID == 0 {
resp403(c)
return
}
if db.QueryRow("SELECT 1 FROM user_clans WHERE user = ? AND clan = ? AND perms = 8", getContext(c).User.ID, i).
Scan(new(int)) == sql.ErrNoRows {
// ดวาคนนมแคลนหรอยง
if db.QueryRow("SELECT 1 FROM user_clans WHERE user = ? AND clan = ?", getContext(c).User.ID, i).
Scan(new(int)) == sql.ErrNoRows {
addMessage(c, errorMessage{T(c, "What happened...? We just got... unexpected error?")})
return
}
// กไมรหรอกวามนจะไดผลมย แตควยชงแมงเยดแม
db.Exec("DELETE FROM user_clans WHERE user = ? AND clan = ?", getContext(c).User.ID, i)
addMessage(c, successMessage{T(c, "You've left the clan.")})
getSession(c).Save()
c.Redirect(302, "/c/"+i)
} else {
// เดยวไอเหย มนออกไปยงวะ!!!
if db.QueryRow("SELECT 1 FROM user_clans WHERE user = ? AND clan = ?", getContext(c).User.ID, i).
Scan(new(int)) == sql.ErrNoRows {
addMessage(c, errorMessage{T(c, "What happened...? We just got... unexpected error?")})
return
}
// ลบคำเชญออก
db.Exec("DELETE FROM clans_invites WHERE clan = ?", i)
// ลบทกคนออกจากแคลน :c
db.Exec("DELETE FROM user_clans WHERE clan = ?", i)
// ควยไมสรางแมงละสส :c
db.Exec("DELETE FROM clans WHERE id = ?", i)
addMessage(c, successMessage{T(c, "Your clan has been disbanded!")})
getSession(c).Save()
c.Redirect(302, "/clans?mode=0")
}
}
func clanPage(c *gin.Context) {
var (
clanID int
clanName string
clanDescription string
clanIcon string
)
// ctx := getContext(c)
i := c.Param("cid")
if _, err := strconv.Atoi(i); err != nil {
err := db.QueryRow("SELECT id, name, description, icon FROM clans WHERE name = ? LIMIT 1", i).Scan(&clanID, &clanName, &clanDescription, &clanIcon)
if err != nil && err != sql.ErrNoRows {
c.Error(err)
}
} else {
err := db.QueryRow(`SELECT id, name, description, icon FROM clans WHERE id = ? LIMIT 1`, i).Scan(&clanID, &clanName, &clanDescription, &clanIcon)
switch {
case err == nil:
case err == sql.ErrNoRows:
err := db.QueryRow("SELECT id, name, description, icon FROM clans WHERE name = ? LIMIT 1", i).Scan(&clanID, &clanName, &clanDescription, &clanIcon)
if err != nil && err != sql.ErrNoRows {
c.Error(err)
}
default:
c.Error(err)
}
}
data := new(clanData)
data.ClanID = clanID
defer resp(c, 200, "clansample.html", data)
if data.ClanID == 0 {
data.TitleBar = "404 Clan Not Found!"
data.Messages = append(data.Messages, warningMessage{T(c, "That clan could not be found.")})
return
}
if getContext(c).User.Privileges&1 > 0 {
if db.QueryRow("SELECT 1 FROM clans WHERE clan = ?", clanID).Scan(new(string)) != sql.ErrNoRows {
var bg string
db.QueryRow("SELECT background FROM clans WHERE id = ?", clanID).Scan(&bg)
data.KyutGrill = bg
data.KyutGrillAbsolute = true
}
}
data.TitleBar = T(c, "%s's Clan Page", clanName)
data.DisableHH = true
// data.Scripts = append(data.Scripts, "/static/profile.js")
}
func checkCount(rows *sql.Rows) (count int) {
for rows.Next() {
err := rows.Scan(&count)
if err != nil {
panic(err)
}
}
return count
}
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
func randSeq(n int) string {
rand.Seed(time.Now().UnixNano() + int64(3))
b := make([]rune, n)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
return string(b)
}
func createInvite(c *gin.Context) {
ctx := getContext(c)
if string(c.PostForm("description")) == "" && string(c.PostForm("icon")) == "" && string(c.PostForm("tag")) == "" && string(c.PostForm("bg")) == "" {
if ctx.User.ID == 0 {
resp403(c)
return
}
// เชคแปปวายศสงพอมย
var perms int
db.QueryRow("SELECT perms FROM user_clans WHERE user = ? AND perms = 8 LIMIT 1", ctx.User.ID).Scan(&perms)
// ลบคำเชญเกาออก
var clan int
db.QueryRow("SELECT clan FROM user_clans WHERE user = ? AND perms = 8 LIMIT 1", ctx.User.ID).Scan(&clan)
if clan == 0 {
resp403(c)
return
}
db.Exec("DELETE FROM clans_invites WHERE clan = ?", clan)
var s string
s = randSeq(8)
db.Exec("INSERT INTO clans_invites(clan, invite) VALUES (?, ?)", clan, s)
} else {
// เชคแปปวายศสงพอมย
var perms int
db.QueryRow("SELECT perms FROM user_clans WHERE user = ? AND perms = 8 LIMIT 1", ctx.User.ID).Scan(&perms)
// ลบคำเชญเกาออก
var clan int
db.QueryRow("SELECT clan FROM user_clans WHERE user = ? AND perms = 8 LIMIT 1", ctx.User.ID).Scan(&clan)
if clan == 0 {
resp403(c)
return
}
tag := "0"
if c.PostForm("tag") != "" {
tag = c.PostForm("tag")
}
if db.QueryRow("SELECT 1 FROM clans WHERE tag = ? AND id != ?", c.PostForm("tag"), clan).
Scan(new(int)) != sql.ErrNoRows {
resp403(c)
addMessage(c, errorMessage{T(c, "Someone already used that TAG! Please try another!")})
return
}
db.Exec("UPDATE clans SET description = ?, icon = ?, tag = ?, background = ? WHERE id = ?", c.PostForm("description"), c.PostForm("icon"), tag, c.PostForm("bg"), clan)
}
addMessage(c, successMessage{T(c, "Success!")})
getSession(c).Save()
c.Redirect(302, "/settings/clansettings")
}
func clanInvite(c *gin.Context) {
i := c.Param("inv")
res := resolveInvite(i)
s := strconv.Itoa(res)
if res != 0 {
// ไอบานมนลอกอนยง
if getContext(c).User.ID == 0 {
resp403(c)
return
}
// เหยไอนโดนแบนปะวะ
if getContext(c).User.Privileges&1 != 1 {
resp403(c)
return
}
// มแคลนนปะวะเนย
if db.QueryRow("SELECT 1 FROM clans WHERE id = ?", res).
Scan(new(int)) == sql.ErrNoRows {
addMessage(c, errorMessage{T(c, "Seems like we don't found that clan.")})
getSession(c).Save()
c.Redirect(302, "/c/"+s)
return
}
// ไอเหยนอยในแคลนปะวะ?
if db.QueryRow("SELECT 1 FROM user_clans WHERE user = ?", getContext(c).User.ID).
Scan(new(int)) != sql.ErrNoRows {
addMessage(c, errorMessage{T(c, "Seems like you're already in the clan.")})
getSession(c).Save()
c.Redirect(302, "/c/"+s)
return
}
// ควยไรสส
var count int
var limit int
// เชคคน
db.QueryRow("SELECT COUNT(*) FROM user_clans WHERE clan = ? ", res).Scan(&count)
db.QueryRow("SELECT mlimit FROM clans WHERE id = ? ", res).Scan(&limit)
if count >= limit {
addMessage(c, errorMessage{T(c, "Ow, I'm sorry this clan is already full ;w;")})
getSession(c).Save()
c.Redirect(302, "/c/"+s)
return
}
// เขาแคลน
db.Exec("INSERT INTO `user_clans`(user, clan, perms) VALUES (?, ?, 1);", getContext(c).User.ID, res)
addMessage(c, successMessage{T(c, "You've joined the clan! Hooray!! \\(^o^)/")})
getSession(c).Save()
c.Redirect(302, "/c/"+s)
} else {
resp403(c)
addMessage(c, errorMessage{T(c, "NO!!!")})
}
}
func clanKick(c *gin.Context) {
if getContext(c).User.ID == 0 {
resp403(c)
return
}
if db.QueryRow("SELECT 1 FROM user_clans WHERE user = ? AND perms = 8", getContext(c).User.ID).
Scan(new(int)) == sql.ErrNoRows {
resp403(c)
return
}
member, err := strconv.ParseInt(c.PostForm("member"), 10, 32)
if err != nil {
fmt.Println(err)
}
if member == 0 {
resp403(c)
return
}
if db.QueryRow("SELECT 1 FROM user_clans WHERE user = ? AND perms = 1", member).
Scan(new(int)) == sql.ErrNoRows {
resp403(c)
return
}
db.Exec("DELETE FROM user_clans WHERE user = ?", member)
addMessage(c, successMessage{T(c, "Success!")})
getSession(c).Save()
c.Redirect(302, "/settings/clansettings")
}
func resolveInvite(c string) int {
var clanid int
row := db.QueryRow("SELECT clan FROM clans_invites WHERE invite = ?", c)
err := row.Scan(&clanid)
if err != nil {
fmt.Println(err)
}
fmt.Println(clanid)
return clanid
}