From 49e997c5ffc6d3498b904f2063ec941b6b38799c Mon Sep 17 00:00:00 2001 From: Zorchenhimer Date: Tue, 19 Mar 2019 17:25:49 -0400 Subject: [PATCH] Move some stuff to MovieNight.common Moved to common/utils.go: - IsValidName() (function replaces direct calls to regexp.MatchString()) - RandomColor() Moved to common/emotes.go: - LoadEmotes() --- chatcommands.go | 4 ++-- chatroom.go | 50 ++++-------------------------------------------- common/emotes.go | 28 +++++++++++++++++++++++++++ common/utils.go | 25 ++++++++++++++++++++++++ 4 files changed, 59 insertions(+), 48 deletions(-) create mode 100644 common/utils.go diff --git a/chatcommands.go b/chatcommands.go index be9b200..98bf25a 100644 --- a/chatcommands.go +++ b/chatcommands.go @@ -274,7 +274,7 @@ var commands = &CommandControl{ HelpText: "Reload the emotes on the server.", Function: func(cl *Client, args []string) string { cl.SendServerMessage("Reloading emotes") - num, err := LoadEmotes() + num, err := common.LoadEmotes() if err != nil { fmt.Printf("Unbale to reload emotes: %s\n", err) return fmt.Sprintf("ERROR: %s", err) @@ -401,7 +401,7 @@ var cmdColor = Command{ } if len(args) == 0 { - cl.color = randomColor() + cl.color = common.RandomColor() return "Random color chosen: " + cl.color } diff --git a/chatroom.go b/chatroom.go index b41543d..cf7a52a 100644 --- a/chatroom.go +++ b/chatroom.go @@ -6,9 +6,6 @@ import ( uuid "github.com/satori/go.uuid" - "math/rand" - "path/filepath" - "regexp" "strings" "sync" "time" @@ -22,8 +19,6 @@ const ( ColorServerMessage string = "#ea6260" ) -var re_username *regexp.Regexp = regexp.MustCompile(`^[0-9a-zA-Z_-]+$`) - type ChatRoom struct { clients map[string]*Client // this needs to be a pointer. key is suid. clientsMtx sync.Mutex @@ -48,7 +43,7 @@ func newChatRoom() (*ChatRoom, error) { tempConn: make(map[string]*chatConnection), } - num, err := LoadEmotes() + num, err := common.LoadEmotes() if err != nil { return nil, fmt.Errorf("error loading emotes: %s", err) } @@ -59,43 +54,6 @@ func newChatRoom() (*ChatRoom, error) { return cr, nil } -func LoadEmotes() (int, error) { - newEmotes := map[string]string{} - - emotePNGs, err := filepath.Glob("./static/emotes/*.png") - if err != nil { - return 0, fmt.Errorf("unable to glob emote directory: %s\n", err) - } - - emoteGIFs, err := filepath.Glob("./static/emotes/*.gif") - if err != nil { - return 0, fmt.Errorf("unable to glob emote directory: %s\n", err) - } - globbed_files := []string(emotePNGs) - globbed_files = append(globbed_files, emoteGIFs...) - - fmt.Println("Loading emotes...") - for _, file := range globbed_files { - file = filepath.Base(file) - key := file[0 : len(file)-4] - newEmotes[key] = file - fmt.Printf("%s ", key) - } - common.Emotes = newEmotes - fmt.Println("") - return len(common.Emotes), nil -} - -func randomColor() string { - nums := []int32{} - for i := 0; i < 6; i++ { - nums = append(nums, rand.Int31n(15)) - } - return fmt.Sprintf("#%X%X%X%X%X%X", - nums[0], nums[1], nums[2], - nums[3], nums[4], nums[5]) -} - func (cr *ChatRoom) JoinTemp(conn *chatConnection) (string, error) { defer cr.clientsMtx.Unlock() cr.clientsMtx.Lock() @@ -129,7 +87,7 @@ func (cr *ChatRoom) Join(name, uid string) (*Client, error) { return nil, errors.New("connection is missing from temp connections") } - if len(name) < UsernameMinLength || len(name) > UsernameMaxLength || !re_username.MatchString(name) { + if len(name) < UsernameMinLength || len(name) > UsernameMaxLength || !common.IsValidName(name) { return nil, UserFormatError{Name: name} } @@ -144,7 +102,7 @@ func (cr *ChatRoom) Join(name, uid string) (*Client, error) { name: name, conn: conn, belongsTo: cr, - color: randomColor(), + color: common.RandomColor(), } host := client.Host() @@ -500,7 +458,7 @@ func (cr *ChatRoom) changeName(oldName, newName string, forced bool) error { cr.clientsMtx.Lock() defer cr.clientsMtx.Unlock() - if !re_username.MatchString(newName) { + if !common.IsValidName(newName) { return fmt.Errorf("%q nick is not a valid name", newName) } diff --git a/common/emotes.go b/common/emotes.go index f8108d1..9be8b42 100644 --- a/common/emotes.go +++ b/common/emotes.go @@ -2,6 +2,7 @@ package common import ( "fmt" + "path/filepath" "strings" ) @@ -30,3 +31,30 @@ func ParseEmotes(msg string) string { words := ParseEmotesArray(strings.Split(msg, " ")) return strings.Join(words, " ") } + +func LoadEmotes() (int, error) { + newEmotes := map[string]string{} + + emotePNGs, err := filepath.Glob("./static/emotes/*.png") + if err != nil { + return 0, fmt.Errorf("unable to glob emote directory: %s\n", err) + } + + emoteGIFs, err := filepath.Glob("./static/emotes/*.gif") + if err != nil { + return 0, fmt.Errorf("unable to glob emote directory: %s\n", err) + } + globbed_files := []string(emotePNGs) + globbed_files = append(globbed_files, emoteGIFs...) + + fmt.Println("Loading emotes...") + for _, file := range globbed_files { + file = filepath.Base(file) + key := file[0 : len(file)-4] + newEmotes[key] = file + fmt.Printf("%s ", key) + } + Emotes = newEmotes + fmt.Println("") + return len(Emotes), nil +} diff --git a/common/utils.go b/common/utils.go new file mode 100644 index 0000000..871cfca --- /dev/null +++ b/common/utils.go @@ -0,0 +1,25 @@ +package common + +// Misc utils + +import ( + "fmt" + "math/rand" + "regexp" +) + +var re_username *regexp.Regexp = regexp.MustCompile(`^[0-9a-zA-Z_-]+$`) + +func IsValidName(name string) bool { + return re_username.MatchString(name) +} + +func RandomColor() string { + nums := []int32{} + for i := 0; i < 6; i++ { + nums = append(nums, rand.Int31n(15)) + } + return fmt.Sprintf("#%X%X%X%X%X%X", + nums[0], nums[1], nums[2], + nums[3], nums[4], nums[5]) +}