aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlorian Fischer <florian.fischer@muhq.space>2023-01-20 12:00:32 +0100
committerFlorian Fischer <florian.fischer@muhq.space>2023-01-26 22:17:17 +0100
commit5c8dd17404820e631fa3d5a82a67f07d067695bd (patch)
tree5ce54fc2a15fd005b55f92e3dbee600a5c6237fa
parenta72f4331a052c3e01ee27fc2cb96c9c12fed1651 (diff)
downloadmuhqs-game-5c8dd17404820e631fa3d5a82a67f07d067695bd.tar.gz
muhqs-game-5c8dd17404820e631fa3d5a82a67f07d067695bd.zip
intermediate commit
* Add static analysis and improve Makefile * Implement hints from static code analysis
-rw-r--r--go/Makefile46
-rw-r--r--go/client/main.go39
-rw-r--r--go/game/ai.go6
-rw-r--r--go/game/card.go6
-rw-r--r--go/game/map.go2
-rw-r--r--go/game/map_test.go7
-rw-r--r--go/game/permanent.go2
-rw-r--r--go/game/state.go6
-rw-r--r--go/server/main.go2
-rw-r--r--go/ui/mapView.go2
10 files changed, 61 insertions, 57 deletions
diff --git a/go/Makefile b/go/Makefile
index 6678a0fa..c38e3143 100644
--- a/go/Makefile
+++ b/go/Makefile
@@ -1,30 +1,42 @@
-PACKAGES := assets game server client webtools ui
+PACKAGES := assets game server client ui
APPS := server client
OBJS := $(foreach app,$(APPS),$(app)/$(app))
WASM := client/client.wasm webtools/webtools.wasm
-.PHONY: all fmt test $(OBJS) $(WASM)
-all: $(OBJS) $(WASM)
+GOFMT := go fmt
-server/server:
- @echo Building Server ...
- pushd $(@D) >/dev/null && go build && popd >/dev/null
+.PHONY: all fmt test check $(OBJS) $(WASM)
+all: $(OBJS) $(WASM)
-client/client:
- @echo Building Client ...
- pushd $(@D) >/dev/null && go build && popd >/dev/null
+.PHONY: client server
+client: client/client
+server: server/server
-client/client.wasm:
- @echo Building Client.wasm ...
- pushd $(@D) >/dev/null && env GOOS=js GOARCH=wasm go build -o $(@F) && popd >/dev/null
+$(OBJS):
+ @echo Building $(@F)
+ @pushd $(@D) >/dev/null && go build && popd >/dev/null
-webtools/webtools.wasm:
- @echo Building Webtools.wasm ...
- pushd $(@D) >/dev/null && env GOOS=js GOARCH=wasm go build -o $(@F) && popd >/dev/null
+*/*.wasm:
+ @echo Building $(@F) ...
+ @pushd $(@D) >/dev/null && env GOOS=js GOARCH=wasm go build -o $(@F) && popd >/dev/null
fmt:
- set -e; for package in $(PACKAGES); do pushd $$package >/dev/null && go fmt && popd >/dev/null; done
+ @set -e; for package in $(PACKAGES) webtools; do \
+ pushd $$package >/dev/null && $(GOFMT) && popd >/dev/null; \
+ done
test:
- for package in $(PACKAGES); do pushd $$package >/dev/null && go test && popd >/dev/null; done
+ @STATUS=0; \
+ for package in $(PACKAGES); do \
+ if pushd "$$package" >/dev/null; then \
+ go test; \
+ status="$$?"; \
+ if [ "$$status" -ne 0 ]; then STATUS="$$status"; fi; \
+ popd >/dev/null; \
+ fi; \
+ done; \
+ exit "$$STATUS"
+
+check:
+ golangci-lint run --skip-dirs webtools
diff --git a/go/client/main.go b/go/client/main.go
index 13a07d0b..c8f2e46d 100644
--- a/go/client/main.go
+++ b/go/client/main.go
@@ -64,10 +64,6 @@ func (h *hoverDetector) reset() {
}
}
-func (h *hoverDetector) hovering() bool {
- return h.ticks >= HOVER_THRESHOLD
-}
-
func (h *hoverDetector) startedHovering() bool {
return h.ticks == HOVER_THRESHOLD
}
@@ -192,18 +188,16 @@ func (g *Game) Update() error {
log.Printf("Active object is of type %T\n", g.selectedObject)
- switch obj.(type) {
+ switch obj := obj.(type) {
case ui.Button:
- button := obj.(ui.Button)
- button.Click(x, y)
+ obj.Click(x, y)
case *game.Tile:
- tile := obj.(*game.Tile)
+ tile := obj
if g.selectedObject != nil {
- switch g.selectedObject.(type) {
+ switch obj := g.selectedObject.(type) {
case *game.MoveActionPrototype:
- proto := g.selectedObject.(*game.MoveActionPrototype)
- action := proto.Specialize(tile)
+ action := obj.Specialize(tile)
g.selectedObject = nil
g.declareAction(action)
g.mapView.HighlightTile(tile)
@@ -213,7 +207,7 @@ func (g *Game) Update() error {
}
case game.Permanent:
- perm := obj.(game.Permanent)
+ perm := obj
if g.selectedObject == nil {
actions := perm.GetAvailActionPrototypes()
if len(actions) > 0 {
@@ -225,10 +219,9 @@ func (g *Game) Update() error {
g.addWidget(ui.NewActionChoice(500, 200, actions, onClick))
}
} else {
- switch g.selectedObject.(type) {
+ switch obj := g.selectedObject.(type) {
case *game.AttackActionPrototype:
- proto := g.selectedObject.(*game.AttackActionPrototype)
- action := proto.Specialize(perm)
+ action := obj.Specialize(perm)
g.selectedObject = nil
g.declareAction(action)
g.mapView.HighlightPermanent(perm)
@@ -239,24 +232,20 @@ func (g *Game) Update() error {
log.Fatalf("Object of type %T not handled", obj)
}
- switch g.selectedObject.(type) {
+ switch obj := g.selectedObject.(type) {
case *game.MoveActionPrototype:
- proto := g.selectedObject.(*game.MoveActionPrototype)
- g.mapView.HighlightTiles(proto.Unit.MoveRangeTiles(g.gameState.Map))
+ g.mapView.HighlightTiles(obj.Unit.MoveRangeTiles(g.gameState.Map))
case *game.AttackActionPrototype:
- proto := g.selectedObject.(*game.AttackActionPrototype)
- perms := proto.Unit.AttackablePermanents(g.gameState.Map)
+ perms := obj.Unit.AttackablePermanents(g.gameState.Map)
g.mapView.HighlightPermanents(perms)
}
} else if g.hoverDetector.startedHovering() && g.selectedObject == nil && obj != nil {
var hoverHint *ui.TextBox
- switch obj.(type) {
+ switch obj := obj.(type) {
case *game.Unit:
- u := obj.(*game.Unit)
- hoverHint = ui.NewUnitInfo(x+ui.TILE_WIDTH, y, u)
+ hoverHint = ui.NewUnitInfo(x+ui.TILE_WIDTH, y, obj)
case game.Permanent:
- p := obj.(game.Permanent)
- hoverHint = ui.NewPermInfo(x+ui.TILE_WIDTH, y, p)
+ hoverHint = ui.NewPermInfo(x+ui.TILE_WIDTH, y, obj)
}
if hoverHint != nil {
diff --git a/go/game/ai.go b/go/game/ai.go
index b1921639..18e58957 100644
--- a/go/game/ai.go
+++ b/go/game/ai.go
@@ -66,9 +66,9 @@ out:
}
// 2.
- if u.AvailMoveActions > 0 {
- // TODO: implement path finding
- }
+ // TODO: implement path finding
+ // if u.AvailMoveActions > 0 {
+ // }
// 3.
if u.AvailAttackActions > 0 {
diff --git a/go/game/card.go b/go/game/card.go
index 9d71cc9c..8576a29e 100644
--- a/go/game/card.go
+++ b/go/game/card.go
@@ -109,11 +109,11 @@ func (card *Card) parseDefinition(definition []byte) {
}
delete(data, "name")
- switch name.(type) {
+ switch nameYml := name.(type) {
case string:
- card.Name = name.(string)
+ card.Name = nameYml
case map[string]interface{}:
- cardName := name.(map[string]interface{})["en"]
+ cardName := nameYml["en"]
card.Name = cardName.(string)
default:
log.Fatalf("Unexpected type %T of yml card field 'Name'\n", name)
diff --git a/go/game/map.go b/go/game/map.go
index ebf59d25..c84dc519 100644
--- a/go/game/map.go
+++ b/go/game/map.go
@@ -214,7 +214,7 @@ func (m *Map) RandomTileFromSymbols(symbols []string) (Position, error) {
for _, symbol := range symbols {
tileString, ok := m.symbols[symbol]
if !ok {
- return INVALID_POSITION(), errors.New(fmt.Sprintf("symbol %s not in the map symbols %v", symbol, m.symbols))
+ return INVALID_POSITION(), fmt.Errorf(fmt.Sprintf("symbol %s not in the map symbols %v", symbol, m.symbols))
}
tileType, ok := TileNames[tileString]
diff --git a/go/game/map_test.go b/go/game/map_test.go
index 8212f648..c8933c69 100644
--- a/go/game/map_test.go
+++ b/go/game/map_test.go
@@ -52,10 +52,13 @@ kings:
}
}
-func TestLoadingAllMaps(T *testing.T) {
+func TestLoadingAllMaps(t *testing.T) {
maps := []string{"the-tyrant", "2P-ring-street", "2P-river-king", "3P-ring-street", "4P-islands", "the-kraken"}
for _, m := range maps {
- LoadMap("../../maps/" + m + ".yml")
+ _, err := LoadMap("../../maps/" + m + ".yml")
+ if err != nil {
+ t.Fatalf("Loading map %s failed with %s", m, err)
+ }
}
}
diff --git a/go/game/permanent.go b/go/game/permanent.go
index e90bd833..ce1d3105 100644
--- a/go/game/permanent.go
+++ b/go/game/permanent.go
@@ -32,7 +32,7 @@ type PermanentBase struct {
func NewPermanentBase(card *Card, tile *Tile, owner *Player) PermanentBase {
if tile.Permanent != nil {
- log.Fatal("Tile at %v already occupied by %v", tile.Position, tile.Permanent)
+ log.Fatalf("Tile at %v already occupied by %v", tile.Position, tile.Permanent)
}
return PermanentBase{card, tile, 0, []Permanent{}, owner, owner}
}
diff --git a/go/game/state.go b/go/game/state.go
index 3d600093..5cab9458 100644
--- a/go/game/state.go
+++ b/go/game/state.go
@@ -58,11 +58,11 @@ func removePtr[P *Unit | *Artifact](collection []P, ptr P) []P {
func (s *State) DestroyPermanent(p Permanent) {
s.removePermanent(p)
- switch p.(type) {
+ switch p := p.(type) {
case *Unit:
- s.Units = removePtr(s.Units, p.(*Unit))
+ s.Units = removePtr(s.Units, p)
case *Artifact:
- s.Artifacts = removePtr(s.Artifacts, p.(*Artifact))
+ s.Artifacts = removePtr(s.Artifacts, p)
}
}
diff --git a/go/server/main.go b/go/server/main.go
index ddc689ef..af051b53 100644
--- a/go/server/main.go
+++ b/go/server/main.go
@@ -9,5 +9,5 @@ import (
var games = []game.State{}
func main() {
- fmt.Println("Started muhq's game server")
+ fmt.Println("Started muhq's game server", games)
}
diff --git a/go/ui/mapView.go b/go/ui/mapView.go
index 43ea0f01..39c92ae4 100644
--- a/go/ui/mapView.go
+++ b/go/ui/mapView.go
@@ -98,7 +98,7 @@ func (vw *MapView) drawMapLayer(screen *ebiten.Image) {
x_px, y_px := 0.0, 0.0
for y := 0; y < len(vw.gameState.Map.Tiles); y++ {
for x := 0; x < len(vw.gameState.Map.Tiles[y]); x++ {
- pos := game.Position{x, y}
+ pos := game.Position{X: x, Y: y}
tile := vw.gameState.Map.TileAt(pos)
tileImg := assets.GetTile(tile.Raw)