ops.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package config
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io/ioutil"
  6. "os"
  7. "strconv"
  8. "strings"
  9. )
  10. func initOps(serverFolder string) Op {
  11. jsonFile, err := os.Open(serverFolder + "ops.json")
  12. if err != nil {
  13. fmt.Println(err)
  14. }
  15. byte, _ := ioutil.ReadAll(jsonFile)
  16. var dataArray Op
  17. json.Unmarshal(byte, &dataArray)
  18. return dataArray
  19. }
  20. //ReadAllOps is exported function
  21. func (mch *Handler) ReadAllOps() Op {
  22. return mch.ops
  23. }
  24. //WriteOps is exported function
  25. func (mch *Handler) WriteOps(UUID string, Name string, Level int, BypassesPlayerLimit bool) bool {
  26. newItem := Op{}
  27. newItem = append(newItem, struct {
  28. UUID string `json:"uuid"`
  29. Name string `json:"name"`
  30. Level int `json:"level"`
  31. BypassesPlayerLimit bool `json:"bypassesPlayerLimit"`
  32. }{UUID, Name, Level, BypassesPlayerLimit})
  33. mch.ops = append(mch.ops, newItem...)
  34. return true
  35. }
  36. //ReadOps is exported function
  37. func (mch *Handler) ReadOps(search string, field string) Op {
  38. var list Op
  39. for _, item := range mch.ops {
  40. fieldValue := ""
  41. switch strings.ToLower(field) {
  42. case "uuid":
  43. fieldValue = item.UUID
  44. case "name":
  45. fieldValue = item.Name
  46. case "level":
  47. fieldValue = strconv.Itoa(item.Level)
  48. case "bypassesplayerlimit":
  49. fieldValue = strconv.FormatBool(item.BypassesPlayerLimit)
  50. default:
  51. fieldValue = ""
  52. }
  53. if fieldValue == search {
  54. list = append(list, item)
  55. }
  56. }
  57. return list
  58. }
  59. //RemoveOps is exported function
  60. func (mch *Handler) RemoveOps(search string, field string) bool {
  61. for i, item := range mch.ops {
  62. fieldValue := ""
  63. switch strings.ToLower(field) {
  64. case "uuid":
  65. fieldValue = item.UUID
  66. case "name":
  67. fieldValue = item.Name
  68. case "level":
  69. fieldValue = strconv.Itoa(item.Level)
  70. case "bypassesplayerlimit":
  71. fieldValue = strconv.FormatBool(item.BypassesPlayerLimit)
  72. default:
  73. fieldValue = ""
  74. }
  75. if fieldValue == search {
  76. if len(mch.ops)-1 != i {
  77. mch.ops = append(mch.ops[:i], mch.ops[i+1:]...)
  78. } else {
  79. // if it is the last item, just remove it
  80. mch.ops = mch.ops[:i]
  81. }
  82. }
  83. }
  84. return true
  85. }
  86. //SaveAllOps is exported function
  87. func (mch *Handler) SaveAllOps() bool {
  88. JSON, _ := json.Marshal(mch.ops)
  89. err := ioutil.WriteFile(mch.serverFolder+"/ops.json", JSON, 0777)
  90. if err != nil {
  91. fmt.Println(err)
  92. }
  93. return true
  94. }