package config import ( "encoding/json" "fmt" "io/ioutil" "os" "strconv" "strings" ) func initOps(serverFolder string) Op { jsonFile, err := os.Open(serverFolder + "ops.json") if err != nil { fmt.Println(err) } byte, _ := ioutil.ReadAll(jsonFile) var dataArray Op json.Unmarshal(byte, &dataArray) return dataArray } //ReadAllOps is exported function func (mch *Handler) ReadAllOps() Op { return mch.ops } //WriteOps is exported function func (mch *Handler) WriteOps(UUID string, Name string, Level int, BypassesPlayerLimit bool) bool { mch.reloadOp() newItem := Op{} newItem = append(newItem, struct { UUID string `json:"uuid"` Name string `json:"name"` Level int `json:"level"` BypassesPlayerLimit bool `json:"bypassesPlayerLimit"` }{UUID, Name, Level, BypassesPlayerLimit}) mch.ops = append(mch.ops, newItem...) mch.SaveAllOps() return true } //ReadOps is exported function func (mch *Handler) ReadOps(search string, field string) Op { var list Op for _, item := range mch.ops { fieldValue := "" switch strings.ToLower(field) { case "uuid": fieldValue = item.UUID case "name": fieldValue = item.Name case "level": fieldValue = strconv.Itoa(item.Level) case "bypassesplayerlimit": fieldValue = strconv.FormatBool(item.BypassesPlayerLimit) default: fieldValue = "" } if fieldValue == search { list = append(list, item) } } return list } //RemoveOps is exported function func (mch *Handler) RemoveOps(search string, field string) bool { mch.reloadOp() for i, item := range mch.ops { fieldValue := "" switch strings.ToLower(field) { case "uuid": fieldValue = item.UUID case "name": fieldValue = item.Name case "level": fieldValue = strconv.Itoa(item.Level) case "bypassesplayerlimit": fieldValue = strconv.FormatBool(item.BypassesPlayerLimit) default: fieldValue = "" } if fieldValue == search { if len(mch.ops)-1 != i { mch.ops = append(mch.ops[:i], mch.ops[i+1:]...) } else { // if it is the last item, just remove it mch.ops = mch.ops[:i] } } } mch.SaveAllOps() return true } //SaveAllOps is exported function func (mch *Handler) SaveAllOps() bool { JSON, _ := json.Marshal(mch.ops) err := ioutil.WriteFile(mch.serverFolder+"/ops.json", JSON, 0777) if err != nil { fmt.Println(err) } return true } //CheckDuplicateOps is exported function func (mch *Handler) CheckDuplicateOps(uuid string, name string) bool { for _, item := range mch.ops { if item.UUID == uuid { return true } if item.Name == name { return true } } return false }