backend: Don't fail if an hint is already opened

This commit is contained in:
nemunaire 2022-07-12 18:39:23 +02:00
parent 9bc9851b12
commit 41fb5a1cd0
2 changed files with 9 additions and 2 deletions

View file

@ -42,7 +42,7 @@ func treatOpeningHint(pathname string, team *fic.Team) {
log.Printf("%s [!!!] The team asks to open an hint whereas it doesn't have access to the exercice\n", id) log.Printf("%s [!!!] The team asks to open an hint whereas it doesn't have access to the exercice\n", id)
} else if !team.CanSeeHint(hint) { } else if !team.CanSeeHint(hint) {
log.Printf("%s [!!!] The team asks to open an hint whereas it doesn't have access to it due to hint dependencies\n", id) log.Printf("%s [!!!] The team asks to open an hint whereas it doesn't have access to it due to hint dependencies\n", id)
} else if err = team.OpenHint(hint); err != nil { } else if err = team.OpenHint(hint); err != nil && !fic.DBIsDuplicateKeyError(err) { // Skip DUPLICATE KEY errors
log.Printf("%s [ERR] Unable to open hint: %s\n", id, err) log.Printf("%s [ERR] Unable to open hint: %s\n", id, err)
} else { } else {
// Write event // Write event

View file

@ -2,11 +2,13 @@ package fic
import ( import (
"database/sql" "database/sql"
_ "github.com/go-sql-driver/mysql" "errors"
"log" "log"
"os" "os"
"strings" "strings"
"time" "time"
"github.com/go-sql-driver/mysql"
) )
// db stores the connection to the database // db stores the connection to the database
@ -47,6 +49,11 @@ func DSNGenerator() string {
return db_user + ":" + db_password + "@" + db_host + "/" + db_db return db_user + ":" + db_password + "@" + db_host + "/" + db_db
} }
func DBIsDuplicateKeyError(err error) bool {
var mysqlErr *mysql.MySQLError
return errors.As(err, &mysqlErr) && mysqlErr.Number == 1062
}
// DBInit establishes the connection to the database // DBInit establishes the connection to the database
func DBInit(dsn string) (err error) { func DBInit(dsn string) (err error) {
if db, err = sql.Open("mysql", dsn+"?parseTime=true&foreign_key_checks=1"); err != nil { if db, err = sql.Open("mysql", dsn+"?parseTime=true&foreign_key_checks=1"); err != nil {