From a7b469b5238e91d58f85f7b8e1571a38c894594f Mon Sep 17 00:00:00 2001 From: liwei1dao Date: Thu, 24 Nov 2022 11:06:10 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E6=88=98=E6=96=97=E6=A8=A1?= =?UTF-8?q?=E5=9D=97=E8=BF=9C=E7=A8=8B=E8=BF=9E=E6=8E=A5=E5=AF=B9=E8=B1=A1?= =?UTF-8?q?=E5=B0=81=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- modules/battle/client.go | 40 +++++++++++++++++++++++++++++++ modules/battle/module_test.go | 13 +++++++++- modules/battle/options.go | 45 +++++++++++++++++++++++++++++++++++ 3 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 modules/battle/client.go create mode 100644 modules/battle/options.go diff --git a/modules/battle/client.go b/modules/battle/client.go new file mode 100644 index 000000000..c33801b19 --- /dev/null +++ b/modules/battle/client.go @@ -0,0 +1,40 @@ +package battle + +import ( + "go_dreamfactory/modules" + + "go_dreamfactory/lego/core" + + "github.com/gorilla/websocket" +) + +/* +战斗服务客户端组件 +*/ +type clientComp struct { + modules.MCompGate + options *Options + service core.IService + module *Battle + conn *websocket.Conn +} + +//组件初始化接口 +func (this *clientComp) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) { + this.MCompGate.Init(service, module, comp, options) + this.options = options.(*Options) + this.module = module.(*Battle) + return +} + +func (this *clientComp) Start() (err error) { + err = this.MCompGate.Start() + dialer := websocket.Dialer{} + this.conn, _, err = dialer.Dial(this.options.BattleServerAddr, nil) + return +} + +//校验战斗过程 +func (this *clientComp) CheckBattle() { + +} diff --git a/modules/battle/module_test.go b/modules/battle/module_test.go index f56773078..c7242c0fc 100644 --- a/modules/battle/module_test.go +++ b/modules/battle/module_test.go @@ -18,6 +18,8 @@ import ( "os" "testing" "time" + + "github.com/gorilla/websocket" ) func newService(ops ...rpcx.Option) core.IService { @@ -86,4 +88,13 @@ func testdefer(data *[]int) { *data = append(*data, 0) } -// 3 +func Test_Comment(t *testing.T) { + dialer := websocket.Dialer{} + if connect, _, err := dialer.Dial("ws://127.0.0.1:9898", nil); err != nil { + fmt.Println(err) + return + } else { + defer connect.Close() + connect.WriteMessage(websocket.TextMessage, []byte("from client to server")) + } +} diff --git a/modules/battle/options.go b/modules/battle/options.go new file mode 100644 index 000000000..334bc85cd --- /dev/null +++ b/modules/battle/options.go @@ -0,0 +1,45 @@ +package battle + +import ( + "errors" + "go_dreamfactory/lego/sys/log" + "go_dreamfactory/lego/utils/mapstructure" + "go_dreamfactory/modules" +) + +type ( + IOptions interface { + modules.IOptions + } + Options struct { + modules.Options + BattleServerAddr string + } +) + +func (this *Options) GetDebug() bool { + return this.Debug +} + +func (this *Options) GetLog() log.ILogger { + return this.Log +} + +func (this *Options) GetBattleServerAddr() string { + return this.BattleServerAddr +} + +func (this *Options) LoadConfig(settings map[string]interface{}) (err error) { + if settings != nil { + if err = mapstructure.Decode(settings, &this.Options); err != nil { + return + } + if err = mapstructure.Decode(settings, this); err != nil { + return + } + } + if this.Log = log.NewTurnlog(true, log.Clone("", 4)); this.Log == nil { + err = errors.New("log is nil") + } + return +}