-
Notifications
You must be signed in to change notification settings - Fork 373
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Adding a lottery to examples #1850
base: master
Are you sure you want to change the base?
Changes from 27 commits
3d20ae0
f0a477d
26a2dd2
e56ea56
f9f4993
4d095c8
793f749
303dfd4
f6b51f2
ca1398d
8780052
88e2171
0832c33
add8778
769d870
92caa54
a006915
e54628a
3b683a7
444b5ef
b0bccca
bba81f5
1cdf875
4c649fd
58e0675
7fbf161
3c2409b
5ea48cc
a4a5f38
1fb9689
4805e22
db4c8c4
63d4d90
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module gno.land/p/demo/gnolotto |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package gnolotto | ||
|
||
import ( | ||
"std" | ||
"strconv" | ||
"strings" | ||
"time" | ||
) | ||
|
||
type Ticket struct { | ||
Numbers []int // Holds the selected numbers for the lottery ticket | ||
Owner std.Address // Address of the ticket owner | ||
} | ||
|
||
type Lottery struct { | ||
Tickets []Ticket // All tickets in the lottery | ||
WinningNumbers []int // Winning numbers after the draw | ||
DrawTime time.Time // Time of the draw | ||
PrizePool int64 // Total prize pool amount | ||
} | ||
|
||
// Intializes a new lottery instance with a specified draw time and prize pool | ||
func NewLottery(drawTime time.Time, prizePool int64) *Lottery { | ||
return &Lottery{ | ||
DrawTime: drawTime, | ||
PrizePool: prizePool, | ||
Tickets: make([]Ticket, 0), | ||
} | ||
} | ||
|
||
const MaxLottoNumbers = 5 | ||
|
||
// Adds a new ticket to the lottery | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you follow godoc? Start the comment with the func name. Applies for all comments |
||
func (l *Lottery) AddTicket(numbers []int, owner std.Address) { | ||
l.Tickets = append(l.Tickets, Ticket{Numbers: numbers, Owner: owner}) | ||
} | ||
|
||
// Conducts the draw by generating 5 pseudo-random numbers between 1 and 15 inclusive | ||
func (l *Lottery) Draw() { | ||
var blockHeight int64 = std.GetHeight() | ||
|
||
l.WinningNumbers = nil | ||
numbersMap := make(map[int]bool) | ||
|
||
// Add variability to the pseudo-random number generation | ||
var variabilityFactor int64 = 1 | ||
|
||
for len(l.WinningNumbers) < MaxLottoNumbers { | ||
simpleSeed := (blockHeight + variabilityFactor*251) % 233280 | ||
number := int(simpleSeed%15) + 1 // Ensure number is between 1 and 15 | ||
|
||
if !numbersMap[number] { | ||
l.WinningNumbers = append(l.WinningNumbers, number) | ||
numbersMap[number] = true | ||
} | ||
|
||
variabilityFactor += 13 // Adjusts for increased variability | ||
} | ||
} | ||
Comment on lines
+39
to
+59
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now that we have |
||
|
||
// Iterate over all tickets to identify and return the addresses of the winningOwners | ||
func (l *Lottery) CheckWinners() []std.Address { | ||
var winningOwners []std.Address | ||
kazai777 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
for _, ticket := range l.Tickets { | ||
isWinner := true | ||
|
||
for i := 0; i < MaxLottoNumbers; i++ { | ||
if ticket.Numbers[i] != l.WinningNumbers[i] { | ||
isWinner = false | ||
break | ||
} | ||
} | ||
|
||
if isWinner { | ||
winningOwners = append(winningOwners, ticket.Owner) | ||
} | ||
} | ||
|
||
return winningOwners | ||
} | ||
|
||
// Distributes the prize pool equally among the winning ticket owners | ||
func (l *Lottery) PayWinners(winningOwners []std.Address) { | ||
kazai777 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if len(winningOwners) == 0 { | ||
return | ||
} else { | ||
kazai777 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Calculate reward per winner | ||
var reward int64 = l.PrizePool / int64(len(winningOwners)) | ||
|
||
banker := std.GetBanker(std.BankerTypeRealmSend) | ||
|
||
for _, owner := range winningOwners { | ||
send := std.Coins{{"ugnot", reward}} | ||
banker.SendCoins(std.GetOrigPkgAddr(), owner, send) // Send reward to each winner | ||
} | ||
|
||
l.PrizePool = 0 // Reset the prize pool after distribution | ||
} | ||
} | ||
|
||
func StringToIntSlice(numbersStr string) ([]int, error) { | ||
numbersSlice := strings.Split(numbersStr, ",") | ||
numbers := make([]int, len(numbersSlice)) | ||
for i, numStr := range numbersSlice { | ||
num, err := strconv.Atoi(strings.TrimSpace(numStr)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
numbers[i] = num | ||
} | ||
return numbers, nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a short godoc comment about the package to give a brief intro as to what it's about, what it can be used for?