NOW Things are Rolling
This commit is contained in:
parent
01f9aaa035
commit
c17e4dc06d
259
util/config.go
259
util/config.go
@ -12,12 +12,12 @@ import (
|
||||
|
||||
type Config struct {
|
||||
// The JSON object of what was read
|
||||
LoadedJson jason.Object
|
||||
Options jason.Object
|
||||
FeatureTPHome bool
|
||||
FeatureTPVisit bool
|
||||
Users []*User
|
||||
U User
|
||||
LoadedJson jason.Object
|
||||
Options jason.Object
|
||||
FeatureTPHome bool
|
||||
FeatureTPVisit bool
|
||||
FeatureDayNight bool
|
||||
Users []*User
|
||||
}
|
||||
|
||||
var c *Config
|
||||
@ -30,6 +30,7 @@ func LoadConfig(mm *MessageManager) {
|
||||
o, _ := j.GetObjectArray("options")
|
||||
|
||||
// Add the "Stop" listener
|
||||
fmt.Println("Activating 'stop' listener")
|
||||
AddListener(func(i *Message) bool {
|
||||
//listeners = append(mm.listeners, func(i *Message) bool {
|
||||
if i.User.IsOp && i.Text == "!stop\n" {
|
||||
@ -44,48 +45,136 @@ func LoadConfig(mm *MessageManager) {
|
||||
opt_enabled, _ := option.GetBoolean("enabled")
|
||||
if opt_name == "home" {
|
||||
c.FeatureTPHome = opt_enabled
|
||||
// Add the listener for "!set home"
|
||||
// Add !set home listener
|
||||
AddListener(func(i *Message) bool {
|
||||
// mm.listeners = append(mm.listeners, func(i *Message) bool {
|
||||
if i.User.Name != "" && i.Text == "!set home\n" {
|
||||
AddTempListener(func(inp *Message) bool {
|
||||
//mm.tempListeners = append(mm.tempListeners, func(inp *Message) bool {
|
||||
listen_for := "Teleported " + i.User.Name + " to "
|
||||
if inp.User.Name == "" && strings.Contains(inp.Text, listen_for) {
|
||||
// Found the text
|
||||
r := strings.Split(inp.Text, listen_for)
|
||||
if len(r) > 0 {
|
||||
p_str := r[1]
|
||||
p_str = strings.Replace(p_str, ",", "", -1)
|
||||
SetHome(i.User.Name, p_str)
|
||||
mm.Output("tell " + i.User.Name + " Set your home to " + p_str)
|
||||
return true
|
||||
if opt_enabled {
|
||||
fmt.Println("Activating 'home' listeners")
|
||||
// Add !set home listener
|
||||
AddListener(func(i *Message) bool {
|
||||
if i.User.Name != "" && i.Text == "!set home\n" {
|
||||
AddTempListener(func(inp *Message) bool {
|
||||
listen_for := "Teleported " + i.User.Name + " to "
|
||||
if inp.User.Name == "" && strings.Contains(inp.Text, listen_for) {
|
||||
// Found the text
|
||||
r := strings.Split(inp.Text, listen_for)
|
||||
if len(r) > 0 {
|
||||
p_str := r[1]
|
||||
p_str = strings.Replace(p_str, ",", "", -1)
|
||||
SetHome(i.User.Name, p_str)
|
||||
mm.Output("tellraw " + i.User.Name + " Set your home to " + p_str)
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
})
|
||||
mm.Output("tp " + i.User.Name + " ~ ~ ~")
|
||||
return true
|
||||
}
|
||||
return false
|
||||
})
|
||||
// Add !home listener
|
||||
AddListener(func(i *Message) bool {
|
||||
//mm.listeners = append(mm.listeners, func(i *Message) bool {
|
||||
if i.User.Name != "" && i.Text == "!home\n" {
|
||||
home_str, found := GetHome(i.User.Name)
|
||||
if found {
|
||||
mm.Output("tp " + i.User.Name + " " + home_str)
|
||||
} else {
|
||||
mm.Output("tell " + i.User.Name + " I don't know where your home is. Set it to your current position by typing '!set home'")
|
||||
return false
|
||||
})
|
||||
mm.Output("tp " + i.User.Name + " ~ ~ ~")
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
})
|
||||
return false
|
||||
})
|
||||
// Add !home listener
|
||||
AddListener(func(i *Message) bool {
|
||||
if i.User.Name != "" && i.Text == "!home\n" {
|
||||
home_str, found := GetHome(i.User.Name)
|
||||
if found {
|
||||
mm.Output("tp " + i.User.Name + " " + home_str)
|
||||
} 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'")
|
||||
}
|
||||
}
|
||||
return false
|
||||
})
|
||||
}
|
||||
} else if opt_name == "visit" {
|
||||
c.FeatureTPVisit = opt_enabled
|
||||
if opt_enabled {
|
||||
fmt.Println("Activating 'visit' listeners")
|
||||
// Add !set porch listener
|
||||
AddListener(func(i *Message) bool {
|
||||
if i.User.Name != "" && i.Text == "!set porch\n" {
|
||||
AddTempListener(func(inp *Message) bool {
|
||||
listen_for := "Teleported " + i.User.Name + " to "
|
||||
if inp.User.Name == "" && strings.Contains(inp.Text, listen_for) {
|
||||
// Found the text
|
||||
r := strings.Split(inp.Text, listen_for)
|
||||
if len(r) > 0 {
|
||||
p_str := r[1]
|
||||
p_str = strings.Replace(p_str, ",", "", -1)
|
||||
SetPorch(i.User.Name, p_str)
|
||||
mm.Output("tellraw " + i.User.Name + " Set your porch to " + p_str)
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
})
|
||||
mm.Output("tp " + i.User.Name + " ~ ~ ~")
|
||||
return true
|
||||
}
|
||||
return false
|
||||
})
|
||||
// Add !visit listener
|
||||
AddListener(func(i *Message) bool {
|
||||
if i.User.Name != "" && strings.HasPrefix(i.Text, "!visit ") {
|
||||
// Find the user we're trying to visit
|
||||
r := strings.Split(strings.Replace(i.Text, "\n", "", -1), "!visit ")
|
||||
if len(r) > 0 {
|
||||
username := r[1]
|
||||
porch_str, found := GetPorch(username)
|
||||
if found {
|
||||
mm.Output("tp " + i.User.Name + " " + porch_str)
|
||||
} 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'")
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
})
|
||||
}
|
||||
} else if opt_name == "daynight" {
|
||||
c.FeatureDayNight = opt_enabled
|
||||
if opt_enabled {
|
||||
fmt.Println("Activating 'daynight' listeners")
|
||||
// Add !switch day listener
|
||||
AddListener(func(i *Message) bool {
|
||||
if i.User.Name != "" && i.Text == "!switch day\n" {
|
||||
// TODO: Start vote
|
||||
mm.Output("time set day")
|
||||
return true
|
||||
}
|
||||
return false
|
||||
})
|
||||
// Add !switch night listener
|
||||
AddListener(func(i *Message) bool {
|
||||
if i.User.Name != "" && i.Text == "!switch night\n" {
|
||||
// TODO: Start vote
|
||||
mm.Output("time set night")
|
||||
return true
|
||||
}
|
||||
return false
|
||||
})
|
||||
}
|
||||
}
|
||||
// Add !help listener
|
||||
AddListener(func(i *Message) bool {
|
||||
if i.User.Name != "" && i.Text == "!help\n" {
|
||||
hlp_txt := " {text:\"-=( mc_man Manager Help )=-\",color:blue}"
|
||||
mm.Output("tellraw " + i.User.Name + hlp_txt)
|
||||
hlp_txt = " {text:\"!set home -- Set your 'home' to your current position.\"}"
|
||||
mm.Output("tellraw " + i.User.Name + hlp_txt)
|
||||
hlp_txt = " {text:\"!home -- Request a teleport to your 'home' position.\"}"
|
||||
mm.Output("tellraw " + i.User.Name + hlp_txt)
|
||||
hlp_txt = " {text:\"!set porch -- Set your 'porch' to your current position.\"}"
|
||||
mm.Output("tellraw " + i.User.Name + hlp_txt)
|
||||
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 false
|
||||
})
|
||||
}
|
||||
|
||||
c.Users = make([]*User, 0, 10)
|
||||
@ -100,13 +189,18 @@ func LoadConfig(mm *MessageManager) {
|
||||
us.Home = user_home
|
||||
us.Porch = user_porch
|
||||
c.Users = append(c.Users, us)
|
||||
c.U = *us
|
||||
}
|
||||
}
|
||||
fmt.Printf("Loaded %d Users\n", len(c.Users))
|
||||
}
|
||||
}
|
||||
|
||||
func AddUser(username string) {
|
||||
us := NewUser(username)
|
||||
c.Users = append(c.Users, us)
|
||||
WriteConfig()
|
||||
}
|
||||
|
||||
func WriteConfig() {
|
||||
// Generate the JSON string for the config file
|
||||
d := "{\"options\":["
|
||||
@ -123,9 +217,17 @@ func WriteConfig() {
|
||||
} else {
|
||||
d = d + "false"
|
||||
}
|
||||
d = d + "},{\"name\":\"daynight\",\"enabled\":"
|
||||
if c.FeatureDayNight {
|
||||
d = d + "true"
|
||||
} else {
|
||||
d = d + "false"
|
||||
}
|
||||
d = d + "}],\"users\":["
|
||||
d = d + c.U.ToJSONString()
|
||||
// Output users array
|
||||
for _, u := range c.Users {
|
||||
d = d + u.ToJSONString()
|
||||
}
|
||||
/*
|
||||
num_users := len(c.Users)
|
||||
fmt.Printf("Number of Users: %d", num_users)
|
||||
@ -144,59 +246,48 @@ func WriteConfig() {
|
||||
*/
|
||||
d = d + "]}"
|
||||
do := []byte(d)
|
||||
ioutil.WriteFile("mc_man.config", do, 0644)
|
||||
ioutil.WriteFile("mc_man.config", do, 0664)
|
||||
}
|
||||
|
||||
func SetHome(user string, loc string) {
|
||||
/*
|
||||
u, idx := FindUser(user)
|
||||
if idx == -1 {
|
||||
u = NewUser(user)
|
||||
c.Users = append(c.Users, u)
|
||||
idx = len(c.Users) - 1
|
||||
}
|
||||
u.Home = strings.Replace(loc, "\n", "", -1)
|
||||
// Replace the user in the Users array
|
||||
c.Users[idx] = u
|
||||
*/
|
||||
c.U.Home = strings.Replace(loc, "\n", "", -1)
|
||||
u, idx := FindUser(user)
|
||||
if idx == -1 {
|
||||
u = NewUser(user)
|
||||
c.Users = append(c.Users, u)
|
||||
idx = len(c.Users) - 1
|
||||
}
|
||||
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) {
|
||||
/*
|
||||
u, idx := FindUser(user)
|
||||
if idx == -1 || u.Home == "" {
|
||||
return "", false
|
||||
}
|
||||
*/
|
||||
return c.U.Home, true
|
||||
u, idx := FindUser(user)
|
||||
if idx == -1 || u.Home == "" {
|
||||
return "", false
|
||||
}
|
||||
return u.Home, true
|
||||
}
|
||||
|
||||
func SetPorch(user string, loc string) {
|
||||
/*
|
||||
u, idx := FindUser(user)
|
||||
if idx == -1 {
|
||||
u = NewUser(user)
|
||||
c.Users = append(c.Users, u)
|
||||
idx = len(c.Users) - 1
|
||||
}
|
||||
u.Porch = strings.Replace(loc, "\n", "", -1)
|
||||
c.Users[idx] = u
|
||||
*/
|
||||
c.U.Porch = strings.Replace(loc, "\n", "", -1)
|
||||
u, idx := FindUser(user)
|
||||
if idx == -1 {
|
||||
u = NewUser(user)
|
||||
c.Users = append(c.Users, u)
|
||||
idx = len(c.Users) - 1
|
||||
}
|
||||
u.Porch = strings.Replace(loc, "\n", "", -1)
|
||||
c.Users[idx] = u
|
||||
WriteConfig()
|
||||
}
|
||||
|
||||
func GetPorch(user string) (string, bool) {
|
||||
/*
|
||||
u, idx := FindUser(user)
|
||||
if idx > -1 || u.Porch == "" {
|
||||
return "", false
|
||||
}
|
||||
return u.Porch, true
|
||||
*/
|
||||
return c.U.Porch, true
|
||||
u, idx := FindUser(user)
|
||||
if idx == -1 || u.Porch == "" {
|
||||
return "", false
|
||||
}
|
||||
return u.Porch, true
|
||||
}
|
||||
|
||||
func FindUser(name string) (*User, int) {
|
||||
|
@ -44,22 +44,27 @@ func (mm MessageManager) Output(o string) {
|
||||
func (mm MessageManager) ProcessMessage(inp string) bool {
|
||||
// First of all, create the message from inp
|
||||
m := NewMessage(inp)
|
||||
fmt.Printf("[%d]l, [%d]tmpl: Processing Message: >>> %s\n", len(Listeners), len(TempListeners), strings.Replace(inp, "\n", "", -1))
|
||||
// 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
|
||||
for i := range TempListeners {
|
||||
fmt.Printf("Processing message in tempListener %d\n", i)
|
||||
// Pop the listener off of the stack
|
||||
consumed := TempListeners[i](m)
|
||||
if consumed {
|
||||
// When a temp listener is consumed, we delete it
|
||||
fmt.Println("Consumed Event from Temporary Listener.")
|
||||
RemoveTempListener(i)
|
||||
return true
|
||||
}
|
||||
}
|
||||
// and run the message through all of mm's listeners
|
||||
for i := range Listeners {
|
||||
fmt.Printf("Processing message in listener %d\n", i)
|
||||
consumed := Listeners[i](m)
|
||||
if consumed {
|
||||
return true
|
||||
@ -81,7 +86,7 @@ func RemoveListener(i int) {
|
||||
if i > -1 && i < len(Listeners) {
|
||||
t := append(Listeners[:i], Listeners[i+1:]...)
|
||||
Listeners = make(([]func(*Message) bool), len(t))
|
||||
copy(LIsteners, t)
|
||||
copy(Listeners, t)
|
||||
}
|
||||
}
|
||||
func RemoveTempListener(i int) {
|
||||
|
Loading…
Reference in New Issue
Block a user