修复提交flag的code错误问题
This commit is contained in:
@@ -13,18 +13,19 @@ import (
|
|||||||
func hasAlreadySolved(uid int, cid int) (exists bool) {
|
func hasAlreadySolved(uid int, cid int) (exists bool) {
|
||||||
Link()
|
Link()
|
||||||
DB := db.DBsnctf
|
DB := db.DBsnctf
|
||||||
err := DB.Raw("SELECT EXISTS(SELECT 1 FROM solve WHERE uid=? AND cid=?)",uid,cid).Scan(&exists).Error
|
err := DB.Raw("SELECT EXISTS(SELECT 1 FROM solve WHERE uid=? AND cid=?)", uid, cid).Scan(&exists).Error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
return exists
|
return exists
|
||||||
}
|
}
|
||||||
|
|
||||||
// isChallengeExisted 检查数据库中是否存在某个题目。
|
// isChallengeExisted 检查数据库中是否存在某个题目。
|
||||||
func isChallengeExisted(id int) (exists bool) {
|
func isChallengeExisted(id int) (exists bool) {
|
||||||
Link()
|
Link()
|
||||||
DB := db.DBsnctf
|
DB := db.DBsnctf
|
||||||
command := "SELECT EXISTS(SELECT 1 FROM challenge WHERE id = ?);"
|
command := "SELECT EXISTS(SELECT 1 FROM challenge WHERE id = ?);"
|
||||||
err := DB.Raw(command,id).Scan(&exists).Error
|
err := DB.Raw(command, id).Scan(&exists).Error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
@@ -48,7 +49,7 @@ func addSubmission(s *Submission) error {
|
|||||||
func getFlag(id int) (flag string, err error) {
|
func getFlag(id int) (flag string, err error) {
|
||||||
Link()
|
Link()
|
||||||
DB := db.DBsnctf
|
DB := db.DBsnctf
|
||||||
err = DB.Table("challenge").Select("flag").Where("id = ?",id).Find(&flag).Error
|
err = DB.Table("challenge").Select("flag").Where("id = ?", id).Find(&flag).Error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
@@ -81,10 +82,11 @@ func addUserScore(username string, cid int) error {
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// updateUserScores 操作数据库更新解出用户的分数。
|
// updateUserScores 操作数据库更新解出用户的分数。
|
||||||
func updateUserScores(reducedScore, cid int) error {
|
func updateUserScores(reducedScore, cid int) error {
|
||||||
|
|
||||||
DB:=db.DBsnctf
|
DB := db.DBsnctf
|
||||||
command := "UPDATE score SET score=score-? WHERE EXISTS(SELECT 1 FROM user,solve WHERE user.id=solve.uid AND score.username=user.username AND solve.cid=?);"
|
command := "UPDATE score SET score=score-? WHERE EXISTS(SELECT 1 FROM user,solve WHERE user.id=solve.uid AND score.username=user.username AND solve.cid=?);"
|
||||||
err := DB.Exec(command, reducedScore, cid).Error
|
err := DB.Exec(command, reducedScore, cid).Error
|
||||||
|
|
||||||
@@ -127,12 +129,12 @@ func SubmitFlag(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
session,err := Store.Get(c.Request, "SNCTFSESSID")
|
session, err := Store.Get(c.Request, "SNCTFSESSID")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(400, gin.H{"code": 400, "msg": "GET SNCTFSESSID error!"})
|
c.JSON(400, gin.H{"code": 400, "msg": "GET SNCTFSESSID error!"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
user,ok := session.Values["user"].(User)
|
user, ok := session.Values["user"].(User)
|
||||||
if !ok {
|
if !ok {
|
||||||
c.JSON(400, gin.H{"code": 400, "msg": "GET user error!"})
|
c.JSON(400, gin.H{"code": 400, "msg": "GET user error!"})
|
||||||
return
|
return
|
||||||
@@ -158,21 +160,21 @@ func SubmitFlag(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
// 是否已经解出该题
|
// 是否已经解出该题
|
||||||
if hasAlreadySolved(user.ID, request.Cid) {
|
if hasAlreadySolved(user.ID, request.Cid) {
|
||||||
c.JSON(400, gin.H{"code": 400, "msg": "Already solved!"})
|
c.JSON(200, gin.H{"code": 400, "msg": "Already solved!"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// 获取flag
|
// 获取flag
|
||||||
flag, err := getFlag(request.Cid)
|
flag, err := getFlag(request.Cid)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
c.JSON(400, gin.H{"code": 400, "msg": "Get flag failure!"})
|
c.JSON(200, gin.H{"code": 400, "msg": "Get flag failure!"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// 检查flag是否正确
|
// 检查flag是否正确
|
||||||
if flag != request.Flag {
|
if flag != request.Flag {
|
||||||
c.JSON(400, gin.H{"code": 400, "msg": "Wrong flag!"})
|
c.JSON(200, gin.H{"code": 400, "msg": "Wrong flag!"})
|
||||||
return
|
return
|
||||||
}else {
|
} else {
|
||||||
// Solve记录
|
// Solve记录
|
||||||
solve := &Solve{
|
solve := &Solve{
|
||||||
UserID: user.ID,
|
UserID: user.ID,
|
||||||
@@ -181,7 +183,7 @@ func SubmitFlag(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
err = addSolve(solve)
|
err = addSolve(solve)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(400, gin.H{"code": 400, "msg": "Record solve failure!"})
|
c.JSON(200, gin.H{"code": 400, "msg": "Record solve failure!"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
//加分
|
//加分
|
||||||
@@ -208,4 +210,4 @@ func SubmitFlag(c *gin.Context) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user