Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions internal/conversations/conversations.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,13 @@ func AttemptConversation(initiatorMobId int, initatorInstanceId int, initiatorNa
MobInstanceId1: initatorInstanceId,
MobInstanceId2: participantInstanceId,
StartRound: util.GetRoundCount(),
Position: 0,
ActionList: dataFile[chosenIndex].Conversation,
// LastRound must start at the current round - the cleanup in
// getConversation treats rounds since LastRound as staleness, and a
// zero value makes a brand-new conversation look ancient and
// eligible for deletion before its first action fires.
LastRound: util.GetRoundCount(),
Position: 0,
ActionList: dataFile[chosenIndex].Conversation,
}

return conversationUniqueId
Expand Down
28 changes: 28 additions & 0 deletions internal/conversations/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"

"github.com/GoMudEngine/GoMud/internal/mudlog"
"github.com/GoMudEngine/GoMud/internal/util"
)

func TestMain(m *testing.M) {
Expand Down Expand Up @@ -119,3 +120,30 @@ func TestReadPluginConversationFile_KeyFormat(t *testing.T) {
t.Fatalf("did not expect a file for mob 43")
}
}

// TestAttemptConversation_FreshConversationNotStale guards against a fresh
// conversation being eligible for the random cleanup in getConversation. A
// zero LastRound made new conversations look ancient (the round counter
// starts above one million), so any lookup that won the 2% maintenance roll
// deleted a conversation that was just created.
func TestAttemptConversation_FreshConversationNotStale(t *testing.T) {
resetPluginState()
defer resetPluginState()

RegisterFS(newFakeFS(map[string][]byte{
`conversations/testzone/9001.yaml`: []byte(sampleConversation),
}))

convId := AttemptConversation(9001, 1, "goblin", 2, "rat", "TestZone")
if convId == 0 {
t.Fatalf("expected a non-zero conversation id from plugin file")
}

c := conversations[convId]
if c == nil {
t.Fatalf("expected conversation to be stored")
}
if c.LastRound != util.GetRoundCount() {
t.Fatalf("expected fresh conversation LastRound to equal the current round %d, got %d", util.GetRoundCount(), c.LastRound)
}
}