Skip to content

Commit

Permalink
record lastIP of player to track alt.
Browse files Browse the repository at this point in the history
  • Loading branch information
Harrison-Miller committed Mar 5, 2021
1 parent 483d2a3 commit 359e996
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 42 deletions.
44 changes: 44 additions & 0 deletions admin/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,49 @@ func getNotes(w http.ResponseWriter, r *http.Request) {
JSONResponse(w, &n)
}

type AltInfo struct {
Avatar string `json:"avatar"`
IP string `json:"ip" db:"lastIP"`
ClanTag string `json:"clantag" db:"clantag"`
CharacterName string `json:"charactername"`
Username string `json:"username" db:"username"`
PlayerID int64 `json:"id" db:"ID"`
LeaderboardBan bool `json:"leaderboardBan" db:"leaderboardBan"`
MonthlyLeaderboardBan bool `json:"monthlyLeaderboardBan" db:"monthlyLeaderboardBan"`
StatsBan bool `json:"statsBan" db:"statsBan"`
}

type LastIP struct {
IP string `json:"ip" db:"lastIP"`
}

func getAlts(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
playerID := vars["id"]

var ip LastIP
err := db.Get(&ip, `SELECT lastIP FROM players WHERE players.ID=?`, playerID)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

alts := []AltInfo{}
if ip.IP == "" {
JSONResponse(w, &alts)
return
}

err = db.Select(&alts, `SELECT avatar,lastIP,clantag,charactername,username,ID,leaderboardBan,monthlyLeaderboardBan,statsBan
FROM players WHERE players.lastIP=? AND players.ID!=?`, ip.IP, playerID)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

JSONResponse(w, &alts)
}

func main() {
config, _ = configs.Get()
if value, ok := os.LookupEnv("ADMIN_DB"); ok {
Expand Down Expand Up @@ -269,6 +312,7 @@ func main() {
r.HandleFunc("/delete", BasicAuth(delete)).Methods("POST")
r.HandleFunc("/recalculate", BasicAuth(recalculate)).Methods("POST")
r.HandleFunc("/notes/{id:[0-9]+}", BasicAuth(getNotes)).Methods("GET")
r.HandleFunc("/alts/{id:[0-9]+}", BasicAuth(getAlts)).Methods("GET")

r.Use(LogHandler)

Expand Down
59 changes: 59 additions & 0 deletions admin/templates/player.html
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,20 @@ <h5 class="modal-title">Delete Player</h5>
</div>
</div>

<h2>Alts</h2>
<table class="table table-striped">
<thead>
<tr>
<td>Avatar</td>
<td>Name</td>
<td>IP</td>
<td>Flags</td>
</tr>
</thead>
<tbody id="altlist">
</tbody>
</table>

<h2>Stats</h2>

<table class="table table-striped">
Expand Down Expand Up @@ -231,6 +245,51 @@ <h4 id="historyinfo"></h4>
killsStart=0;
currentHistory=null;

function getAlts() {
altList = $("#altlist");

$.getJSON("{{ .Prefix }}/alts/{{ .PlayerId }}", function(data) {
if (!data) {
return;
}

for (var i = 0; i < data.length; i++) {
player = data[i];
console.log(player);

elem = `<tr onclick="gotoPlayer('${player.id}');">
<td>
<img src="${player.avatar}" width="64px;" height="64px;">
</td>
<td>
${player.clantag} ${player.charactername} (${player.username})
</td>
<td>
${player.ip}
</td>
<td>`

if(player.leaderboardBan) {
elem += `<span class="badge badge-pill badge-warning">Leaderboard Ban</span>`;
}

if(player.monthlyLeaderboardBan) {
elem += `<span class="badge badge-pill badge-warning">Monthly Leaderboard Ban</span>`;
}

if(player.statsBan) {
elem += `<span class="badge badge-pill badge-danger">Stats Ban</span>`;
}

elem += `</td></tr>`;

altList.append(elem);
}
})
}

getAlts();

function getKillHistory() {
$.getJSON(apiEndpoint + "players/" + playerId + "/kills?showbanned=true&start=" + killsStart, function(data){
if(!data) {
Expand Down
74 changes: 34 additions & 40 deletions collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,24 @@ func isClientAlt(username string) bool {
return false
}

func PlayerFromScriptPlayer(s models.ScriptPlayer) models.Player {
return models.Player{
Username: s.Username,
Charactername: s.CharacterName,
Clantag: s.ClanTag,
IP: s.IP,
}
}

func PlayersFromScriptPlayers(s []models.ScriptPlayer) []models.Player {
players := []models.Player{}
for _, player := range s {
players = append(players, PlayerFromScriptPlayer(player))
}
return players
}


func isNewPlayer(player *models.Player) bool {
layout := "2006-01-02 15:04:05"
t, err := time.Parse(layout, player.Registered)
Expand Down Expand Up @@ -63,9 +81,10 @@ func UpdatePlayer(p *models.Player) error {
cache.ServerID = p.ServerID
p.Registered = cache.Registered

if p.Charactername != cache.Charactername || p.Clantag != cache.Clantag {
if p.Charactername != cache.Charactername || p.Clantag != cache.Clantag || p.IP != cache.IP {
cache.Charactername = p.Charactername
cache.Clantag = p.Clantag
cache.IP = p.IP

err := UpdatePlayerInfo(p)
if err != nil {
Expand All @@ -90,12 +109,13 @@ func UpdatePlayer(p *models.Player) error {
}

func (c *Collector) OnPlayerJoined(m rcon.Message, r *rcon.Client) error {
var player models.Player
err := json.Unmarshal([]byte(m.Args["object"]), &player)
var splayer models.ScriptPlayer
err := json.Unmarshal([]byte(m.Args["object"]), &splayer)
if err != nil {
c.logger.Println(err)
return nil
}
player := PlayerFromScriptPlayer(splayer)

if isClientAlt(player.Username) {
return nil
Expand All @@ -110,30 +130,22 @@ func (c *Collector) OnPlayerJoined(m rcon.Message, r *rcon.Client) error {

updater.incoming <- player

/*if player.StatsBan || isNewPlayer(&player) {
return nil
}*/

/*
err = UpdateJoinTime(player.ID, c.server.ID)
if err != nil {
return err
}
*/
c.logger.Printf("%s (%d) joined the game", player.Username, player.ID)
c.playerCount++
}
return nil
}

func (c *Collector) OnPlayerLeave(m rcon.Message, r *rcon.Client) error {
var player models.Player
err := json.Unmarshal([]byte(m.Args["object"]), &player)
var splayer models.ScriptPlayer
err := json.Unmarshal([]byte(m.Args["object"]), &splayer)
if err != nil {
c.logger.Println(err)
return nil
}

player := PlayerFromScriptPlayer(splayer)

if isClientAlt(player.Username) {
return nil
}
Expand All @@ -144,16 +156,6 @@ func (c *Collector) OnPlayerLeave(m rcon.Message, r *rcon.Client) error {
return err
}

/*if player.StatsBan || isNewPlayer(&player) {
return nil
}*/

/*
err = UpdateLeaveTime(player.ID, c.server.ID)
if err != nil {
return err
}
*/
c.logger.Printf("%s (%d) left the game", player.Username, player.ID)
if c.playerCount > 0 {
c.playerCount--
Expand Down Expand Up @@ -218,20 +220,21 @@ func (c *Collector) OnPlayerDie(m rcon.Message, r *rcon.Client) error {
}

func (c *Collector) PlayerList(m rcon.Message, r *rcon.Client) error {
var players []models.Player
err := json.Unmarshal([]byte(m.Args["object"]), &players)
var splayers []models.ScriptPlayer
err := json.Unmarshal([]byte(m.Args["object"]), &splayers)
if err != nil {
c.logger.Println(err)
return nil
}

players := PlayersFromScriptPlayers(splayers)

if len(players) > 0 {
altPlayers := 0
playerCount := 0
for _, p := range players {
p.ServerID = c.server.ID

if isClientAlt(p.Username) {
altPlayers++
continue
}

Expand All @@ -240,20 +243,11 @@ func (c *Collector) PlayerList(m rcon.Message, r *rcon.Client) error {
return err
}

if p.StatsBan || isNewPlayer(&p) {
altPlayers++
continue
}
playerCount++

/*
err = UpdateJoinTime(p.ID, c.server.ID)
if err != nil {
return err
}
*/
c.logger.Printf("%s (%d) was in the game", p.Username, p.ID)
}
c.playerCount = len(players) - altPlayers
c.playerCount = playerCount
}

r.RemoveHandler("^PlayerList (?P<object>.*)")
Expand Down
4 changes: 2 additions & 2 deletions collector/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ func UpdatePlayerInfo(player *models.Player) error {
}
defer tx.Rollback()

_, err = tx.Exec("INSERT INTO players (username, charactername, clantag) VALUES (?,?,?) ON DUPLICATE KEY UPDATE username=?,charactername=?,clantag=?",
player.Username, player.Charactername, player.Clantag, player.Username, player.Charactername, player.Clantag)
_, err = tx.Exec("INSERT INTO players (username, charactername, clantag, lastIP) VALUES (?,?,?,?) ON DUPLICATE KEY UPDATE username=?,charactername=?,clantag=?,lastIP=?",
player.Username, player.Charactername, player.Clantag, player.IP, player.Username, player.Charactername, player.Clantag, player.IP)
if err != nil {
return errors.Wrap(err, "error updating/creating player")
}
Expand Down
13 changes: 13 additions & 0 deletions collector/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ func RunMigrations(db *sqlx.DB) error {
return err
}

err = RunMigration(22, AddLastIP, db)
if err != nil {
return err
}

return nil
}
Expand Down Expand Up @@ -534,5 +538,14 @@ func AddMonthlyLeaderboardBan(db *sqlx.DB) error {
return err
}

return nil
}

func AddLastIP(db *sqlx.DB) error {
err := AddColumn("players", "lastIP", "VARCHAR(60) NOT NULL", "''", db)
if err != nil {
return err
}

return nil
}
8 changes: 8 additions & 0 deletions common/models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,19 @@ import (
"github.com/jmoiron/sqlx"
)

type ScriptPlayer struct {
Username string `json:"username"`
CharacterName string `json:"charactername"`
ClanTag string `json:"clantag"`
IP string `json:"ip"`
}

type Player struct {
ID int64 `json:"id" db:"ID"`
Username string `json:"username"`
Charactername string `json:"charactername"`
Clantag string `json:"clantag"`
IP string `json:"-" db:"lastIP"`

ServerID int64 // used for tracking in the collector
ClanID *int64 `json:"clanID" db:"clanID"`
Expand Down

0 comments on commit 359e996

Please sign in to comment.