This project implements a state machine for tracking character quest progression via a REST API. It includes a server to manage the state machine and a client to send test events.
/Server: Contains the server-side application, state machine logic, and data definitions (events, states, transitions)./Client: Contains a client application to send pre-defined events to the server.
- Navigate to the server directory:
cd Server - Build the server executable:
(This command assumes all necessary
go build -o state_machine *.go.gofiles for the server are in theServerdirectory or its subdirectories if you adjust the*.gopattern or use module-aware mode builds). - Run the server:
The server will start on
./state_machine
http://localhost:8080.
- Ensure the server is running.
- Navigate to the client directory:
cd Client - Run the client:
The client will load events from
go run main.go
Client/Input/input_events.json, adjust their timestamps to be in the future, and send them to the server.
-
Asynchronous Event Processing:
- When an event is received via the
/quest-eventAPI, it is validated and scheduled for future processing based on its timestamp. The API call returns an immediate response (e.g., "accepted" or an error) and does not wait for the event's scheduled time to execute. - If an event is rejected during initial validation (e.g., timestamp in the past, unknown event type), an error status is returned immediately.
- If a scheduled event is later found to be invalid at its processing time (e.g., if character deletion were implemented and the character no longer exists, or no valid transition exists), it is silently ignored (a log message is generated on the server, but no direct response is sent back to the original API caller for that past request).
- When an event is received via the
-
Data Configuration:
- The server relies on
events.json,states.json, andstate_machine.jsonfiles located in aServer/Data/subdirectory. These files must be correctly formatted and present at startup. - A state named "NotStarted" must be defined in
states.jsonas it's the default initial state for characters.
- The server relies on
-
Quest and Character Handling:
- For a
begin_questevent, theQuestIDfor the character's new quest is currently derived from theQuestIDof the incoming request. - For events other than
begin_quest, theQuestIDin the request must match the character's currently activeQuestIDas long as the character has an active quest.
- For a
-
Timestamps:
- Incoming event timestamps in
QuestEventRequestare expected to be in RFC3339 format. - Events are only scheduled if their timestamp is strictly in the future relative to the server's current time when the request is received.
- Incoming event timestamps in
-
State Machine Definitions:
- Self-transitions (a state transitioning to itself on an event) are disallowed by the current design in
createStateMachine. - Conflicting transitions (defining multiple different target states for the same
from_stateandeventcombination) are disallowed.
- Self-transitions (a state transitioning to itself on an event) are disallowed by the current design in
-
Client Behavior:
- The provided client (
Client/main.go) loads events fromClient/Input/input_events.json. - The client actively adjusts the timestamps of these loaded events to be in the near future before sending them to ensure they are accepted by the server's "timestamp in the future" validation.
- The provided client (