Files
scrum-solitare/src/state/types.go
2026-03-06 12:30:05 +02:00

200 lines
6.2 KiB
Go

package state
import (
"errors"
"sync"
"time"
)
const (
RoleParticipant = "participant"
RoleViewer = "viewer"
RevealModeManual = "manual"
RevealModeAutoAll = "all_voted"
)
var (
ErrRoomNotFound = errors.New("room not found")
ErrParticipantNotFound = errors.New("participant not found")
ErrUnauthorized = errors.New("unauthorized")
ErrRoomFull = errors.New("room is full")
ErrInvalidRole = errors.New("invalid role")
ErrSpectatorsBlocked = errors.New("spectators are not allowed")
ErrPasswordRequired = errors.New("password required or invalid")
ErrInvalidCard = errors.New("invalid card")
ErrVoteChangeLocked = errors.New("vote changes are disabled for this room")
)
type RoomSettings struct {
RoomName string `json:"roomName"`
MaxPeople int `json:"maxPeople"`
Cards []string `json:"cards"`
AllowSpectators bool `json:"allowSpectators"`
AnonymousVoting bool `json:"anonymousVoting"`
AutoReset bool `json:"autoReset"`
AllowVoteChange bool `json:"allowVoteChange"`
RevealMode string `json:"revealMode"`
VotingTimeoutSec int `json:"votingTimeoutSec"`
PasswordSalt string `json:"passwordSalt,omitempty"`
PasswordHash string `json:"passwordHash,omitempty"`
}
type persistedRoomSettings struct {
RoomName string `json:"roomName"`
MaxPeople int `json:"maxPeople"`
Cards []string `json:"cards"`
AllowSpectators bool `json:"allowSpectators"`
AnonymousVoting bool `json:"anonymousVoting"`
AutoReset bool `json:"autoReset"`
AllowVoteChange *bool `json:"allowVoteChange,omitempty"`
RevealMode string `json:"revealMode"`
VotingTimeoutSec int `json:"votingTimeoutSec"`
PasswordSalt string `json:"passwordSalt,omitempty"`
PasswordHash string `json:"passwordHash,omitempty"`
}
type Participant struct {
ID string `json:"id"`
SessionToken string `json:"-"`
Username string `json:"username"`
Role string `json:"role"`
IsAdmin bool `json:"isAdmin"`
Connected bool `json:"connected"`
HasVoted bool `json:"hasVoted"`
VoteValue string `json:"voteValue,omitempty"`
JoinedAt time.Time `json:"joinedAt"`
UpdatedAt time.Time `json:"updatedAt"`
}
type persistedParticipant struct {
ID string `json:"id"`
SessionToken string `json:"sessionToken,omitempty"`
Username string `json:"username"`
Role string `json:"role"`
IsAdmin bool `json:"isAdmin"`
Connected bool `json:"connected"`
HasVoted bool `json:"hasVoted"`
VoteValue string `json:"voteValue,omitempty"`
JoinedAt time.Time `json:"joinedAt"`
UpdatedAt time.Time `json:"updatedAt"`
}
type RoundState struct {
Revealed bool `json:"revealed"`
}
type ActivityLogEntry struct {
At time.Time `json:"at"`
Message string `json:"message"`
}
type persistedRoom struct {
ID string `json:"id"`
AdminToken string `json:"adminToken"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
Settings persistedRoomSettings `json:"settings"`
Round RoundState `json:"round"`
Participants []*persistedParticipant `json:"participants"`
ActivityLog []ActivityLogEntry `json:"activityLog,omitempty"`
}
type subscriber struct {
participantID string
ch chan []byte
}
type Room struct {
ID string
AdminToken string
CreatedAt time.Time
UpdatedAt time.Time
Settings RoomSettings
Round RoundState
Participants map[string]*Participant
ActivityLog []ActivityLogEntry
mu sync.RWMutex
subscribers map[string]*subscriber
}
type CreateRoomInput struct {
RoomName string
CreatorUsername string
MaxPeople int
Cards []string
AllowSpectators bool
AnonymousVoting bool
AutoReset bool
AllowVoteChange *bool
RevealMode string
VotingTimeoutSec int
Password string
}
type JoinRoomInput struct {
ParticipantID string
SessionToken string
Username string
Role string
Password string
AdminToken string
}
type CreateRoomResult struct {
RoomID string `json:"roomId"`
CreatorParticipantID string `json:"creatorParticipantId"`
CreatorSessionToken string `json:"creatorSessionToken"`
AdminToken string `json:"adminToken"`
ParticipantLink string `json:"participantLink"`
AdminLink string `json:"adminLink"`
}
type JoinRoomResult struct {
ParticipantID string `json:"participantId"`
SessionToken string `json:"sessionToken"`
IsAdmin bool `json:"isAdmin"`
Role string `json:"role"`
Username string `json:"username"`
}
type PublicParticipant struct {
ID string `json:"id"`
Username string `json:"username"`
Role string `json:"role"`
IsAdmin bool `json:"isAdmin"`
Connected bool `json:"connected"`
HasVoted bool `json:"hasVoted"`
VoteValue string `json:"voteValue,omitempty"`
}
type RoomLinks struct {
ParticipantLink string `json:"participantLink"`
AdminLink string `json:"adminLink,omitempty"`
}
type PublicActivityLogEntry struct {
At time.Time `json:"at"`
Message string `json:"message"`
}
type PublicRoomState struct {
RoomID string `json:"roomId"`
RoomName string `json:"roomName"`
Cards []string `json:"cards"`
Revealed bool `json:"revealed"`
RevealMode string `json:"revealMode"`
MaxPeople int `json:"maxPeople"`
AllowSpectators bool `json:"allowSpectators"`
AnonymousVoting bool `json:"anonymousVoting"`
AutoReset bool `json:"autoReset"`
AllowVoteChange bool `json:"allowVoteChange"`
VotingTimeoutSec int `json:"votingTimeoutSec"`
Participants []PublicParticipant `json:"participants"`
SelfParticipantID string `json:"selfParticipantId"`
ViewerIsAdmin bool `json:"viewerIsAdmin"`
Links RoomLinks `json:"links"`
AdminLogs []PublicActivityLogEntry `json:"adminLogs,omitempty"`
}