package common
import (
"encoding/json"
"errors"
"fmt"
)
type NewChatDataFunc func() (ChatData, error)
type DataInterface interface {
HTML() string
}
type ChatData struct {
Type DataType
Data json.RawMessage
}
func (c ChatData) GetData() (DataInterface, error) {
var data DataInterface
var err error
switch c.Type {
case DTInvalid:
return nil, errors.New("data type is invalid")
case DTChat:
d := DataMessage{}
err = json.Unmarshal(c.Data, &d)
data = d
case DTCommand:
d := DataCommand{}
err = json.Unmarshal(c.Data, &d)
data = d
case DTEvent:
d := DataEvent{}
err = json.Unmarshal(c.Data, &d)
data = d
case DTClient:
d := ClientData{}
err = json.Unmarshal(c.Data, &d)
data = d
case DTHidden:
d := HiddenMessage{}
err = json.Unmarshal(c.Data, &d)
data = d
default:
err = fmt.Errorf("unhandled data type: %d", c.Type)
}
return data, err
}
func newChatData(dtype DataType, d DataInterface) (ChatData, error) {
rawData, err := json.Marshal(d)
return ChatData{
Type: dtype,
Data: rawData,
}, err
}
type ClientData struct {
Type ClientDataType
Message string
}
func (c ClientData) HTML() string {
// Client data is for client to server communication only, so clients should not see this
return `
The developer messed up. You should not be seeing this.
`
}
type DataMessage struct {
From string
Color string
Message string
Level CommandLevel
Type MessageType
}
// TODO: Read this HTML from a template somewhere
func (dc DataMessage) HTML() string {
switch dc.Type {
case MsgAction:
return `` + dc.From +
` ` + dc.Message + `
`
case MsgServer:
return `` + dc.Message + `
`
case MsgError:
return `` + dc.Message + `
`
case MsgNotice:
return `` + dc.Message + `
`
case MsgCommandResponse:
return `` + dc.Message + `
`
default:
badge := ""
switch dc.Level {
case CmdMod:
badge = ``
case CmdAdmin:
badge = ``
}
return `` + badge + `` + dc.From +
`: ` + dc.Message + `
`
}
}
func NewChatMessage(name, color, msg string, lvl CommandLevel, msgtype MessageType) NewChatDataFunc {
return func() (ChatData, error) {
return newChatData(DTChat, DataMessage{
From: name,
Color: color,
Message: msg,
Type: msgtype,
Level: lvl,
})
}
}
type DataCommand struct {
Command CommandType
Arguments []string
}
func (de DataCommand) HTML() string {
return ""
}
func NewChatCommand(command CommandType, args []string) NewChatDataFunc {
return func() (ChatData, error) {
return newChatData(DTCommand, DataCommand{
Command: command,
Arguments: args,
})
}
}
type DataEvent struct {
Event EventType
User string
Color string
}
func (de DataEvent) HTML() string {
switch de.Event {
case EvKick:
return `` +
de.User + ` has been kicked.
`
case EvLeave:
return `` +
de.User + ` has left the chat.
`
case EvBan:
return `` +
de.User + ` has been banned.
`
case EvJoin:
return `` +
de.User + ` has joined the chat.
`
}
return ""
}
func NewChatEvent(event EventType, name, color string) NewChatDataFunc {
return func() (ChatData, error) {
return newChatData(DTEvent, DataEvent{
Event: event,
User: name,
Color: color,
})
}
}
// DataHidden is for the server to send instructions and data
// to the client without the purpose of outputting it on the chat
type HiddenMessage struct {
Type ClientDataType
Data interface{}
}
func (h HiddenMessage) HTML() string {
return ""
}
func NewChatHiddenMessage(clientType ClientDataType, data interface{}) NewChatDataFunc {
return func() (ChatData, error) {
return newChatData(DTHidden, HiddenMessage{
Type: clientType,
Data: data,
})
}
}
func jsonifyChatData(data ChatData) (string, error) {
raw, err := json.Marshal(data)
if err != nil {
return "", err
}
return string(raw), nil
}
func DecodeData(rawjson string) (ChatData, error) {
var data ChatData
err := json.Unmarshal([]byte(rawjson), &data)
return data, err
}