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