-
Notifications
You must be signed in to change notification settings - Fork 379
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
Open
kazai777
wants to merge
33
commits into
gnolang:master
Choose a base branch
from
kazai777:add_kazai_gnolotto
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
3d20ae0
add realm and README
kazai777 f0a477d
add package gnolotto
kazai777 26a2dd2
Merge branch 'master' into add_kazai_gnolotto
kazai777 e56ea56
replace comment random by pseudo-random
kazai777 f9f4993
comment modification, replacing random by pesudo-random
kazai777 4d095c8
Merge branch 'add_kazai_gnolotto' of https://github.com/kazai777/gno …
kazai777 793f749
Add constant for max lottery numbers
kazai777 303dfd4
Correct typo for `iterate`
kazai777 f6b51f2
replace winners by winningOwners
kazai777 ca1398d
remove casting to int64
kazai777 8780052
remove refund manually and add panic
kazai777 88e2171
remove refund manually and add panic
kazai777 0832c33
remove refund manually and add panic
kazai777 add8778
remove refund manually and add panic
kazai777 769d870
remove refund manually and add panic and modify condition
kazai777 92caa54
remove refund manually and add panic
kazai777 a006915
replace return by panic
kazai777 e54628a
replace return by panic
kazai777 3b683a7
replace return by panic
kazai777 444b5ef
Modify ID lottery by seqid
kazai777 b0bccca
add const TICKET_PRICE
kazai777 bba81f5
separation of string to int slice conversion function
kazai777 1cdf875
formatting gnokey commands
kazai777 4c649fd
refactoring README
kazai777 58e0675
delete import not used in realm
kazai777 7fbf161
make tidy
kazai777 3c2409b
modification CheckWinners()
kazai777 5ea48cc
Merge branch 'gnolang:master' into add_kazai_gnolotto
kazai777 a4a5f38
add test file for realm and fix code
kazai777 1fb9689
add testfile for package and fix code
kazai777 4805e22
fix commit
kazai777 db4c8c4
Merge branch 'master' into add_kazai_gnolotto
kazai777 63d4d90
Merge branch 'master' into add_kazai_gnolotto
thehowl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module gno.land/p/demo/gnolotto |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
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), | ||
} | ||
} | ||
|
||
// 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 random numbers between 1 and 15 | ||
kazai777 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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) < 5 { | ||
kazai777 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 | ||
} | ||
} | ||
|
||
// Itterate over all tickets to identify and return the addresses of the winners | ||
kazai777 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 { | ||
matchCount := 0 | ||
|
||
for _, tNum := range ticket.Numbers { | ||
for _, wNum := range l.WinningNumbers { | ||
if tNum == wNum { | ||
matchCount++ | ||
break | ||
} | ||
} | ||
} | ||
|
||
if matchCount == len(l.WinningNumbers) { | ||
winningOwners = append(winningOwners, ticket.Owner) | ||
} | ||
} | ||
|
||
kazai777 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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?