Ejemplo n.º 1
0
// NewContext generates a new context with randomly populated content
func NewContext() Context {
	ctx := Context{}
	smokingChoices := []randutil.Choice{
		{2, "Smoker"},
		{3, "Non-smoker"},
		{1, "Ex-smoker"}}
	sc, _ := randutil.WeightedChoice(smokingChoices)
	ctx.Smoker = sc.Item.(string)

	alcoholChoices := []randutil.Choice{
		{2, "Occasional"},
		{1, "Heavy"},
		{1, "None"}}
	ac, _ := randutil.WeightedChoice(alcoholChoices)
	ctx.Alcohol = ac.Item.(string)

	cholesterolChoices := []randutil.Choice{
		{3, "Optimal"},
		{1, "Near Optimal"},
		{2, "Borderline"},
		{1, "High"},
		{2, "Very High"}}
	cc, _ := randutil.WeightedChoice(cholesterolChoices)
	ctx.Cholesterol = cc.Item.(string)

	hc, _ := randutil.ChoiceString([]string{"Normal", "Pre-hypertension", "Hypertension"})
	ctx.Hypertention = hc

	dc, _ := randutil.ChoiceString([]string{"Normal", "Pre-diabetes", "Diabetes"})
	ctx.Diabetes = dc
	return ctx
}
Ejemplo n.º 2
0
func main() {
	account, err := venmoAccount()
	if err != nil {
		log.Println("Error refreshing account:", err)
	} else {
		log.Println("Account refreshed")
	}

	log.Println("balance is", account.Balance)
	payments, err := paymentsSinceLastRun(&account)
	if err != nil {
		log.Println("Error fetching payments:", err)
		return
	}

	balance := 0.0

	choices := make([]randutil.Choice, 0)

	for _, payment := range payments {
		actor := payment.Actor.DisplayName
		target := payment.Target.User.DisplayName
		note := payment.Note
		amount := payment.Amount
		log.Println(actor, "paid", target, amount, "dollars for", note)

		if target == "Smick Share" {
			balance += amount
			choice := randutil.Choice{
				Weight: int(100 * amount),
				Item:   payment.Actor.Id,
			}
			choices = append(choices, choice)
		}
	}

	if balance == 0 {
		log.Println("no money to give out")
		return
	}

	log.Println("Final balance is", balance)
	log.Println(choices)
	winner, err := randutil.WeightedChoice(choices)
	if err != nil {
		log.Println("Error selecting winner:", err)
		return
	}
	winnerId := winner.Item

	target := govenmo.Target{}
	target.User.Id = winnerId.(string)

	sentPayment, err := account.PayOrCharge(target, balance, "ayy lmao!", "public")
	if err != nil {
		log.Println("Error sending payment:", err)
	} else {
		log.Println("Payment succeeded", sentPayment)
	}
}
Ejemplo n.º 3
0
func (c *ChoiceList) Choose() (string, error) {
	var ch interface{}
	Debugf("Choose among %v", c.choices)
	ch, err := R.WeightedChoice(c.choices)
	if err != nil {
		return "", err
	}
	return ch.(R.Choice).Item.(chooser).Choose()
}
Ejemplo n.º 4
0
func (s *Suffix) Pick() string {
	var c []randutil.Choice

	for k, v := range s.M {
		c = append(c, randutil.Choice{int(v), k})
	}

	choice, err := randutil.WeightedChoice(c)
	if err != nil {
		panic(err)
	}

	return choice.Item.(string)
}