Skip to content
Snippets Groups Projects
Commit c3d59946 authored by Balthazar Wilson's avatar Balthazar Wilson
Browse files

add: new ballot creation

parent 9aaf8ff9
No related branches found
No related tags found
1 merge request!7Partie serveur
...@@ -4,15 +4,18 @@ import ( ...@@ -4,15 +4,18 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"net/http" "net/http"
"time"
rad "gitlab.utc.fr/gvandevi/ia04binome2a" rad "gitlab.utc.fr/gvandevi/ia04binome2a"
cs "gitlab.utc.fr/gvandevi/ia04binome2a/comsoc" cs "gitlab.utc.fr/gvandevi/ia04binome2a/comsoc"
) )
type BallotInfo struct { type BallotInfo struct {
profile cs.Profile profile cs.Profile
isOpen bool votersId []string
results rad.ResultResponse nbAlts int
isOpen bool
results rad.ResultResponse
} }
func (rsa *BallotServerAgent) createBallot(w http.ResponseWriter, r *http.Request) { func (rsa *BallotServerAgent) createBallot(w http.ResponseWriter, r *http.Request) {
...@@ -33,24 +36,45 @@ func (rsa *BallotServerAgent) createBallot(w http.ResponseWriter, r *http.Reques ...@@ -33,24 +36,45 @@ func (rsa *BallotServerAgent) createBallot(w http.ResponseWriter, r *http.Reques
fmt.Fprint(w, err.Error()) fmt.Fprint(w, err.Error())
return return
} }
// Check for valid deadline formatting
deadline, errTime := time.Parse(time.RFC3339, req.Deadline)
if errTime != nil {
w.WriteHeader(http.StatusBadRequest)
msg := fmt.Sprintf("'%s' n'utilise pas le bon format, merci d'utiliser RFC3339 ", req.Deadline)
w.Write([]byte(msg))
return
}
// Check if the deadline is in the future
if deadline.Before(time.Now()) {
w.WriteHeader(http.StatusBadRequest)
msg := fmt.Sprintf("'%s' est déjà passé ", req.Deadline)
w.Write([]byte(msg))
return
}
// traitement de la requête // traitement de la requête
var resp rad.Ballot var resp rad.Ballot
resp.BallotID = fmt.Sprintf("scrutin%d", len(rsa.ballots)+1) resp.BallotID = fmt.Sprintf("scrutin%d", len(rsa.ballots)+1)
rsa.ballots[resp] = BallotInfo{ rsa.ballots[resp] = BallotInfo{
profile: make(cs.Profile, 0), profile: make(cs.Profile, 0),
isOpen: true, votersId: req.VotersID,
results: rad.ResultResponse{}} nbAlts: req.NbAlts,
isOpen: true,
results: rad.ResultResponse{}}
tb := make([]cs.Alternative, 0)
for _, alt := range req.TieBreak {
tb = append(tb, cs.Alternative(alt))
}
switch req.Rule { switch req.Rule {
case "majority": case "majority":
go rsa.handleBallot(resp, cs.MajoritySWF, cs.MajoritySCF, tb, deadline)
case "borda": case "borda":
go rsa.handleBallot(resp, cs.BordaSWF, cs.BordaSCF, tb, deadline)
case "approval": //case "approval":
// go rsa.handleBallot(resp, cs.ApprovalSWF, cs.ApprovalSCF, tb, deadline)
case "stv":
default: default:
w.WriteHeader(http.StatusNotImplemented) w.WriteHeader(http.StatusNotImplemented)
msg := fmt.Sprintf("Unkonwn rule '%s'", req.Rule) msg := fmt.Sprintf("Unkonwn rule '%s'", req.Rule)
...@@ -62,3 +86,40 @@ func (rsa *BallotServerAgent) createBallot(w http.ResponseWriter, r *http.Reques ...@@ -62,3 +86,40 @@ func (rsa *BallotServerAgent) createBallot(w http.ResponseWriter, r *http.Reques
serial, _ := json.Marshal(resp) serial, _ := json.Marshal(resp)
w.Write(serial) w.Write(serial)
} }
func (rsa *BallotServerAgent) handleBallot(
ballot rad.Ballot,
swf func(cs.Profile) (cs.Count, error),
scf func(cs.Profile) ([]cs.Alternative, error),
orderedTBAlts []cs.Alternative,
deadline time.Time,
) {
targetBallot := rsa.ballots[ballot]
fmt.Println(targetBallot)
time.Sleep(time.Until(deadline))
targetBallot.isOpen = false
profile := targetBallot.profile
// If profile is empty, set winner as 0 and ranking as empty list
if len(profile) == 0 {
targetBallot.results = rad.ResultResponse{Winner: 0, Ranking: make([]int, 0)}
fmt.Println(ballot.BallotID, "n'a pas reçu de votes.")
return
}
tb := cs.TieBreakFactory(orderedTBAlts)
ballotSWF := cs.SWFFactory(swf, tb)
ballotSCF := cs.SCFFactory(scf, tb)
ranking, _ := ballotSWF(profile)
winner, err := ballotSCF(profile)
if err != nil {
fmt.Println(err)
return
}
intRanking := make([]int, 0)
for _, alt := range ranking {
intRanking = append(intRanking, int(alt))
}
targetBallot.results = rad.ResultResponse{Winner: int(winner), Ranking: intRanking}
fmt.Println(targetBallot)
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment