From c093b69ff9feb4183921389e1d2701068d2b1e5f Mon Sep 17 00:00:00 2001 From: Shingo INADA Date: Thu, 17 Jun 2021 02:13:59 +0900 Subject: [PATCH 1/2] support gdxsv- prefixed flycast version --- gdxsv/lbs_handler.go | 46 ++++++++++++++++++------------- gdxsv/lbs_handler_test.go | 57 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 19 deletions(-) create mode 100644 gdxsv/lbs_handler_test.go diff --git a/gdxsv/lbs_handler.go b/gdxsv/lbs_handler.go index 2f8fb18..661f896 100644 --- a/gdxsv/lbs_handler.go +++ b/gdxsv/lbs_handler.go @@ -8,6 +8,7 @@ import ( "encoding/json" "fmt" "go.uber.org/zap" + "golang.org/x/mod/semver" "hash/fnv" "io/ioutil" "math" @@ -15,7 +16,6 @@ import ( "strconv" "strings" - "golang.org/x/mod/semver" "golang.org/x/text/encoding/japanese" "golang.org/x/text/transform" "golang.org/x/text/width" @@ -251,6 +251,17 @@ func sendUserList(p *LbsPeer) { p.SendMessage(n) } +func isOldFlycastVersion(userVersion string) bool { + if strings.HasPrefix(userVersion, "gdxsv-") { + // New version-string have the prefix. + userVersion = "v" + strings.TrimPrefix(userVersion, "gdxsv-") + } + if semver.Compare(userVersion, requiredFlycastVersion) < 0 { + return true + } + return false +} + var _ = register(lbsLoginType, func(p *LbsPeer, m *LbsMessage) { loginType := m.Reader().Read8() @@ -274,13 +285,12 @@ var _ = register(lbsLoginType, func(p *LbsPeer, m *LbsMessage) { switch loginType { case 0: - if p.PlatformInfo["flycast"] != "" { - if semver.Compare(p.PlatformInfo["flycast"], requiredFlycastVersion) < 0 { - p.SendMessage(NewServerNotice(lbsShutDown).Writer(). - WriteString("
PLEASE UPDATE Flycast").Msg()) - return - } + if p.PlatformInfo["flycast"] != "" && isOldFlycastVersion(p.PlatformInfo["flycast"]) { + p.SendMessage(NewServerNotice(lbsShutDown).Writer(). + WriteString("
PLEASE UPDATE Flycast").Msg()) + return } + if p.LoginKey != "" { // Check login key is banned if p.app.IsBannedAccount(p.LoginKey) { @@ -313,12 +323,10 @@ var _ = register(lbsLoginType, func(p *LbsPeer, m *LbsMessage) { p.SendMessage(NewServerNotice(lbsShutDown).Writer(). WriteString("
UNSUPPORTED LOGIN TYPE").Msg()) case 2: - if p.PlatformInfo["flycast"] != "" { - if semver.Compare(p.PlatformInfo["flycast"], requiredFlycastVersion) < 0 { - p.SendMessage(NewServerNotice(lbsShutDown).Writer(). - WriteString("
PLEASE UPDATE Flycast").Msg()) - return - } + if p.PlatformInfo["flycast"] != "" && isOldFlycastVersion(p.PlatformInfo["flycast"]) { + p.SendMessage(NewServerNotice(lbsShutDown).Writer(). + WriteString("
PLEASE UPDATE Flycast").Msg()) + return } // Go to account registration flow. @@ -1266,9 +1274,9 @@ var _ = register(lbsPostChatMessage, func(p *LbsPeer, m *LbsMessage) { WriteString(p.UserID). WriteString(p.Name). WriteString(text). - Write8(0). // chat_type - Write8(0). // id color - Write8(0). // handle color + Write8(0). // chat_type + Write8(0). // id color + Write8(0). // handle color Write8(0).Msg() // msg color // broadcast chat message to users in the same place. @@ -1295,9 +1303,9 @@ var _ = register(lbsPostChatMessage, func(p *LbsPeer, m *LbsMessage) { WriteString(""). WriteString(""). WriteString(hint). - Write8(0). // chat_type - Write8(0). // id color - Write8(0). // handle color + Write8(0). // chat_type + Write8(0). // id color + Write8(0). // handle color Write8(0).Msg() // msg color } diff --git a/gdxsv/lbs_handler_test.go b/gdxsv/lbs_handler_test.go new file mode 100644 index 0000000..cdadd01 --- /dev/null +++ b/gdxsv/lbs_handler_test.go @@ -0,0 +1,57 @@ +package main + +import "testing" + +func Test_isOldFlycastVersion(t *testing.T) { + type args struct { + userVersion string + } + tests := []struct { + name string + requiredVersion string + args args + want bool + }{ + { + name: "same version ok", + requiredVersion: "v0.7.0", + args: args{userVersion: "v0.7.0"}, + want: false, + }, + { + name: "old version ng", + requiredVersion: "v0.7.0", + args: args{userVersion: "v0.6.9"}, + want: true, + }, + { + name: "new version ok", + requiredVersion: "v0.7.0", + args: args{userVersion: "v9.9.9"}, + want: false, + }, + { + name: "gdxsv prefix version ok", + requiredVersion: "v0.7.0", + args: args{userVersion: "gdxsv-0.7.0"}, + want: false, + }, + { + name: "gdxsv prefix version ng", + requiredVersion: "v0.7.0", + args: args{userVersion: "gdxsv-0.6.9"}, + want: true, + }, + } + + backup := requiredFlycastVersion + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + requiredFlycastVersion = tt.requiredVersion + if got := isOldFlycastVersion(tt.args.userVersion); got != tt.want { + t.Errorf("isOldFlycastVersion() = %v, want %v", got, tt.want) + } + }) + } + requiredFlycastVersion = backup +} From 439981c0eed4d69e29f0f3ceec7a24e124279f12 Mon Sep 17 00:00:00 2001 From: Shingo INADA Date: Thu, 17 Jun 2021 02:16:17 +0900 Subject: [PATCH 2/2] go fmt --- gdxsv/lbs_handler.go | 19 +++++++++---------- gdxsv/lbs_handler_test.go | 36 ++++++++++++++++++------------------ 2 files changed, 27 insertions(+), 28 deletions(-) diff --git a/gdxsv/lbs_handler.go b/gdxsv/lbs_handler.go index 661f896..de0165f 100644 --- a/gdxsv/lbs_handler.go +++ b/gdxsv/lbs_handler.go @@ -9,16 +9,15 @@ import ( "fmt" "go.uber.org/zap" "golang.org/x/mod/semver" + "golang.org/x/text/encoding/japanese" + "golang.org/x/text/transform" + "golang.org/x/text/width" "hash/fnv" "io/ioutil" "math" "sort" "strconv" "strings" - - "golang.org/x/text/encoding/japanese" - "golang.org/x/text/transform" - "golang.org/x/text/width" ) type LbsHandler func(*LbsPeer, *LbsMessage) @@ -1274,9 +1273,9 @@ var _ = register(lbsPostChatMessage, func(p *LbsPeer, m *LbsMessage) { WriteString(p.UserID). WriteString(p.Name). WriteString(text). - Write8(0). // chat_type - Write8(0). // id color - Write8(0). // handle color + Write8(0). // chat_type + Write8(0). // id color + Write8(0). // handle color Write8(0).Msg() // msg color // broadcast chat message to users in the same place. @@ -1303,9 +1302,9 @@ var _ = register(lbsPostChatMessage, func(p *LbsPeer, m *LbsMessage) { WriteString(""). WriteString(""). WriteString(hint). - Write8(0). // chat_type - Write8(0). // id color - Write8(0). // handle color + Write8(0). // chat_type + Write8(0). // id color + Write8(0). // handle color Write8(0).Msg() // msg color } diff --git a/gdxsv/lbs_handler_test.go b/gdxsv/lbs_handler_test.go index cdadd01..253acdf 100644 --- a/gdxsv/lbs_handler_test.go +++ b/gdxsv/lbs_handler_test.go @@ -7,40 +7,40 @@ func Test_isOldFlycastVersion(t *testing.T) { userVersion string } tests := []struct { - name string + name string requiredVersion string - args args - want bool + args args + want bool }{ { - name: "same version ok", + name: "same version ok", requiredVersion: "v0.7.0", - args: args{userVersion: "v0.7.0"}, - want: false, + args: args{userVersion: "v0.7.0"}, + want: false, }, { - name: "old version ng", + name: "old version ng", requiredVersion: "v0.7.0", - args: args{userVersion: "v0.6.9"}, - want: true, + args: args{userVersion: "v0.6.9"}, + want: true, }, { - name: "new version ok", + name: "new version ok", requiredVersion: "v0.7.0", - args: args{userVersion: "v9.9.9"}, - want: false, + args: args{userVersion: "v9.9.9"}, + want: false, }, { - name: "gdxsv prefix version ok", + name: "gdxsv prefix version ok", requiredVersion: "v0.7.0", - args: args{userVersion: "gdxsv-0.7.0"}, - want: false, + args: args{userVersion: "gdxsv-0.7.0"}, + want: false, }, { - name: "gdxsv prefix version ng", + name: "gdxsv prefix version ng", requiredVersion: "v0.7.0", - args: args{userVersion: "gdxsv-0.6.9"}, - want: true, + args: args{userVersion: "gdxsv-0.6.9"}, + want: true, }, }