Backend: extract file generation and team renaming in dedicated files
This commit is contained in:
parent
aefd078ebf
commit
1c62f61bf0
8 changed files with 261 additions and 140 deletions
|
@ -4,20 +4,17 @@ import (
|
|||
"encoding/base64"
|
||||
"encoding/binary"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"math/rand"
|
||||
"os"
|
||||
"path"
|
||||
"regexp"
|
||||
"strconv"
|
||||
|
||||
"srs.epita.fr/fic-server/libfic"
|
||||
)
|
||||
|
||||
func treatSubmission(pathname string, team_id string, exercice_id string) {
|
||||
func treatSubmission(pathname string, team fic.Team, exercice_id string) {
|
||||
bid := make([]byte, 5)
|
||||
binary.LittleEndian.PutUint32(bid, rand.Uint32())
|
||||
id := "[" + base64.StdEncoding.EncodeToString(bid) + "]"
|
||||
|
@ -25,28 +22,10 @@ func treatSubmission(pathname string, team_id string, exercice_id string) {
|
|||
|
||||
var keys map[string]string
|
||||
|
||||
if tid, err := strconv.Atoi(team_id); err != nil {
|
||||
log.Println(id, "[ERR]", err)
|
||||
} else if team, err := fic.GetTeam(tid); err != nil {
|
||||
log.Println(id, "[ERR]", err)
|
||||
} else if cnt_raw, err := ioutil.ReadFile(pathname); err != nil {
|
||||
if cnt_raw, err := ioutil.ReadFile(pathname); err != nil {
|
||||
log.Println(id, "[ERR]", err)
|
||||
} else if err := json.Unmarshal(cnt_raw, &keys); err != nil {
|
||||
log.Println(id, "[ERR]", err)
|
||||
} else if exercice_id == "name" {
|
||||
if match, err := regexp.MatchString("^[A-Za-z0-9 àéèêëîïôùûü_-]{1,32}$", keys["newName"]); err == nil && match {
|
||||
team.Name = keys["newName"]
|
||||
if _, err := team.Update(); err != nil {
|
||||
log.Println(id, "[WRN] Unable to change team name:", err)
|
||||
}
|
||||
genTeamMyFile(team)
|
||||
if _, err := fic.NewEvent(fmt.Sprintf("Souhaitons bonne chance à l'équipe <strong>%s</strong> qui vient de nous rejoindre !", team.Name), "alert-info"); err != nil {
|
||||
log.Println(id, "[WRN] Unable to create event:", err)
|
||||
}
|
||||
}
|
||||
if err := os.Remove(pathname); err != nil {
|
||||
log.Println(id, "[ERR]", err)
|
||||
}
|
||||
} else if eid, err := strconv.Atoi(exercice_id); err != nil {
|
||||
log.Println(id, "[ERR]", err)
|
||||
} else if exercice, err := fic.GetExercice(int64(eid)); err != nil {
|
||||
|
@ -54,10 +33,11 @@ func treatSubmission(pathname string, team_id string, exercice_id string) {
|
|||
} else if theme, err := exercice.GetTheme(); err != nil {
|
||||
log.Println(id, "[ERR]", err)
|
||||
} else if s, tm, _ := team.HasSolved(exercice); s {
|
||||
log.Printf("$s [WRN] Team %d ALREADY solved exercice %d ($s : $s)\n", id, team.Id, exercice.Id, theme.Name, exercice.Title)
|
||||
log.Printf("%s [WRN] Team %d ALREADY solved exercice %d (%s : %s)\n", id, team.Id, exercice.Id, theme.Name, exercice.Title)
|
||||
} else {
|
||||
if solved, err := exercice.CheckResponse(keys, team); err != nil {
|
||||
log.Println(id, "[ERR]", err)
|
||||
genTeamMyFile(team)
|
||||
} else if solved {
|
||||
exercice.Solved(team)
|
||||
log.Printf("%s Team %d SOLVED exercice %d (%s : %s)\n", id, team.Id, exercice.Id, theme.Name, exercice.Title)
|
||||
|
@ -71,6 +51,7 @@ func treatSubmission(pathname string, team_id string, exercice_id string) {
|
|||
} else if _, err := fic.NewEvent(fmt.Sprintf("L'équipe %s a résolu le <strong>%d<sup>e</sup></strong> challenge %s !", team.Name, lvl, theme.Name), "alert-success"); err != nil {
|
||||
log.Println(id, "[WRN] Unable to create event:", err)
|
||||
}
|
||||
genTeamAll(team)
|
||||
} else {
|
||||
log.Printf("%s Team %d submit an invalid solution for exercice %d (%s : %s)\n", id, team.Id, exercice.Id, theme.Name, exercice.Title)
|
||||
if err := os.Remove(pathname); err != nil {
|
||||
|
@ -85,42 +66,7 @@ func treatSubmission(pathname string, team_id string, exercice_id string) {
|
|||
log.Println(id, "[WRN] Unable to create event:", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
genTeamMyFile(team)
|
||||
}
|
||||
}
|
||||
|
||||
func genTeamMyFile(team fic.Team) error {
|
||||
dirPath := path.Join(TeamsDir, fmt.Sprintf("%d", team.Id))
|
||||
|
||||
started := true
|
||||
if _, err := os.Stat(path.Join(TeamsDir, "started")); os.IsNotExist(err) {
|
||||
started = false
|
||||
}
|
||||
|
||||
if s, err := os.Stat(dirPath); os.IsNotExist(err) {
|
||||
os.MkdirAll(dirPath, 0777)
|
||||
} else if !s.IsDir() {
|
||||
return errors.New("dirPath is not a directory")
|
||||
}
|
||||
|
||||
if my, err := fic.MyJSONTeam(&team, true); err != nil {
|
||||
return err
|
||||
} else if j, err := json.Marshal(my); err != nil {
|
||||
return err
|
||||
} else if err := ioutil.WriteFile(path.Join(dirPath, "my.json"), j, 0666); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !started {
|
||||
if my, err := fic.MyJSONTeam(&team, false); err != nil {
|
||||
return err
|
||||
} else if j, err := json.Marshal(my); err != nil {
|
||||
return err
|
||||
} else if err := ioutil.WriteFile(path.Join(dirPath, "wait.json"), j, 0666); err != nil {
|
||||
return err
|
||||
genTeamMyFile(team)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
Reference in a new issue