Skip to content

Commit

Permalink
docker: Added the docker logic to be able to deploy it
Browse files Browse the repository at this point in the history
Needed to change a lot of things basically to not have any 'ebiten' imports from the server
because then it's really difficult to deploy it as 'ebiten' requires a lot of libs not installed
by default on servers and it's quite obnoxious, so for this reason now there are 2 binaries
and other changes related to that.
  • Loading branch information
xescugc committed Dec 9, 2023
1 parent 4f4c8f5 commit 5b2fded
Show file tree
Hide file tree
Showing 30 changed files with 499 additions and 86 deletions.
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@ build:
test: wasm ## Run the tests
@xvfb-run go test ./...

.PHONY: dc-serve
dc-serve: ## Starts the server using docker-compose
@docker-compose -f docker/docker-compose.yml -f docker/develop.yml up --build --no-deps maze-wars

.PHONY: serve
serve: wasm ## Starts the server
@go run . server
@go run ./cmd/server

.PHONY: wa-build
wa-build: ## Build the wasm Game
Expand Down
3 changes: 2 additions & 1 deletion client/game.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ func (g *Game) Draw(screen *ebiten.Image) {
s := g.Camera.GetState().(CameraState)
op.GeoM.Scale(s.Zoom, s.Zoom)
inverseZoom := maxZoom - s.Zoom + zoomScale
screen.DrawImage(g.Store.Map.GetState().(store.MapState).Image.(*ebiten.Image).SubImage(image.Rect(int(s.X), int(s.Y), int((s.X+s.W)*inverseZoom), int((s.Y+s.H)*inverseZoom))).(*ebiten.Image), op)
mi := ebiten.NewImageFromImage(g.Store.Map.GetState().(store.MapState).Image)
screen.DrawImage(mi.SubImage(image.Rect(int(s.X), int(s.Y), int((s.X+s.W)*inverseZoom), int((s.Y+s.H)*inverseZoom))).(*ebiten.Image), op)

g.Camera.Draw(screen)
g.HUD.Draw(screen)
Expand Down
6 changes: 3 additions & 3 deletions client/hud.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ func (hs *HUDStore) Draw(screen *ebiten.Image) {
if !cp.CanSummonUnit(u.Unit.Type.String()) {
op.ColorM.Scale(2, 0.5, 0.5, 0.9)
}
screen.DrawImage(u.Unit.Faceset.(*ebiten.Image), op)
screen.DrawImage(ebiten.NewImageFromImage(u.Unit.Faceset), op)
}

for _, t := range hst.Towers {
Expand All @@ -304,7 +304,7 @@ func (hs *HUDStore) Draw(screen *ebiten.Image) {
// Once the tower is selected we gray it out
op.ColorM.Scale(0.5, 0.5, 0.5, 0.5)
}
screen.DrawImage(t.Tower.Faceset.(*ebiten.Image), op)
screen.DrawImage(ebiten.NewImageFromImage(t.Tower.Faceset), op)
}

op := &ebiten.DrawImageOptions{}
Expand All @@ -320,7 +320,7 @@ func (hs *HUDStore) Draw(screen *ebiten.Image) {
op.ColorM.Scale(2, 0.5, 0.5, 0.9)
}

screen.DrawImage(hst.SelectedTower.Faceset().(*ebiten.Image), op)
screen.DrawImage(ebiten.NewImageFromImage(hst.SelectedTower.Faceset()), op)
}

psit := hs.game.Store.Players.GetState().(store.PlayersState).IncomeTimer
Expand Down
4 changes: 2 additions & 2 deletions client/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ func NewRouterStore(d *flux.Dispatcher, g *Game, l *LobbyStore) *RouterStore {
}

rs.ReduceStore = flux.NewReduceStore(d, rs.Reduce, RouterState{
Route: LobbyRoute,
//Route: GameRoute,
//Route: LobbyRoute,
Route: GameRoute,
})

return rs
Expand Down
2 changes: 1 addition & 1 deletion client/towers.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (ts *Towers) DrawTower(screen *ebiten.Image, c *CameraStore, t *store.Tower
op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(t.X-cs.X, t.Y-cs.Y)
op.GeoM.Scale(cs.Zoom, cs.Zoom)
screen.DrawImage(t.Faceset().(*ebiten.Image), op)
screen.DrawImage(ebiten.NewImageFromImage(t.Faceset()), op)

if t.ID == hst.TowerOpenMenuID {
op := &ebiten.DrawImageOptions{}
Expand Down
15 changes: 8 additions & 7 deletions client/units.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/xescugc/ltw/assets"
"github.com/xescugc/ltw/store"
"github.com/xescugc/ltw/unit"
"github.com/xescugc/ltw/utils"
)

type Units struct {
Expand All @@ -19,11 +20,11 @@ type Units struct {
}

var (
facingToTile = map[ebiten.Key]int{
ebiten.KeyS: 0,
ebiten.KeyW: 1,
ebiten.KeyA: 2,
ebiten.KeyD: 3,
directionToTile = map[utils.Direction]int{
utils.Down: 0,
utils.Up: 1,
utils.Left: 2,
utils.Right: 3,
}
)

Expand Down Expand Up @@ -97,10 +98,10 @@ func (us *Units) DrawUnit(screen *ebiten.Image, c *CameraStore, u *store.Unit) {
op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(u.X-cs.X, u.Y-cs.Y)
op.GeoM.Scale(cs.Zoom, cs.Zoom)
sx := facingToTile[u.Facing] * int(u.W)
sx := directionToTile[u.Facing] * int(u.W)
i := (u.MovingCount / 5) % 4
sy := i * int(u.H)
screen.DrawImage(u.Sprite().(*ebiten.Image).SubImage(image.Rect(sx, sy, sx+int(u.W), sy+int(u.H))).(*ebiten.Image), op)
screen.DrawImage(ebiten.NewImageFromImage(u.Sprite()).SubImage(image.Rect(sx, sy, sx+int(u.W), sy+int(u.H))).(*ebiten.Image), op)

// Only draw the Health bar if the unit has been hit
h := unit.Units[u.Type].Health
Expand Down
90 changes: 90 additions & 0 deletions cmd/client/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package main

import (
"context"
"fmt"
"os"

"github.com/spf13/cobra"
"github.com/xescugc/go-flux"
"github.com/xescugc/ltw/client"
"github.com/xescugc/ltw/inputer"
"github.com/xescugc/ltw/store"
)

var (
room string
name string
hostURL string
screenW int
screenH int

clientCmd = &cobra.Command{
Use: "client",
RunE: func(cmd *cobra.Command, args []string) error {
var err error

d := flux.NewDispatcher()
ad := client.NewActionDispatcher(d)

s := store.NewStore(d)

g := &client.Game{
Store: s,
}

i := inputer.NewEbiten()

// TODO: Change this to pass the specific store needed instead of all the game object
cs := client.NewCameraStore(d, s, screenW, screenH)
g.Camera = cs
g.Units, err = client.NewUnits(g)
if err != nil {
return fmt.Errorf("failed to initialize Units: %w", err)
}

g.Towers, err = client.NewTowers(g)
if err != nil {
return fmt.Errorf("failed to initialize Towers: %w", err)
}

g.HUD, err = client.NewHUDStore(d, i, g)
if err != nil {
return fmt.Errorf("failed to initialize HUDStore: %w", err)
}

l, err := client.NewLobbyStore(d, i, s, cs)
if err != nil {
return fmt.Errorf("failed to initialize LobbyStore: %w", err)
}
rs := client.NewRouterStore(d, g, l)
ctx := context.Background()

err = client.New(ctx, ad, rs, client.Options{
HostURL: hostURL,
Room: room,
Name: name,
ScreenW: screenW,
ScreenH: screenH,
})

return err
},
}
)

func init() {
clientCmd.Flags().StringVar(&hostURL, "port", "localhost:5555", "The URL of the server")
clientCmd.Flags().StringVar(&room, "room", "room", "The room name to join")
clientCmd.Flags().StringVar(&name, "name", "john doe", "The name of the client")
clientCmd.Flags().IntVar(&screenW, "screenw", 288, "The default width of the screen when not full screen")
clientCmd.Flags().IntVar(&screenH, "screenh", 240, "The default height of the screen when not full screen")
}

func main() {
if err := clientCmd.Execute(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)

}
}
48 changes: 48 additions & 0 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package main

import (
"fmt"
"os"
"strings"

"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/xescugc/go-flux"
"github.com/xescugc/ltw/server"
)

var (
serverCmd = &cobra.Command{
Use: "server",
RunE: func(cmd *cobra.Command, args []string) error {
d := flux.NewDispatcher()
ad := server.NewActionDispatcher(d)
rooms := server.NewRoomsStore(d)

err := server.New(ad, rooms, server.Options{
Port: viper.GetString("port"),
})
if err != nil {
return fmt.Errorf("server error: %w", err)
}

return nil
},
}
)

func init() {
viper.AutomaticEnv()
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))

serverCmd.Flags().String("port", "5555", "The port in which the sever is open")
viper.BindPFlag("port", serverCmd.Flags().Lookup("port"))
}

func main() {
if err := serverCmd.Execute(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)

}
}
15 changes: 15 additions & 0 deletions docker/Dockerfile.maze-wars.dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
FROM golang:1.21 as builder

WORKDIR /app

COPY go.mod .
COPY go.sum .

RUN go mod download && go mod verify

COPY . .

RUN make wasm

RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o maze-wars_app ./cmd/server/
ENTRYPOINT ["/app/maze-wars_app"]
21 changes: 21 additions & 0 deletions docker/Dockerfile.maze-wars.prod
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
FROM golang:1.21 as builder

WORKDIR /app

COPY go.mod .
COPY go.sum .

RUN go mod download && go mod verify

COPY . .

RUN make wasm

RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o maze-wars_app ./cmd/server/
ENTRYPOINT ["/app/maze-wars_app"]

# final stage

FROM scratch
COPY --from=builder /app/maze-wars_app /app/
ENTRYPOINT ["/app/maze-wars_app"]
11 changes: 11 additions & 0 deletions docker/develop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
version: "3"
services:
maze-wars:
build:
context: ..
dockerfile: docker/Dockerfile.maze-wars.dev
ports:
#- '5555:5555'
- '4000:4000'
environment:
- PORT=4000
15 changes: 15 additions & 0 deletions docker/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
version: "3"
services:
maze-wars:
volumes:
- cache:/go
networks:
maze-wars:

volumes:
cache:
attachments:
driver: local

networks:
maze-wars:
21 changes: 21 additions & 0 deletions docker/production.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
version: "3"
services:
nginx-proxy:
image: nginxproxy/nginx-proxy:0.9.1
ports:
- "80:80"
volumes:
- /var/run/docker.sock:/tmp/docker.sock:ro
networks:
maze-wars:

maze-wars:
build:
context: ..
dockerfile: docker/Dockerfile.maze-wars.prod
expose:
- 3000
environment:
- PORT=3000
- VIRTUAL_HOST=maze-wars.com,www.maze-wars.com
restart: on-failure
30 changes: 23 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ module github.com/xescugc/ltw
go 1.21

require (
github.com/davecgh/go-spew v1.1.0
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc
github.com/gofrs/uuid v4.4.0+incompatible
github.com/golang/mock v1.6.0
github.com/gorilla/handlers v1.5.2
github.com/gorilla/mux v1.8.1
github.com/hajimehoshi/ebiten/v2 v2.5.9
github.com/spf13/cobra v1.8.0
github.com/stretchr/testify v1.7.0
github.com/spf13/viper v1.18.1
github.com/stretchr/testify v1.8.4
github.com/xescugc/go-flux v1.0.1
golang.org/x/image v0.10.0
nhooyr.io/websocket v1.8.10
Expand All @@ -19,15 +20,30 @@ require (
require (
github.com/ebitengine/purego v0.4.0 // indirect
github.com/felixge/httpsnoop v1.0.3 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20221017161538-93cebf72946b // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jezek/xgb v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/exp v0.0.0-20190731235908-ec7cb31e5a56 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/exp/shiny v0.0.0-20231206192017-f3f8817b8deb // indirect
golang.org/x/mobile v0.0.0-20230301163155-e0f57694e12c // indirect
golang.org/x/sync v0.3.0 // indirect
golang.org/x/sys v0.11.0 // indirect
golang.org/x/text v0.11.0 // indirect
golang.org/x/sync v0.5.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit 5b2fded

Please sign in to comment.