vegapokerbot/internal/handler/util/payload.go
2024-05-10 11:46:43 +03:00

145 lines
2.8 KiB
Go

package util
import (
"encoding/json"
)
type PayloadAction uint8
const (
PayloadActionYesNo = iota
PayloadActionVote
PayloadActionChooseKeyboard
PayloadActionVoteFinish
PayloadActionVoteSendRedmine
)
type QuestionType uint8
const (
QuestionTypeRedmine = iota
QuestionTypeRedmineHours
QuestionTypeRedmineSendResult
)
type Payload struct {
User int64 `json:"u,omitempty"`
Chat int64 `json:"c,omitempty"`
Message int `json:"m,omitempty"`
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
}
type Member struct {
ID int64
Name string
}
type KBChooserData struct {
Type uint8 `json:"k"`
}
type Vote struct {
Vote float64 `json:"v"`
}
type Answer struct {
Type QuestionType `json:"t"`
Result bool `json:"r"`
}
func NewKeyboardChooserPayload(userID, chatID int64, kbType uint8) *Payload {
return &Payload{
Action: PayloadActionChooseKeyboard,
User: userID,
Chat: chatID,
Data: marshal(KBChooserData{
Type: kbType,
}),
}
}
func NewVotePayload(chatID int64, vote float64) *Payload {
return &Payload{
Action: PayloadActionVote,
Chat: chatID,
Data: marshal(Vote{Vote: vote}),
}
}
func NewVoteFinishPayload(chatID int64) *Payload {
return &Payload{
Action: PayloadActionVoteFinish,
Chat: chatID,
}
}
func NewRedmineQuestionPayload(userID int64, chatID int64, isYes bool) *Payload {
return &Payload{
Action: PayloadActionYesNo,
User: userID,
Chat: chatID,
Data: marshal(Answer{Type: QuestionTypeRedmine, Result: isYes}),
}
}
func NewRedmineHoursQuestionPayload(userID int64, chatID int64, isYes bool) *Payload {
return &Payload{
Action: PayloadActionYesNo,
User: userID,
Chat: chatID,
Data: marshal(Answer{Type: QuestionTypeRedmineHours, Result: isYes}),
}
}
func NewRedmineSendResultQuestionPayload(userID int64, chatID int64, isYes bool) *Payload {
return &Payload{
Action: PayloadActionYesNo,
User: userID,
Chat: chatID,
Data: marshal(Answer{Type: QuestionTypeRedmineSendResult, Result: isYes}),
}
}
func NewRedmineSendResultPayload(chatID int64, resultToSend float64) *Payload {
return &Payload{
Action: PayloadActionVoteSendRedmine,
Chat: chatID,
Data: marshal(Vote{Vote: resultToSend}),
}
}
func marshal(val interface{}) json.RawMessage {
data, _ := json.Marshal(val)
return data
}