package common
import (
"encoding/json"
"errors"
"fmt"
)
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 DTError:
d := DataError{}
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 DataError struct {
Message string
}
func (de DataError) HTML() string {
return `Error: ` + de.Message + `
`
}
func EncodeError(message string) (string, error) {
d, err := newChatData(DTError, DataError{Message: message})
if err != nil {
return "", err
}
return jsonifyChatData(d)
}
type DataMessage struct {
From string
Color string
Message string
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 + `
`
default:
return `` + dc.From +
`: ` + dc.Message + `
`
}
}
func EncodeMessage(name, color, msg string, msgtype MessageType) (string, error) {
d, err := newChatData(DTChat, DataMessage{
From: name,
Color: color,
Message: msg,
Type: msgtype,
})
if err != nil {
return "", err
}
return jsonifyChatData(d)
}
type DataCommand struct {
Command CommandType
Arguments []string
}
func (de DataCommand) HTML() string {
return ""
}
func EncodeCommand(command CommandType, args []string) (string, error) {
d, err := newChatData(DTCommand, DataCommand{
Command: command,
Arguments: args,
})
if err != nil {
return "", err
}
return jsonifyChatData(d)
}
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 EncodeEvent(event EventType, name, color string) (string, error) {
d, err := newChatData(DTEvent, DataEvent{
Event: event,
User: name,
Color: color,
})
if err != nil {
return "", err
}
return jsonifyChatData(d)
}
// 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 EncodeHiddenMessage(clientType ClientDataType, data interface{}) (string, error) {
d, err := newChatData(DTHidden, HiddenMessage{
Type: clientType,
Data: data,
})
if err != nil {
return "", err
}
return jsonifyChatData(d)
}
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
}