@@ 27,8 27,11 @@ func (el *Election) CalculateResult() {
}
func (el *Election) PrintResults() {
- for el.Result == nil {
- time.Sleep(time.Millisecond * 200)
+ if el.Result == nil {
+ fmt.Println("waiting for results...")
+ for el.Result == nil {
+ time.Sleep(time.Millisecond * 200)
+ }
}
result := *el.Result
candidates := el.Candidates
@@ 144,26 144,28 @@ func RoomTUI(client *mautrix.Client, roomID id.RoomID, elections *election.Elect
elections.RUnlock()
if i > 0 {
- // user wants to join election
+ // user selected election
el = L[i - 1]
- // don't need to lock because this goroutine controls LocalVoter
- if el.LocalVoter == nil {
- // ask user if s/he wants to join election (or
- // display results if it's already started)
- if !electionConfirmation(el) {
- // user needs to select a different
- // election
- el, err = RoomTUI(client, roomID, elections)
- } else if el.StartID == nil {
- // user selected yes on joining the
- // election
- err = el.JoinElection(client, elections.EventStore)
- }
+ el.RLock()
+ userJoined := el.LocalVoter != nil
+ el.RUnlock()
+ if userJoined {
+ return // good to go
+ }
+ // ask user if s/he wants to join election (or display
+ // results if it's already started)
+ if !electionConfirmation(el) {
+ // user needs to select a different election
+ el, err = RoomTUI(client, roomID, elections)
+ } else if el.StartID == nil {
+ // user wants to join the election
+ err = el.JoinElection(client, elections.EventStore)
}
+ // otherwise, user wants to wait for results
return
}
- // user wants to create election (i == 0)
+ // user opted to create election (i == 0)
title, candidates := CreateElectionTUI()
if candidates == nil {
return
@@ 180,23 182,24 @@ func RoomTUI(client *mautrix.Client, roomID id.RoomID, elections *election.Elect
}
})
app.SetRoot(list, true)
- err = app.Run()
+ err2 := app.Run()
+ if err == nil {
+ err = err2
+ }
return
}
func electionConfirmation(el *election.Election) (shouldJoin bool) {
app := newTallyardApplication()
-
- var buttons []string
var text string
el.RLock()
// TODO: handle when election starts while in modal
- if el.StartID != nil {
- buttons = []string{"Yes", "No"}
- text = "Election has already started; display results?"
+ if el.Result != nil {
+ text = "Election has already concluded; show results?"
+ } else if el.StartID != nil {
+ text = "Election has already started; wait for results?"
} else {
- buttons = []string{"Yes", "No"}
text = "Join election?"
}
el.RUnlock()
@@ 208,7 211,7 @@ func electionConfirmation(el *election.Election) (shouldJoin bool) {
modal := tview.NewModal().
SetText(text).
- AddButtons(buttons).
+ AddButtons([]string{"Yes", "No"}).
SetDoneFunc(func(_ int, buttonLabel string) {
app.Stop()
if buttonLabel == "Yes" {