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"
"os"
"os/exec"
//"strings"
"time"
)
func main() {

View File

@ -18,6 +18,7 @@ type Config struct {
FeatureTPVisit bool
FeatureDayNight bool
Users []*User
LoggedInUsers []*User
}
var c *Config
@ -32,7 +33,6 @@ func LoadConfig(mm *MessageManager) {
// 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" {
mm.Output("stop")
return true
@ -58,8 +58,9 @@ func LoadConfig(mm *MessageManager) {
if len(r) > 0 {
p_str := r[1]
p_str = strings.Replace(p_str, ",", "", -1)
p_str = strings.Replace(p_str, "\n", "", -1)
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
}
}
@ -77,7 +78,7 @@ func LoadConfig(mm *MessageManager) {
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'")
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
@ -99,7 +100,7 @@ func LoadConfig(mm *MessageManager) {
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)
mm.Tell(i.User.Name, "Set your porch to "+p_str, "blue")
return true
}
}
@ -121,7 +122,7 @@ func LoadConfig(mm *MessageManager) {
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'")
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" {
// TODO: Start vote
mm.Output("time set day")
mm.Tell("@a", "Day Time switch initiated by "+i.User.Name, "yellow")
return true
}
return false
@ -146,31 +148,43 @@ func LoadConfig(mm *MessageManager) {
if i.User.Name != "" && i.Text == "!switch night\n" {
// TODO: Start vote
mm.Output("time set night")
mm.Tell("@a", "Night Time switch initiated by "+i.User.Name, "blue")
return true
}
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
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)
mm.Tell(i.User.Name, "-=( mc_man Manager Help )=-", "blue")
mm.Tell(i.User.Name, "!set home -- Set your 'home' to your current position.", "white")
mm.Tell(i.User.Name, "!home -- Request a teleport to your 'home' position.", "white")
mm.Tell(i.User.Name, "!set porch -- Set your 'porch' to your current position.", "white")
mm.Tell(i.User.Name, "!visit <username> -- Request a teleport to <username>'s 'porch' position.", "white")
mm.Tell(i.User.Name, "!switch day -- Ask the server to switch the time to 'day'.", "white")
mm.Tell(i.User.Name, "!switch night -- Ask the server to switch the time to 'night'.", "white")
mm.Tell(i.User.Name, "-=========================-", "blue")
return true
}
return false
@ -186,6 +200,9 @@ func LoadConfig(mm *MessageManager) {
user_porch, _ := user.GetString("porch")
us := NewUser(user_name)
// TODO: Check if this user is an op on the server
if user_name == "br0xen" {
us.IsOp = true
}
us.Home = user_home
us.Porch = user_porch
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) {
us := NewUser(username)
c.Users = append(c.Users, us)
WriteConfig()
if username != "" {
us := NewUser(username)
fmt.Println("Adding new user: " + username)
c.Users = append(c.Users, us)
WriteConfig()
}
}
func WriteConfig() {
// TODO: Make mc_man aware of the world
// Generate the JSON string for the config file
d := "{\"options\":["
// Output options array
@ -228,73 +270,59 @@ func WriteConfig() {
for _, u := range c.Users {
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 + "]}"
do := []byte(d)
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 := FindUser(user, true)
if u.Index != -1 {
u.Home = strings.Replace(loc, "\n", "", -1)
// Replace the user in the Users array
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) {
u, idx := FindUser(user)
if idx == -1 || u.Home == "" {
u := FindUser(user, false)
if u.Index == -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 := FindUser(user, true)
if u.Index != -1 {
u.Porch = strings.Replace(loc, "\n", "", -1)
c.Users[u.Index] = u
WriteConfig()
}
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 == "" {
u := FindUser(user, false)
if u.Index == -1 || u.Porch == "" {
return "", false
}
return u.Porch, true
}
func FindUser(name string) (*User, int) {
for i, user := range c.Users {
func FindUser(name string, create bool) *User {
for _, user := range c.Users {
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
import (
//"fmt"
"regexp"
"strings"
)
@ -10,13 +9,7 @@ type Message struct {
User *User
Text string
IsStopRequest func() bool
IsHomeRequest func() bool
IsSetHomeRequest func() bool
IsVisitRequest func() bool
IsSetPorchRequest func() bool
VisitingUser func() (string, bool)
Output func() string
Output func() string
}
func NewMessage(t string) *Message {
@ -25,65 +18,17 @@ func NewMessage(t string) *Message {
tmpUser := msg_user.FindString(t)
tmpUser = strings.Replace(tmpUser, "<", "", -1)
tmpUser = strings.Replace(tmpUser, ">", "", -1)
m.User = NewUser(tmpUser)
if tmpUser == "br0xen" {
m.User.IsOp = true
}
m.User = FindUser(tmpUser, true)
m.Text = t
if m.User.Name != "" {
if m.User.Index != -1 && m.User.Name != "" {
res := strings.Split(t, "<"+m.User.Name+"> ")
if len(res) > 0 {
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 {
if m.User.Name != "" {
if m.User.Index != -1 && m.User.Name != "" {
return "<" + m.User.Name + "> " + m.Text
} else {
return m.Text

View File

@ -34,25 +34,39 @@ type MessageManager struct {
var Listeners []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) {
if !strings.HasSuffix("\n", o) {
o = o + "\n"
}
mm.output.Write([]byte(o))
fmt.Printf("%s", o)
}
func (mm MessageManager) ProcessMessage(inp string) bool {
// First of all, create the message from 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
for i := range TempListeners {
// Pop the listener off of the stack

View File

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