vegapokerbot/internal/handler/fsm/machine/dto/payload.go

120 lines
2.5 KiB
Go
Raw Normal View History

2024-05-09 16:37:50 +03:00
package dto
import (
"encoding/json"
)
type PayloadAction uint8
const (
PayloadActionNext = iota
PayloadActionPrevious
PayloadActionAddMember
PayloadActionRemoveMember
PayloadActionYesNo
PayloadActionVote
)
type QuestionType uint8
const (
QuestionTypeChatMembers QuestionType = iota
QuestionTypeRedmine
QuestionTypeRedmineHours
)
type Payload struct {
User uint64 `json:"u,omitempty"`
Chat uint64 `json:"c,omitempty"`
Action PayloadAction `json:"a"`
Data json.RawMessage `json:"d,omitempty"`
}
type Member struct {
ID uint64
Name string
}
type Vote struct {
Vote float32 `json:"v"`
}
type Answer struct {
Type QuestionType `json:"t"`
Result bool `json:"r"`
}
func NewNextPageMembersPayload(userID, chatID uint64) *Payload {
return &Payload{
Action: PayloadActionNext,
User: userID,
Chat: chatID,
}
}
func NewPrevPageMembersPayload(userID, chatID uint64) *Payload {
return &Payload{
Action: PayloadActionPrevious,
User: userID,
Chat: chatID,
}
}
func NewAddMemberPayload(userID, chatID, memberID uint64, memberName string) *Payload {
return &Payload{
Action: PayloadActionAddMember,
User: userID,
Chat: chatID,
Data: marshal(Member{ID: memberID, Name: memberName}),
}
}
func NewRemoveMemberPayload(userID, chatID, memberID uint64, memberName string) *Payload {
return &Payload{
Action: PayloadActionRemoveMember,
User: userID,
Chat: chatID,
Data: marshal(Member{ID: memberID, Name: memberName}),
}
}
func NewVotePayload(chatID uint64, vote float32) *Payload {
return &Payload{
Action: PayloadActionVote,
Chat: chatID,
Data: marshal(Vote{Vote: vote}),
}
}
func NewConfirmMembersPayload(userID uint64, chatID uint64) *Payload {
return &Payload{
Action: PayloadActionYesNo,
User: userID,
Chat: chatID,
Data: marshal(Answer{Type: QuestionTypeChatMembers, Result: true}),
}
}
func NewRedmineQuestionPayload(userID uint64, chatID uint64, isYes bool) *Payload {
return &Payload{
Action: PayloadActionYesNo,
User: userID,
Chat: chatID,
Data: marshal(Answer{Type: QuestionTypeRedmine, Result: isYes}),
}
}
func NewRedmineHoursQuestionPayload(userID uint64, chatID uint64, isYes bool) *Payload {
return &Payload{
Action: PayloadActionYesNo,
User: userID,
Chat: chatID,
Data: marshal(Answer{Type: QuestionTypeRedmineHours, Result: isYes}),
}
}
func marshal(val interface{}) json.RawMessage {
data, _ := json.Marshal(val)
return data
}