Settings are now given through TEAMS/settings.json instead of been given through command line arguments
This commit is contained in:
parent
37310e41f5
commit
10fe40e4a8
@ -11,8 +11,10 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"gopkg.in/fsnotify.v1"
|
|
||||||
"srs.epita.fr/fic-server/libfic"
|
"srs.epita.fr/fic-server/libfic"
|
||||||
|
"srs.epita.fr/fic-server/settings"
|
||||||
|
|
||||||
|
"gopkg.in/fsnotify.v1"
|
||||||
)
|
)
|
||||||
|
|
||||||
var TeamsDir string
|
var TeamsDir string
|
||||||
@ -41,19 +43,31 @@ func watchsubdir(watcher *fsnotify.Watcher, pathname string) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func reloadSettings(config settings.FICSettings) {
|
||||||
|
fic.PartialValidation = config.PartialValidation
|
||||||
|
fic.UnlockedChallenges = !config.EnableExerciceDepend
|
||||||
|
|
||||||
|
fic.FirstBlood = config.FirstBlood
|
||||||
|
fic.SubmissionCostBase = config.SubmissionCostBase
|
||||||
|
|
||||||
|
log.Println("Generating files...")
|
||||||
|
go func() {
|
||||||
|
genAll()
|
||||||
|
log.Println("Full generation done")
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var dsn = flag.String("dsn", "fic:fic@/fic", "DSN to connect to the MySQL server")
|
var dsn = flag.String("dsn", "fic:fic@/fic", "DSN to connect to the MySQL server")
|
||||||
flag.StringVar(&SubmissionDir, "submission", "./submissions", "Base directory where save submissions")
|
flag.StringVar(&SubmissionDir, "submission", "./submissions", "Base directory where save submissions")
|
||||||
flag.StringVar(&TeamsDir, "teams", "../TEAMS", "Base directory where save teams JSON files")
|
flag.StringVar(&TeamsDir, "teams", "./TEAMS", "Base directory where save teams JSON files")
|
||||||
flag.StringVar(&fic.FilesDir, "files", "/files", "Request path prefix to reach files")
|
flag.StringVar(&fic.FilesDir, "files", "/files", "Request path prefix to reach files")
|
||||||
var skipFullGeneration = flag.Bool("skipFullGeneration", false, "Skip initial full generation (safe to skip after start)")
|
|
||||||
flag.BoolVar(&fic.PartialValidation, "partialValidation", false, "Validates flags which are corrects, don't be binary")
|
|
||||||
flag.BoolVar(&fic.UnlockedChallenges, "unlockedChallenges", false, "Make all challenges accessible without having to validate previous level")
|
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
log.SetPrefix("[backend] ")
|
log.SetPrefix("[backend] ")
|
||||||
|
|
||||||
SubmissionDir = path.Clean(SubmissionDir)
|
SubmissionDir = path.Clean(SubmissionDir)
|
||||||
|
TeamsDir = path.Clean(TeamsDir)
|
||||||
|
|
||||||
rand.Seed(time.Now().UnixNano())
|
rand.Seed(time.Now().UnixNano())
|
||||||
|
|
||||||
@ -70,6 +84,9 @@ func main() {
|
|||||||
}
|
}
|
||||||
defer fic.DBClose()
|
defer fic.DBClose()
|
||||||
|
|
||||||
|
// Load configuration
|
||||||
|
settings.LoadAndWatchSettings(path.Join(TeamsDir, settings.SettingsFile), reloadSettings)
|
||||||
|
|
||||||
log.Println("Registering directory events...")
|
log.Println("Registering directory events...")
|
||||||
watcher, err := fsnotify.NewWatcher()
|
watcher, err := fsnotify.NewWatcher()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -81,14 +98,6 @@ func main() {
|
|||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !*skipFullGeneration {
|
|
||||||
log.Println("Generating files...")
|
|
||||||
go func() {
|
|
||||||
genAll()
|
|
||||||
log.Println("Full generation done")
|
|
||||||
}()
|
|
||||||
}
|
|
||||||
|
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case ev := <-watcher.Events:
|
case ev := <-watcher.Events:
|
||||||
|
@ -6,13 +6,21 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var denyNameChange bool = true
|
||||||
|
|
||||||
type ChNameHandler struct {}
|
type ChNameHandler struct {}
|
||||||
|
|
||||||
func (n ChNameHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (n ChNameHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
log.Printf("Handling %s name change request from %s: %s [%s]\n", r.Method, r.RemoteAddr, r.URL.Path, r.UserAgent())
|
|
||||||
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
|
||||||
|
if denyNameChange {
|
||||||
|
log.Printf("UNHANDELED %s name change request from %s: %s [%s]\n", r.Method, r.RemoteAddr, r.URL.Path, r.UserAgent())
|
||||||
|
http.Error(w, "{\"errmsg\":\"Le changement de nom est prohibé.\"}", http.StatusForbidden)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("Handling %s name change request from %s: %s [%s]\n", r.Method, r.RemoteAddr, r.URL.Path, r.UserAgent())
|
||||||
|
|
||||||
// Check request type and size
|
// Check request type and size
|
||||||
if r.Method != "POST" {
|
if r.Method != "POST" {
|
||||||
http.Error(w, "{\"errmsg\":\"Requête invalide.\"}", http.StatusBadRequest)
|
http.Error(w, "{\"errmsg\":\"Requête invalide.\"}", http.StatusBadRequest)
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
@ -9,6 +8,8 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"srs.epita.fr/fic-server/settings"
|
||||||
)
|
)
|
||||||
|
|
||||||
const startedFile = "started"
|
const startedFile = "started"
|
||||||
@ -16,8 +17,9 @@ const startedFile = "started"
|
|||||||
var TeamsDir string
|
var TeamsDir string
|
||||||
var SubmissionDir string
|
var SubmissionDir string
|
||||||
|
|
||||||
func touchStartedFile(startSub time.Duration) {
|
var touchTimer *time.Timer = nil
|
||||||
time.Sleep(startSub)
|
|
||||||
|
func touchStartedFile() {
|
||||||
if fd, err := os.Create(path.Join(TeamsDir, startedFile)); err == nil {
|
if fd, err := os.Create(path.Join(TeamsDir, startedFile)); err == nil {
|
||||||
log.Println("Started! Go, Go, Go!!")
|
log.Println("Started! Go, Go, Go!!")
|
||||||
fd.Close()
|
fd.Close()
|
||||||
@ -26,20 +28,48 @@ func touchStartedFile(startSub time.Duration) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func reloadSettings(config settings.FICSettings) {
|
||||||
|
if challengeStart != config.Start || challengeEnd != config.End {
|
||||||
|
if touchTimer != nil {
|
||||||
|
touchTimer.Stop()
|
||||||
|
}
|
||||||
|
startSub := config.Start.Sub(time.Now())
|
||||||
|
if startSub > 0 {
|
||||||
|
log.Println("Challenge will starts at", config.Start, "in", startSub)
|
||||||
|
|
||||||
|
if _, err := os.Stat(path.Join(TeamsDir, startedFile)); !os.IsNotExist(err) {
|
||||||
|
os.Remove(path.Join(TeamsDir, startedFile))
|
||||||
|
}
|
||||||
|
|
||||||
|
touchTimer = time.AfterFunc(config.Start.Sub(time.Now().Add(time.Duration(1 * time.Second))), touchStartedFile)
|
||||||
|
} else {
|
||||||
|
log.Println("Challenge started at", config.Start, "since", -startSub)
|
||||||
|
touchStartedFile()
|
||||||
|
}
|
||||||
|
log.Println("Challenge ends on", config.End)
|
||||||
|
|
||||||
|
challengeStart = config.Start
|
||||||
|
challengeEnd = config.End
|
||||||
|
} else {
|
||||||
|
log.Println("Configuration reloaded, but start/end times doesn't change.")
|
||||||
|
}
|
||||||
|
|
||||||
|
enableResolutionRoute = config.EnableResolutionRoute
|
||||||
|
denyNameChange = config.DenyNameChange
|
||||||
|
allowRegistration = config.AllowRegistration
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var bind = flag.String("bind", "127.0.0.1:8080", "Bind port/socket")
|
var bind = flag.String("bind", "127.0.0.1:8080", "Bind port/socket")
|
||||||
var prefix = flag.String("prefix", "", "Request path prefix to strip (from proxy)")
|
var prefix = flag.String("prefix", "", "Request path prefix to strip (from proxy)")
|
||||||
var start = flag.Int64("start", 0, fmt.Sprintf("Challenge start timestamp (in 2 minutes: %d)", time.Now().Unix()/60*60+120))
|
|
||||||
var duration = flag.Duration("duration", 180*time.Minute, "Challenge duration")
|
|
||||||
var denyChName = flag.Bool("denyChName", false, "Deny team to change their name")
|
|
||||||
var allowRegistration = flag.Bool("allowRegistration", false, "New team can add itself")
|
|
||||||
var resolutionRoute = flag.Bool("resolutionRoute", false, "Enable resolution route")
|
|
||||||
flag.StringVar(&TeamsDir, "teams", "./TEAMS", "Base directory where save teams JSON files")
|
flag.StringVar(&TeamsDir, "teams", "./TEAMS", "Base directory where save teams JSON files")
|
||||||
flag.StringVar(&SubmissionDir, "submission", "./submissions/", "Base directory where save submissions")
|
flag.StringVar(&SubmissionDir, "submission", "./submissions/", "Base directory where save submissions")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
log.SetPrefix("[frontend] ")
|
log.SetPrefix("[frontend] ")
|
||||||
|
|
||||||
|
SubmissionDir = path.Clean(SubmissionDir)
|
||||||
|
|
||||||
log.Println("Creating submission directory...")
|
log.Println("Creating submission directory...")
|
||||||
if _, err := os.Stat(SubmissionDir); os.IsNotExist(err) {
|
if _, err := os.Stat(SubmissionDir); os.IsNotExist(err) {
|
||||||
if err := os.MkdirAll(SubmissionDir, 0777); err != nil {
|
if err := os.MkdirAll(SubmissionDir, 0777); err != nil {
|
||||||
@ -47,41 +77,18 @@ func main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
startTime := time.Unix(*start, 0)
|
// Load configuration
|
||||||
startSub := startTime.Sub(time.Now())
|
settings.LoadAndWatchSettings(path.Join(TeamsDir, settings.SettingsFile), reloadSettings)
|
||||||
end := startTime.Add(*duration).Add(time.Duration(1 * time.Second))
|
|
||||||
|
|
||||||
log.Println("Challenge ends on", end)
|
// Register handlers
|
||||||
if startSub > 0 {
|
http.Handle(fmt.Sprintf("%s/chname/", *prefix), http.StripPrefix(fmt.Sprintf("%s/chname/", *prefix), ChNameHandler{}))
|
||||||
log.Println("Challenge starts at", startTime, "in", startSub)
|
|
||||||
|
|
||||||
fmt.Printf("PRESS ENTER TO LAUNCH THE COUNTDOWN ")
|
|
||||||
bufio.NewReader(os.Stdin).ReadLine()
|
|
||||||
|
|
||||||
if _, err := os.Stat(path.Join(TeamsDir, startedFile)); !os.IsNotExist(err) {
|
|
||||||
os.Remove(path.Join(TeamsDir, startedFile))
|
|
||||||
}
|
|
||||||
|
|
||||||
go touchStartedFile(startTime.Sub(time.Now().Add(time.Duration(1 * time.Second))))
|
|
||||||
} else {
|
|
||||||
log.Println("Challenge started at", startTime, "since", -startSub)
|
|
||||||
go touchStartedFile(time.Duration(0))
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Println("Registering handlers...")
|
|
||||||
http.Handle(fmt.Sprintf("%s/time.json", *prefix), http.StripPrefix(*prefix, TimeHandler{startTime, *duration}))
|
|
||||||
if *resolutionRoute {
|
|
||||||
http.Handle(fmt.Sprintf("%s/resolution/", *prefix), http.StripPrefix(fmt.Sprintf("%s/resolution/", *prefix), ResolutionHandler{}))
|
|
||||||
}
|
|
||||||
if *allowRegistration {
|
|
||||||
http.Handle(fmt.Sprintf("%s/registration", *prefix), http.StripPrefix(fmt.Sprintf("%s/registration", *prefix), RegistrationHandler{}))
|
|
||||||
}
|
|
||||||
if !*denyChName {
|
|
||||||
http.Handle(fmt.Sprintf("%s/chname/", *prefix), http.StripPrefix(fmt.Sprintf("%s/chname/", *prefix), ChNameHandler{}))
|
|
||||||
}
|
|
||||||
http.Handle(fmt.Sprintf("%s/openhint/", *prefix), http.StripPrefix(fmt.Sprintf("%s/openhint/", *prefix), HintHandler{}))
|
http.Handle(fmt.Sprintf("%s/openhint/", *prefix), http.StripPrefix(fmt.Sprintf("%s/openhint/", *prefix), HintHandler{}))
|
||||||
http.Handle(fmt.Sprintf("%s/submission/", *prefix), http.StripPrefix(fmt.Sprintf("%s/submission/", *prefix), SubmissionHandler{end}))
|
http.Handle(fmt.Sprintf("%s/registration", *prefix), http.StripPrefix(fmt.Sprintf("%s/registration", *prefix), RegistrationHandler{}))
|
||||||
|
http.Handle(fmt.Sprintf("%s/resolution/", *prefix), http.StripPrefix(fmt.Sprintf("%s/resolution/", *prefix), ResolutionHandler{}))
|
||||||
|
http.Handle(fmt.Sprintf("%s/submission/", *prefix), http.StripPrefix(fmt.Sprintf("%s/submission/", *prefix), SubmissionHandler{}))
|
||||||
|
http.Handle(fmt.Sprintf("%s/time.json", *prefix), http.StripPrefix(*prefix, TimeHandler{}))
|
||||||
|
|
||||||
|
// Serve pages
|
||||||
log.Println(fmt.Sprintf("Ready, listening on %s", *bind))
|
log.Println(fmt.Sprintf("Ready, listening on %s", *bind))
|
||||||
if err := http.ListenAndServe(*bind, nil); err != nil {
|
if err := http.ListenAndServe(*bind, nil); err != nil {
|
||||||
log.Fatal("Unable to listen and serve: ", err)
|
log.Fatal("Unable to listen and serve: ", err)
|
||||||
|
@ -6,13 +6,21 @@ import (
|
|||||||
"path"
|
"path"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var allowRegistration bool = false
|
||||||
|
|
||||||
type RegistrationHandler struct {}
|
type RegistrationHandler struct {}
|
||||||
|
|
||||||
func (e RegistrationHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (e RegistrationHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
log.Printf("Handling %s registration request from %s: %s [%s]\n", r.Method, r.RemoteAddr, r.URL.Path, r.UserAgent())
|
|
||||||
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
|
||||||
|
if !allowRegistration {
|
||||||
|
log.Printf("UNHANDLED %s registration request from %s: %s [%s]\n", r.Method, r.RemoteAddr, r.URL.Path, r.UserAgent())
|
||||||
|
http.Error(w, "{\"errmsg\":\"L'enregistrement d'équipe n'est pas permis.\"}", http.StatusForbidden)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("Handling %s registration request from %s: %s [%s]\n", r.Method, r.RemoteAddr, r.URL.Path, r.UserAgent())
|
||||||
|
|
||||||
// Check request type and size
|
// Check request type and size
|
||||||
if r.Method != "POST" {
|
if r.Method != "POST" {
|
||||||
http.Error(w, "{\"errmsg\":\"Requête invalide.\"}", http.StatusBadRequest)
|
http.Error(w, "{\"errmsg\":\"Requête invalide.\"}", http.StatusBadRequest)
|
||||||
|
@ -7,6 +7,8 @@ import (
|
|||||||
"text/template"
|
"text/template"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var enableResolutionRoute bool = false
|
||||||
|
|
||||||
type ResolutionHandler struct {}
|
type ResolutionHandler struct {}
|
||||||
|
|
||||||
const resolutiontpl = `<!DOCTYPE html>
|
const resolutiontpl = `<!DOCTYPE html>
|
||||||
@ -22,6 +24,12 @@ const resolutiontpl = `<!DOCTYPE html>
|
|||||||
`
|
`
|
||||||
|
|
||||||
func (s ResolutionHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (s ResolutionHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if !enableResolutionRoute {
|
||||||
|
log.Printf("UNHANDELED %s request from %s: /resolution%s [%s]\n", r.Method, r.RemoteAddr, r.URL.Path, r.UserAgent())
|
||||||
|
http.NotFound(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
log.Printf("Handling %s request from %s: /resolution%s [%s]\n", r.Method, r.RemoteAddr, r.URL.Path, r.UserAgent())
|
log.Printf("Handling %s request from %s: /resolution%s [%s]\n", r.Method, r.RemoteAddr, r.URL.Path, r.UserAgent())
|
||||||
|
|
||||||
w.Header().Set("Content-Type", "text/html")
|
w.Header().Set("Content-Type", "text/html")
|
||||||
|
@ -9,12 +9,12 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type SubmissionHandler struct {
|
var challengeEnd time.Time = time.Unix(0, 0)
|
||||||
ChallengeEnd time.Time
|
|
||||||
}
|
type SubmissionHandler struct {}
|
||||||
|
|
||||||
func (s SubmissionHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (s SubmissionHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
log.Printf("Handling %s request from %s: %s [%s]\n", r.Method, r.RemoteAddr, r.URL.Path, r.UserAgent())
|
log.Printf("Handling %s submission request from %s: %s [%s]\n", r.Method, r.RemoteAddr, r.URL.Path, r.UserAgent())
|
||||||
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
|
||||||
@ -36,7 +36,7 @@ func (s SubmissionHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
team := sURL[0]
|
team := sURL[0]
|
||||||
|
|
||||||
if time.Now().Sub(s.ChallengeEnd) > 0 {
|
if time.Now().Sub(challengeEnd) > 0 {
|
||||||
http.Error(w, "{\"errmsg\":\"Vous ne pouvez plus soumettre, le challenge est terminé.\"}", http.StatusForbidden)
|
http.Error(w, "{\"errmsg\":\"Vous ne pouvez plus soumettre, le challenge est terminé.\"}", http.StatusForbidden)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -8,10 +8,9 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type TimeHandler struct {
|
var challengeStart time.Time
|
||||||
StartTime time.Time
|
|
||||||
Duration time.Duration
|
type TimeHandler struct {}
|
||||||
}
|
|
||||||
|
|
||||||
type timeObject struct {
|
type timeObject struct {
|
||||||
Started int64 `json:"st"`
|
Started int64 `json:"st"`
|
||||||
@ -24,7 +23,7 @@ func (t TimeHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
|
||||||
if j, err := json.Marshal(timeObject{t.StartTime.Unix(), time.Now().Unix(), int(t.Duration.Seconds())}); err != nil {
|
if j, err := json.Marshal(timeObject{challengeStart.Unix(), time.Now().Unix(), int(challengeEnd.Sub(challengeStart).Seconds())}); err != nil {
|
||||||
http.Error(w, fmt.Sprintf("{\"errmsg\":\"%q\"}", err), http.StatusInternalServerError)
|
http.Error(w, fmt.Sprintf("{\"errmsg\":\"%q\"}", err), http.StatusInternalServerError)
|
||||||
} else {
|
} else {
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
|
@ -2,14 +2,18 @@ package fic
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var FirstBlood = 0.12
|
||||||
|
var SubmissionCostBase = 0.5
|
||||||
|
|
||||||
// Points
|
// Points
|
||||||
|
|
||||||
func (t Team) GetPoints() (float64, error) {
|
func (t Team) GetPoints() (float64, error) {
|
||||||
var nb *float64
|
var nb *float64
|
||||||
err := DBQueryRow("SELECT SUM(A.points * A.coeff) AS score FROM (SELECT S.id_team, E.gain AS points, coeff FROM (SELECT id_team, id_exercice, MIN(time) AS time, 0.12 AS coeff FROM exercice_solved GROUP BY id_exercice UNION SELECT id_team, id_exercice, time, 1 AS coeff FROM exercice_solved) S INNER JOIN exercices E ON S.id_exercice = E.id_exercice UNION ALL SELECT D.id_team, H.cost AS points, -1.0 AS coeff FROM team_hints D INNER JOIN exercice_hints H ON H.id_hint = D.id_hint UNION ALL SELECT id_team, ((FLOOR(COUNT(*)/10 - 1) * (FLOOR(COUNT(*)/10)))/0.2 + (FLOOR(COUNT(*)/10) * (COUNT(*)%10)))/2 AS points, -1 AS coeff FROM exercice_tries GROUP BY id_exercice HAVING points != 0) A WHERE A.id_team = ? GROUP BY A.id_team", t.Id).Scan(&nb)
|
err := DBQueryRow("SELECT SUM(A.points * A.coeff) AS score FROM (SELECT S.id_team, E.gain AS points, coeff FROM (SELECT id_team, id_exercice, MIN(time) AS time, " + fmt.Sprintf("%f", FirstBlood) + " AS coeff FROM exercice_solved GROUP BY id_exercice UNION SELECT id_team, id_exercice, time, 1 AS coeff FROM exercice_solved) S INNER JOIN exercices E ON S.id_exercice = E.id_exercice UNION ALL SELECT D.id_team, H.cost AS points, -1.0 AS coeff FROM team_hints D INNER JOIN exercice_hints H ON H.id_hint = D.id_hint UNION ALL SELECT id_team, ((FLOOR(COUNT(*)/10 - 1) * (FLOOR(COUNT(*)/10)))/0.2 + (FLOOR(COUNT(*)/10) * (COUNT(*)%10)))/" + fmt.Sprintf("%f", 1/SubmissionCostBase) + " AS points, -1 AS coeff FROM exercice_tries GROUP BY id_exercice HAVING points != 0) A WHERE A.id_team = ? GROUP BY A.id_team", t.Id).Scan(&nb)
|
||||||
if nb != nil {
|
if nb != nil {
|
||||||
return *nb, err
|
return *nb, err
|
||||||
} else {
|
} else {
|
||||||
@ -18,7 +22,7 @@ func (t Team) GetPoints() (float64, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func GetRank() (map[int64]int, error) {
|
func GetRank() (map[int64]int, error) {
|
||||||
if rows, err := DBQuery("SELECT A.id_team, SUM(A.points * A.coeff) AS score, MAX(A.time) AS time FROM (SELECT S.id_team, S.time, E.gain AS points, coeff FROM (SELECT id_team, id_exercice, MIN(time) AS time, 0.12 AS coeff FROM exercice_solved GROUP BY id_exercice UNION SELECT id_team, id_exercice, time, 1 AS coeff FROM exercice_solved) S INNER JOIN exercices E ON S.id_exercice = E.id_exercice UNION ALL SELECT D.id_team, D.time, H.cost AS points, -1.0 AS coeff FROM team_hints D INNER JOIN exercice_hints H ON H.id_hint = D.id_hint UNION ALL SELECT id_team, MAX(time) AS time, ((FLOOR(COUNT(*)/10 - 1) * (FLOOR(COUNT(*)/10)))/0.2 + (FLOOR(COUNT(*)/10) * (COUNT(*)%10)))/2 AS points, -1 AS coeff FROM exercice_tries GROUP BY id_exercice HAVING points != 0) A GROUP BY A.id_team ORDER BY score DESC, time ASC"); err != nil {
|
if rows, err := DBQuery("SELECT A.id_team, SUM(A.points * A.coeff) AS score, MAX(A.time) AS time FROM (SELECT S.id_team, S.time, E.gain AS points, coeff FROM (SELECT id_team, id_exercice, MIN(time) AS time, " + fmt.Sprintf("%f", FirstBlood) + " AS coeff FROM exercice_solved GROUP BY id_exercice UNION SELECT id_team, id_exercice, time, 1 AS coeff FROM exercice_solved) S INNER JOIN exercices E ON S.id_exercice = E.id_exercice UNION ALL SELECT D.id_team, D.time, H.cost AS points, -1.0 AS coeff FROM team_hints D INNER JOIN exercice_hints H ON H.id_hint = D.id_hint UNION ALL SELECT id_team, MAX(time) AS time, ((FLOOR(COUNT(*)/10 - 1) * (FLOOR(COUNT(*)/10)))/0.2 + (FLOOR(COUNT(*)/10) * (COUNT(*)%10)))/" + fmt.Sprintf("%f", 1/SubmissionCostBase) + " AS points, -1 AS coeff FROM exercice_tries GROUP BY id_exercice HAVING points != 0) A GROUP BY A.id_team ORDER BY score DESC, time ASC"); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
} else {
|
} else {
|
||||||
defer rows.Close()
|
defer rows.Close()
|
||||||
|
97
settings/settings.go
Normal file
97
settings/settings.go
Normal file
@ -0,0 +1,97 @@
|
|||||||
|
package settings
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"path"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"gopkg.in/fsnotify.v1"
|
||||||
|
)
|
||||||
|
|
||||||
|
const SettingsFile = "settings.json"
|
||||||
|
|
||||||
|
type FICSettings struct {
|
||||||
|
Start time.Time `json:"start"`
|
||||||
|
End time.Time `json:"end"`
|
||||||
|
|
||||||
|
FirstBlood float64 `json:"firstBlood"`
|
||||||
|
SubmissionCostBase float64 `json:"submissionCostBase"`
|
||||||
|
|
||||||
|
AllowRegistration bool `json:"allowRegistration"`
|
||||||
|
DenyNameChange bool `json:"denyNameChange"`
|
||||||
|
EnableResolutionRoute bool `json:"enableResolutionRoute"`
|
||||||
|
PartialValidation bool `json:"partialValidation"`
|
||||||
|
EnableExerciceDepend bool `json:"enableExerciceDepend"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func ReadSettings(path string) (FICSettings, error) {
|
||||||
|
var s FICSettings
|
||||||
|
if fd, err := os.Open(path); err != nil {
|
||||||
|
return s, err
|
||||||
|
} else {
|
||||||
|
defer fd.Close()
|
||||||
|
jdec := json.NewDecoder(fd)
|
||||||
|
|
||||||
|
if err := jdec.Decode(&s); err != nil {
|
||||||
|
return s, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return s, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func SaveSettings(path string, s FICSettings) error {
|
||||||
|
if fd, err := os.Create(path); err != nil {
|
||||||
|
return err
|
||||||
|
} else {
|
||||||
|
defer fd.Close()
|
||||||
|
jenc := json.NewEncoder(fd)
|
||||||
|
|
||||||
|
if err := jenc.Encode(s); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func LoadAndWatchSettings(settingsPath string, reload func (FICSettings)) {
|
||||||
|
// First load of configuration if it exists
|
||||||
|
if _, err := os.Stat(settingsPath); !os.IsNotExist(err) {
|
||||||
|
if config, err := ReadSettings(settingsPath); err != nil {
|
||||||
|
log.Println("ERROR: Unable to read challenge settings:", err)
|
||||||
|
} else {
|
||||||
|
reload(config)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Watch the configuration file
|
||||||
|
if watcher, err := fsnotify.NewWatcher(); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
} else {
|
||||||
|
if err := watcher.Add(path.Dir(settingsPath)); err != nil {
|
||||||
|
log.Fatal("Unable to watch: ", path.Dir(settingsPath), ": ", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
defer watcher.Close()
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case ev := <-watcher.Events:
|
||||||
|
if path.Base(ev.Name) == SettingsFile && ev.Op & fsnotify.Write == fsnotify.Write {
|
||||||
|
log.Println("Settings file changes, reloading it!")
|
||||||
|
if config, err := ReadSettings(settingsPath); err != nil {
|
||||||
|
log.Println("ERROR: Unable to read challenge settings:", err)
|
||||||
|
} else {
|
||||||
|
reload(config)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case err := <-watcher.Errors:
|
||||||
|
log.Println("watcher error:", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user