Skip to content

Commit

Permalink
cmd/*/verison: Added Version validation on Client and Server
Browse files Browse the repository at this point in the history
Now before everything starts the Client will check that the current local version is the one expected
from the server, if not then it'll block with a  message to download the new version
  • Loading branch information
xescugc committed Mar 22, 2024
1 parent 19dcd51 commit b467d0b
Show file tree
Hide file tree
Showing 27 changed files with 210 additions and 55 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,4 @@ tags
*.wasm

dist/
.env
2 changes: 1 addition & 1 deletion .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ builds:
command: " "
flags:
gcflags: " "
ldflags: "-s -w -X main.defaultHost=https://maze-wars.com"
ldflags: "-s -w -X main.defaultHost=https://maze-wars.com -X main.version={{.Version}}"
env: []
main: ./cmd/client/
goos:
Expand Down
10 changes: 6 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@ wa-copy: ## Copy the 'wasm_exec.js' to execute WebAssembly binary
wasm: wa-copy wa-build ## Runs all the WASM related commands to have the code ready to run

.PHONY: local-goreleaser
local-goreleaser:
./bins/goreleaser release --snapshot --clean
local-goreleaser: ## Generates a local release without publishing it
@./bins/goreleaser release --snapshot --clean
@cat ./dist/metadata.json | jq .version -r | sed -e 's/^/VERSION=/;' > ./docker/.env

.PHONY: release
release:
./bins/goreleaser release --clean
release: ## Makes a full release to GitHub
@./bins/goreleaser release --clean
@cat ./dist/metadata.json | jq .version -r | sed -e 's/^/VERSION=/;' > ./docker/.env
14 changes: 14 additions & 0 deletions action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type Action struct {
GoHome *GoHomePayload `json:"go_home,omitempty"`
ToggleStats *ToggleStatsPayload `json:"toggle_stats,omitempty"`
TPS *TPSPayload `json:"tps,omitempty"`
VersionError *VersionErrorPayload `json:"version_error,omitempty"`

OpenTowerMenu *OpenTowerMenuPayload `json:"open_tower_menu,omitempty"`
CloseTowerMenu *CloseTowerMenuPayload `json:"close_tower_menu,omitempty"`
Expand Down Expand Up @@ -547,3 +548,16 @@ func NewSyncUsers(totalUsers int) *Action {
},
}
}

type VersionErrorPayload struct {
Error string
}

func NewVersionError(err string) *Action {
return &Action{
Type: VersionError,
VersionError: &VersionErrorPayload{
Error: err,
},
}
}
1 change: 1 addition & 0 deletions action/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const (
JoinWaitingRoom
ExitWaitingRoom
ToggleStats
VersionError

// Specific to WS
AddPlayer
Expand Down
60 changes: 32 additions & 28 deletions action/type_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions client/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,29 @@ func (ac *ActionDispatcher) NavigateTo(route string) {
ac.Dispatch(nt)
}

func (ac *ActionDispatcher) CheckVersion() {
httpu, _ := url.Parse(ac.opt.HostURL)
httpu.Path = "/version"
resp, err := http.Post(httpu.String(), "application/json", bytes.NewBuffer([]byte(fmt.Sprintf(`{"version":"%s"}`, ac.opt.Version))))
if err != nil {
ac.Dispatch(action.NewVersionError(err.Error()))
return
}
body := struct {
Error string `json:"error"`
}{}
if resp.StatusCode != http.StatusOK {
err = json.NewDecoder(resp.Body).Decode(&body)
if err != nil {
ac.Dispatch(action.NewVersionError(err.Error()))
return
}
ac.Dispatch(action.NewVersionError(body.Error))
return
}
return
}

func (ac *ActionDispatcher) SignUpSubmit(un string) {
httpu, _ := url.Parse(ac.opt.HostURL)
httpu.Path = "/users"
Expand Down
2 changes: 2 additions & 0 deletions client/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ func New(ctx context.Context, ad *ActionDispatcher, rs *RouterStore, opt Options

actionDispatcher = ad

actionDispatcher.CheckVersion()

err := ebiten.RunGame(rs)
if err != nil {
return fmt.Errorf("failed to RunGame: %w", err)
Expand Down
1 change: 1 addition & 0 deletions client/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ type Options struct {
HostURL string
ScreenW int
ScreenH int
Version string
}
1 change: 0 additions & 1 deletion client/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ func NewRouterStore(d *flux.Dispatcher, su *SignUpStore, ls *LobbyStore, wr *Wai

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

return rs
Expand Down
33 changes: 24 additions & 9 deletions client/sign_up.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,16 @@ type SignUpStore struct {
Store *store.Store
Logger *slog.Logger

ui *ebitenui.UI
inputErrorW *widget.Text
ui *ebitenui.UI
textErrorW *widget.Text
inputW *widget.TextInput
buttonW *widget.Button
}

type SignUpState struct {
Error string

VersionError string
}

func NewSignUpStore(d *flux.Dispatcher, s *store.Store, l *slog.Logger) (*SignUpStore, error) {
Expand Down Expand Up @@ -57,8 +61,14 @@ func (su *SignUpStore) Draw(screen *ebiten.Image) {

sutate := su.GetState().(SignUpState)
if sutate.Error != "" {
su.inputErrorW.GetWidget().Visibility = widget.Visibility_Show
su.inputErrorW.Label = sutate.Error
su.textErrorW.GetWidget().Visibility = widget.Visibility_Show
su.textErrorW.Label = sutate.Error
}
if sutate.VersionError != "" {
su.textErrorW.GetWidget().Visibility = widget.Visibility_Show
su.textErrorW.Label = sutate.VersionError
su.inputW.GetWidget().Disabled = true
su.buttonW.GetWidget().Disabled = true
}
su.ui.Draw(screen)
}
Expand All @@ -77,6 +87,8 @@ func (su *SignUpStore) Reduce(state, a interface{}) interface{} {
switch act.Type {
case action.SignUpError:
sutate.Error = act.SignUpError.Error
case action.VersionError:
sutate.VersionError = act.VersionError.Error
}

return sutate
Expand Down Expand Up @@ -168,7 +180,7 @@ func (su *SignUpStore) buildUI() {
}),
)

inputErrorW := widget.NewText(
textErrorW := widget.NewText(
widget.TextOpts.Text(su.GetState().(SignUpState).Error, cutils.NormalFont, cutils.Red),
widget.TextOpts.Position(widget.TextPositionCenter, widget.TextPositionCenter),
widget.TextOpts.WidgetOpts(
Expand All @@ -193,7 +205,8 @@ func (su *SignUpStore) buildUI() {

// specify the button's text, the font face, and the color
widget.ButtonOpts.Text("Enter", cutils.SmallFont, &widget.ButtonTextColor{
Idle: color.NRGBA{0xdf, 0xf4, 0xff, 0xff},
Idle: color.NRGBA{0xdf, 0xf4, 0xff, 0xff},
Disabled: color.NRGBA{R: 200, G: 200, B: 200, A: 255},
}),

// specify that the button's text needs some padding for correct display
Expand All @@ -211,12 +224,14 @@ func (su *SignUpStore) buildUI() {
)

inputW.Focus(true)
inputErrorW.GetWidget().Visibility = widget.Visibility_Hide
su.inputErrorW = inputErrorW
textErrorW.GetWidget().Visibility = widget.Visibility_Hide
su.textErrorW = textErrorW
su.buttonW = buttonW
su.inputW = inputW

titleInputC.AddChild(titleW)
titleInputC.AddChild(inputW)
titleInputC.AddChild(inputErrorW)
titleInputC.AddChild(textErrorW)
titleInputC.AddChild(buttonW)

rootContainer.AddChild(titleInputC)
Expand Down
6 changes: 4 additions & 2 deletions client/wasm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,20 @@ func main() {

func NewClient() js.Func {
return js.FuncOf(func(this js.Value, args []js.Value) any {
if len(args) != 1 || (args[0].String() == "") {
return fmt.Errorf("requires 1 parameter: host")
if len(args) != 2 || (args[0].String() == "") || (args[1].String() == "") {
return fmt.Errorf("requires 2 parameter: host, version")
}
var (
err error
hostURL = args[0].String()
version = args[1].String()
screenW = 288
screenH = 240
opt = client.Options{
HostURL: hostURL,
ScreenW: screenW,
ScreenH: screenH,
Version: version,
}
)

Expand Down
3 changes: 3 additions & 0 deletions cmd/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ var (
HostURL: hostURL,
ScreenW: screenW,
ScreenH: screenH,
Version: version,
}

d := flux.NewDispatcher()
Expand Down Expand Up @@ -103,6 +104,8 @@ func init() {
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")
clientCmd.Flags().BoolVar(&verbose, "verbose", false, fmt.Sprintf("If all the logs are gonna be printed to %s", logFile))

clientCmd.AddCommand(versionCmd)
}

func main() {
Expand Down
25 changes: 25 additions & 0 deletions cmd/client/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package main

import (
"fmt"

"github.com/spf13/cobra"
)

var (
// version is the value of the current version, this
// is set via -ldflags
version string

versionCmd = &cobra.Command{
Use: "version",
Short: "Prints the current build version",
Run: func(cmd *cobra.Command, args []string) {
if version != "" {
fmt.Printf("The current version is: %s\n", version)
} else {
fmt.Printf("No version defined\n")
}
},
}
)
3 changes: 3 additions & 0 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ var (
err := server.New(ad, ss, server.Options{
Port: viper.GetString("port"),
Verbose: viper.GetBool("verbose"),
Version: version,
})
if err != nil {
return fmt.Errorf("server error: %w", err)
Expand All @@ -65,6 +66,8 @@ func init() {

serverCmd.Flags().Bool("verbose", false, fmt.Sprintf("If all the logs are gonna be printed to %s", logFile))
viper.BindPFlag("verbose", serverCmd.Flags().Lookup("verbose"))

serverCmd.AddCommand(versionCmd)
}

func main() {
Expand Down
25 changes: 25 additions & 0 deletions cmd/server/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package main

import (
"fmt"

"github.com/spf13/cobra"
)

var (
// version is the value of the current version, this
// is set via -ldflags
version string

versionCmd = &cobra.Command{
Use: "version",
Short: "Prints the current build version",
Run: func(cmd *cobra.Command, args []string) {
if version != "" {
fmt.Printf("The current version is: %s\n", version)
} else {
fmt.Printf("No version defined\n")
}
},
}
)
Loading

0 comments on commit b467d0b

Please sign in to comment.