package common import ( "encoding/json" "errors" "fmt" "strconv" ) type DataInterface interface { HTML() string } type ChatData struct { Hidden bool 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 } return data, err } func newChatData(hidden bool, dtype DataType, d DataInterface) (ChatData, error) { rawData, err := json.Marshal(d) return ChatData{ Hidden: hidden, Type: dtype, Data: rawData, }, err } type ClientData struct { Type ClientDataType Message string } type DataError struct { Message string } type DataMessage struct { From string Color string Message string Type MessageType } type DataCommand struct { Command CommandType Arguments []string } type DataEvent struct { Event EventType User string Color string } type ClientDataType int const ( CdMessage ClientDataType = iota // a normal message from the client meant to be broadcast CdUsers // get a list of users ) type DataType int // Data Types const ( DTInvalid DataType = iota DTChat // chat message DTError // something went wrong with the previous request DTCommand // non-chat function DTEvent // join/leave/kick/ban events DTClient // a message coming from the client ) func ParseDataType(token json.Token) (DataType, error) { d := fmt.Sprintf("%.0f", token) val, err := strconv.ParseInt(d, 10, 32) if err != nil { return DTInvalid, fmt.Errorf("invalid data type value: %q\n", d) } return DataType(val), nil } type CommandType int // Command Types const ( CmdPlaying CommandType = iota CmdRefreshPlayer CmdPurgeChat CmdHelp ) type EventType int // Event Types const ( EvJoin EventType = iota EvLeave EvKick EvBan EvServerMessage ) type MessageType int // Message Types const ( MsgChat MessageType = iota // standard chat MsgAction // /me command MsgServer // server message MsgError ) func (c ClientData) HTML() string { // Client data is for client to server communication only, so clients should not see this return `