tamagotchi.gno

2.89 Kb ยท 175 lines
  1package tamagotchi
  2
  3import (
  4	"time"
  5
  6	"gno.land/p/demo/ufmt"
  7)
  8
  9// Tamagotchi structure
 10type Tamagotchi struct {
 11	name        string
 12	hunger      int
 13	happiness   int
 14	health      int
 15	age         int
 16	maxAge      int
 17	sleepy      int
 18	created     time.Time
 19	lastUpdated time.Time
 20}
 21
 22func New(name string) *Tamagotchi {
 23	now := time.Now()
 24	return &Tamagotchi{
 25		name:        name,
 26		hunger:      50,
 27		happiness:   50,
 28		health:      50,
 29		maxAge:      100,
 30		lastUpdated: now,
 31		created:     now,
 32	}
 33}
 34
 35func (t *Tamagotchi) Name() string {
 36	t.update()
 37	return t.name
 38}
 39
 40func (t *Tamagotchi) Hunger() int {
 41	t.update()
 42	return t.hunger
 43}
 44
 45func (t *Tamagotchi) Happiness() int {
 46	t.update()
 47	return t.happiness
 48}
 49
 50func (t *Tamagotchi) Health() int {
 51	t.update()
 52	return t.health
 53}
 54
 55func (t *Tamagotchi) Age() int {
 56	t.update()
 57	return t.age
 58}
 59
 60func (t *Tamagotchi) Sleepy() int {
 61	t.update()
 62	return t.sleepy
 63}
 64
 65// Feed method for Tamagotchi
 66func (t *Tamagotchi) Feed() {
 67	t.update()
 68	if t.dead() {
 69		return
 70	}
 71	t.hunger = bound(t.hunger-10, 0, 100)
 72}
 73
 74// Play method for Tamagotchi
 75func (t *Tamagotchi) Play() {
 76	t.update()
 77	if t.dead() {
 78		return
 79	}
 80	t.happiness = bound(t.happiness+10, 0, 100)
 81}
 82
 83// Heal method for Tamagotchi
 84func (t *Tamagotchi) Heal() {
 85	t.update()
 86
 87	if t.dead() {
 88		return
 89	}
 90	t.health = bound(t.health+10, 0, 100)
 91}
 92
 93func (t Tamagotchi) dead() bool { return t.health == 0 }
 94
 95// Update applies changes based on the duration since the last update
 96func (t *Tamagotchi) update() {
 97	if t.dead() {
 98		return
 99	}
100
101	now := time.Now()
102	if t.lastUpdated == now {
103		return
104	}
105
106	duration := now.Sub(t.lastUpdated)
107	elapsedMins := int(duration.Minutes())
108
109	t.hunger = bound(t.hunger+elapsedMins, 0, 100)
110	t.happiness = bound(t.happiness-elapsedMins, 0, 100)
111	t.health = bound(t.health-elapsedMins, 0, 100)
112	t.sleepy = bound(t.sleepy+elapsedMins, 0, 100)
113
114	// age is hours since created
115	t.age = int(now.Sub(t.created).Hours())
116	if t.age > t.maxAge {
117		t.age = t.maxAge
118		t.health = 0
119	}
120	if t.health == 0 {
121		t.sleepy = 0
122		t.happiness = 0
123		t.hunger = 0
124	}
125
126	t.lastUpdated = now
127}
128
129// Face returns an ASCII art representation of the Tamagotchi's current state
130func (t *Tamagotchi) Face() string {
131	t.update()
132	return t.face()
133}
134
135func (t *Tamagotchi) face() string {
136	switch {
137	case t.health == 0:
138		return "๐Ÿ˜ต" // dead face
139	case t.health < 30:
140		return "๐Ÿ˜ท" // sick face
141	case t.happiness < 30:
142		return "๐Ÿ˜ข" // sad face
143	case t.hunger > 70:
144		return "๐Ÿ˜ซ" // hungry face
145	case t.sleepy > 70:
146		return "๐Ÿ˜ด" // sleepy face
147	default:
148		return "๐Ÿ˜ƒ" // happy face
149	}
150}
151
152// Markdown method for Tamagotchi
153func (t *Tamagotchi) Markdown() string {
154	t.update()
155	return ufmt.Sprintf(`# %s %s
156
157* age: %d
158* hunger: %d
159* happiness: %d
160* health: %d
161* sleepy: %d`,
162		t.name, t.Face(),
163		t.age, t.hunger, t.happiness, t.health, t.sleepy,
164	)
165}
166
167func bound(n, min, max int) int {
168	if n < min {
169		return min
170	}
171	if n > max {
172		return max
173	}
174	return n
175}