From 666125b50ff6bee8ec27e47cedd1999fe4a9327b Mon Sep 17 00:00:00 2001 From: Elliott Pardee Date: Sat, 28 Feb 2015 06:46:44 -0500 Subject: [PATCH] irc-osric is now complete, all that's left is bugfixes. --- README.md | 7 +++---- bot.go | 25 ++++++++++++++++++++++++ character.go | 55 +++++++++++++++++++++++++++++++++++++++++++++++----- 3 files changed, 78 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 684044a..8f90515 100755 --- a/README.md +++ b/README.md @@ -15,9 +15,8 @@ TODO: * ~~Dictionary for abbreviations (hp, ap, algn, xp)~~ * ~~Dice System~~ * ~~Voting System~~ -* Save Progress Midway -* Character System +* ~~Character System~~ * ~~Importing Characters~~ * ~~Reading (printing) Character Info~~ - * Writing Character Info - * Exporting Characters + * ~~Writing Character Info~~ + * ~~Exporting Characters~~ diff --git a/bot.go b/bot.go index ea9561d..6eee102 100755 --- a/bot.go +++ b/bot.go @@ -48,6 +48,7 @@ var dict = map[string]string{ } var argmap = map[string]int{ + ".set": 4, ".print": 3, ".vote": 1, ".d": 1, @@ -129,6 +130,30 @@ func (b *Bot) Command(nick string, msg string) { fmt.Println("[cmd] import " + args[0]) } + case ".set": + if nick == dunmas { + if len(args) == 5 { + if setChar(args[0], args[1], args[2], args[3], args[4]) { + fmt.Println("[cmd] set") + } + } else { + if setChar(args[0], args[1], "nil", args[2], args[3]) { + fmt.Println("[cmd] set") + } + } + } else if stringInSlice(nick, admins) && stringInSlice(modeopt[0], rulemod) { + if len(args) == 5 { + if setChar(args[0], args[1], args[2], args[3], args[4]) { + fmt.Println("[cmd] set") + b.Say(nick + " used override, it's super effective!") + } + } else { + if setChar(args[0], args[1], "nil", args[2], args[3]) { + fmt.Println("[cmd] set") + b.Say(nick + " used override, it's super effective!") + } + } + } case ".print": if len(args) == 4 { b.Say(args[0] + "[" + args[3] + "] = " + printChar(args[0], args[1], args[2], args[3])) diff --git a/character.go b/character.go index d292eb2..15d5f5e 100644 --- a/character.go +++ b/character.go @@ -4,7 +4,9 @@ package main import ( "github.com/antonholmquist/jason" "io/ioutil" + "os" "strconv" + "strings" ) var charmap map[string]*jason.Object @@ -25,12 +27,10 @@ func importChar(nick string) bool { return true } -func exportChar(nick string) { - // TODO -} - func printChar(nick string, cat string, scat string, item string) string { - // TODO: Checking if nick exists, to prevent crashing the bot. + if _, err := os.Stat("json/" + nick + ".json"); os.IsNotExist(err) { + return "does not exist" + } if scat == "nil" { test, _ := charmap[nick].GetString(cat, item) @@ -58,3 +58,48 @@ func printChar(nick string, cat string, scat string, item string) string { return "does not exist" } } + +func setChar(nick string, cat string, scat string, item string, value string) bool { + file, err := ioutil.ReadFile("json/" + nick + ".json") + + if err != nil { + return false + } + + lines := strings.Split(string(file), "\n") + + for i, l := range lines { + if strings.Contains(l, item) { + if scat == "nil" { + test, _ := charmap[nick].GetString(cat, item) + + if len(test) == 0 { + lines[i] = "\"" + item + "\": " + value + "," + } else { + lines[i] = "\"" + item + "\": \"" + value + "\"," + } + } else { + test, _ := charmap[nick].GetString(cat, scat, item) + + if len(test) == 0 { + lines[i] = "\"" + item + "\": " + value + "," + } else { + lines[i] = "\"" + item + "\": \"" + value + "\"," + } + } + } + } + + oput := strings.Join(lines, "\n") + err = ioutil.WriteFile("json/"+nick+".json", []byte(oput), 0644) + + if err != nil { + return false + } + + if !importChar(nick) { + return false + } + + return true +}