WORKING: final bugfixes & add stored procedure for prev mission cleanup

This commit is contained in:
2023-06-22 10:27:09 -07:00
parent 3bb8c358fa
commit dbd3d68537
8 changed files with 153 additions and 97 deletions

View File

@@ -115,7 +115,7 @@ func getMissionHash() string {
functionName := "getMissionHash"
// get md5 hash of string
// https://stackoverflow.com/questions/2377881/how-to-get-a-md5-hash-from-a-string-in-golang
hash := md5.Sum([]byte(time.Now().Format("2006-01-02 15:04:05")))
hash := md5.Sum([]byte(time.Now().UTC().Format("2006-01-02 15:04:05")))
// convert to string
hashString := fmt.Sprintf(`%x`, hash)
@@ -154,7 +154,7 @@ func connectDB() string {
return "ERROR"
}
// Connect and check the server version
// Check the server version
var version string
err = db.QueryRow("SELECT VERSION()").Scan(&version)
if err != nil {
@@ -310,23 +310,22 @@ type AttendanceLogItem struct {
IsJIP bool `json:"isJIP"`
RoleDescription string `json:"roleDescription"`
MissionHash string `json:"missionHash"`
//
RowID int64 `json:"rowID"` // optional
}
func writeAttendance(data string) {
functionName := "writeAttendance"
var err error
// data is json, parse it
stringjson := fixEscapeQuotes(trimQuotes(data))
var event AttendanceLogItem
err := json.Unmarshal([]byte(stringjson), &event)
err = json.Unmarshal([]byte(stringjson), &event)
if err != nil {
writeLog(functionName, fmt.Sprintf(`["%s", "ERROR"]`, err))
return
}
// get MySQL friendly NOW
now := time.Now().Format("2006-01-02 15:04:05")
now := time.Now().UTC().Format("2006-01-02 15:04:05")
// prevent crash
if db == nil {
@@ -335,22 +334,44 @@ func writeAttendance(data string) {
}
// send to DB
result, err := db.ExecContext(
context.Background(),
fmt.Sprintf(
var result sql.Result
if event.EventType == "Server" {
sql := fmt.Sprintf(
`INSERT INTO %s (join_time, event_type, player_id, player_uid, profile_name, steam_name, is_jip, role_description) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,
ATTENDANCE_TABLE,
)
result, err = db.ExecContext(
context.Background(),
sql,
now,
event.EventType,
event.PlayerId,
event.PlayerUID,
event.ProfileName,
event.SteamName,
event.IsJIP,
event.RoleDescription,
)
} else if event.EventType == "Mission" {
sql := fmt.Sprintf(
`INSERT INTO %s (join_time, event_type, player_id, player_uid, profile_name, steam_name, is_jip, role_description, mission_hash) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`,
ATTENDANCE_TABLE,
),
now,
event.EventType,
event.PlayerId,
event.PlayerUID,
event.ProfileName,
event.SteamName,
event.IsJIP,
event.RoleDescription,
event.MissionHash,
)
)
result, err = db.ExecContext(
context.Background(),
sql,
now,
event.EventType,
event.PlayerId,
event.PlayerUID,
event.ProfileName,
event.SteamName,
event.IsJIP,
event.RoleDescription,
event.MissionHash,
)
}
if err != nil {
writeLog(functionName, fmt.Sprintf(`["%s", "ERROR"]`, err))
@@ -373,8 +394,9 @@ func writeAttendance(data string) {
}
type DisconnectItem struct {
PlayerId string `json:"playerId"`
RowId string `json:"rowId"`
EventType string `json:"eventType"`
PlayerId string `json:"playerId"`
MissionHash string `json:"missionHash"`
}
func writeDisconnectEvent(data string) {
@@ -389,7 +411,7 @@ func writeDisconnectEvent(data string) {
}
// get MySQL friendly NOW
now := time.Now().Format("2006-01-02 15:04:05")
now := time.Now().UTC().Format("2006-01-02 15:04:05")
// prevent crash
if db == nil {
@@ -397,31 +419,88 @@ func writeDisconnectEvent(data string) {
return
}
// send to DB
result, err := db.ExecContext(
context.Background(),
fmt.Sprintf(
`UPDATE %s SET disconnect_time = ? WHERE id = ?`,
ATTENDANCE_TABLE,
),
now,
event.RowId,
)
// first, check if a row exists for this player
var sql string
if event.EventType == "Mission" {
sql = fmt.Sprintf(
`
SELECT id FROM attendance
WHERE player_id = '%s' and event_type = '%s' and mission_hash = '%s' and disconnect_time IS NULL and join_time >= (NOW() - INTERVAL 24 hour)
ORDER BY join_time DESC
`,
event.PlayerId,
event.EventType,
event.MissionHash,
)
} else if event.EventType == "Server" {
sql = fmt.Sprintf(
`
SELECT id FROM attendance
WHERE player_id = '%s' and event_type = '%s' and disconnect_time IS NULL and join_time >= (NOW() - INTERVAL 24 hour)
ORDER BY join_time DESC
`,
event.PlayerId,
event.EventType,
)
} else {
writeLog(functionName, fmt.Sprintf(`["Unknown event type %s", "ERROR"]`, event.EventType))
return
}
rows, err := db.QueryContext(context.Background(), sql)
if err != nil {
writeLog(functionName, fmt.Sprintf(`["%s", "ERROR"]`, err))
return
}
defer rows.Close()
rowsAffected, err := result.RowsAffected()
// if there is a row, update it
if rows.Next() {
// create interface to hold values
var rowId int64
err = rows.Scan(&rowId)
if err != nil {
writeLog(functionName, fmt.Sprintf(`["%s", "ERROR"]`, err))
return
}
// update the row
sql = fmt.Sprintf(
`UPDATE attendance SET disconnect_time = '%s' WHERE id = %d`,
now,
rowId,
)
_, err := db.ExecContext(context.Background(), sql)
if err != nil {
writeLog(functionName, fmt.Sprintf(`["%s", "ERROR"]`, err))
return
}
writeLog(functionName, fmt.Sprintf(`["Saved disconnect event for %s to row id %d", "INFO"]`, event.PlayerId, rowId))
} else {
// otherwise, log an error
writeLog(functionName, fmt.Sprintf(`["No row found for %s, %s", "ERROR"]`, event.PlayerId, event.EventType))
}
}
func fillLastMissionNull() {
functionName := "fillLastMissionNull"
// prevent crash
if db == nil {
writeLog(functionName, `["db is nil", "ERROR"]`)
return
}
sql := `call proc_filllastmissionnull`
_, err := db.ExecContext(context.Background(), sql)
if err != nil {
writeLog(functionName, fmt.Sprintf(`["%s", "ERROR"]`, err))
return
}
if rowsAffected == 1 {
writeLog(functionName, fmt.Sprintf(`["Saved disconnect event for %s to row id %s", "INFO"]`, event.PlayerId, event.RowId))
}
writeLog(functionName, `["Filled mission event NULLs", "INFO"]`)
}
func runExtensionCallback(name *C.char, function *C.char, data *C.char) C.int {
@@ -452,6 +531,10 @@ func goRVExtensionArgs(output *C.char, outputsize C.size_t, input *C.char, argv
temp := fmt.Sprintf("Function: %s nb params: %d", C.GoString(input), argc)
switch C.GoString(input) {
case "fillLastMissionNull":
{
go fillLastMissionNull()
}
case "writeAttendance":
{ // callExtension ["logAttendance", [_hash] call CBA_fnc_encodeJSON]];
if argc == 1 {
@@ -503,7 +586,7 @@ func callBackExample() {
func getTimestamp() string {
// get the current unix timestamp in nanoseconds
// return time.Now().Local().Unix()
return time.Now().Format("2006-01-02 15:04:05")
return time.Now().UTC().Format("2006-01-02 15:04:05")
}
func trimQuotes(s string) string {