~edwargix/tallyard

a0cfdb69853b8245a2deae1a12721b123f39da89 — David Florness 5 years ago 6c57039
A few fixes
3 files changed, 22 insertions(+), 18 deletions(-)

M election/msg.go
M election/voter.go
M ui/tui.go
M election/msg.go => election/msg.go +5 -3
@@ 219,11 219,12 @@ func OnSumMessage(client *mautrix.Client, evt *event.Event, elections *Elections
		log.Warnf("ignoring %s's sum since we couldn't cast message content to SumMessageContent", evt.Sender)
		return
	}
	sum, ok := new(big.Int).SetString(content.Sum, 64)
	if !ok {
		log.Warnf("ignoring %s's sum since we couldn't base64 decode the sum", evt.Sender)
	bytes, err := base64.StdEncoding.DecodeString(content.Sum)
	if err != nil {
		log.Warnf("ignoring %s's sum since we couldn't decode their sum", evt.Sender)
		return
	}
	sum := new(big.Int).SetBytes(bytes)
	el := getElection(client, evt.RoomID, content.CreateEventId, elections)
	if el == nil {
		log.Warnf("ignoring %s's sum since the election does not exist", evt.Sender)


@@ 231,6 232,7 @@ func OnSumMessage(client *mautrix.Client, evt *event.Event, elections *Elections
	}
	el.Lock()
	defer el.Unlock()
	// TODO: ensure voter exists
	voter := el.Voters[evt.Sender]
	voter.Sum = sum
}

M election/voter.go => election/voter.go +2 -1
@@ 121,6 121,7 @@ func (locelVoter *LocalVoter) SendSum(client *mautrix.Client, el *Election) erro
				time.Sleep(time.Millisecond * 100)
			}
			sum.Add(sum, voter.Output)
			wg.Done()
		}(voter)
	}
	el.RUnlock()


@@ 141,7 142,7 @@ func GetSums(client *mautrix.Client, el *Election) {
			for voter.Sum == nil {
				time.Sleep(time.Millisecond * 100)
			}

			wg.Done()
		}(voter)
	}
	el.RUnlock()

M ui/tui.go => ui/tui.go +15 -14
@@ 138,6 138,7 @@ func RoomTUI(client *mautrix.Client, roomID id.RoomID, elections *election.Elect
		if err != nil {
			panic(err)
		}
		ballot = ElectionTUI(client, el, localVoter)
	})
	if err := app.SetRoot(list, true).SetFocus(list).Run(); err != nil {
		panic(err)


@@ 224,27 225,27 @@ func CreateElectionTUI() (title string, candidates []election.Candidate) {
func ElectionTUI(client *mautrix.Client, el *election.Election, localVoter *election.LocalVoter) (ballot []byte) {
	votersTextView := tview.NewTextView()
	frame := tview.NewFrame(votersTextView)
	app := tview.NewApplication()
	el.RLock()
	if el.Creator == localVoter.UserID {
		frame.AddText("Press enter to start the election", false, tview.AlignCenter, tcell.ColorWhite)
		app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
			if event.Key() == tcell.KeyEnter {
				app.QueueUpdateDraw(func() {
					frame.Clear()
					frame.AddText("Starting election...", false, tview.AlignCenter, tcell.ColorWhite)
				})
				err := election.StartElection(client, el)
				if err != nil {
					panic(err)
				}
			}
			return event
		})
	} else {
		frame.AddText("Waiting for election to start...", false, tview.AlignCenter, tcell.ColorWhite)
	}
	el.RUnlock()
	app := tview.NewApplication()
	app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
		if event.Key() == tcell.KeyEnter {
			app.QueueUpdateDraw(func() {
				frame.Clear()
				frame.AddText("Starting election...", false, tview.AlignCenter, tcell.ColorWhite)
			})
			err := election.StartElection(client, el)
			if err != nil {
				panic(err)
			}
		}
		return event
	})
	update := func() {
		el.RLock()
		voters := make([]string, 0, len(el.Voters))