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()
This commit is contained in:
Zorchenhimer 2019-03-19 17:25:49 -04:00
parent 1c6c23da3e
commit 49e997c5ff
4 changed files with 59 additions and 48 deletions

View File

@ -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
}

View File

@ -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)
}

View File

@ -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
}

25
common/utils.go Normal file
View File

@ -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])
}