2024-05-09 20:48:59 +03:00
|
|
|
package util
|
2024-05-09 16:37:50 +03:00
|
|
|
|
|
|
|
import (
|
2024-05-09 17:42:42 +03:00
|
|
|
"encoding/json"
|
2024-05-09 20:48:59 +03:00
|
|
|
"gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/db/model"
|
2024-05-09 16:37:50 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type PayloadAction uint8
|
|
|
|
|
|
|
|
const (
|
2024-05-09 20:48:59 +03:00
|
|
|
PayloadActionYesNo = iota
|
2024-05-09 16:37:50 +03:00
|
|
|
PayloadActionVote
|
2024-05-09 20:48:59 +03:00
|
|
|
PayloadActionChooseKeyboard
|
2024-05-09 16:37:50 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type QuestionType uint8
|
|
|
|
|
|
|
|
const (
|
2024-05-09 20:48:59 +03:00
|
|
|
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"`
|
|
|
|
}
|
|
|
|
|
2024-05-09 20:48:59 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-05-09 20:48:59 +03:00
|
|
|
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"`
|
|
|
|
}
|
|
|
|
|
2024-05-09 20:48:59 +03:00
|
|
|
func NewKeyboardChooserPayload(userID, chatID int64, kbType model.KeyboardType) *Payload {
|
2024-05-09 16:37:50 +03:00
|
|
|
return &Payload{
|
2024-05-09 20:48:59 +03:00
|
|
|
Action: PayloadActionChooseKeyboard,
|
2024-05-09 16:37:50 +03:00
|
|
|
User: userID,
|
|
|
|
Chat: chatID,
|
2024-05-09 20:48:59 +03:00
|
|
|
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
|
|
|
|
}
|