Skip to content

Commit

Permalink
chore: go improvements (#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
moul authored Sep 24, 2023
1 parent d94e33c commit 9ff6b19
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 28 deletions.
11 changes: 11 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
default: help

help: ## Display this help message.
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[0-9a-zA-Z_-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 }' $(MAKEFILE_LIST)

Expand Down Expand Up @@ -90,3 +92,12 @@ z_use_local_gno: ## Use the local '../gno' directory instead of the remote 'gith
z_use_remote_gno: ## Use the remote 'github.com/gnolang/gno' module and remove any replacements.
@echo "Switching to remote gno module..."
@go mod edit -dropreplace github.com/gnolang/gno

z_test_realm: ## Test the realm.
go run github.com/gnolang/gno/gnovm/cmd/gno test --verbose ./realm

z_build_realm: ## Precompile and build the generated Go files. Assumes a working clone of gno in ../gno.
mkdir -p ../gno/examples/gno.land/r/gnochess
cp -rf realm/*.gno ../gno/examples/gno.land/r/gnochess
go run github.com/gnolang/gno/gnovm/cmd/gno precompile --verbose ../gno/examples/gno.land
go run github.com/gnolang/gno/gnovm/cmd/gno build --verbose ../gno/examples/gno.land/r/gnochess
26 changes: 11 additions & 15 deletions realm/chess.gno
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ type Game struct {

func (g Game) json() string {
s, err := g.MarshalJSON()
if err != nil {
panic(err)
}
checkErr(err)
return string(s)
}

Expand Down Expand Up @@ -237,7 +235,7 @@ func assertGamesFinished(games []*Game) {
if g.State.IsFinished() {
continue
}
err := claimTimeout(g)
err := g.claimTimeout()
if err != nil {
panic("can't start new game: game " + g.ID + " is not yet finished")
}
Expand Down Expand Up @@ -379,7 +377,7 @@ func MakeMove(gameID, from, to string, promote Piece) string {
} else {
g.Winner = WinnerWhite
}
saveGameResult(g)
g.saveResult()
return g.json()
}
}
Expand Down Expand Up @@ -432,12 +430,12 @@ func MakeMove(gameID, from, to string, promote Piece) string {
g.Winner = WinnerDraw
}
g.DrawOfferer = nil
saveGameResult(g)
g.saveResult()

return g.json()
}

func claimTimeout(g *Game) error {
func (g *Game) claimTimeout() error {
// no assert origin call or caller check: anyone can claim a game to have
// finished in timeout.

Expand Down Expand Up @@ -469,7 +467,7 @@ func claimTimeout(g *Game) error {
g.Winner = WinnerWhite
}
g.DrawOfferer = nil
saveGameResult(g)
g.saveResult()

return nil
}
Expand All @@ -479,10 +477,8 @@ func claimTimeout(g *Game) error {
func ClaimTimeout(gameID string) string {
g := getGame(gameID, true)

err := claimTimeout(g)
if err != nil {
panic(err.Error())
}
err := g.claimTimeout()
checkErr(err)

return g.json()
}
Expand Down Expand Up @@ -525,7 +521,7 @@ func Resign(gameID string) string {
panic("you are not involved in this game")
}
g.DrawOfferer = nil
saveGameResult(g)
g.saveResult()

return g.json()
}
Expand Down Expand Up @@ -590,7 +586,7 @@ func Draw(gameID string) string {
g.Winner = WinnerDraw
g.Concluder = &caller

saveGameResult(g)
g.saveResult()

return g.json()
}
Expand All @@ -610,7 +606,7 @@ func Draw(gameID string) string {
g.Winner = WinnerDraw
g.DrawOfferer = nil

saveGameResult(g)
g.saveResult()

return g.json()
}
4 changes: 1 addition & 3 deletions realm/chess_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -350,9 +350,7 @@ func parseCommandTest(t *testing.T, command string) (funcs []testCommandRunner,
lines := strings.Split(command, "\n")
atoi := func(s string) int {
n, err := strconv.Atoi(s)
if err != nil {
panic(err)
}
checkErr(err)
return n
}
// used to detect whether to auto-add a panic checker
Expand Down
14 changes: 4 additions & 10 deletions realm/discovery.gno
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,7 @@ func GetPlayer(player string) string {
panic("player not found")
}
b, err := v.(*Player).MarshalJSON()
if err != nil {
panic(err)
}
checkErr(err)
return string(b)
}

Expand Down Expand Up @@ -150,7 +148,7 @@ func (p Player) MarshalJSON() ([]byte, error) {
return buf.Bytes(), nil
}

func saveGameResult(g *Game) {
func (g *Game) saveResult() {
w, b := getPlayer(g.White), getPlayer(g.Black)

cat := g.Time.Category()
Expand Down Expand Up @@ -282,9 +280,7 @@ func Leaderboard(category string) string {
for idx, entry := range leaderboard[cat] {
p, _ := playerStore.Get(entry.addr.String())
d, err := p.(Player).MarshalJSON()
if err != nil {
panic(err)
}
checkErr(err)
buf.Write(d)
if idx != len(leaderboard[cat])-1 {
buf.WriteByte(',')
Expand Down Expand Up @@ -420,9 +416,7 @@ func parseFilters(filters string) (r listGamesFilters) {
r.minID = pred
case "limit:":
n, err := strconv.Atoi(pred)
if err != nil {
panic(err)
}
checkErr(err)
r.limit = n
case "sort:":
r.reverse = pred == "desc"
Expand Down
7 changes: 7 additions & 0 deletions realm/util.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package chess

func checkErr(err error) {
if err != nil {
panic(err.Error())
}
}

0 comments on commit 9ff6b19

Please sign in to comment.