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

fix: now handles votes with options (like approval)

parent a4c270c6
No related branches found
No related tags found
1 merge request!7Partie serveur
......@@ -12,6 +12,7 @@ import (
type BallotInfo struct {
profile cs.Profile
options []int
votersId []string
nbAlts int
isOpen bool
......@@ -58,6 +59,7 @@ func (rsa *BallotServerAgent) createBallot(w http.ResponseWriter, r *http.Reques
resp.BallotID = fmt.Sprintf("scrutin%d", len(rsa.ballots)+1)
rsa.ballots[resp] = BallotInfo{
profile: make(cs.Profile, 0),
options: make([]int, 0),
votersId: req.VotersID,
nbAlts: req.NbAlts,
isOpen: true,
......@@ -123,3 +125,41 @@ func (rsa *BallotServerAgent) handleBallot(
targetBallot.results = rad.ResultResponse{Winner: int(winner), Ranking: intRanking}
fmt.Println(targetBallot)
}
func (rsa *BallotServerAgent) handleBallotWithOptions(
ballot rad.Ballot,
swf func(cs.Profile, []int) (cs.Count, error),
scf func(cs.Profile, []int) ([]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
options := targetBallot.options
// 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.SWFFactoryWithOptions(swf, tb)
ballotSCF := cs.SCFFactoryWithOptions(scf, tb)
ranking, _ := ballotSWF(profile, options)
winner, err := ballotSCF(profile, options)
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