Skip to content

Commit 25d1933

Browse files
authored
add lcs2 (#79)
* add lcs2 * update readme * add comments
1 parent b8322e7 commit 25d1933

File tree

6 files changed

+212
-0
lines changed

6 files changed

+212
-0
lines changed

live-coding/2-home-realm/README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Gno Live Coding Session #2
2+
3+
#### Topic: A gno.land home realm
4+
#### Date: Friday, Aug 30th 2024
5+
#### Presenter: [@leohhhn](https://github.com/leohhhn)
6+
7+
This live coding session showed attendees how to build their own home realm for
8+
gno.land.
9+
10+
The home realm for gno.land is meant to be similar to what a GitHub profile page
11+
is for GitHub. It's personal, and it includes information about the user.
12+
13+
In this specific example, we create a home realm that adds an image, text about
14+
the user, and also showcases dynamic imports of `r/demo/art` realms. All of this
15+
is then combined into an output to showcase how the `Render()` function works
16+
in Gno.
17+
18+
## Following up
19+
20+
We encourage anyone who wishes to build upon the example given in the coding
21+
session to do so. Please make a PR adding your code to the
22+
[`followup-work/`](./followup-work) folder, and ping the presenter.
23+
24+
## Video
25+
26+
Video recording will be uploaded soon.
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// Package config is simply Leon's configuration file.
2+
// It can be used externally to dynamically give Leon the rights to call something;
3+
// Say, an admin variable can be set to r/leon/config.Address(). Then, Leon can
4+
// update his address and still retain admin priveleges.
5+
package config
6+
7+
import (
8+
"errors"
9+
"std"
10+
)
11+
12+
var (
13+
main std.Address
14+
backup std.Address
15+
)
16+
17+
func init() {
18+
main = "g125em6arxsnj49vx35f0n0z34putv5ty3376fg5" // registered to @leon
19+
}
20+
21+
func Address() std.Address {
22+
return main
23+
}
24+
25+
func Backup() std.Address {
26+
return backup
27+
}
28+
29+
func SetAddress(newAddr std.Address) {
30+
AssertAuthorized()
31+
if !newAddr.IsValid() {
32+
panic("config: invalid address")
33+
}
34+
35+
main = newAddr
36+
}
37+
38+
func SetBackup(newAddr std.Address) {
39+
AssertAuthorized()
40+
if !newAddr.IsValid() {
41+
panic("config: invalid address")
42+
}
43+
44+
backup = newAddr
45+
}
46+
47+
func CheckAuthorized() error {
48+
caller := std.PrevRealm().Addr()
49+
50+
if caller != main || caller != backup {
51+
return errors.New("config: unauthorized")
52+
}
53+
54+
return nil
55+
}
56+
57+
func AssertAuthorized() {
58+
caller := std.PrevRealm().Addr()
59+
60+
if caller != main || caller != backup {
61+
panic("config: unauthorized")
62+
}
63+
}
64+
65+
// Possible improvements
66+
// Add errors, add a non-panicking function for Authorized
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module gno.land/r/leon/config
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module gno.land/r/leon/home
2+
3+
require (
4+
gno.land/p/demo/ufmt v0.0.0-latest
5+
gno.land/r/demo/art/gnoface v0.0.0-latest
6+
gno.land/r/leon/config v0.0.0-latest
7+
)
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
// Package home is Leon's personal home realm, deployed under gno.land/r/leon/home
2+
// It is meant to act as Leon's profile page on gno.land
3+
package home
4+
5+
import (
6+
"std"
7+
"strconv"
8+
9+
"gno.land/p/demo/ufmt"
10+
"gno.land/r/demo/art/gnoface"
11+
"gno.land/r/demo/art/millipede"
12+
"gno.land/r/leon/config"
13+
)
14+
15+
var (
16+
pfp string // link to my profile pic
17+
pfpCaption string
18+
abtMe [2]string
19+
)
20+
21+
func init() {
22+
pfp = "https://i.imgflip.com/91vskx.jpg"
23+
pfpCaption = "[My favourite painting & pfp](https://en.wikipedia.org/wiki/Wanderer_above_the_Sea_of_Fog)"
24+
abtMe = [2]string{
25+
`### About me
26+
Hi, I'm Leon, a DevRel Engineer at gno.land. I am a tech enthusiast,
27+
life-long learner, and sharer of knowledge.`,
28+
`### Contributions
29+
My contributions to gno.land can mainly be found
30+
[here](https://github.com/gnolang/gno/issues?q=sort:updated-desc+author:leohhhn).
31+
32+
TODO import r/gh`,
33+
}
34+
}
35+
36+
// Updating
37+
38+
func UpdateAbtMe(col1, col2 string) {
39+
config.AssertAuthorized()
40+
abtMe[0] = col1
41+
abtMe[1] = col2
42+
}
43+
44+
// Todo: make setters for all pkg-level variables
45+
46+
// Rendering
47+
48+
func Render(_ string) string {
49+
output := "# Leon's Homepage\n\n"
50+
51+
output += renderAboutMe()
52+
output += "\n\n"
53+
output += renderArt()
54+
55+
return output
56+
}
57+
58+
func renderAboutMe() string {
59+
out := "<div class='columns-3'>"
60+
61+
out += "<div>\n\n"
62+
out += ufmt.Sprintf("![](%s)\n\n%s\n\n", pfp, pfpCaption)
63+
out += "</div>\n\n"
64+
65+
out += "<div>\n\n"
66+
out += abtMe[0] + "\n\n"
67+
out += "</div>\n\n"
68+
69+
out += "<div>\n\n"
70+
out += abtMe[1] + "\n\n"
71+
out += "</div>\n\n"
72+
73+
out += "</div>"
74+
return out
75+
}
76+
77+
func renderArt() string {
78+
out := `<div class=jumbotron>` + "\n\n"
79+
out += "## Gno Art\n\n"
80+
out += "<div class='columns-3'>"
81+
82+
out += renderGnoFace()
83+
out += renderMillipede()
84+
out += renderGnoFace()
85+
86+
out += "</div><!-- /columns-3 -->"
87+
out += "</div><!-- /jumbotron -->"
88+
89+
return out
90+
}
91+
92+
func renderGnoFace() string {
93+
out := "<div>\n\n"
94+
out += gnoface.Render(strconv.Itoa(int(std.GetHeight())))
95+
out += "</div>\n\n"
96+
97+
return out
98+
}
99+
100+
func renderMillipede() string {
101+
out := "<div>\n\n"
102+
out += "Millipede\n\n"
103+
out += "```\n" + millipede.Draw(int(std.GetHeight()%10+1)) + "```\n"
104+
out += "</div>\n\n"
105+
106+
return out
107+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Followup work
2+
3+
To add your code for this GnoLCS, create a directory named after your GitHub username
4+
under this folder.
5+

0 commit comments

Comments
 (0)