vegapokerbot/internal/handler/util/payload.go

127 lines
2.4 KiB
Go
Raw Normal View History

package util
2024-05-09 16:37:50 +03:00
import (
2024-05-09 17:42:42 +03:00
"encoding/json"
2024-05-09 16:37:50 +03:00
)
type PayloadAction uint8
const (
PayloadActionYesNo = iota
2024-05-09 16:37:50 +03:00
PayloadActionVote
PayloadActionChooseKeyboard
2024-05-09 16:37:50 +03:00
)
type QuestionType uint8
const (
QuestionTypeRedmine = iota
2024-05-09 16:37:50 +03:00
QuestionTypeRedmineHours
2024-05-09 23:26:21 +03:00
QuestionTypeRedmineSendResult
2024-05-09 16:37:50 +03:00
)
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"`
}
func (p Payload) String() string {
data, _ := json.Marshal(&p)
return string(data)
}
func (p Payload) Vote() (val Vote) {
if p.Action != PayloadActionVote {
return
}
_ = json.Unmarshal(p.Data, &val)
return
}
func (p Payload) KeyboardChoice() (val KBChooserData) {
if p.Action != PayloadActionVote {
return
}
_ = json.Unmarshal(p.Data, &val)
return
}
func (p Payload) YesNoAnswer() (val Answer) {
if p.Action != PayloadActionYesNo {
return
}
_ = json.Unmarshal(p.Data, &val)
return
}
2024-05-09 16:37:50 +03:00
type Member struct {
2024-05-09 17:42:42 +03:00
ID int64
2024-05-09 16:37:50 +03:00
Name string
}
type KBChooserData struct {
2024-05-09 23:26:21 +03:00
Type uint8 `json:"k"`
}
2024-05-09 16:37:50 +03:00
type Vote struct {
Vote float32 `json:"v"`
}
type Answer struct {
Type QuestionType `json:"t"`
Result bool `json:"r"`
}
2024-05-09 23:26:21 +03:00
func NewKeyboardChooserPayload(userID, chatID int64, kbType uint8) *Payload {
2024-05-09 16:37:50 +03:00
return &Payload{
Action: PayloadActionChooseKeyboard,
2024-05-09 16:37:50 +03:00
User: userID,
Chat: chatID,
Data: marshal(KBChooserData{
Type: kbType,
}),
2024-05-09 16:37:50 +03:00
}
}
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 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}),
}
}
2024-05-09 23:26:21 +03:00
func NewRedmineSendResultQuestionPayload(userID int64, chatID int64, isYes bool) *Payload {
return &Payload{
Action: PayloadActionYesNo,
User: userID,
Chat: chatID,
Data: marshal(Answer{Type: QuestionTypeRedmineSendResult, Result: isYes}),
}
}
2024-05-09 16:37:50 +03:00
func marshal(val interface{}) json.RawMessage {
data, _ := json.Marshal(val)
return data
}