123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- package config
- import (
- "encoding/json"
- "fmt"
- "io/ioutil"
- "log"
- "os"
- "strings"
- )
- func initBannedPlayer(serverFolder string) BannedPlayer {
- jsonFile, err := os.Open(serverFolder + "/banned-players.json")
- if err != nil {
- fmt.Println(err)
- }
- byte, _ := ioutil.ReadAll(jsonFile)
- var dataArray BannedPlayer
- json.Unmarshal(byte, &dataArray)
- return dataArray
- }
- //ReadAllBannedPlayers is exported function
- func (mch *Handler) ReadAllBannedPlayers() BannedPlayer {
- return mch.bannedPlayers
- }
- //WriteBannedPlayer is exported function
- func (mch *Handler) WriteBannedPlayer(UUID string, Name string, Created string, Source string, Expires string, Reason string) bool {
- mch.reloadBanPlayer()
- newItem := BannedPlayer{}
- newItem = append(newItem, struct {
- UUID string `json:"uuid"`
- Name string `json:"name"`
- Created string `json:"created"`
- Source string `json:"source"`
- Expires string `json:"expires"`
- Reason string `json:"reason"`
- }{UUID, Name, Created, Source, Expires, Reason})
- mch.bannedPlayers = append(mch.bannedPlayers, newItem...)
- mch.SaveAllBannedPlayers()
- return true
- }
- //ReadBannedPlayer is exported function
- func (mch *Handler) ReadBannedPlayer(search string, field string) BannedPlayer {
- var list BannedPlayer
- for _, item := range mch.bannedPlayers {
- fieldValue := ""
- switch strings.ToLower(field) {
- case "uuid":
- fieldValue = item.UUID
- case "name":
- fieldValue = item.Name
- case "created":
- fieldValue = item.Created
- case "source":
- fieldValue = item.Source
- case "expires":
- fieldValue = item.Expires
- case "reason":
- fieldValue = item.Reason
- default:
- fieldValue = ""
- }
- if fieldValue == search {
- list = append(list, item)
- }
- }
- return list
- }
- //RemoveBannedPlayer is exported function
- func (mch *Handler) RemoveBannedPlayer(search string, field string) bool {
- mch.reloadBanPlayer()
- for i, item := range mch.bannedPlayers {
- fieldValue := ""
- switch strings.ToLower(field) {
- case "uuid":
- fieldValue = item.UUID
- case "name":
- fieldValue = item.Name
- case "created":
- fieldValue = item.Created
- case "source":
- fieldValue = item.Source
- case "expires":
- fieldValue = item.Expires
- case "reason":
- fieldValue = item.Reason
- default:
- fieldValue = ""
- }
- if fieldValue == search {
- log.Println(len(mch.bannedPlayers)-1, i, mch.bannedPlayers)
- if len(mch.bannedPlayers)-1 != i {
- log.Println("Runned")
- mch.bannedPlayers = append(mch.bannedPlayers[:i], mch.bannedPlayers[i+1:]...)
- } else {
- // if it is the last item, just remove it
- mch.bannedPlayers = mch.bannedPlayers[:i]
- }
- }
- }
- mch.SaveAllBannedPlayers()
- return true
- }
- //SaveAllBannedPlayers is exported function
- func (mch *Handler) SaveAllBannedPlayers() bool {
- JSON, _ := json.Marshal(mch.bannedPlayers)
- err := ioutil.WriteFile(mch.serverFolder+"/banned-players.json", JSON, 0777)
- if err != nil {
- fmt.Println(err)
- }
- return true
- }
- //CheckDuplicateBannedPlayers is exported function
- func (mch *Handler) CheckDuplicateBannedPlayers(uuid string, name string) bool {
- for _, item := range mch.bannedPlayers {
- if item.UUID == uuid {
- return true
- }
- if item.Name == name {
- return true
- }
- }
- return false
- }
|