| 1 | package chat |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "strings" |
| 6 | |
| 7 | "amadan.net/oficina/equipo/internal/wire" |
| 8 | "gorm.io/gorm" |
| 9 | ) |
| 10 | |
| 11 | // The coordinator moves in on its own: when the runner reports an empty |
| 12 | // desk it asks for a move_in turn (look around, take notes, introduce |
| 13 | // itself once); when the reachable namespaces change, for an explore turn |
| 14 | // (the difference only). Both run like person turns, one at a time, and |
| 15 | // give way to the person's messages. |
| 16 | |
| 17 | func coordinatorView(c coordinatorRecord) wire.Coordinator { |
| 18 | return wire.Coordinator{ID: c.Person, Revision: c.Revision, State: c.State, TurnID: c.TurnID, MovedInAt: c.MovedInAt} |
| 19 | } |
| 20 | |
| 21 | // coordinator reads the person's coordinator; one that never moved in is |
| 22 | // new at revision 0. |
| 23 | func (t *txn) coordinator(person string) (coordinatorRecord, error) { |
| 24 | var c coordinatorRecord |
| 25 | err := t.db.Where("person = ?", person).First(&c).Error |
| 26 | if errors.Is(err, gorm.ErrRecordNotFound) { |
| 27 | return coordinatorRecord{Person: person, State: wire.CoordinatorNew}, nil |
| 28 | } |
| 29 | return c, err |
| 30 | } |
| 31 | |
| 32 | // setCoordinator records a state change and its event. |
| 33 | func (t *txn) setCoordinator(person, state, turn string) error { |
| 34 | c, err := t.coordinator(person) |
| 35 | if err != nil { |
| 36 | return err |
| 37 | } |
| 38 | fresh := c.Revision == 0 |
| 39 | c.State, c.TurnID, c.Revision, c.UpdatedAt = state, turn, c.Revision+1, t.now() |
| 40 | if state == wire.CoordinatorReady { |
| 41 | now := t.now() |
| 42 | c.MovedInAt = &now |
| 43 | } |
| 44 | if fresh { |
| 45 | err = t.db.Create(&c).Error |
| 46 | } else { |
| 47 | err = t.db.Model(&coordinatorRecord{}).Where("person = ?", person).Select("state", "turn_id", "revision", "moved_in_at", "updated_at").Updates(&c).Error |
| 48 | } |
| 49 | if err != nil { |
| 50 | return err |
| 51 | } |
| 52 | return t.event("", person, wire.EventCoordinator, "coordinator", person, c.Revision, turn, "", 0, coordinatorView(c)) |
| 53 | } |
| 54 | |
| 55 | // moveIn starts a move_in or explore turn for the runner's own person, |
| 56 | // unless one is already queued or running. It goes after any person |
| 57 | // turn: dispatchNext takes the person's turns first. |
| 58 | func (t *txn) moveIn(l Lease, v *wire.MoveIn) error { |
| 59 | person := l.Runner.Person |
| 60 | if v.Person != person || v.Kind != wire.TurnMoveIn && v.Kind != wire.TurnExplore { |
| 61 | return nil |
| 62 | } |
| 63 | primary, err := t.s.authority.PrimaryRunner(t.db, person) |
| 64 | if err != nil || primary != l.Runner.RunnerID { |
| 65 | return err |
| 66 | } |
| 67 | var n int64 |
| 68 | if err := t.db.Model(&turnRecord{}).Where("person = ? AND state IN ? AND coalesce(kind, '') <> ''", person, wire.InFlight).Count(&n).Error; err != nil || n > 0 { |
| 69 | return err |
| 70 | } |
| 71 | // The coordinator state here is the authority on introductions: one |
| 72 | // that has moved in may look around again, never introduce itself |
| 73 | // again (a runner that lost its desk record asks for a move-in). |
| 74 | kind := v.Kind |
| 75 | c, err := t.coordinator(person) |
| 76 | if err != nil { |
| 77 | return err |
| 78 | } |
| 79 | if kind == wire.TurnMoveIn && c.State == wire.CoordinatorReady { |
| 80 | kind = wire.TurnExplore |
| 81 | } |
| 82 | return t.queueKind(person, kind) |
| 83 | } |
| 84 | |
| 85 | func (t *txn) queueKind(person, kind string) error { |
| 86 | now := t.now() |
| 87 | r := turnRecord{ID: newID("t_"), Person: person, State: wire.TurnQueued, Attempt: 1, Kind: kind, CreatedAt: now, UpdatedAt: now} |
| 88 | if err := t.db.Create(&r).Error; err != nil { |
| 89 | return err |
| 90 | } |
| 91 | if err := t.saveTurn(&r); err != nil { |
| 92 | return err |
| 93 | } |
| 94 | if kind == wire.TurnMoveIn { |
| 95 | if err := t.setCoordinator(person, wire.CoordinatorMovingIn, r.ID); err != nil { |
| 96 | return err |
| 97 | } |
| 98 | } |
| 99 | return t.dispatchNext(person, wire.LaneCoordinator) |
| 100 | } |
| 101 | |
| 102 | // kindEnded follows a move-in or explore turn that ended without a reply: |
| 103 | // one that gave way to the person continues in a new turn; a move-in that |
| 104 | // failed or was cancelled leaves the coordinator new, so the runner asks |
| 105 | // again later. Call before dispatchNext. |
| 106 | func (t *txn) kindEnded(r turnRecord) error { |
| 107 | // A specialist_added turn is once per card: it is never queued again. |
| 108 | if r.Kind == wire.TurnPerson || r.Kind == wire.TurnSpecialistAdded { |
| 109 | return nil |
| 110 | } |
| 111 | if r.Reason == wire.ReasonYielded { |
| 112 | return t.queueKind(r.Person, r.Kind) |
| 113 | } |
| 114 | if r.Kind == wire.TurnMoveIn { |
| 115 | return t.setCoordinator(r.Person, wire.CoordinatorNew, "") |
| 116 | } |
| 117 | return nil |
| 118 | } |
| 119 | |
| 120 | // kindReplied marks the coordinator moved in after its move-in replied. |
| 121 | func (t *txn) kindReplied(r turnRecord) error { |
| 122 | if r.Kind != wire.TurnMoveIn { |
| 123 | return nil |
| 124 | } |
| 125 | return t.setCoordinator(r.Person, wire.CoordinatorReady, "") |
| 126 | } |
| 127 | |
| 128 | // quietReply reports whether a reply posts no message: a move-in or |
| 129 | // explore turn with nothing to say. |
| 130 | func quietReply(r turnRecord, text string) bool { |
| 131 | return r.Kind != wire.TurnPerson && strings.TrimSpace(text) == "" |
| 132 | } |
| 133 | |