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 +}