Part 1 of Characters, importing and reading

This commit is contained in:
Elliott Pardee 2015-02-26 05:24:31 -05:00
parent b537551405
commit 0580748b9c
4 changed files with 90 additions and 32 deletions

View File

@ -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

50
bot.go
View File

@ -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

60
character.go Normal file
View File

@ -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"
}
}

2
run.sh Executable file
View File

@ -0,0 +1,2 @@
#!/bin/bash
go run bot.go character.go smap.go slices.go