From 0580748b9c140e4837fe80d2971cce4fc6303fa4 Mon Sep 17 00:00:00 2001 From: Elliott Pardee Date: Thu, 26 Feb 2015 05:24:31 -0500 Subject: [PATCH] Part 1 of Characters, importing and reading --- README.md | 10 +++++---- bot.go | 50 +++++++++++++++++++------------------------ character.go | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++ run.sh | 2 ++ 4 files changed, 90 insertions(+), 32 deletions(-) create mode 100644 character.go create mode 100755 run.sh diff --git a/README.md b/README.md index f0edfc9..684044a 100755 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ irc-osric [![Build Status](https://travis-ci.org/vypr/irc-osric.svg?branch=master)](https://travis-ci.org/vypr/irc-osric) -Run with `go run bot.go smap.go slices.go`. +Run with `./run.sh`. Goal: Create an IRC bot that can effectively maintain an active OSRIC session. @@ -12,10 +12,12 @@ TODO: * ~~Logging~~ * ~~Stat Manipulation~~ * ~~Permission System~~ -* ~~Character System~~ * ~~Dictionary for abbreviations (hp, ap, algn, xp)~~ * ~~Dice System~~ * ~~Voting System~~ -* Import/Export Character * Save Progress Midway -* Battle System +* Character System + * ~~Importing Characters~~ + * ~~Reading (printing) Character Info~~ + * Writing Character Info + * Exporting Characters diff --git a/bot.go b/bot.go index b013874..ea9561d 100755 --- a/bot.go +++ b/bot.go @@ -26,8 +26,6 @@ var ( modeopt = []string{"adminoverride", "saves", "logging", "voting"} initLog = true - charmap = make(map[string]map[string]map[string]string) - monsmap = make(map[string]string) votemap = make(map[string]int) filename = "log/log_" + time.Now().Local().Format("20060102") + "_" + time.Now().Local().Format("150405") + ".txt" @@ -50,28 +48,18 @@ var dict = map[string]string{ } var argmap = map[string]int{ - ".set": 4, ".print": 3, ".vote": 1, ".d": 1, ".mode": 1, ".rmmode": 1, ".dm": 1, + ".import": 1, ".choose": 0, ".resetdm": 0, ".quit": 0, } -func fillCharmap(nick string, cat string, item string, val string) { - charmap = map[string]map[string]map[string]string{ - nick: map[string]map[string]string{ - cat: map[string]string{ - item: val, - }, - }, - } -} - func roll(amount int, side int) int { var numbers = make([]int, amount) var finaln = 0 @@ -135,23 +123,19 @@ func (b *Bot) Command(nick string, msg string) { } switch command { - case ".set": - if nick == dunmas { - fillCharmap(args[0], args[1], args[2], args[3]) - fmt.Println("[cmd] set - " + args[0] + "'s " + args[2] + "in " + args[1] + " is set to " + args[3]) - } else if stringInSlice(nick, admins) && stringInSlice(modeopt[0], rulemod) { - fillCharmap(args[0], args[1], args[2], args[3]) - fmt.Println("[cmd] set - " + args[0] + "'s " + args[2] + " in " + args[1] + " is set to " + args[3]) - b.Say(nick + " used override, it's super effective!") + case ".import": + if importChar(args[0]) { + b.Say("importing " + args[0] + " successful") + fmt.Println("[cmd] import " + args[0]) } - break case ".print": - if len(charmap[args[0]][args[1]][args[2]]) == 0 { - b.Say("there is no setting for " + args[0] + "'s " + args[2]) + if len(args) == 4 { + b.Say(args[0] + "[" + args[3] + "] = " + printChar(args[0], args[1], args[2], args[3])) + fmt.Println("[cmd] print") } else { - fmt.Println("[cmd] print - " + args[0] + "'s " + args[2] + " in " + args[1]) - b.Say(args[0] + "'s " + args[2] + " is set to " + charmap[args[0]][args[1]][args[2]]) + b.Say(args[0] + "[" + args[2] + "] = " + printChar(args[0], args[1], "nil", args[2])) + fmt.Println("[cmd] print") } break @@ -221,15 +205,25 @@ func (b *Bot) Command(nick string, msg string) { break case ".resetdm": - if nick == dunmas { + if nick == dunmas && len(dunmas) > 0 { dunmas = "" fmt.Println("[cmd] resetdm") b.Say("dm has been reset") - } else if stringInSlice(nick, admins) && stringInSlice(modeopt[0], rulemod) { + + if stringInSlice(modeopt[3], rulemod) { + rulemod = append(rulemod, modeopt[3]) + b.Say("voting is now enabled") + } + } else if stringInSlice(nick, admins) && stringInSlice(modeopt[0], rulemod) && len(dunmas) > 0 { dunmas = "" fmt.Println("[cmd] resetdm") b.Say("dm has been reset") b.Say(nick + " used override, it's super effective!") + + if stringInSlice(modeopt[3], rulemod) { + rulemod = append(rulemod, modeopt[3]) + b.Say("voting is now enabled") + } } break diff --git a/character.go b/character.go new file mode 100644 index 0000000..d292eb2 --- /dev/null +++ b/character.go @@ -0,0 +1,60 @@ +// characters and moar by vypr +package main + +import ( + "github.com/antonholmquist/jason" + "io/ioutil" + "strconv" +) + +var charmap map[string]*jason.Object + +func importChar(nick string) bool { + file, err := ioutil.ReadFile("json/" + nick + ".json") + + if err != nil { + return false + } + + char, _ := jason.NewObjectFromBytes(file) + + charmap = map[string]*jason.Object{ + nick: char, + } + + 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 scat == "nil" { + test, _ := charmap[nick].GetString(cat, item) + + if len(test) == 0 { + val, _ := charmap[nick].GetInt64(cat, item) + return strconv.FormatInt(val, 10) + } else { + val, _ := charmap[nick].GetString(cat, item) + return val + } + + return "does not exist" + } else { + test, _ := charmap[nick].GetString(cat, scat, item) + + if len(test) == 0 { + val, _ := charmap[nick].GetInt64(cat, scat, item) + return strconv.FormatInt(val, 10) + } else { + val, _ := charmap[nick].GetString(cat, scat, item) + return val + } + + return "does not exist" + } +} diff --git a/run.sh b/run.sh new file mode 100755 index 0000000..ceeba75 --- /dev/null +++ b/run.sh @@ -0,0 +1,2 @@ +#!/bin/bash +go run bot.go character.go smap.go slices.go