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

finish: vote feature

parent 7cec03a3
No related branches found
No related tags found
1 merge request!8implémentation des votants par Balthazar
......@@ -8,7 +8,7 @@ import (
cs "gitlab.utc.fr/gvandevi/ia04binome2a/comsoc"
)
func contains[S []string | []rad.Ballot, E string | rad.Ballot](s S, e E) bool {
func contains[S string | rad.Ballot](s []S, e S) bool {
for _, a := range s {
if a == e {
return true
......@@ -25,6 +25,18 @@ func intToAlt(s []int) []cs.Alternative {
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()
......@@ -55,7 +67,7 @@ func (rsa *BallotServerAgent) receiveVote(w http.ResponseWriter, r *http.Request
ballots[i] = k
i++
}
if !contains[[]rad.Ballot, rad.Ballot](ballots, voteBallot) {
if !contains[rad.Ballot](ballots, voteBallot) {
w.WriteHeader(http.StatusBadRequest)
msg := fmt.Sprintf("The ballot '%s' does not exist", scrutin)
w.Write([]byte(msg))
......@@ -90,15 +102,14 @@ func (rsa *BallotServerAgent) receiveVote(w http.ResponseWriter, r *http.Request
}
// Register the vote
ballot.profile = append(ballot.profile, intToAlt(req.Prefs))
if len(req.Options) != 0 {
ballot.options = append(ballot.options, req.Options)
}
// On elève l'agent de la liste des agents votants
for i, agt := range ballot.votersId {
if agt == req.AgentID {
ballot.votersId[i] = ballot.votersId[len(ballot.votersId)-1]
ballot.votersId = ballot.votersId[:len(ballot.votersId)-1]
}
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{},
}
fmt.Println(rsa.ballots[voteBallot])
}
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