banPlayer.go 3.3 KB

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