ops.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. mch.reloadOp()
  27. newItem := Op{}
  28. newItem = append(newItem, struct {
  29. UUID string `json:"uuid"`
  30. Name string `json:"name"`
  31. Level int `json:"level"`
  32. BypassesPlayerLimit bool `json:"bypassesPlayerLimit"`
  33. }{UUID, Name, Level, BypassesPlayerLimit})
  34. mch.ops = append(mch.ops, newItem...)
  35. mch.SaveAllOps()
  36. return true
  37. }
  38. //ReadOps is exported function
  39. func (mch *Handler) ReadOps(search string, field string) Op {
  40. var list Op
  41. for _, item := range mch.ops {
  42. fieldValue := ""
  43. switch strings.ToLower(field) {
  44. case "uuid":
  45. fieldValue = item.UUID
  46. case "name":
  47. fieldValue = item.Name
  48. case "level":
  49. fieldValue = strconv.Itoa(item.Level)
  50. case "bypassesplayerlimit":
  51. fieldValue = strconv.FormatBool(item.BypassesPlayerLimit)
  52. default:
  53. fieldValue = ""
  54. }
  55. if fieldValue == search {
  56. list = append(list, item)
  57. }
  58. }
  59. return list
  60. }
  61. //RemoveOps is exported function
  62. func (mch *Handler) RemoveOps(search string, field string) bool {
  63. mch.reloadOp()
  64. for i, item := range mch.ops {
  65. fieldValue := ""
  66. switch strings.ToLower(field) {
  67. case "uuid":
  68. fieldValue = item.UUID
  69. case "name":
  70. fieldValue = item.Name
  71. case "level":
  72. fieldValue = strconv.Itoa(item.Level)
  73. case "bypassesplayerlimit":
  74. fieldValue = strconv.FormatBool(item.BypassesPlayerLimit)
  75. default:
  76. fieldValue = ""
  77. }
  78. if fieldValue == search {
  79. if len(mch.ops)-1 != i {
  80. mch.ops = append(mch.ops[:i], mch.ops[i+1:]...)
  81. } else {
  82. // if it is the last item, just remove it
  83. mch.ops = mch.ops[:i]
  84. }
  85. }
  86. }
  87. mch.SaveAllOps()
  88. return true
  89. }
  90. //SaveAllOps is exported function
  91. func (mch *Handler) SaveAllOps() bool {
  92. JSON, _ := json.Marshal(mch.ops)
  93. err := ioutil.WriteFile(mch.serverFolder+"/ops.json", JSON, 0777)
  94. if err != nil {
  95. fmt.Println(err)
  96. }
  97. return true
  98. }
  99. //CheckDuplicateOps is exported function
  100. func (mch *Handler) CheckDuplicateOps(uuid string, name string) bool {
  101. for _, item := range mch.ops {
  102. if item.UUID == uuid {
  103. return true
  104. }
  105. if item.Name == name {
  106. return true
  107. }
  108. }
  109. return false
  110. }