Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update file I/O #7

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified RankGrapher.dll
Binary file not shown.
188 changes: 110 additions & 78 deletions RankGrapher/RankGrapher.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#include <ctime>
#include <filesystem>

#include "pch.h"
#include "RankGrapher.h"

Expand Down Expand Up @@ -37,7 +40,7 @@ gameWrapper->RegisterDrawable([this](CanvasWrapper canvas) {
gameWrapper->HookEvent("Function TAGame.GameEvent_Soccar_TA.Destroyed", bind(&RankGrapher::loadMenu, this, std::placeholders::_1));



bool create_directory = std::filesystem::create_directory(gameWrapper->GetDataFolder() / "RankGrapher");

rankgrapherbg = std::make_shared<ImageWrapper>(gameWrapper->GetDataFolder() / "RankGrapher" / "ranktrackerbg.png", true, false);
closeImg = std::make_shared<ImageWrapper>(gameWrapper->GetDataFolder() / "RankGrapher" / "closebutton.png", false, true);
Expand Down Expand Up @@ -190,7 +193,14 @@ void RankGrapher::RenderCanvas(CanvasWrapper canvas) {

ifstream inputFile;

inputFile.open(gameWrapper->GetDataFolder() / "RankGrapher" / (to_string(playlistNum) + "data.csv"));
if (isRanked) {
fileName = to_string(playlistNum) + "data.csv";
}
else {
fileName = "0data.csv";
}

inputFile.open(gameWrapper->GetDataFolder() / "RankGrapher" / fileName);

int gameNumber = 0;
string gameId;
Expand All @@ -202,31 +212,18 @@ void RankGrapher::RenderCanvas(CanvasWrapper canvas) {
CVarWrapper timeframeCVar = cvarManager->getCvar("timeframe");
if (!timeframeCVar) { return; }
string timeframe = timeframeCVar.getStringValue();

if (timeframe == "all") {
while (getline(inputFile, line)) {

stringstream inputString(line);

getline(inputString, tempString, ',');
gameNumber = atoi(tempString.c_str());

getline(inputString, gameId, ',');

getline(inputString, tempString, ',');
mmr = atof(tempString.c_str());
allMMR.push_back(mmr);
if (timeframe == "all" || timeframe == "session") {

line = "";

}
}
if (timeframe == "session") {
while (getline(inputFile, line)) {

stringstream inputString(line);

getline(inputString, tempString, ',');
// skip CSV header
if (tempString.compare("game_number") == 0) {
continue;
}
gameNumber = atoi(tempString.c_str());

getline(inputString, gameId, ',');
Expand All @@ -240,10 +237,11 @@ void RankGrapher::RenderCanvas(CanvasWrapper canvas) {
//LOG("game session {}", gameSessionId);
//LOG("current session {}", currentSession);

if (gameSessionId == currentSession) {
allMMR.push_back(mmr);
if (timeframe == "session" && gameSessionId != currentSession) {
continue;
}

allMMR.push_back(mmr);

line = "";
//LOG("session {}", mmr);
Expand Down Expand Up @@ -480,99 +478,133 @@ void RankGrapher::onGameEntry() {
// Getting the mmr
userMMR = gameWrapper->GetMMRWrapper().GetPlayerMMR(uniqueID, playlistNum);
gotNewMMR = true;
}
}


currentMatchGuid = gameWrapper->GetOnlineGame().GetMatchGUID();



if (!server.IsNull()) {

PlayerControllerWrapper localPrimaryPlayerController = server.GetLocalPrimaryPlayer();
if (!localPrimaryPlayerController.IsNull()) {
PriWrapper localPrimaryPlayer = localPrimaryPlayerController.GetPRI();

fileName = to_string(playlistNum) + "data.csv";
fileNameTemp = to_string(playlistNum) + "data_temp.csv";

if (!localPrimaryPlayer.IsNull()) {
goals = localPrimaryPlayer.GetMatchGoals();
assists = localPrimaryPlayer.GetMatchAssists();
saves = localPrimaryPlayer.GetMatchSaves();
demos = localPrimaryPlayer.GetMatchDemolishes();
shots = localPrimaryPlayer.GetMatchShots();
score = localPrimaryPlayer.GetMatchScore();
mvp = localPrimaryPlayer.GetbMatchMVP();
}

TeamWrapper matchWinner = server.GetMatchWinner();
if (!matchWinner.IsNull()) {
win = (localPrimaryPlayer.GetTeamNum() == matchWinner.GetTeamNum());
}
}
}
}

}

if (currentPlaylist.GetbRanked()) {
isRanked = true;
fileName = to_string(playlistNum) + "data.csv";
fileNameTemp = to_string(playlistNum) + "data_temp.csv";
}
else {
isRanked = false;
fileName = "0data.csv";
fileNameTemp = "0data_temp.csv";
}

ofstream outputFile;
ifstream inputFile;

inputFile.open(gameWrapper->GetDataFolder() / "RankGrapher" / fileName);
outputFile.open(gameWrapper->GetDataFolder() / "RankGrapher" / fileNameTemp);
string line = "";

int gameNumber = 0;
string gameId;
float mmr = 0.0;
vector<string> row;
string tempString;

//checking if data file exists, if not then creates file with first row, otherwise add data to file

if (inputFile.fail()) {
outputFile.open(gameWrapper->GetDataFolder() / "RankGrapher" / fileName);

outputFile << "1" << "," << currentMatchGuid << "," << to_string(userMMR) << "," << currentSession << "\n";
outputFile.close();
inputFile.close();
//if input file does not exist, create it
//if it exists, copy contents and append most recent stats

if (inputFile.fail()) {
outputFile << "game_number" << ",";
outputFile << "match_guid" << ",";
outputFile << "mmr" << ",";
outputFile << "session_number" << ",";
outputFile << "playlist" << ",";
outputFile << "timestamp" << ",";
outputFile << "goals" << ",";
outputFile << "assists" << ",";
outputFile << "saves" << ",";
outputFile << "demos" << ",";
outputFile << "shots" << ",";
outputFile << "score" << ",";
outputFile << "mvp" << ",";
outputFile << "win";
outputFile << "\n";
}

else {



outputFile.open(gameWrapper->GetDataFolder() / "RankGrapher" / fileNameTemp);

int gameNumber;
gameNumber = 0;
string gameId;
float mmr;
vector<string> row;
string tempString;
int gameSession;

while (getline(inputFile, line)) {



stringstream inputString(line);

getline(inputString, tempString, ',');
gameNumber = atoi(tempString.c_str());
getline(inputString, gameId, ',');

getline(inputString, tempString, ',');
mmr = atof(tempString.c_str());

// skip CSV header
if (tempString.compare("game_number") != 0) {
gameNumber = atoi(tempString.c_str());
getline(inputString, gameId, ',');

getline(inputString, tempString, ',');
gameSession = atoi(tempString.c_str());
getline(inputString, tempString, ',');
mmr = atof(tempString.c_str());
}

outputFile << to_string(gameNumber) << "," << gameId << "," << to_string(mmr) << "," << to_string(gameSession) << "\n";
outputFile << line << "\n";

line = "";


}

if (userMMR != mmr) {
outputFile << to_string(gameNumber + 1) << "," << currentMatchGuid << "," << to_string(userMMR) << "," << to_string(currentSession) << "\n";
}

outputFile.close();
inputFile.close();



remove(gameWrapper->GetDataFolder() / "RankGrapher" / fileName.c_str());


// renaming the updated file with the existing file name
rename(gameWrapper->GetDataFolder() / "RankGrapher" / fileNameTemp.c_str(), gameWrapper->GetDataFolder() / "RankGrapher" / fileName.c_str());
}

if (userMMR != mmr) {

time_t now = time(0);
currentMatchGuid = server.GetMatchGUID();

outputFile << to_string(gameNumber + 1) << ",";
outputFile << currentMatchGuid << ",";
outputFile << to_string(userMMR) << ",";
outputFile << to_string(currentSession) << ",";
outputFile << to_string(playlistNum) << ",";
outputFile << to_string(now) << ",";
outputFile << to_string(goals) << ",";
outputFile << to_string(assists) << ",";
outputFile << to_string(saves) << ",";
outputFile << to_string(demos) << ",";
outputFile << to_string(shots) << ",";
outputFile << to_string(score) << ",";
outputFile << to_string(mvp) << ",";
outputFile << to_string(win);
outputFile << "\n";
}

outputFile.close();
inputFile.close();


remove(gameWrapper->GetDataFolder() / "RankGrapher" / fileName.c_str());

}
// renaming the updated file with the existing file name
rename(gameWrapper->GetDataFolder() / "RankGrapher" / fileNameTemp.c_str(), gameWrapper->GetDataFolder() / "RankGrapher" / fileName.c_str());
}
}

Expand Down
17 changes: 14 additions & 3 deletions RankGrapher/RankGrapher.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,23 @@ class RankGrapher: public BakkesMod::Plugin::BakkesModPlugin, public BakkesMod::
bool shouldHideCursor = false;
bool shouldRenderImGui = false;
bool isGameEnd = 0;
float gameEndMMR;
bool gotNewMMR;
float gameEndMMR = 0.0;
bool gotNewMMR = false;
float userMMR = 0;
int currentSession = 0;
int gameSessionId;
int playlistNum = 1;
int playlistNum = -1;
bool isRanked = 0;

int goals = -1;
int assists = -1;
int saves = -1;
int demos = -1;
int shots = -1;
int score = -1;
long mvp = 0;
bool win = false;

float xOffset;
float yOffset;
Vector2 screenSize;
Expand Down
2 changes: 1 addition & 1 deletion RankGrapher/version.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#define VERSION_MAJOR 1
#define VERSION_MINOR 0
#define VERSION_PATCH 0
#define VERSION_BUILD 626
#define VERSION_BUILD 630

#define stringify(a) stringify_(a)
#define stringify_(a) #a
Expand Down