Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
I
IA04binôme2A
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Gabrielle Van De Vijver
IA04binôme2A
Commits
edb0354d
Commit
edb0354d
authored
1 year ago
by
Balthazar Wilson
Browse files
Options
Downloads
Patches
Plain Diff
add: vote function
parent
681ffd77
No related branches found
Branches containing commit
No related tags found
1 merge request
!8
implémentation des votants par Balthazar
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
agt/ballotagent/vote.go
+77
-2
77 additions, 2 deletions
agt/ballotagent/vote.go
with
77 additions
and
2 deletions
agt/ballotagent/vote.go
+
77
−
2
View file @
edb0354d
...
...
@@ -3,8 +3,28 @@ 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
,
E
string
|
rad
.
Ballot
](
s
S
,
e
E
)
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
(
rsa
*
BallotServerAgent
)
receiveVote
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
// mise à jour du nombre de requêtes
rsa
.
Lock
()
...
...
@@ -17,7 +37,7 @@ func (rsa *BallotServerAgent) receiveVote(w http.ResponseWriter, r *http.Request
}
// décodage de la requête
req
,
err
:=
rsa
.
decodeRequest
(
r
)
req
,
err
:=
decodeRequest
[
rad
.
Vote
]
(
r
)
if
err
!=
nil
{
w
.
WriteHeader
(
http
.
StatusBadRequest
)
fmt
.
Fprint
(
w
,
err
.
Error
())
...
...
@@ -25,5 +45,60 @@ func (rsa *BallotServerAgent) receiveVote(w http.ResponseWriter, r *http.Request
}
// recupération des infos
scrutin
:=
req
.
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
,
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
ballot
.
profile
=
append
(
ballot
.
profile
,
intToAlt
(
req
.
Prefs
))
if
len
(
req
.
Options
)
!=
0
{
ballot
.
options
=
append
(
ballot
.
options
,
req
.
Options
[
0
])
}
// 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
]
}
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment