server/backend/hint.go

64 lines
2.2 KiB
Go
Raw Normal View History

2016-12-09 10:49:29 +00:00
package main
import (
"encoding/base64"
"encoding/binary"
2016-12-09 10:49:29 +00:00
"encoding/json"
"fmt"
"html"
2016-12-09 10:49:29 +00:00
"io/ioutil"
2019-07-11 17:52:13 +00:00
"log"
"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
}
func treatOpeningHint(pathname string, team fic.Team) {
// 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 {
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 {
log.Printf("%s [ERR] %s\n", id, err)
2016-12-09 10:49:29 +00:00
} else if ask.HintId == 0 {
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 {
log.Printf("%s [ERR] Unable to retrieve the given hint: %s\n", id, err)
} else if exercice, err := hint.GetExercice(); err != nil {
log.Printf("%s [ERR] Unable to retrieve the hint's underlying exercice: %s\n", id, err)
} else if !team.HasAccess(exercice) {
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)
2019-01-17 15:55:54 +00:00
} else if err = team.OpenHint(hint); err != nil {
log.Printf("%s [ERR] Unable to open hint: %s\n", id, err)
2016-12-09 10:49:29 +00:00
} else {
// Write event
if lvl, err := exercice.GetLevel(); err != nil {
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 {
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&#160;!", html.EscapeString(team.Name), lvl, theme.Name), "info"); err != nil {
log.Printf("%s [WRN] Unable to create event: %s\n", id, err)
}
2019-07-11 17:52:13 +00:00
genTeamQueue <- &team
appendGenQueue(genStruct{Type: GenEvents})
2019-01-17 15:55:54 +00:00
if err = os.Remove(pathname); err != nil {
log.Printf("%s [ERR] %s\n", id, err)
2016-12-09 10:49:29 +00:00
}
}
}