// 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()) }
// ProcessEvent counts the event for the return value of the counter func // and emits this value. func (b *counterBehavior) ProcessEvent(event cells.Event) error { switch event.Topic() { case cells.CountersTopic: response := b.copyCounters() if err := event.Respond(response); err != nil { return err } case cells.ResetTopic: b.counters = make(map[string]int64) default: cids := b.counterFunc(b.ctx.ID(), event) if cids != nil { for _, cid := range cids { v, ok := b.counters[cid] if ok { b.counters[cid] = v + 1 } else { b.counters[cid] = 1 } topic := "counter:" + cid b.ctx.EmitNew(topic, b.counters[cid], event.Scene()) } } } return nil }
// ProcessEvent executes the state function and stores // the returned new state. func (b *fsmBehavior) ProcessEvent(event cells.Event) error { switch event.Topic() { case cells.StatusTopic: status := FSMStatus{ Done: b.done, Error: b.err, } if err := event.Respond(status); err != nil { return err } default: if b.done { return nil } state, err := b.state(b.ctx, event) if err != nil { b.done = true b.err = err } else if state == nil { b.done = true } b.state = state } return nil }
func (b *restorerBehavior) ProcessEvent(event cells.Event) error { switch event.Topic() { case "grab!": cents := b.cents b.cents = 0 return event.Respond(cents) case "drop!": b.cents += payloadCents(event) } return nil }
// 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()) }
// ProcessEvent collects and re-emits events. func (b *collectorBehavior) ProcessEvent(event cells.Event) error { switch event.Topic() { case cells.CollectedTopic: response := make([]EventData, len(b.collected)) copy(response, b.collected) if err := event.Respond(response); err != nil { return err } case cells.ResetTopic: b.collected = []EventData{} default: b.collected = append(b.collected, newEventData(event)) if len(b.collected) > b.max { b.collected = b.collected[1:] } b.ctx.Emit(event) } return nil }
func (c *censor) ProcessEvent(event cells.Event) (err error) { switch event.Topic() { case CENSOR: ok := c.filter(event) userID, _ := event.Payload().Get("from") event.Respond(ok) if _, ok := c.warnings[userID.(string)]; !ok { c.warnings[userID.(string)] = 0 } if !ok { return c.ctx.EmitNew(SAYS_TO, cells.PayloadValues{ "message": fmt.Sprintf("You can't say that! You've been warned %d times.", c.warnings[userID.(string)]), "from": c.id, "to": userID.(string), }, nil) } } return }
func (b *building) ProcessEvent(event cells.Event) (err error) { switch event.Topic() { case ROOM_ADDED: room, _ := event.Payload().Get("room") b.rooms[room.(string)] = true log.Infof("added %v", room) case LIST_ROOMS: rooms := []string{} for room := range b.rooms { rooms = append(rooms, room) } event.Respond(rooms) case SAYS_ALL: user, ok := event.Payload().Get("from") if !ok || user.(string) != PublicAddressUserID { return fmt.Errorf("invalid public address request") } b.ctx.Emit(event) } return }
func (r *room) ProcessEvent(event cells.Event) (err error) { switch event.Topic() { case SAYS_ALL, SAYS_TO: user, _ := event.Payload().Get("from") switch user.(string) { case PublicAddressUserID, r.censorID: r.ctx.Emit(event) default: env := r.ctx.Environment() ok, err := env.Request(r.censorID, CENSOR, event.Payload(), nil, time.Second) if err != nil { log.Error(err) return err } if ok.(bool) { r.ctx.Emit(event) } } case IN_ROOM: userID, _ := event.Payload().Get("user") ok, _ := r.users[userID.(string)] event.Respond(ok) case USER_ADDED: userID, _ := event.Payload().Get("user") if ok, _ := r.users[userID.(string)]; !ok { r.users[userID.(string)] = true r.ctx.Environment().EmitNew(r.ctx.ID(), SAYS_ALL, cells.PayloadValues{ "message": fmt.Sprintf("%s has entered the room", userID), "from": PublicAddressUserID, }, nil) } case USER_COUNT: event.Respond(len(r.users)) } return }
func (b *collectBehavior) ProcessEvent(event cells.Event) error { switch event.Topic() { case cells.ProcessedTopic: processed := make([]string, len(b.processed)) copy(processed, b.processed) err := event.Respond(processed) if err != nil { return err } case cells.ResetTopic: b.processed = []string{} case cells.PingTopic: err := event.Respond(cells.PongResponse) if err != nil { return err } case iterateTopic: err := b.ctx.SubscribersDo(func(s cells.Subscriber) error { return s.ProcessNewEvent("love", b.ctx.ID()+" loves "+s.ID(), event.Scene()) }) if err != nil { return err } case panicTopic: panic("Ouch!") case subscribersTopic: var ids []string b.ctx.SubscribersDo(func(s cells.Subscriber) error { ids = append(ids, s.ID()) return nil }) err := event.Respond(ids) if err != nil { return err } default: b.processed = append(b.processed, fmt.Sprintf("%v", event)) return b.ctx.Emit(event) } return nil }