-
Notifications
You must be signed in to change notification settings - Fork 10
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: add player stats for raffle #173
Open
moul
wants to merge
7
commits into
main
Choose a base branch
from
dev/moul/stats
base: main
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 all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
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
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
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,130 @@ | ||
package chess | ||
|
||
import ( | ||
"std" | ||
|
||
"gno.land/p/demo/avl" | ||
"gno.land/p/demo/ufmt" | ||
) | ||
|
||
var allPlayerStats avl.Tree // std.Address -> *playerStats | ||
|
||
type playerStats struct { | ||
Addr std.Address // Not stored when in avl.Tree, but lazily filled for public-facing helpers returning playerStats. | ||
Moves uint | ||
StartedGames uint | ||
WonGames uint | ||
LostGames uint | ||
TimedoutGames uint | ||
ResignedGames uint | ||
DrawnGames uint | ||
SeriousGames uint // finished, or resigned/drawn after 20 full moves (40 turns), used for the raffle. | ||
// later we can add achievements: | ||
// SuperFastAchievement // if a game is finished in less than N seconds. | ||
// OnlyPawnsAchievement // winning with only pawns, etc. | ||
} | ||
|
||
func (s playerStats) String() string { | ||
return ufmt.Sprintf( | ||
"addr:%s moves:%d started:%d won:%d lost:%d timedout:%d resigned:%d drawn:%d serious:%d", | ||
s.Addr, s.Moves, s.StartedGames, s.WonGames, s.LostGames, s.TimedoutGames, | ||
s.ResignedGames, s.DrawnGames, s.SeriousGames, | ||
) | ||
} | ||
|
||
func getPlayerStats(addr std.Address) *playerStats { | ||
addrStr := string(addr) | ||
res, found := allPlayerStats.Get(addrStr) | ||
if found { | ||
return res.(*playerStats) | ||
} | ||
|
||
newStats := playerStats{} | ||
allPlayerStats.Set(addrStr, &newStats) | ||
return &newStats | ||
} | ||
|
||
func GetPlayerStats(addr std.Address) playerStats { | ||
stats := getPlayerStats(addr) | ||
cpy := *stats | ||
cpy.Addr = addr | ||
return cpy | ||
} | ||
|
||
func AllPlayerStats() []playerStats { | ||
ret := []playerStats{} | ||
|
||
allPlayerStats.Iterate("", "", func(addrString string, v interface{}) bool { | ||
stats := *(v.(*playerStats)) | ||
stats.Addr = std.Address(addrString) | ||
ret = append(ret, stats) | ||
return false | ||
}) | ||
|
||
return ret | ||
} | ||
|
||
type gameStats struct { | ||
caller, opponent, white, black *playerStats | ||
} | ||
|
||
func (g Game) getStats(caller std.Address) gameStats { | ||
stats := gameStats{ | ||
white: getPlayerStats(g.White), | ||
black: getPlayerStats(g.Black), | ||
} | ||
|
||
if caller != "" { // if caller is empty, we just fill "black" and "white". | ||
if caller == g.White { | ||
stats.caller = stats.white | ||
stats.opponent = stats.black | ||
} else { | ||
stats.caller = stats.black | ||
stats.opponent = stats.white | ||
} | ||
} | ||
|
||
return stats | ||
} | ||
|
||
func (g Game) updateEndgameStats() { | ||
stats := g.getStats("") | ||
|
||
// serious games | ||
isSerious := false | ||
switch { | ||
case g.State == GameStateCheckmated: // checkmates | ||
isSerious = true | ||
case len(g.Position.Moves) >= 40: // long games | ||
isSerious = true | ||
} | ||
if isSerious { | ||
stats.black.SeriousGames++ | ||
stats.white.SeriousGames++ | ||
} | ||
|
||
// timeouts, aborted, etc | ||
if g.State == GameStateTimeout || g.State == GameStateAborted { | ||
stats.black.TimedoutGames++ | ||
stats.white.TimedoutGames++ | ||
} | ||
|
||
// winners, losers, draws | ||
switch g.Winner { | ||
case WinnerWhite: | ||
stats.white.WonGames++ | ||
stats.black.LostGames++ | ||
if g.State == GameStateResigned { | ||
stats.black.ResignedGames++ | ||
} | ||
case WinnerBlack: | ||
stats.black.WonGames++ | ||
stats.white.LostGames++ | ||
if g.State == GameStateResigned { | ||
stats.white.ResignedGames++ | ||
} | ||
case WinnerDraw: | ||
stats.black.DrawnGames++ | ||
stats.white.DrawnGames++ | ||
} | ||
} |
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.
Why not
?
And so why doing that in the public function instead (line 50) ?
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.
to save on storage since the address is already known by the
avl.Tree
; but I'm okay to move to your solution too. Feel free to patch my PR if you prefer 👍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.
No that's fine, it's also good to save storage!