Things are working swimmingly

This commit is contained in:
Brian Buller 2014-12-24 11:04:09 -06:00
parent c17e4dc06d
commit 0c96801906
5 changed files with 125 additions and 134 deletions

View File

@ -6,7 +6,7 @@ import (
"log" "log"
"os" "os"
"os/exec" "os/exec"
//"strings" "time"
) )
func main() { func main() {

View File

@ -18,6 +18,7 @@ type Config struct {
FeatureTPVisit bool FeatureTPVisit bool
FeatureDayNight bool FeatureDayNight bool
Users []*User Users []*User
LoggedInUsers []*User
} }
var c *Config var c *Config
@ -32,7 +33,6 @@ func LoadConfig(mm *MessageManager) {
// Add the "Stop" listener // Add the "Stop" listener
fmt.Println("Activating 'stop' listener") fmt.Println("Activating 'stop' listener")
AddListener(func(i *Message) bool { AddListener(func(i *Message) bool {
//listeners = append(mm.listeners, func(i *Message) bool {
if i.User.IsOp && i.Text == "!stop\n" { if i.User.IsOp && i.Text == "!stop\n" {
mm.Output("stop") mm.Output("stop")
return true return true
@ -58,8 +58,9 @@ func LoadConfig(mm *MessageManager) {
if len(r) > 0 { if len(r) > 0 {
p_str := r[1] p_str := r[1]
p_str = strings.Replace(p_str, ",", "", -1) p_str = strings.Replace(p_str, ",", "", -1)
p_str = strings.Replace(p_str, "\n", "", -1)
SetHome(i.User.Name, p_str) SetHome(i.User.Name, p_str)
mm.Output("tellraw " + i.User.Name + " Set your home to " + p_str) mm.Tell(i.User.Name, "Set your home to "+p_str, "blue")
return true return true
} }
} }
@ -77,7 +78,7 @@ func LoadConfig(mm *MessageManager) {
if found { if found {
mm.Output("tp " + i.User.Name + " " + home_str) mm.Output("tp " + i.User.Name + " " + home_str)
} else { } else {
mm.Output("tellraw " + i.User.Name + " I don't know where your home is. Set it to your current position by typing '!set home'") mm.Tell(i.User.Name, "I don't know where your home is. Set it to your current position by typing '!set home'", "red")
} }
} }
return false return false
@ -99,7 +100,7 @@ func LoadConfig(mm *MessageManager) {
p_str := r[1] p_str := r[1]
p_str = strings.Replace(p_str, ",", "", -1) p_str = strings.Replace(p_str, ",", "", -1)
SetPorch(i.User.Name, p_str) SetPorch(i.User.Name, p_str)
mm.Output("tellraw " + i.User.Name + " Set your porch to " + p_str) mm.Tell(i.User.Name, "Set your porch to "+p_str, "blue")
return true return true
} }
} }
@ -121,7 +122,7 @@ func LoadConfig(mm *MessageManager) {
if found { if found {
mm.Output("tp " + i.User.Name + " " + porch_str) mm.Output("tp " + i.User.Name + " " + porch_str)
} else { } else {
mm.Output("tellraw " + i.User.Name + " I don't know where " + username + "s porch is. They can set it to their current position by typing '!set porch'") mm.Tell(i.User.Name, "I don't know where "+username+"'s porch is. They can set it to their current position by typing '!set porch'", "red")
} }
} }
} }
@ -137,6 +138,7 @@ func LoadConfig(mm *MessageManager) {
if i.User.Name != "" && i.Text == "!switch day\n" { if i.User.Name != "" && i.Text == "!switch day\n" {
// TODO: Start vote // TODO: Start vote
mm.Output("time set day") mm.Output("time set day")
mm.Tell("@a", "Day Time switch initiated by "+i.User.Name, "yellow")
return true return true
} }
return false return false
@ -146,31 +148,43 @@ func LoadConfig(mm *MessageManager) {
if i.User.Name != "" && i.Text == "!switch night\n" { if i.User.Name != "" && i.Text == "!switch night\n" {
// TODO: Start vote // TODO: Start vote
mm.Output("time set night") mm.Output("time set night")
mm.Tell("@a", "Night Time switch initiated by "+i.User.Name, "blue")
return true return true
} }
return false return false
}) })
} }
} }
// Add login listener
AddListener(func(i *Message) bool {
if i.User.Name == "" && strings.Contains(i.Text, " logged in with entity id ") {
// TODO: User Logged in Function
// Find the user that just logged in
r := strings.Split(i.Text, "]: ")
find := ""
if len(r) > 0 {
find = r[1]
r := strings.Split(find, "[/")
if len(r) > 0 {
find = r[0]
// find should be the user name now
LoginUser(*FindUser(find, true))
}
}
}
return false
})
// Add !help listener // Add !help listener
AddListener(func(i *Message) bool { AddListener(func(i *Message) bool {
if i.User.Name != "" && i.Text == "!help\n" { if i.User.Name != "" && i.Text == "!help\n" {
hlp_txt := " {text:\"-=( mc_man Manager Help )=-\",color:blue}" mm.Tell(i.User.Name, "-=( mc_man Manager Help )=-", "blue")
mm.Output("tellraw " + i.User.Name + hlp_txt) mm.Tell(i.User.Name, "!set home -- Set your 'home' to your current position.", "white")
hlp_txt = " {text:\"!set home -- Set your 'home' to your current position.\"}" mm.Tell(i.User.Name, "!home -- Request a teleport to your 'home' position.", "white")
mm.Output("tellraw " + i.User.Name + hlp_txt) mm.Tell(i.User.Name, "!set porch -- Set your 'porch' to your current position.", "white")
hlp_txt = " {text:\"!home -- Request a teleport to your 'home' position.\"}" mm.Tell(i.User.Name, "!visit <username> -- Request a teleport to <username>'s 'porch' position.", "white")
mm.Output("tellraw " + i.User.Name + hlp_txt) mm.Tell(i.User.Name, "!switch day -- Ask the server to switch the time to 'day'.", "white")
hlp_txt = " {text:\"!set porch -- Set your 'porch' to your current position.\"}" mm.Tell(i.User.Name, "!switch night -- Ask the server to switch the time to 'night'.", "white")
mm.Output("tellraw " + i.User.Name + hlp_txt) mm.Tell(i.User.Name, "-=========================-", "blue")
hlp_txt = " {text:\"!visit <username> -- Request a teleport to <username>'s 'porch' position.\"}"
mm.Output("tellraw " + i.User.Name + hlp_txt)
hlp_txt = " {text:\"!switch day -- Ask the server to switch the time to 'day'.\"}"
mm.Output("tellraw " + i.User.Name + hlp_txt)
hlp_txt = " {text:\"!switch night -- Ask the server to switch the time to 'night'.\"}"
mm.Output("tellraw " + i.User.Name + hlp_txt)
hlp_txt = " {text:\"-=========================-\",color:blue}"
mm.Output("tellraw " + i.User.Name + hlp_txt)
return true return true
} }
return false return false
@ -186,6 +200,9 @@ func LoadConfig(mm *MessageManager) {
user_porch, _ := user.GetString("porch") user_porch, _ := user.GetString("porch")
us := NewUser(user_name) us := NewUser(user_name)
// TODO: Check if this user is an op on the server // TODO: Check if this user is an op on the server
if user_name == "br0xen" {
us.IsOp = true
}
us.Home = user_home us.Home = user_home
us.Porch = user_porch us.Porch = user_porch
c.Users = append(c.Users, us) c.Users = append(c.Users, us)
@ -195,13 +212,38 @@ func LoadConfig(mm *MessageManager) {
} }
} }
func LoginUser(u User) {
for _, user := range c.LoggedInUsers {
if user.Name == u.Name {
// User is already logged in
return
}
}
c.LoggedInUsers = append(c.LoggedInUsers, &u)
}
func LogoutUser(u User) {
for idx, user := range c.LoggedInUsers {
if user.Name == u.Name {
t := append(c.LoggedInUsers[:idx], c.LoggedInUsers[idx+1:]...)
c.LoggedInUsers = make([]*User, len(t))
copy(c.LoggedInUsers, t)
return
}
}
}
func AddUser(username string) { func AddUser(username string) {
us := NewUser(username) if username != "" {
c.Users = append(c.Users, us) us := NewUser(username)
WriteConfig() fmt.Println("Adding new user: " + username)
c.Users = append(c.Users, us)
WriteConfig()
}
} }
func WriteConfig() { func WriteConfig() {
// TODO: Make mc_man aware of the world
// Generate the JSON string for the config file // Generate the JSON string for the config file
d := "{\"options\":[" d := "{\"options\":["
// Output options array // Output options array
@ -228,73 +270,59 @@ func WriteConfig() {
for _, u := range c.Users { for _, u := range c.Users {
d = d + u.ToJSONString() d = d + u.ToJSONString()
} }
/*
num_users := len(c.Users)
fmt.Printf("Number of Users: %d", num_users)
for i := 0; i < num_users; i++ {
user := c.Users[i]
// for _, user := range c.Users {
fmt.Println(d)
num_users--
d = d + user.ToJSONString()
if user.Name != "" {
if num_users > 0 {
d = d + ","
}
}
}
*/
d = d + "]}" d = d + "]}"
do := []byte(d) do := []byte(d)
ioutil.WriteFile("mc_man.config", do, 0664) ioutil.WriteFile("mc_man.config", do, 0664)
} }
func SetHome(user string, loc string) { func SetHome(user string, loc string) {
u, idx := FindUser(user) u := FindUser(user, true)
if idx == -1 { if u.Index != -1 {
u = NewUser(user) u.Home = strings.Replace(loc, "\n", "", -1)
c.Users = append(c.Users, u) // Replace the user in the Users array
idx = len(c.Users) - 1 c.Users[u.Index] = u
WriteConfig()
} }
u.Home = strings.Replace(loc, "\n", "", -1)
// Replace the user in the Users array
c.Users[idx] = u
WriteConfig()
} }
func GetHome(user string) (string, bool) { func GetHome(user string) (string, bool) {
u, idx := FindUser(user) u := FindUser(user, false)
if idx == -1 || u.Home == "" { if u.Index == -1 || u.Home == "" {
return "", false return "", false
} }
return u.Home, true return u.Home, true
} }
func SetPorch(user string, loc string) { func SetPorch(user string, loc string) {
u, idx := FindUser(user) u := FindUser(user, true)
if idx == -1 { if u.Index != -1 {
u = NewUser(user) u.Porch = strings.Replace(loc, "\n", "", -1)
c.Users = append(c.Users, u) c.Users[u.Index] = u
idx = len(c.Users) - 1 WriteConfig()
} }
u.Porch = strings.Replace(loc, "\n", "", -1)
c.Users[idx] = u
WriteConfig()
} }
func GetPorch(user string) (string, bool) { func GetPorch(user string) (string, bool) {
u, idx := FindUser(user) u := FindUser(user, false)
if idx == -1 || u.Porch == "" { if u.Index == -1 || u.Porch == "" {
return "", false return "", false
} }
return u.Porch, true return u.Porch, true
} }
func FindUser(name string) (*User, int) { func FindUser(name string, create bool) *User {
for i, user := range c.Users { for _, user := range c.Users {
if user.Name == name { if user.Name == name {
return user, i return user
} }
} }
return nil, -1 if create && name != "" {
AddUser(name)
return FindUser(name, false)
}
return NewUser("")
}
func GetConfig() *Config {
return c
} }

View File

@ -1,7 +1,6 @@
package util package util
import ( import (
//"fmt"
"regexp" "regexp"
"strings" "strings"
) )
@ -10,13 +9,7 @@ type Message struct {
User *User User *User
Text string Text string
IsStopRequest func() bool Output func() string
IsHomeRequest func() bool
IsSetHomeRequest func() bool
IsVisitRequest func() bool
IsSetPorchRequest func() bool
VisitingUser func() (string, bool)
Output func() string
} }
func NewMessage(t string) *Message { func NewMessage(t string) *Message {
@ -25,65 +18,17 @@ func NewMessage(t string) *Message {
tmpUser := msg_user.FindString(t) tmpUser := msg_user.FindString(t)
tmpUser = strings.Replace(tmpUser, "<", "", -1) tmpUser = strings.Replace(tmpUser, "<", "", -1)
tmpUser = strings.Replace(tmpUser, ">", "", -1) tmpUser = strings.Replace(tmpUser, ">", "", -1)
m.User = NewUser(tmpUser) m.User = FindUser(tmpUser, true)
if tmpUser == "br0xen" {
m.User.IsOp = true
}
m.Text = t m.Text = t
if m.User.Name != "" { if m.User.Index != -1 && m.User.Name != "" {
res := strings.Split(t, "<"+m.User.Name+"> ") res := strings.Split(t, "<"+m.User.Name+"> ")
if len(res) > 0 { if len(res) > 0 {
m.Text = res[1] m.Text = res[1]
} }
} }
m.IsStopRequest = func() bool {
return (m.User.IsOp && m.Text == "!stop\n")
}
m.IsHomeRequest = func() bool {
if m.User.Name != "" {
return (m.Text == "!home\n")
} else {
return false
}
}
m.IsSetHomeRequest = func() bool {
if m.User.Name != "" {
return (m.Text == "!set home\n")
} else {
return false
}
}
m.IsSetPorchRequest = func() bool {
if m.User.Name != "" {
return (m.Text == "!set porch\n")
} else {
return false
}
}
m.IsVisitRequest = func() bool {
if m.User.Name != "" {
return strings.HasPrefix(m.Text, "!visit ")
} else {
return false
}
}
m.VisitingUser = func() (string, bool) {
if m.IsVisitRequest() {
s := strings.Replace(m.Text, "!visit ", "", -1)
s = strings.Replace(s, "\n", "", -1)
return s, false
}
return "", true
}
m.Output = func() string { m.Output = func() string {
if m.User.Name != "" { if m.User.Index != -1 && m.User.Name != "" {
return "<" + m.User.Name + "> " + m.Text return "<" + m.User.Name + "> " + m.Text
} else { } else {
return m.Text return m.Text

View File

@ -34,25 +34,39 @@ type MessageManager struct {
var Listeners []func(inp *Message) bool var Listeners []func(inp *Message) bool
var TempListeners []func(inp *Message) bool var TempListeners []func(inp *Message) bool
func (mm MessageManager) Tell(user string, what string, color string) {
valid_color := false
switch color {
case "black", "dark_blue", "dark_green", "dark_aqua",
"dark_red", "dark_purple", "gold", "gray", "dark_gray",
"blue", "green", "aqua", "red", "light_purple", "yellow",
"white":
valid_color = true
}
if !valid_color {
color = "white"
}
strings.Replace(what, "\n", "", -1)
o := "tellraw " + user + " {text:\"" + what + "\",color:" + color + "}"
mm.Output(o)
}
func (mm MessageManager) TellRaw(user string, what string) {
o := "tellraw " + user + " {text:\"" + what + "\"}"
mm.Output(o)
}
func (mm MessageManager) Output(o string) { func (mm MessageManager) Output(o string) {
if !strings.HasSuffix("\n", o) { if !strings.HasSuffix("\n", o) {
o = o + "\n" o = o + "\n"
} }
mm.output.Write([]byte(o)) mm.output.Write([]byte(o))
fmt.Printf("%s", o)
} }
func (mm MessageManager) ProcessMessage(inp string) bool { func (mm MessageManager) ProcessMessage(inp string) bool {
// First of all, create the message from inp // First of all, create the message from inp
m := NewMessage(inp) m := NewMessage(inp)
// Check if we know the user that sent the message
if m.User.Name != "" {
_, idx := FindUser(m.User.Name)
if idx == -1 {
// Nope, create a new user
fmt.Printf("Adding New User: %s\n", m.User.Name)
AddUser(m.User.Name)
}
}
// Now run the message through all of mm's tempListeners // Now run the message through all of mm's tempListeners
for i := range TempListeners { for i := range TempListeners {
// Pop the listener off of the stack // Pop the listener off of the stack

View File

@ -2,6 +2,7 @@ package util
type User struct { type User struct {
Name string Name string
Index int
IsOp bool IsOp bool
Home string Home string
Porch string Porch string
@ -11,6 +12,9 @@ type User struct {
func NewUser(nm string) *User { func NewUser(nm string) *User {
m := new(User) m := new(User)
m.Name = nm m.Name = nm
if nm == "" {
m.Index = -1
}
m.IsOp = false m.IsOp = false
m.Home = "" m.Home = ""
m.Porch = "" m.Porch = ""