banPlayer.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package config
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io/ioutil"
  6. "os"
  7. "strings"
  8. )
  9. func initBannedPlayer(serverFolder string) BannedPlayer {
  10. jsonFile, err := os.Open(serverFolder + "/banned-players.json")
  11. if err != nil {
  12. fmt.Println(err)
  13. }
  14. byte, _ := ioutil.ReadAll(jsonFile)
  15. var dataArray BannedPlayer
  16. json.Unmarshal(byte, &dataArray)
  17. return dataArray
  18. }
  19. //ReadAllBannedPlayers is exported function
  20. func (mch *Handler) ReadAllBannedPlayers() BannedPlayer {
  21. return mch.bannedPlayers
  22. }
  23. //WriteBannedPlayer is exported function
  24. func (mch *Handler) WriteBannedPlayer(UUID string, Name string, Created string, Source string, Expires string, Reason string) bool {
  25. newItem := BannedPlayer{}
  26. newItem = append(newItem, struct {
  27. UUID string `json:"uuid"`
  28. Name string `json:"name"`
  29. Created string `json:"created"`
  30. Source string `json:"source"`
  31. Expires string `json:"expires"`
  32. Reason string `json:"reason"`
  33. }{UUID, Name, Created, Source, Expires, Reason})
  34. mch.bannedPlayers = append(mch.bannedPlayers, newItem...)
  35. return true
  36. }
  37. //ReadBannedPlayer is exported function
  38. func (mch *Handler) ReadBannedPlayer(search string, field string) BannedPlayer {
  39. var list BannedPlayer
  40. for _, item := range mch.bannedPlayers {
  41. fieldValue := ""
  42. switch strings.ToLower(field) {
  43. case "uuid":
  44. fieldValue = item.UUID
  45. case "name":
  46. fieldValue = item.Name
  47. case "created":
  48. fieldValue = item.Created
  49. case "source":
  50. fieldValue = item.Source
  51. case "expires":
  52. fieldValue = item.Expires
  53. case "reason":
  54. fieldValue = item.Reason
  55. default:
  56. fieldValue = ""
  57. }
  58. if fieldValue == search {
  59. list = append(list, item)
  60. }
  61. }
  62. return list
  63. }
  64. //RemoveBannedPlayer is exported function
  65. func (mch *Handler) RemoveBannedPlayer(search string, field string) bool {
  66. for i, item := range mch.bannedPlayers {
  67. fieldValue := ""
  68. switch strings.ToLower(field) {
  69. case "uuid":
  70. fieldValue = item.UUID
  71. case "name":
  72. fieldValue = item.Name
  73. case "created":
  74. fieldValue = item.Created
  75. case "source":
  76. fieldValue = item.Source
  77. case "expires":
  78. fieldValue = item.Expires
  79. case "reason":
  80. fieldValue = item.Reason
  81. default:
  82. fieldValue = ""
  83. }
  84. if fieldValue == search {
  85. if len(mch.bannedPlayers)-1 != i {
  86. mch.bannedPlayers = append(mch.bannedPlayers[:i], mch.bannedPlayers[i+1])
  87. } else {
  88. // if it is the last item, just remove it
  89. mch.bannedPlayers = mch.bannedPlayers[:i]
  90. }
  91. }
  92. }
  93. return true
  94. }
  95. //SaveAllBannedPlayers is exported function
  96. func (mch *Handler) SaveAllBannedPlayers() bool {
  97. JSON, _ := json.Marshal(mch.bannedPlayers)
  98. err := ioutil.WriteFile(mch.serverFolder+"/banned-players.json", JSON, 0777)
  99. if err != nil {
  100. fmt.Println(err)
  101. }
  102. return true
  103. }