// Locked represents the locked state receiving coins. func (m *lockMachine) Locked(ctx cells.Context, event cells.Event) (behaviors.FSMState, error) { switch event.Topic() { case "cents?": return m.Locked, event.Respond(m.cents) case "info?": info := fmt.Sprintf("state 'locked' with %d cents", m.cents) return m.Locked, event.Respond(info) case "coin!": cents := payloadCents(event) if cents < 1 { return nil, fmt.Errorf("do not insert buttons") } m.cents += cents if m.cents > 100 { m.cents -= 100 return m.Unlocked, nil } return m.Locked, nil case "button-press!": if m.cents > 0 { ctx.Environment().EmitNew("restorer", "drop!", m.cents, event.Scene()) m.cents = 0 } return m.Locked, nil case "screwdriver!": // Allow a screwdriver to bring the lock into an undefined state. return nil, nil } return m.Locked, fmt.Errorf("illegal topic in state 'locked': %s", event.Topic()) }
// Unlocked represents the unlocked state receiving coins. func (m *lockMachine) Unlocked(ctx cells.Context, event cells.Event) (behaviors.FSMState, error) { switch event.Topic() { case "cents?": return m.Unlocked, event.Respond(m.cents) case "info?": info := fmt.Sprintf("state 'unlocked' with %d cents", m.cents) return m.Unlocked, event.Respond(info) case "coin!": cents := payloadCents(event) ctx.EmitNew("return", cents, event.Scene()) return m.Unlocked, nil case "button-press!": ctx.Environment().EmitNew("restorer", "drop!", m.cents, event.Scene()) m.cents = 0 return m.Locked, nil } return m.Unlocked, fmt.Errorf("illegal topic in state 'unlocked': %s", event.Topic()) }