Skip to content

feat(examples): pokemon fight simulator game at r/leo/pokemon #4072

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

Open
wants to merge 33 commits into
base: master
Choose a base branch
from

Conversation

Tchips46
Copy link

@Tchips46 Tchips46 commented Apr 7, 2025

Added

Realms:

r/leo/pokemon
A small Pokémon fight simulator game to showcase what a ranked game could look like using blockchain.

  • Pokémon fight base system
  • Team fight
    • Swap Pokémon during fight
    • Use items during fight
    • Attack enemy
    • PVE
      • Random attack selection
      • Random team selector
    • Transaction to end fight and validate data
  • Pokémon DAO implementation
  • Ranking system
  • Team selector
  • Shop to buy items
  • Login system
  • Replace p/leo/randomizer by math/rand
  • Replace p/leo/renderer by p/moul/md and p/moul/mdtable

@Tchips46 Tchips46 marked this pull request as draft April 7, 2025 17:04
@github-actions github-actions bot added the 🧾 package/realm Tag used for new Realms or Packages. label Apr 7, 2025
@Gno2D2 Gno2D2 requested a review from a team April 7, 2025 17:05
@Gno2D2
Copy link
Collaborator

Gno2D2 commented Apr 7, 2025

🛠 PR Checks Summary

All Automated Checks passed. ✅

Manual Checks (for Reviewers):
  • IGNORE the bot requirements for this PR (force green CI check)
  • The pull request description provides enough details
Read More

🤖 This bot helps streamline PR reviews by verifying automated checks and providing guidance for contributors and reviewers.

✅ Automated Checks (for Contributors):

🟢 Maintainers must be able to edit this pull request (more info)
🟢 Pending initial approval by a review team member, or review from tech-staff

☑️ Contributor Actions:
  1. Fix any issues flagged by automated checks.
  2. Follow the Contributor Checklist to ensure your PR is ready for review.
    • Add new tests, or document why they are unnecessary.
    • Provide clear examples/screenshots, if necessary.
    • Update documentation, if required.
    • Ensure no breaking changes, or include BREAKING CHANGE notes.
    • Link related issues/PRs, where applicable.
☑️ Reviewer Actions:
  1. Complete manual checks for the PR, including the guidelines and additional checks if applicable.
📚 Resources:
Debug
Automated Checks
Maintainers must be able to edit this pull request (more info)

If

🟢 Condition met
└── 🟢 And
    ├── 🟢 The base branch matches this pattern: ^master$
    └── 🟢 The pull request was created from a fork (head branch repo: Tchips46/gno)

Then

🟢 Requirement satisfied
└── 🟢 Maintainer can modify this pull request

Pending initial approval by a review team member, or review from tech-staff

If

🟢 Condition met
└── 🟢 And
    ├── 🟢 The base branch matches this pattern: ^master$
    └── 🟢 Not (🔴 Pull request author is a member of the team: tech-staff)

Then

🟢 Requirement satisfied
└── 🟢 If
    ├── 🟢 Condition
    │   └── 🟢 Or
    │       ├── 🔴 At least 1 user(s) of the organization reviewed the pull request (with state "APPROVED")
    │       ├── 🟢 At least 1 user(s) of the team tech-staff reviewed pull request
    │       └── 🔴 This pull request is a draft
    └── 🟢 Then
        └── 🟢 And
            ├── 🟢 Not (🔴 This label is applied to pull request: review/triage-pending)
            └── 🟢 At least 1 user(s) of the team tech-staff reviewed pull request

Manual Checks
**IGNORE** the bot requirements for this PR (force green CI check)

If

🟢 Condition met
└── 🟢 On every pull request

Can be checked by

  • Any user with comment edit permission
The pull request description provides enough details

If

🟢 Condition met
└── 🟢 And
    ├── 🟢 Not (🔴 Pull request author is a member of the team: core-contributors)
    └── 🟢 Not (🔴 Pull request author is user: dependabot[bot])

Can be checked by

  • team core-contributors

Copy link

codecov bot commented Apr 7, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

📢 Thoughts on this report? Let us know!

@Tchips46 Tchips46 force-pushed the feat/add-leo-realm-pokemon-game-for-demo branch from 14b4c01 to 6be7a81 Compare April 14, 2025 14:22
@Tchips46 Tchips46 marked this pull request as ready for review April 15, 2025 12:04
@Gno2D2 Gno2D2 added the review/triage-pending PRs opened by external contributors that are waiting for the 1st review label Apr 15, 2025
Copy link
Contributor

@leohhhn leohhhn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

leaving some comments

@leohhhn
Copy link
Contributor

leohhhn commented Apr 15, 2025

Also please check the CI!

@Tchips46 Tchips46 requested a review from leohhhn April 16, 2025 12:22
Copy link
Contributor

@leohhhn leohhhn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, leaving some comments.

There are a couple of things that could be improved generally, like code readability, comments, etc. Go (and Gno) code should strive to be clean, readable, straightforward whenever possible. This is a pretty cool game and I can't wait to have it on the chain, so please fix up the comments and we'll get it moving :)

Apart from this you will have to implement the Inter-realm changes into your app soon.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can remove this and publish it permissionlessly, as it's just some text render.

Copy link
Author

@Tchips46 Tchips46 Jun 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll juste remove it since it doesn't showcase any concrete gno usage acd5857

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was a good learning for you guys. Now that we have packages like commondao, I suggest you use them to make DAOs. Try it out via p/nt/commondao or r/nt/commondao (the factory)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I completely removed DAO since i only used it for member managing 36e9afc

Comment on lines 81 to 127
for i := teamSize; i < len(state); i += 2 {
actionParam := state[i+1] - '0'
switch state[i] {
case 's':
if me.team[actionParam].name != "" {
game.myPokemon = &me.team[actionParam]
} else {
err = errors.New("invalid swap : " + string(state[i+1]))
}
case 'a':
if game.myPokemon != nil && game.myPokemon.pv > 0 && game.myPokemon.attacks[actionParam].pc > 0 {
game.myPokemon.attacks[actionParam].pc -= 1
game.otherPokemon.pv -= game.myPokemon.attacks[actionParam].damage
if game.otherPokemon.pv <= 0 {
for i := 0; i < teamSize; i++ {
if game.otherTeam[i].pv > 0 {
game.otherPokemon = &game.otherTeam[i]
break
}
}
}
} else {
err = errors.New("invalid ally attack : " + string(state[i+1]))
}
case 'i':
if game.myPokemon != nil && len(me.inventory) > int(actionParam) {
itemData[me.inventory[actionParam]].applyer(game.myPokemon)
me.inventory = append(me.inventory[:actionParam], me.inventory[actionParam+1:]...)
} else {
err = errors.New("invalid item use : " + string(state[i+1]))
}
case 'e':
if game.otherPokemon != nil && game.otherPokemon.pv > 0 && game.otherPokemon.attacks[actionParam].pc > 0 {
game.otherPokemon.attacks[actionParam].pc -= 1
game.myPokemon.pv -= game.otherPokemon.attacks[actionParam].damage
} else {
err = errors.New("invalid enemy attack : " + string(state[i+1]))
}
default:
err = errors.New("invalid action : " + string(state[i]))
}
if checkGameEnd(me.team, &game) {
return
}
}
return
}
Copy link
Contributor

@leohhhn leohhhn May 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit hard to read/understand without context. Do you think it could be 1) optimized, written more cleanly 2) commented so that the reader has more context on what's each case supposed to mean?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I optimised it using a function pointer map
eda6c09

Comment on lines +134 to +135
// getEnnemyNextAttack selects a random valid attack for the enemy Pokémon
func getEnnemyNextAttack(p *Pokemon) string {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo

Suggested change
// getEnnemyNextAttack selects a random valid attack for the enemy Pokémon
func getEnnemyNextAttack(p *Pokemon) string {
// getEnemyNextAttack selects a random valid attack for the enemy Pokémon
func getEnemyNextAttack(p *Pokemon) string {

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


// renderTeamSwap renders the team swap interface for the player
func renderTeamSwap(game GameState, team [teamSize]Pokemon, user, state string) (rend string) {
have_pokemon := false
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

camel case is nicer for go :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines 156 to 180
table := mdtable.Table{Headers: []string{"Swap for "}, Rows: [][]string{[]string{"HP"}}}
for i, p := range team {
if p.name == "" {
continue
}
have_pokemon = true
if &team[i] == game.myPokemon {
table.Headers = append(table.Headers, "Selected : "+p.name+md.Image(p.name, p.assetPath))
} else {
link := getFightLink(user, state) + "s" + strconv.Itoa(i)
table.Headers = append(table.Headers, md.Link(p.name, link)+md.InlineImageWithLink(p.name, p.assetPath, link))
}
if p.pv > 0 {
table.Rows[0] = append(table.Rows[0], strconv.Itoa(p.pv)+" / "+strconv.Itoa(p.getDefault().pv))
} else {
table.Rows[0] = append(table.Rows[0], strconv.Itoa(0)+" / "+strconv.Itoa(p.getDefault().pv))
}
}
if !have_pokemon {
rend += "Team : " + md.Bold("None !") + nl
} else {
rend += nl + table.String()
}
return
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as before. the code is a bit hard to read/understand initially. It could be made better with some comments, optimization, and cleaner writing.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines 182 to 194
// renderInventoryUse renders the player's inventory for item usage
func renderInventoryUse(inventory []ItemType, user, state string) (rend string) {
if len(inventory) != 0 {
table := mdtable.Table{Headers: []string{"Item", "Description"}}
for i, e := range inventory {
table.Append([]string{md.Link("Use "+itemData[e].name, getFightLink(user, state)+"i"+strconv.Itoa(i)), itemData[e].description})
}
rend += nl + table.String()
} else {
rend += "Inventory : " + md.Bold("Empty !") + nl
}
return
}
Copy link
Contributor

@leohhhn leohhhn May 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple of points here:
Practice early returns; no need for the else statement here. See code below; much cleaner.

if len(inventory) == 0 {
	return "Inventory : " + md.Bold("Empty !") + nl
}

table := mdtable.Table{Headers: []string{"Item", "Description"}}
for i, e := range inventory {
	table.Append([]string{md.Link("Use "+itemData[e].name, getFightLink(user, state)+"i"+strconv.Itoa(i)), itemData[e].description})
}
return nl + table.String()

Two more things:

  1. if you have return values in your function, please return them explicitly
  2. There is no need to name the return values unless its hard to understand what's what, when there are multiple ones. In this case, it's obvious with context that "render" returns a string to render out. Being able to name return vars is not meant to be used to shorten your package by one line of code, unless size is really critical :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

// renderFightPokemon renders a Pokémon's details and attack options for the fight page
func renderFightPokemon(p *Pokemon, user, state string, canAttack bool, otherP *Pokemon) (rend string) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same comment about the return value as before

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rend += md.Link("Go back to lobby", atmPath+":lobby?user="+user) + nl
}
if game.state == gameLoose {
rend += md.H3("You LOOSE...")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
rend += md.H3("You LOOSE...")
rend += md.H3("You LOSE...")

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@leohhhn leohhhn moved this from In Review to In Progress in 🧑‍🎓Student contributors May 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🤝 contribs 🧾 package/realm Tag used for new Realms or Packages.
Projects
Status: In Progress
Status: In Progress
Development

Successfully merging this pull request may close these issues.

4 participants