2016-12-09 10:49:29 +00:00
package main
import (
2019-11-25 15:16:27 +00:00
"encoding/base64"
"encoding/binary"
2016-12-09 10:49:29 +00:00
"encoding/json"
2017-01-24 01:20:20 +00:00
"fmt"
2020-01-30 17:59:45 +00:00
"html"
2016-12-09 10:49:29 +00:00
"io/ioutil"
2019-07-11 17:52:13 +00:00
"log"
2019-11-25 15:16:27 +00:00
"math/rand"
2016-12-09 10:49:29 +00:00
"os"
"srs.epita.fr/fic-server/libfic"
)
type askOpenHint struct {
2019-07-11 17:52:13 +00:00
HintId int64 ` json:"id" `
2016-12-09 10:49:29 +00:00
}
2021-11-22 14:35:07 +00:00
func treatOpeningHint ( pathname string , team * fic . Team ) {
2019-11-25 15:16:27 +00:00
// Generate a unique identifier to follow the request in logs
bid := make ( [ ] byte , 5 )
binary . LittleEndian . PutUint32 ( bid , rand . Uint32 ( ) )
id := "[" + base64 . StdEncoding . EncodeToString ( bid ) + "]"
log . Println ( id , "New openingHint receive" , pathname )
2016-12-09 10:49:29 +00:00
var ask askOpenHint
if cnt_raw , err := ioutil . ReadFile ( pathname ) ; err != nil {
2019-11-25 15:16:27 +00:00
log . Printf ( "%s [ERR] %s\n" , id , err )
2019-01-17 15:55:54 +00:00
} else if err = json . Unmarshal ( cnt_raw , & ask ) ; err != nil {
2019-11-25 15:16:27 +00:00
log . Printf ( "%s [ERR] %s\n" , id , err )
2016-12-09 10:49:29 +00:00
} else if ask . HintId == 0 {
2019-11-25 15:16:27 +00:00
log . Printf ( "%s [WRN] Invalid content in hint file: %s\n" , id , pathname )
2016-12-09 10:49:29 +00:00
os . Remove ( pathname )
} else if hint , err := fic . GetHint ( ask . HintId ) ; err != nil {
2019-11-25 15:16:27 +00:00
log . Printf ( "%s [ERR] Unable to retrieve the given hint: %s\n" , id , err )
2019-02-05 02:24:52 +00:00
} else if exercice , err := hint . GetExercice ( ) ; err != nil {
2019-11-25 15:16:27 +00:00
log . Printf ( "%s [ERR] Unable to retrieve the hint's underlying exercice: %s\n" , id , err )
2023-03-20 10:23:03 +00:00
} else if exercice . Disabled {
log . Println ( "[!!!] The team submits something for a disabled exercice" )
2019-02-05 02:24:52 +00:00
} else if ! team . HasAccess ( exercice ) {
2019-11-25 15:16:27 +00:00
log . Printf ( "%s [!!!] The team asks to open an hint whereas it doesn't have access to the exercice\n" , id )
2019-11-25 15:21:58 +00:00
} 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 )
2022-07-12 16:39:23 +00:00
} else if err = team . OpenHint ( hint ) ; err != nil && ! fic . DBIsDuplicateKeyError ( err ) { // Skip DUPLICATE KEY errors
2019-11-25 15:16:27 +00:00
log . Printf ( "%s [ERR] Unable to open hint: %s\n" , id , err )
2016-12-09 10:49:29 +00:00
} else {
2017-01-24 01:20:20 +00:00
// Write event
2019-02-05 02:24:52 +00:00
if lvl , err := exercice . GetLevel ( ) ; err != nil {
2019-11-25 15:16:27 +00:00
log . Printf ( "%s [WRN] %s\n" , id , err )
2018-12-08 20:17:04 +00:00
} else if theme , err := fic . GetTheme ( exercice . IdTheme ) ; err != nil {
2019-11-25 15:16:27 +00:00
log . Printf ( "%s [WRN] %s\n" , id , err )
2020-01-30 17:59:45 +00:00
} else if _ , err = fic . NewEvent ( fmt . Sprintf ( "L'équipe %s a dévoilé un indice pour le <strong>%d<sup>e</sup></strong> défi %s !" , html . EscapeString ( team . Name ) , lvl , theme . Name ) , "info" ) ; err != nil {
2019-11-25 15:16:27 +00:00
log . Printf ( "%s [WRN] Unable to create event: %s\n" , id , err )
2017-01-24 01:20:20 +00:00
}
2023-07-10 07:17:02 +00:00
appendGenQueue ( fic . GenStruct { Id : id , Type : fic . GenTeam , TeamId : team . Id } )
appendGenQueue ( fic . GenStruct { Id : id , Type : fic . GenEvents } )
2019-01-17 15:55:54 +00:00
if err = os . Remove ( pathname ) ; err != nil {
2019-11-25 15:16:27 +00:00
log . Printf ( "%s [ERR] %s\n" , id , err )
2016-12-09 10:49:29 +00:00
}
}
}