diff --git a/character.go b/character.go index b28e6f1..18a4988 100644 --- a/character.go +++ b/character.go @@ -56,6 +56,191 @@ type WealthData struct { var charmap map[string]*Character +func accessChar(set bool, nick string, cat string, item string, val string) string { + var pdata, edata, sdata, wdata bool + + switch (cat) { + case "personal": + pdata = true + break + case "equipment": + edata = true + break + case "stats": + sdata = true + break + case "wealth": + wdata = true + break + case "nil": + return charmap[nick].irc // can't be anything else but + break + default: + return "invalid category" + } + + if personal { + switch (item) { + case "name": + return charmap[nick].personal.name + break + case "lvl": + return strconv.Itoa(charmap[nick].personal.lvl) + break + case "race": + return charmap[nick].personal.race + break + case "xp": + return strconv.Itoa(charmap[nick].personal.xp) + break + case "height": + return strconv.Itoa(charmap[nick].personal.height) + break + case "alignment": + return charmap[nick].personal.alignment + break + case "classes": + var classes string + for i in range charmap[nick].personal.classes { + if len(charmap[nick].personal.classes > 0 { + if len(classes) > 0 { + classes = classes + ", " + i + } else { + classes = i + } + } + } + break + case "weight": + return strconv.Itoa(charmap[nick].personal.weight) + break + case "sex": + return charmap[nick].personal.sex + break + case "hp": + return strconv.Itoa(charmap[nick].personal.hp) + break + case "ac": + return strconv.Itoa(charmap[nick].personal.ac) + break + case "age": + return strconv.Itoa(charmap[nick].personal.age) + break + default: + return "invalid item" + break + } + } else if equipment { + switch (item) { + case "armour": + var armour string + for i in range charmap[nick].equipment.armour { + if len(charmap[nick].equipment.armour) > 0 { + if len(armour) > 0 { + armour = armour + ", " + i + } else { + armour = i + } + } + } + break + case "weapons": + var weapons string + for i in range charmap[nick].equipment.weapons { + if len(charmap[nick].equipment.weapons) > 0 { + if len(weapons) > 0 { + weapons = weapons + ", " + i + } else { + weapons = i + } + } + } + break + case "items": + var items string + for i in range charmap[nick].equipment.items { + if len(charmap[nick].equipment.items) > 0 { + if len(items) > 0 { + items = items + ", " + i + } else { + items = i + } + } + } + break + case "missiles": + var missiles string + for i in range charmap[nick].equipment.missiles { + if len(charmap[nick].equipment.missiles) > 0 { + if len(missiles) > 0 { + missiles = missiles + ", " + i + } else { + missiles = i + } + } + } + break + default: + return "invalid item" + break + } + } else if stats { + switch (item) { + case "con": + return strconv.Itoa(charmap[nick].stats.con) + break + case "str": + return strconv.Itoa(charmap[nick].stats.str) + break + case "intl": + return strconv.Itoa(charmap[nick].stats.intl) + break + case "cha": + return strconv.Itoa(charmap[nick].stats.cha) + break + case "wis": + return strconv.Itoa(charmap[nick].stats.wis) + break + case "dex": + return strconv.Itoa(charmap[nick].stats.dex) + break + default: + return "invalid item" + break + } + } else if wealth { + switch (item) { + case "coins": + return strconv.Itoa(charmap[nick].wealth.coins) + break + case "other": + var other string + for i in range charmap[nick].wealth.other { + if len(charmap[nick].wealth.other) > 0 { + if len(other) > 0 { + other = other + ", " + i + } else { + other = i + } + } + } + break + case "gems": + var gems string + for i in range charmap[nick].wealth.gems { + if len(charmap[nick].wealth.gems) > 0 { + if len(gems) > 0 { + gems = gems + ", " + i + } else { + gems = i + } + } + } + break + } + } +} + func importChar(nick string) bool { file, err := ioutil.ReadFile("json/" + nick + ".json") @@ -67,100 +252,13 @@ func importChar(nick string) bool { json.Unmarshal(file, char) - charmap = map[string]*jason.Object{ + charmap = map[string]*Character{ char.irc: char, } return true } -func printChar(nick string, cat string, scat string, item string) string { - if _, err := os.Stat("json/" + nick + ".json"); os.IsNotExist(err) { - return "does not exist" - } - - var fixedCat string - - - - switch (cat) { - case "personal": - fixedCat = "PersonalData" - break - case "equipment": - fixedCat = "EquipmentData" - break - case "stats": - fixedCat = "StatData" - break - case "wealth": - fixedCat = "WealthData" - break - case "nil": - fixedCat = "nil" - break - default: - return "invalid category" - } - - if scat == "nil" { - return charmap[nick].fixedCat. - } 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" - } -} - 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 + // With the changes I'm currently making, this will have to be changed as well. }