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
2024-03-17 11:01:09 +00:00
cnt_raw , err := ioutil . ReadFile ( pathname )
if err != nil {
2019-11-25 15:16:27 +00:00
log . Printf ( "%s [ERR] %s\n" , id , err )
2024-03-17 11:01:09 +00:00
return
}
err = json . Unmarshal ( cnt_raw , & ask )
if err != nil {
2019-11-25 15:16:27 +00:00
log . Printf ( "%s [ERR] %s\n" , id , err )
2024-03-17 11:01:09 +00:00
return
}
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 )
2024-03-17 11:01:09 +00:00
return
}
hint , err := fic . GetHint ( ask . HintId )
if err != nil {
2019-11-25 15:16:27 +00:00
log . Printf ( "%s [ERR] Unable to retrieve the given hint: %s\n" , id , err )
2024-03-17 11:01:09 +00:00
return
}
exercice , err := hint . GetExercice ( )
if 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 )
2024-03-17 11:01:09 +00:00
return
}
if exercice . Disabled {
2023-03-20 10:23:03 +00:00
log . Println ( "[!!!] The team submits something for a disabled exercice" )
2024-03-17 11:01:09 +00:00
return
}
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 )
2024-03-17 11:01:09 +00:00
return
}
if ! team . CanSeeHint ( hint ) {
2019-11-25 15:21:58 +00:00
log . Printf ( "%s [!!!] The team asks to open an hint whereas it doesn't have access to it due to hint dependencies\n" , id )
2024-03-17 11:01:09 +00:00
return
}
// Find the corresponding theme
var theme * fic . Theme
if exercice . IdTheme != nil {
theme , err = fic . GetTheme ( * exercice . IdTheme )
if err != nil {
log . Printf ( "%s [ERR] Unable to retrieve theme for exercice %d: %s\n" , id , exercice . Id , err )
return
}
// Theme should not be locked
if theme . Locked {
log . Printf ( "%s [!!!] Open hint received for locked theme %d (hid=%d): %s\n" , id , exercice . IdTheme , ask . HintId , theme . Name )
return
}
}
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 )
2024-03-17 11:01:09 +00:00
return
}
if theme == nil {
if _ , err = fic . NewEvent ( fmt . Sprintf ( "L'équipe %s a dévoilé un indice pour le défi %s !" , html . EscapeString ( team . Name ) , exercice . Title ) , "info" ) ; err != nil {
log . Printf ( "%s [WRN] Unable to create event: %s\n" , id , err )
}
2016-12-09 10:49:29 +00:00
} else {
2024-03-17 11:01:09 +00:00
// Write event
if lvl , err := exercice . GetLevel ( ) ; err != nil {
log . Printf ( "%s [WRN] %s\n" , id , err )
} 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 {
log . Printf ( "%s [WRN] Unable to create event: %s\n" , id , err )
2017-01-24 01:20:20 +00:00
}
2024-03-17 11:01:09 +00:00
}
2017-01-24 01:20:20 +00:00
2024-03-17 11:01:09 +00:00
appendGenQueue ( fic . GenStruct { Id : id , Type : fic . GenTeam , TeamId : team . Id } )
appendGenQueue ( fic . GenStruct { Id : id , Type : fic . GenEvents } )
if err = os . Remove ( pathname ) ; err != nil {
log . Printf ( "%s [ERR] %s\n" , id , err )
2016-12-09 10:49:29 +00:00
}
}