package common import ( "encoding/json" "fmt" "strconv" "strings" ) const CommandNameSeparator = "," type ChatCommandNames []string func (c ChatCommandNames) String() string { return strings.Join(c, CommandNameSeparator) } // Names for commands var ( // User Commands CNMe ChatCommandNames = []string{"me"} CNHelp ChatCommandNames = []string{"help"} CNCount ChatCommandNames = []string{"count"} CNColor ChatCommandNames = []string{"color", "colour"} CNWhoAmI ChatCommandNames = []string{"w", "whoami"} CNAuth ChatCommandNames = []string{"auth"} CNUsers ChatCommandNames = []string{"users"} // Mod Commands CNSv ChatCommandNames = []string{"sv"} CNPlaying ChatCommandNames = []string{"playing"} CNUnmod ChatCommandNames = []string{"unmod"} CNKick ChatCommandNames = []string{"kick"} CNBan ChatCommandNames = []string{"ban"} CNUnban ChatCommandNames = []string{"unban"} // Admin Commands CNMod ChatCommandNames = []string{"mod"} CNReloadPlayer ChatCommandNames = []string{"reloadplayer"} CNReloadEmotes ChatCommandNames = []string{"reloademotes"} CNModpass ChatCommandNames = []string{"modpass"} ) var ChatCommands = []ChatCommandNames{ CNMe, CNHelp, CNCount, CNColor, CNWhoAmI, CNAuth, CNUsers, CNSv, CNPlaying, CNUnmod, CNKick, CNBan, CNUnban, CNMod, CNReloadPlayer, CNReloadEmotes, CNModpass, } func GetFullChatCommand(c string) string { for _, names := range ChatCommands { for _, n := range names { if c == n { return names.String() } } } return "" } type ClientData struct { Type ClientDataType Message string } type ChatData struct { Type DataType Data DataInterface } 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 DataInterface interface { GetType() DataType HTML() string } func (c ClientData) GetType() DataType { return DTClient } func (d DataMessage) GetType() DataType { return DTChat } func (d DataError) GetType() DataType { return DTError } func (d DataCommand) GetType() DataType { return DTCommand } func (d DataEvent) GetType() DataType { return DTEvent } 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 { fmt.Printf("Invalid data type value: %q\n", d) return DTInvalid, err } 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 `