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

Merge branch 'full_baltha' into 'main'

implémentation des votants par Balthazar

See merge request !8
parents 0fb36f1a 883479a5
No related branches found
No related tags found
1 merge request!8implémentation des votants par Balthazar
...@@ -57,8 +57,8 @@ func (rsa *BallotServerAgent) Start() { ...@@ -57,8 +57,8 @@ func (rsa *BallotServerAgent) Start() {
// création du multiplexer // création du multiplexer
mux := http.NewServeMux() mux := http.NewServeMux()
mux.HandleFunc("/new_ballot", rsa.createBallot) mux.HandleFunc("/new_ballot", rsa.createBallot)
mux.HandleFunc("/vote", rsa.doReqcount) mux.HandleFunc("/vote", rsa.receiveVote)
mux.HandleFunc("/result", rsa.doReqcount) mux.HandleFunc("/result", rsa.sendResults)
rsa.ballots = make(map[rad.Ballot]BallotInfo) rsa.ballots = make(map[rad.Ballot]BallotInfo)
......
package ballotagent
import (
"encoding/json"
"fmt"
"net/http"
rad "gitlab.utc.fr/gvandevi/ia04binome2a"
)
func (rsa *BallotServerAgent) sendResults(w http.ResponseWriter, r *http.Request) {
// mise à jour du nombre de requêtes
rsa.Lock()
defer rsa.Unlock()
rsa.reqCount++
// vérification de la méthode de la requête
if !rsa.checkMethod("POST", w, r) {
return
}
// décodage de la requête
req, err := decodeRequest[rad.Ballot](r)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprint(w, err.Error())
return
}
// recupération des infos
scrutin := req.BallotID
voteBallot := rad.Ballot{BallotID: scrutin}
ballots := make([]rad.Ballot, len(rsa.ballots))
// Check if ballot exists
i := 0
for k := range rsa.ballots {
ballots[i] = k
i++
}
if !contains[rad.Ballot](ballots, voteBallot) {
w.WriteHeader(http.StatusNotFound)
msg := fmt.Sprintf("The ballot '%s' does not exist", scrutin)
w.Write([]byte(msg))
return
}
ballot := rsa.ballots[voteBallot]
// Check que la deadline n'est pas déjà passée
if ballot.isOpen {
w.WriteHeader(http.StatusTooEarly)
msg := fmt.Sprintf("The ballot '%s' is not closed yet.", scrutin)
w.Write([]byte(msg))
return
}
w.WriteHeader(http.StatusOK)
serial, _ := json.Marshal(rsa.ballots[voteBallot].results)
w.Write(serial)
}
package ballotagent
import (
"fmt"
"net/http"
rad "gitlab.utc.fr/gvandevi/ia04binome2a"
cs "gitlab.utc.fr/gvandevi/ia04binome2a/comsoc"
)
func contains[S string | rad.Ballot](s []S, e S) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}
func intToAlt(s []int) []cs.Alternative {
res := make([]cs.Alternative, len(s))
for i, v := range s {
res[i] = cs.Alternative(v)
}
return res
}
func removeFromSlice(slice []string, elem string) []string {
res := make([]string, 0)
i := 0
for _, el := range slice {
if el != elem {
res = append(res, el)
i++
}
}
return res
}
func (rsa *BallotServerAgent) receiveVote(w http.ResponseWriter, r *http.Request) {
// mise à jour du nombre de requêtes
rsa.Lock()
defer rsa.Unlock()
rsa.reqCount++
// vérification de la méthode de la requête
if !rsa.checkMethod("POST", w, r) {
return
}
// décodage de la requête
req, err := decodeRequest[rad.Vote](r)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprint(w, err.Error())
return
}
// recupération des infos
scrutin := req.BallotID
voteBallot := rad.Ballot{BallotID: scrutin}
ballots := make([]rad.Ballot, len(rsa.ballots))
// Check if ballot exists
i := 0
for k := range rsa.ballots {
ballots[i] = k
i++
}
if !contains[rad.Ballot](ballots, voteBallot) {
w.WriteHeader(http.StatusBadRequest)
msg := fmt.Sprintf("The ballot '%s' does not exist", scrutin)
w.Write([]byte(msg))
return
}
ballot := rsa.ballots[voteBallot]
// Check que la deadline n'est pas déjà passée
if !ballot.isOpen {
w.WriteHeader(http.StatusServiceUnavailable)
msg := fmt.Sprintf("The deadline has passed, the ballot '%s' is therefore closed.", scrutin)
w.Write([]byte(msg))
return
}
// Check que l'agent fait bien partie des votants
if !contains(ballot.votersId, req.AgentID) {
w.WriteHeader(http.StatusForbidden)
msg := fmt.Sprintf("The user '%s' has either already voted or cannot vote", req.AgentID)
w.Write([]byte(msg))
return
}
// Check if vote is valid beforehand (don't want to register a vote that could break the results' calculation)
tempProf := make(cs.Profile, 1)
tempProf[0] = intToAlt(req.Prefs)
_, errPrefs := cs.MajoritySWF(tempProf)
if errPrefs != nil || len(req.Prefs) != ballot.nbAlts {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprint(w, errPrefs.Error())
return
}
// Register the vote
rsa.ballots[voteBallot] = BallotInfo{
profile: append(ballot.profile, intToAlt(req.Prefs)), // add preferences to profile
options: append(ballot.options, req.Options), // add options to all options
votersId: removeFromSlice(rsa.ballots[voteBallot].votersId, req.AgentID), // remove voter from the list of voters
nbAlts: rsa.ballots[voteBallot].nbAlts,
isOpen: true,
results: rad.ResultResponse{},
}
}
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