diff --git a/agt/ballotagent/vote.go b/agt/ballotagent/vote.go index 0d9a1426366d863fd00fe5d544d4998292fa775d..a45e392ef77975e24b01b4dbd1994f3cacd4e9ce 100644 --- a/agt/ballotagent/vote.go +++ b/agt/ballotagent/vote.go @@ -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]) }