vegapokerbot/internal/handler/util/payload.go

118 lines
2.2 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"
"gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/db/model"
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
)
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 {
Type model.KeyboardType `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"`
}
func NewKeyboardChooserPayload(userID, chatID int64, kbType model.KeyboardType) *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}),
}
}
func marshal(val interface{}) json.RawMessage {
data, _ := json.Marshal(val)
return data
}