-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6a4d313
commit 5c9bf14
Showing
8 changed files
with
1,705 additions
and
374 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package database | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
type IPFilter struct { | ||
Id string `gorm:"primaryKey;AutoIncrement;"` | ||
Action FilterAction | ||
Address string `gorm:"index;not null;"` | ||
Description string | ||
} | ||
|
||
type FilterAction int32 | ||
|
||
const ( | ||
ALLOW FilterAction = iota | ||
DENY | ||
) | ||
|
||
func (s *Mysql) AddIPFilter(entry *IPFilter) error { | ||
r := s.client.First(&IPFilter{}, "address = ?", entry.Address) | ||
|
||
if r.RowsAffected != 0 { | ||
return errors.New("already exists") | ||
} else if r.Error != nil && r.RowsAffected != 0 { | ||
return r.Error | ||
} | ||
|
||
result := s.client.Create(entry) | ||
if result.Error != nil { | ||
logrus.WithError(result.Error).Errorf("[IPFW] Failed AddIPFilter") | ||
return result.Error | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (s *Mysql) RemoveIPFilter(address string) error { | ||
r := s.client.Delete(&IPFilter{}, "address = ?", address) | ||
return r.Error | ||
} | ||
|
||
func (s *Mysql) GetIPFilter(address string) (*IPFilter, error) { | ||
var entry *IPFilter | ||
r := s.client.Find(&entry, "address = ?", address) | ||
return entry, r.Error | ||
} | ||
|
||
// func (s *Mysql) ListIPFilter() ([]IPFilter, error) { | ||
// var entries []IPFilter | ||
// | ||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package database | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"github.com/sirupsen/logrus" | ||
"github.com/synchthia/nebula-api/nebulapb" | ||
"gorm.io/gorm/clause" | ||
) | ||
|
||
type Players struct { | ||
ID uint `gorm:"primary_key;AutoIncrement;"` | ||
UUID string `gorm:"index;unique;"` | ||
Name string `gorm:"index;not null;"` | ||
CurrentServer string | ||
Latency int64 | ||
RawProperties string `gorm:"type:text"` | ||
} | ||
|
||
type UpdateOption struct { | ||
IsQuit bool | ||
} | ||
|
||
func (p *Players) ToProtobuf() *nebulapb.PlayerProfile { | ||
return &nebulapb.PlayerProfile{ | ||
PlayerUUID: p.UUID, | ||
PlayerName: p.Name, | ||
PlayerLatency: int64(p.Latency), | ||
CurrentServer: p.CurrentServer, | ||
Properties: func() []*nebulapb.PlayerProperty { | ||
properties := []*nebulapb.PlayerProperty{} | ||
if err := json.Unmarshal([]byte(p.RawProperties), &properties); err != nil { | ||
return nil | ||
} | ||
return properties | ||
}(), | ||
} | ||
} | ||
|
||
func PlayersFromProtobuf(p *nebulapb.PlayerProfile) *Players { | ||
return &Players{ | ||
UUID: p.PlayerUUID, | ||
Name: p.PlayerName, | ||
Latency: int64(p.PlayerLatency), | ||
CurrentServer: p.CurrentServer, | ||
RawProperties: func() string { | ||
if b, err := json.Marshal(p.Properties); err != nil { | ||
return "[]" | ||
} else { | ||
return string(b) | ||
} | ||
}(), | ||
} | ||
} | ||
|
||
func (s *Mysql) GetAllPlayers() ([]Players, error) { | ||
var players []Players | ||
r := s.client.Where("current_server != ?", "").Find(&players) | ||
if r.Error != nil { | ||
logrus.WithError(r.Error).Errorf("[Player] Failed Find Player") | ||
return nil, r.Error | ||
} | ||
|
||
return players, nil | ||
} | ||
|
||
func (s *Mysql) UpdateAllPlayers(players []Players) error { | ||
r := s.client.Clauses(clause.OnConflict{ | ||
Columns: []clause.Column{{Name: "uuid"}}, | ||
UpdateAll: true, | ||
}).Create(players) | ||
|
||
return r.Error | ||
} | ||
|
||
func (s *Mysql) SyncPlayer(newPlayer *Players, opts *UpdateOption) (bool, error) { | ||
var player Players | ||
findRes := s.client.Clauses(clause.Locking{Strength: "UPDATE"}).Model(&Players{}).First(&player, "uuid = ?", newPlayer.UUID) | ||
if findRes.Error != nil { | ||
logrus.WithError(findRes.Error).Errorf("[Player] SyncPlayer: Failed update player data (%s)", newPlayer.UUID) | ||
return false, findRes.Error | ||
} | ||
|
||
quit := false | ||
if opts != nil { | ||
was := player.CurrentServer | ||
if opts.IsQuit { | ||
if was == newPlayer.CurrentServer { | ||
newPlayer.CurrentServer = "" | ||
quit = true | ||
} else { | ||
return false, nil | ||
} | ||
} else { | ||
player.CurrentServer = newPlayer.CurrentServer | ||
} | ||
} | ||
|
||
r := s.client.Clauses(clause.OnConflict{ | ||
Columns: []clause.Column{{Name: "uuid"}}, | ||
UpdateAll: true, | ||
}).Create(newPlayer) | ||
|
||
return quit, r.Error | ||
} |
Oops, something went wrong.