interact: pull out the interaction related code to the caller

This commit is contained in:
Andy Cheng 2022-03-23 12:05:35 +08:00
parent e122c12eef
commit 0974b1c7fd
No known key found for this signature in database
GPG Key ID: 936427CF651A9D28

View File

@ -61,23 +61,18 @@ func NewCoreInteraction(environment *Environment, trader *Trader) *CoreInteracti
} }
} }
func (it *CoreInteraction) AddSupportedStrategyButtons(reply *interact.Reply, checkInterface interface{}) bool { func (it *CoreInteraction) AddSupportedStrategyButtons(checkInterface interface{}) ([][3]string, bool) {
found := false found := false
var buttonsForm [][3]string
rt := reflect.TypeOf(checkInterface).Elem() rt := reflect.TypeOf(checkInterface).Elem()
for signature, strategy := range it.exchangeStrategies { for signature, strategy := range it.exchangeStrategies {
if ok := reflect.TypeOf(strategy).Implements(rt); ok { if ok := reflect.TypeOf(strategy).Implements(rt); ok {
(*reply).AddButton(signature, "strategy", signature) buttonsForm = append(buttonsForm, [3]string{signature, "strategy", signature})
found = true found = true
} }
} }
if found { return buttonsForm, found
(*reply).Message("Please choose one strategy")
} else {
(*reply).Message(fmt.Sprintf("No any strategy supports %s", rt.Name()))
}
return found
} }
func (it *CoreInteraction) Commands(i *interact.Interact) { func (it *CoreInteraction) Commands(i *interact.Interact) {
@ -127,7 +122,12 @@ func (it *CoreInteraction) Commands(i *interact.Interact) {
i.PrivateCommand("/position", "Show Position", func(reply interact.Reply) error { i.PrivateCommand("/position", "Show Position", func(reply interact.Reply) error {
// it.trader.exchangeStrategies // it.trader.exchangeStrategies
// send symbol options // send symbol options
_ = it.AddSupportedStrategyButtons(&reply, (*PositionReader)(nil)) if buttonsForm, found := it.AddSupportedStrategyButtons((*PositionReader)(nil)); found {
reply.AddMultipleButtons(buttonsForm)
reply.Message("Please choose one strategy")
} else {
reply.Message("No any strategy supports PositionReader")
}
return nil return nil
}).Cycle(func(signature string, reply interact.Reply) error { }).Cycle(func(signature string, reply interact.Reply) error {
strategy, ok := it.exchangeStrategies[signature] strategy, ok := it.exchangeStrategies[signature]
@ -163,7 +163,12 @@ func (it *CoreInteraction) Commands(i *interact.Interact) {
i.PrivateCommand("/closeposition", "Close position", func(reply interact.Reply) error { i.PrivateCommand("/closeposition", "Close position", func(reply interact.Reply) error {
// it.trader.exchangeStrategies // it.trader.exchangeStrategies
// send symbol options // send symbol options
_ = it.AddSupportedStrategyButtons(&reply, (*PositionCloser)(nil)) if buttonsForm, found := it.AddSupportedStrategyButtons((*PositionCloser)(nil)); found {
reply.AddMultipleButtons(buttonsForm)
reply.Message("Please choose one strategy")
} else {
reply.Message("No any strategy supports PositionCloser")
}
return nil return nil
}).Next(func(signature string, reply interact.Reply) error { }).Next(func(signature string, reply interact.Reply) error {
strategy, ok := it.exchangeStrategies[signature] strategy, ok := it.exchangeStrategies[signature]
@ -227,7 +232,12 @@ func (it *CoreInteraction) Commands(i *interact.Interact) {
i.PrivateCommand("/status", "Strategy Status", func(reply interact.Reply) error { i.PrivateCommand("/status", "Strategy Status", func(reply interact.Reply) error {
// it.trader.exchangeStrategies // it.trader.exchangeStrategies
// send symbol options // send symbol options
_ = it.AddSupportedStrategyButtons(&reply, (*StrategyStatusProvider)(nil)) if buttonsForm, found := it.AddSupportedStrategyButtons((*StrategyStatusProvider)(nil)); found {
reply.AddMultipleButtons(buttonsForm)
reply.Message("Please choose one strategy")
} else {
reply.Message("No any strategy supports StrategyStatusProvider")
}
return nil return nil
}).Cycle(func(signature string, reply interact.Reply) error { }).Cycle(func(signature string, reply interact.Reply) error {
strategy, ok := it.exchangeStrategies[signature] strategy, ok := it.exchangeStrategies[signature]
@ -260,7 +270,12 @@ func (it *CoreInteraction) Commands(i *interact.Interact) {
i.PrivateCommand("/suspend", "Suspend Strategy", func(reply interact.Reply) error { i.PrivateCommand("/suspend", "Suspend Strategy", func(reply interact.Reply) error {
// it.trader.exchangeStrategies // it.trader.exchangeStrategies
// send symbol options // send symbol options
_ = it.AddSupportedStrategyButtons(&reply, (*StrategyController)(nil)) if buttonsForm, found := it.AddSupportedStrategyButtons((*StrategyController)(nil)); found {
reply.AddMultipleButtons(buttonsForm)
reply.Message("Please choose one strategy")
} else {
reply.Message("No any strategy supports StrategyController")
}
return nil return nil
}).Cycle(func(signature string, reply interact.Reply) error { }).Cycle(func(signature string, reply interact.Reply) error {
strategy, ok := it.exchangeStrategies[signature] strategy, ok := it.exchangeStrategies[signature]
@ -300,7 +315,12 @@ func (it *CoreInteraction) Commands(i *interact.Interact) {
i.PrivateCommand("/resume", "Resume Strategy", func(reply interact.Reply) error { i.PrivateCommand("/resume", "Resume Strategy", func(reply interact.Reply) error {
// it.trader.exchangeStrategies // it.trader.exchangeStrategies
// send symbol options // send symbol options
_ = it.AddSupportedStrategyButtons(&reply, (*StrategyController)(nil)) if buttonsForm, found := it.AddSupportedStrategyButtons((*StrategyController)(nil)); found {
reply.AddMultipleButtons(buttonsForm)
reply.Message("Please choose one strategy")
} else {
reply.Message("No any strategy supports StrategyController")
}
return nil return nil
}).Cycle(func(signature string, reply interact.Reply) error { }).Cycle(func(signature string, reply interact.Reply) error {
strategy, ok := it.exchangeStrategies[signature] strategy, ok := it.exchangeStrategies[signature]
@ -340,7 +360,12 @@ func (it *CoreInteraction) Commands(i *interact.Interact) {
i.PrivateCommand("/emergencystop", "Emergency Stop", func(reply interact.Reply) error { i.PrivateCommand("/emergencystop", "Emergency Stop", func(reply interact.Reply) error {
// it.trader.exchangeStrategies // it.trader.exchangeStrategies
// send symbol options // send symbol options
_ = it.AddSupportedStrategyButtons(&reply, (*EmergencyStopper)(nil)) if buttonsForm, found := it.AddSupportedStrategyButtons((*EmergencyStopper)(nil)); found {
reply.AddMultipleButtons(buttonsForm)
reply.Message("Please choose one strategy")
} else {
reply.Message("No any strategy supports EmergencyStopper")
}
return nil return nil
}).Cycle(func(signature string, reply interact.Reply) error { }).Cycle(func(signature string, reply interact.Reply) error {
strategy, ok := it.exchangeStrategies[signature] strategy, ok := it.exchangeStrategies[signature]