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

120 lines
2.4 KiB
Go
Raw Normal View History

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