Skip to content

Commit

Permalink
Merge branch 'master' into hariom/gno-mod-why
Browse files Browse the repository at this point in the history
  • Loading branch information
harry-hov committed Dec 14, 2023
2 parents 0e04589 + eef0c98 commit 5755a56
Show file tree
Hide file tree
Showing 17 changed files with 704 additions and 13 deletions.
3 changes: 3 additions & 0 deletions examples/gno.land/p/demo/tamagotchi/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module gno.land/p/demo/tamagotchi

require gno.land/p/demo/ufmt v0.0.0-latest
175 changes: 175 additions & 0 deletions examples/gno.land/p/demo/tamagotchi/tamagotchi.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
package tamagotchi

import (
"time"

"gno.land/p/demo/ufmt"
)

// Tamagotchi structure
type Tamagotchi struct {
name string
hunger int
happiness int
health int
age int
maxAge int
sleepy int
created time.Time
lastUpdated time.Time
}

func New(name string) *Tamagotchi {
now := time.Now()
return &Tamagotchi{
name: name,
hunger: 50,
happiness: 50,
health: 50,
maxAge: 100,
lastUpdated: now,
created: now,
}
}

func (t *Tamagotchi) Name() string {
t.update()
return t.name
}

func (t *Tamagotchi) Hunger() int {
t.update()
return t.hunger
}

func (t *Tamagotchi) Happiness() int {
t.update()
return t.happiness
}

func (t *Tamagotchi) Health() int {
t.update()
return t.health
}

func (t *Tamagotchi) Age() int {
t.update()
return t.age
}

func (t *Tamagotchi) Sleepy() int {
t.update()
return t.sleepy
}

// Feed method for Tamagotchi
func (t *Tamagotchi) Feed() {
t.update()
if t.dead() {
return
}
t.hunger = bound(t.hunger-10, 0, 100)
}

// Play method for Tamagotchi
func (t *Tamagotchi) Play() {
t.update()
if t.dead() {
return
}
t.happiness = bound(t.happiness+10, 0, 100)
}

// Heal method for Tamagotchi
func (t *Tamagotchi) Heal() {
t.update()

if t.dead() {
return
}
t.health = bound(t.health+10, 0, 100)
}

func (t Tamagotchi) dead() bool { return t.health == 0 }

// Update applies changes based on the duration since the last update
func (t *Tamagotchi) update() {
if t.dead() {
return
}

now := time.Now()
if t.lastUpdated == now {
return
}

duration := now.Sub(t.lastUpdated)
elapsedMins := int(duration.Minutes())

t.hunger = bound(t.hunger+elapsedMins, 0, 100)
t.happiness = bound(t.happiness-elapsedMins, 0, 100)
t.health = bound(t.health-elapsedMins, 0, 100)
t.sleepy = bound(t.sleepy+elapsedMins, 0, 100)

// age is hours since created
t.age = int(now.Sub(t.created).Hours())
if t.age > t.maxAge {
t.age = t.maxAge
t.health = 0
}
if t.health == 0 {
t.sleepy = 0
t.happiness = 0
t.hunger = 0
}

t.lastUpdated = now
}

// Face returns an ASCII art representation of the Tamagotchi's current state
func (t *Tamagotchi) Face() string {
t.update()
return t.face()
}

func (t *Tamagotchi) face() string {
switch {
case t.health == 0:
return "😵" // dead face
case t.health < 30:
return "😷" // sick face
case t.happiness < 30:
return "😢" // sad face
case t.hunger > 70:
return "😫" // hungry face
case t.sleepy > 70:
return "😴" // sleepy face
default:
return "😃" // happy face
}
}

// Markdown method for Tamagotchi
func (t *Tamagotchi) Markdown() string {
t.update()
return ufmt.Sprintf(`# %s %s
* age: %d
* hunger: %d
* happiness: %d
* health: %d
* sleepy: %d`,
t.name, t.Face(),
t.age, t.hunger, t.happiness, t.health, t.sleepy,
)
}

func bound(n, min, max int) int {
if n < min {
return min
}
if n > max {
return max
}
return n
}
106 changes: 106 additions & 0 deletions examples/gno.land/p/demo/tamagotchi/z0_filetest.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package main

import (
"std"
"time"

"internal/os_test"

"gno.land/p/demo/tamagotchi"
)

func main() {
t := tamagotchi.New("Gnome")

println("\n-- INITIAL\n")
println(t.Markdown())

println("\n-- WAIT 20 minutes\n")
os_test.Sleep(20 * time.Minute)
println(t.Markdown())

println("\n-- FEEDx3, PLAYx2, HEALx4\n")
t.Feed()
t.Feed()
t.Feed()
t.Play()
t.Play()
t.Heal()
t.Heal()
t.Heal()
t.Heal()
println(t.Markdown())

println("\n-- WAIT 20 minutes\n")
os_test.Sleep(20 * time.Minute)
println(t.Markdown())

println("\n-- WAIT 20 hours\n")
os_test.Sleep(20 * time.Hour)
println(t.Markdown())

println("\n-- WAIT 20 hours\n")
os_test.Sleep(20 * time.Hour)
println(t.Markdown())
}

// Output:
// -- INITIAL
//
// # Gnome 😃
//
// * age: 0
// * hunger: 50
// * happiness: 50
// * health: 50
// * sleepy: 0
//
// -- WAIT 20 minutes
//
// # Gnome 😃
//
// * age: 0
// * hunger: 70
// * happiness: 30
// * health: 30
// * sleepy: 20
//
// -- FEEDx3, PLAYx2, HEALx4
//
// # Gnome 😃
//
// * age: 0
// * hunger: 40
// * happiness: 50
// * health: 70
// * sleepy: 20
//
// -- WAIT 20 minutes
//
// # Gnome 😃
//
// * age: 0
// * hunger: 60
// * happiness: 30
// * health: 50
// * sleepy: 40
//
// -- WAIT 20 hours
//
// # Gnome 😵
//
// * age: 20
// * hunger: 0
// * happiness: 0
// * health: 0
// * sleepy: 0
//
// -- WAIT 20 hours
//
// # Gnome 😵
//
// * age: 20
// * hunger: 0
// * happiness: 0
// * health: 0
// * sleepy: 0
6 changes: 6 additions & 0 deletions examples/gno.land/r/demo/tamagotchi/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module gno.land/r/demo/tamagotchi

require (
gno.land/p/demo/tamagotchi v0.0.0-latest
gno.land/p/demo/ufmt v0.0.0-latest
)
53 changes: 53 additions & 0 deletions examples/gno.land/r/demo/tamagotchi/realm.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package tamagotchi

import (
"std"

"gno.land/p/demo/tamagotchi"
"gno.land/p/demo/ufmt"
)

var t *tamagotchi.Tamagotchi

func init() {
Reset("gnome#0")
}

func Reset(optionalName string) string {
name := optionalName
if name == "" {
height := std.GetHeight()
name = ufmt.Sprintf("gnome#%d", height)
}

t = tamagotchi.New(name)

return ufmt.Sprintf("A new tamagotchi is born. Their name is %s %s.", t.Name(), t.Face())
}

func Feed() string {
t.Feed()
return t.Markdown()
}

func Play() string {
t.Play()
return t.Markdown()
}

func Heal() string {
t.Heal()
return t.Markdown()
}

func Render(path string) string {
tama := t.Markdown()
links := `Actions:
* [Feed](/r/demo/tamagotchi?help&__func=Feed)
* [Play](/r/demo/tamagotchi?help&__func=Play)
* [Heal](/r/demo/tamagotchi?help&__func=Heal)
* [Reset](/r/demo/tamagotchi?help&__func=Reset)
`

return tama + "\n\n" + links
}
28 changes: 28 additions & 0 deletions examples/gno.land/r/demo/tamagotchi/z0_filetest.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package main

import (
"std"
"time"

"gno.land/r/demo/tamagotchi"
)

func main() {
tamagotchi.Reset("tamagnotchi")
println(tamagotchi.Render(""))
}

// Output:
// # tamagnotchi 😃
//
// * age: 0
// * hunger: 50
// * happiness: 50
// * health: 50
// * sleepy: 0
//
// Actions:
// * [Feed](/r/demo/tamagotchi?help&__func=Feed)
// * [Play](/r/demo/tamagotchi?help&__func=Play)
// * [Heal](/r/demo/tamagotchi?help&__func=Heal)
// * [Reset](/r/demo/tamagotchi?help&__func=Reset)
6 changes: 6 additions & 0 deletions examples/gno.land/r/demo/wugnot/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module gno.land/r/demo/wugnot

require (
gno.land/p/demo/grc/grc20 v0.0.0-latest
gno.land/p/demo/ufmt v0.0.0-latest
)
Loading

0 comments on commit 5755a56

Please sign in to comment.