ops.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. var list Op
  62. for i, item := range mch.ops {
  63. fieldValue := ""
  64. switch strings.ToLower(field) {
  65. case "uuid":
  66. fieldValue = item.UUID
  67. case "name":
  68. fieldValue = item.Name
  69. case "level":
  70. fieldValue = strconv.Itoa(item.Level)
  71. case "bypassesplayerlimit":
  72. fieldValue = strconv.FormatBool(item.BypassesPlayerLimit)
  73. default:
  74. fieldValue = ""
  75. }
  76. if fieldValue == search {
  77. list = append(list[:i], list[i+1])
  78. }
  79. }
  80. return true
  81. }
  82. //SaveAllOps is exported function
  83. func (mch *Handler) SaveAllOps() bool {
  84. JSON, _ := json.Marshal(mch.ops)
  85. err := ioutil.WriteFile(mch.serverFolder+"/banned-players.json", JSON, 0777)
  86. if err != nil {
  87. fmt.Println(err)
  88. }
  89. return true
  90. }