diff --git a/PlayerTracker b/PlayerTracker index 8f0af5e..a03ba5f 100755 Binary files a/PlayerTracker and b/PlayerTracker differ diff --git a/build.sh b/build.sh index d58bfd0..a31f134 100755 --- a/build.sh +++ b/build.sh @@ -18,6 +18,20 @@ if [ $? -ne 0 ]; then echo "Error: Build failed" exit 1 fi + cp ./bin/$1 ../ +cd .. + +echo ">>> Copying PlayerTracker to AC Servers <<<" +for server in ../servers/*; do + if [ -d "$server/utils" ]; then + target="$server/utils/ACPlayer_tracker" + mkdir -p "$target" + cp ./PlayerTracker "$target/" + echo "Installed PlayerTracker to: $target" + fi +done + echo ">>> Build Complete <<<" -echo ">>> Executable: $1 <<<" \ No newline at end of file +echo ">>> Executable: $1 <<<" + diff --git a/include/server_structs.h b/include/server_structs.h index 968da3f..2d175af 100644 --- a/include/server_structs.h +++ b/include/server_structs.h @@ -184,9 +184,9 @@ typedef struct trackAtributesAPI { } __attribute__((packed)) trackAtributesAPI; typedef struct api_packet { + u_char message_type; // ACSP_MessageType u_int8_t tracker_id; - u_int8_t last_updated_carid; - u_int8_t connected_cars; + u_int8_t connected_players; carAtributesAPI cars[64]; trackAtributesAPI track_info; diff --git a/source/main.cpp b/source/main.cpp index ef6ff05..e42f37f 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include #include #include @@ -23,12 +24,12 @@ const char *API_SOCKET_PATH = "/tmp/ACplayer_socket"; const int MAX_PLAYERS = 64; const char *SERVER_OUT_IP = "127.0.0.1"; -int SERVER_ID; +u_int8_t SERVER_ID; api_packet current_packet; -int apiSent = 0; +u_int8_t apiSent = 0; // INFO: This assumes Players pointer to be the same size of MAX_PLAYERS -void update_api_packet(trackAtributes trackInfo, carAtributes *players, u_int8_t last_updated_carid) { +void update_api_packet(trackAtributes trackInfo, carAtributes *players) { current_packet.tracker_id = SERVER_ID; // Update track info @@ -80,11 +81,6 @@ void update_api_packet(trackAtributes trackInfo, carAtributes *players, u_int8_t current_packet.cars[i].normalizedSplinePos = players[i].normalizedSplinePos; } - if (last_updated_carid == NULL) { - current_packet.last_updated_carid = 0xFF; // No specific car updated - } else { - current_packet.last_updated_carid = last_updated_carid; - } apiSent = 0; } @@ -114,8 +110,10 @@ void *send_to_api_socket(void *arg) { printf("[+] Connected to API socket at %s\n", API_SOCKET_PATH); // Handshake - const char *handshake_msg = "ACSP_API_CLIENT"; - send(sock_fd, handshake_msg, strlen(handshake_msg), MSG_NOSIGNAL); + api_packet handshake; + + handshake.tracker_id = 65; + send(sock_fd, &handshake, sizeof(api_packet), MSG_NOSIGNAL); while (1) { if (current_packet.tracker_id == (u_int8_t)-1) { @@ -196,7 +194,7 @@ void init_carupdate(carAtributes *car) { car->total_laps_completed = 0; car->contacts = 0; - car->total_contacts = 0; // TODO: SQL querry to get total contacts of all time; TAG:2 + car->total_contacts = 0; // TODO: SQL querry to get total contacts of all time; TAG:2 car->normalizedSplinePos = 0.0f; } @@ -210,9 +208,9 @@ int main(int argc, char *argv[]) { return EXIT_FAILURE; } - SERVER_ID = atoi(argv[1]); - int SERVER_IN_PORT = atoi(argv[2]); - int SERVER_OUT_PORT = atoi(argv[3]); + SERVER_ID = (u_int8_t)atoi(argv[1]); + u_int16_t SERVER_IN_PORT = (u_int16_t)atoi(argv[2]); + u_int16_t SERVER_OUT_PORT = (u_int16_t)atoi(argv[3]); printf("[+] Starting server...\n"); printf("[+] Server listening on port %d\n", SERVER_IN_PORT); @@ -244,12 +242,14 @@ int main(int argc, char *argv[]) { trackAtributes trackInfo; carAtributes players[MAX_PLAYERS]; + current_packet.connected_players = 0; carAtributes update; // Basically a contrutor for carAtributes (because all strings in carAtributes are pointers) for (int i = 0; i < MAX_PLAYERS; i++) { init_carupdate(&players[i]); } + init_carupdate(&update); trackInfo.server_name = (char *)malloc(128); @@ -274,8 +274,9 @@ int main(int argc, char *argv[]) { printf("[+] Received %zd bytes from client\n", recv_bytes); #endif + current_packet.message_type = (u_char)buffer[0]; // The first byte of the messages is always an ACSP_MessageType - switch ((int)buffer[0]) { + switch ((u_char)buffer[0]) { // ============================ // SERVER → CLIENT MESSAGES // ============================ @@ -338,7 +339,8 @@ int main(int argc, char *argv[]) { printf("\tWeather Graphics: %s\n", trackInfo.weather_graphics); printf("\tElapsed Time: %d ms (%2d:%2d:%2d)\n\n", trackInfo.elapsed_ms, trackInfo.elapsed_ms / 60000, (trackInfo.elapsed_ms % 60000) / 1000, (trackInfo.elapsed_ms % 60000) % 1000); - update_api_packet(trackInfo, players, update.carID); + + update_api_packet(trackInfo, players); break; case ACSP_NEW_CONNECTION: // DONE: @@ -366,7 +368,7 @@ int main(int argc, char *argv[]) { update.isConnected = 1; // Mark as connected update.isLoading = 1; // Mark as Loading - current_packet.connected_cars++; + current_packet.connected_players++; printf("\tDriver Name: \"%s\"\n", update.driver_name); printf("\tDriver GUID: \"%s\"\n", update.driver_GUID); @@ -378,7 +380,7 @@ int main(int argc, char *argv[]) { if (update.carID < MAX_PLAYERS) { memcpy(&players[update.carID], &update, sizeof(carAtributes)); } - update_api_packet(trackInfo, players, update.carID); + update_api_packet(trackInfo, players); break; case ACSP_CONNECTION_CLOSED: // DONE: @@ -405,7 +407,10 @@ int main(int argc, char *argv[]) { } update.isConnected = 0; // Mark as disconnected - current_packet.connected_cars--; + current_packet.connected_players--; + if (current_packet.connected_players < 0) { + current_packet.connected_players = 0; + } printf("\tDriver Name: \"%s\"\n", update.driver_name); printf("\tDriver GUID: \"%s\"\n", update.driver_GUID); @@ -415,9 +420,10 @@ int main(int argc, char *argv[]) { // Store player player into the respective Index if (update.carID < MAX_PLAYERS) { - memcpy(&players[update.carID], &update, sizeof(carAtributes)); + players[update.carID].isConnected = 0; // Mark disconnected + players[update.carID].isLoading = 0; } - update_api_packet(trackInfo, players, update.carID); + update_api_packet(trackInfo, players); break; case ACSP_CAR_UPDATE: // DONE: @@ -426,7 +432,7 @@ int main(int argc, char *argv[]) { #endif offset = 1; - memcpy(&update.carID, buffer, sizeof(uint8_t)); + memcpy(&update.carID, buffer + offset, sizeof(uint8_t)); offset = 1 + sizeof(uint8_t); memcpy(&update.position, buffer + offset, sizeof(postion)); offset += sizeof(postion); @@ -444,18 +450,16 @@ int main(int argc, char *argv[]) { printf("Gear: %d RPM: %d\n", update.carGear, update.carRPM); #endif - // Store player player into the respective index + // Store player player into the respective Index if (update.carID < MAX_PLAYERS) { - memcpy(&players[update.carID].position, &update.position, sizeof(postion)); - memcpy(&players[update.carID].velocity, &update.velocity, sizeof(postion)); + players[update.carID].position = update.position; + players[update.carID].velocity = update.velocity; players[update.carID].carGear = update.carGear; players[update.carID].carRPM = update.carRPM; players[update.carID].normalizedSplinePos = update.normalizedSplinePos; - players[update.carID].carID = update.carID; - players[update.carID].isConnected = 1; // Ensure isConnected is set } - update_api_packet(trackInfo, players, update.carID); + update_api_packet(trackInfo, players); break; case ACSP_CAR_INFO: // DONE: @@ -493,7 +497,7 @@ int main(int argc, char *argv[]) { memcpy(&players[update.carID], &update, sizeof(carAtributes)); } - update_api_packet(trackInfo, players, update.carID); + update_api_packet(trackInfo, players); break; case ACSP_END_SESSION: // DONE: (only session type cycling) @@ -513,7 +517,7 @@ int main(int argc, char *argv[]) { printf("\tNext Session Name: %s\n", trackInfo.session_name); printf("\tNext Session Type: %d\n\n", trackInfo.session_type); - update_api_packet(trackInfo, players, update.carID); + update_api_packet(trackInfo, players); break; case ACSP_VERSION: // DONE: @@ -523,7 +527,7 @@ int main(int argc, char *argv[]) { printf("\tProtocol Version: %d\n\n", trackInfo.protocol_version); - update_api_packet(trackInfo, players, update.carID); + update_api_packet(trackInfo, players); break; case ACSP_CHAT: // DONE: Receive chat messages @@ -537,7 +541,8 @@ int main(int argc, char *argv[]) { printf("\tCar ID: %d (%s)\n", update.carID, players[update.carID].driver_name); printf("\tMessage: \"%s\"\n\n", message); - update_api_packet(trackInfo, players, update.carID); + + update_api_packet(trackInfo, players); break; case ACSP_CLIENT_LOADED: // DONE: Check for client loaded status @@ -548,7 +553,8 @@ int main(int argc, char *argv[]) { players[update.carID].isLoading = 0; printf("\tCar ID: %d (%s) Finished Loading into the session\n\n", update.carID, players[update.carID].driver_name); - update_api_packet(trackInfo, players, update.carID); + + update_api_packet(trackInfo, players); break; case ACSP_ERROR: // DONE: Simple error message from server @@ -578,7 +584,8 @@ int main(int argc, char *argv[]) { printf("\tLap Time: %5d ms\n", update.lap_time); printf("\tCuts this lap: %d\n", update.cuts); printf("\tTotal Cuts (this session): %d\n\n", update.total_cuts); - update_api_packet(trackInfo, players, update.carID); + + update_api_packet(trackInfo, players); break; // ============================ @@ -609,7 +616,7 @@ int main(int argc, char *argv[]) { // TODO: Update total contacts for both players in the database // TAG:2 Update total contacts for both players - update_api_packet(trackInfo, players, update.carID); + update_api_packet(trackInfo, players); } break; case ACSP_CE_COLLISION_WITH_ENV: { @@ -622,7 +629,7 @@ int main(int argc, char *argv[]) { // TODO: Update total contacts for the player in the database // TAG:2 Update total contacts for the player - update_api_packet(trackInfo, players, update.carID); + update_api_packet(trackInfo, players); } break; } @@ -630,12 +637,12 @@ int main(int argc, char *argv[]) { // With X velues and (e.g >5 m/s = 0x, >10 m/s = 2x, >15 m/s = 4x) && Blackflag limits (e.g x17 = DQ) float impact_speed = read_float((const u_int8_t *)buffer, recv_bytes, &offset, &ok); - postion world_pos = {0}; + postion world_pos = {0, 0, 0}; world_pos.x = read_float((const u_int8_t *)buffer, recv_bytes, &offset, &ok); world_pos.y = read_float((const u_int8_t *)buffer, recv_bytes, &offset, &ok); world_pos.z = read_float((const u_int8_t *)buffer, recv_bytes, &offset, &ok); - postion rel_pos = {0}; + postion rel_pos = {0, 0, 0}; rel_pos.x = read_float((const u_int8_t *)buffer, recv_bytes, &offset, &ok); rel_pos.y = read_float((const u_int8_t *)buffer, recv_bytes, &offset, &ok); rel_pos.z = read_float((const u_int8_t *)buffer, recv_bytes, &offset, &ok); @@ -701,7 +708,7 @@ int main(int argc, char *argv[]) { sendto(send_sock_fd, buffer, strlen(command) + 1, 0, NULL, 0); printf("\tSent Admin Command: %s\n\n", command); - free(buffer); + free(__buffer); } break; // ============================