From 1c7d4d09cf9d3d7cc789374f324fd272b98eb6e6 Mon Sep 17 00:00:00 2001 From: c9s Date: Sat, 15 Jan 2022 03:06:36 +0800 Subject: [PATCH] interact: add Cycle state builder --- pkg/bbgo/interact.go | 2 +- pkg/interact/command.go | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/pkg/bbgo/interact.go b/pkg/bbgo/interact.go index a2a56de59..c44c21eaa 100644 --- a/pkg/bbgo/interact.go +++ b/pkg/bbgo/interact.go @@ -60,7 +60,7 @@ func (it *CoreInteraction) Commands(i *interact.Interact) { reply.Message("No any strategy supports PositionReader") } return nil - }).Next(func(signature string, reply interact.Reply) error { + }).Cycle(func(signature string, reply interact.Reply) error { strategy, ok := it.exchangeStrategies[signature] if !ok { reply.Message("Strategy not found") diff --git a/pkg/interact/command.go b/pkg/interact/command.go index 08b11223a..9155730aa 100644 --- a/pkg/interact/command.go +++ b/pkg/interact/command.go @@ -54,6 +54,21 @@ func (c *Command) NamedNext(n State, f interface{}) *Command { return c } +func (c *Command) Cycle(f interface{}) *Command { + var curState State + if c.lastState == "" { + curState = State(c.Name + "_" + strconv.Itoa(c.stateID)) + } else { + curState = c.lastState + } + + nextState := curState + c.states[curState] = nextState + c.statesFunc[curState] = f + c.lastState = nextState + return c +} + // Next defines the next state with the transition function from the last defined state. func (c *Command) Next(f interface{}) *Command { var curState State