update tracking
This commit is contained in:
@@ -1,11 +1,80 @@
|
||||
package tracking
|
||||
|
||||
import (
|
||||
"acc-server-manager/local/model"
|
||||
"acc-server-manager/local/utl/regexHandler"
|
||||
"bufio"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type StateChange int
|
||||
const (
|
||||
PlayerCount StateChange = iota
|
||||
Session
|
||||
)
|
||||
|
||||
var StateChanges = map[StateChange]string {
|
||||
PlayerCount: "player-count",
|
||||
Session: "session",
|
||||
}
|
||||
|
||||
type AccServerInstance struct {
|
||||
Model *model.Server
|
||||
State *model.ServerState
|
||||
OnStateChange func(...StateChange)
|
||||
}
|
||||
|
||||
func NewAccServerInstance(server *model.Server, onStateChange func(...StateChange)) *AccServerInstance {
|
||||
return &AccServerInstance{
|
||||
Model: server,
|
||||
State: &model.ServerState{PlayerCount: 0},
|
||||
OnStateChange: onStateChange,
|
||||
}
|
||||
}
|
||||
|
||||
type StateRegexHandler struct {
|
||||
*regexHandler.RegexHandler
|
||||
test string
|
||||
}
|
||||
|
||||
func NewRegexHandler(str string, test string) *StateRegexHandler {
|
||||
return &StateRegexHandler{
|
||||
RegexHandler: regexHandler.New(str),
|
||||
test: test,
|
||||
}
|
||||
}
|
||||
|
||||
func (rh *StateRegexHandler) Test(line string) bool{
|
||||
return strings.Contains(line, rh.test)
|
||||
}
|
||||
|
||||
func (rh *StateRegexHandler) Count(line string) int{
|
||||
var count int = 0
|
||||
rh.Contains(line, func (strs ...string) {
|
||||
if len(strs) == 2 {
|
||||
if ct, err := strconv.Atoi(strs[1]); err == nil {
|
||||
count = ct
|
||||
}
|
||||
}
|
||||
})
|
||||
return count
|
||||
}
|
||||
|
||||
func (rh *StateRegexHandler) Change(line string) (string, string){
|
||||
var old string = ""
|
||||
var new string = ""
|
||||
rh.Contains(line, func (strs ...string) {
|
||||
if len(strs) == 3 {
|
||||
old = strs[1]
|
||||
new = strs[2]
|
||||
}
|
||||
})
|
||||
return old, new
|
||||
}
|
||||
|
||||
func TailLogFile(path string, callback func(string)) {
|
||||
file, _ := os.Open(path)
|
||||
defer file.Close()
|
||||
@@ -21,4 +90,85 @@ func TailLogFile(path string, callback func(string)) {
|
||||
time.Sleep(500 * time.Millisecond) // wait for new data
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type LogStateType int
|
||||
const (
|
||||
SessionChange LogStateType = iota
|
||||
LeaderboardUpdate
|
||||
UDPCount
|
||||
ClientsOnline
|
||||
)
|
||||
|
||||
var logStateContain = map[LogStateType]string {
|
||||
SessionChange: "Session changed",
|
||||
LeaderboardUpdate: "Updated leaderboard for",
|
||||
UDPCount: "Udp message count",
|
||||
ClientsOnline: "client(s) online",
|
||||
}
|
||||
|
||||
var sessionChangeRegex = NewRegexHandler(`Session changed: (\w+) -> (\w+)`, logStateContain[SessionChange])
|
||||
var leaderboardUpdateRegex = NewRegexHandler(`Updated leaderboard for (\d+) clients`, logStateContain[LeaderboardUpdate])
|
||||
var udpCountRegex = NewRegexHandler(`Udp message count ((\d+) client`, logStateContain[UDPCount])
|
||||
var clientsOnlineRegex = NewRegexHandler(`(\d+) client(s) online`, logStateContain[ClientsOnline])
|
||||
|
||||
var logStateRegex = map[LogStateType]*StateRegexHandler {
|
||||
SessionChange: sessionChangeRegex,
|
||||
LeaderboardUpdate: leaderboardUpdateRegex,
|
||||
UDPCount: udpCountRegex,
|
||||
ClientsOnline: clientsOnlineRegex,
|
||||
}
|
||||
|
||||
func (instance *AccServerInstance) HandleLogLine(line string) {
|
||||
for logState, regexHandler := range logStateRegex {
|
||||
if (regexHandler.Test(line)) {
|
||||
switch logState {
|
||||
case LeaderboardUpdate:
|
||||
case UDPCount:
|
||||
case ClientsOnline:
|
||||
count := regexHandler.Count(line)
|
||||
instance.UpdatePlayerCount(count)
|
||||
case SessionChange:
|
||||
_, new := regexHandler.Change(line)
|
||||
instance.UpdateSessionChange(new)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (instance *AccServerInstance) UpdateState(callback func(state *model.ServerState)) {
|
||||
state := instance.State
|
||||
state.Lock()
|
||||
defer state.Unlock()
|
||||
callback(state)
|
||||
}
|
||||
|
||||
func (instance *AccServerInstance) UpdatePlayerCount(count int) {
|
||||
changes := []StateChange{}
|
||||
instance.UpdateState(func (state *model.ServerState) {
|
||||
if (count > 0 && state.PlayerCount == 0) {
|
||||
state.SessionStart = time.Now()
|
||||
changes = append(changes, Session)
|
||||
} else if (count == 0) {
|
||||
state.SessionStart = time.Time{}
|
||||
changes = append(changes, Session)
|
||||
}
|
||||
state.PlayerCount = count
|
||||
changes = append(changes, PlayerCount)
|
||||
})
|
||||
instance.OnStateChange(changes...)
|
||||
}
|
||||
|
||||
func (instance *AccServerInstance) UpdateSessionChange(session string) {
|
||||
changes := []StateChange{}
|
||||
instance.UpdateState(func (state *model.ServerState) {
|
||||
if (state.PlayerCount > 0) {
|
||||
state.SessionStart = time.Now()
|
||||
} else {
|
||||
state.SessionStart = time.Time{}
|
||||
}
|
||||
state.Session = session
|
||||
changes = append(changes, Session)
|
||||
})
|
||||
instance.OnStateChange(changes...)
|
||||
}
|
||||
Reference in New Issue
Block a user