Permalink
Browse files

Testing sockets

  • Loading branch information...
1 parent 8a1281d commit 8935dddfdd0ab9b89c5c48926e91eb5eb6f8ca07 @SuperV1234 committed Apr 6, 2013
@@ -12,13 +12,13 @@ namespace hg
namespace Online
{
void startCheckUpdates();
- void startSendScore(const std::string& mName, const std::string& mValidator, float mScore);
- void startGetScores(std::string& mTargetString, const std::string& mValidator);
+ void startSendScore(const std::string& mName, const std::string& mValidator, float mDifficulty, float mScore);
+ void startGetScores(std::string& mTargetString, const std::string& mName, const std::string& mValidator, float mDifficulty);
void cleanUp();
void terminateAll();
- std::string getValidator(const std::string& mPackPath, const std::string& mLevelId, const std::string& mLevelRootPath, const std::string& mStyleRootPath, const std::string& mLuaScriptPath, float mDifficultyMultiplier);
+ std::string getValidator(const std::string& mPackPath, const std::string& mLevelId, const std::string& mLevelRootPath, const std::string& mStyleRootPath, const std::string& mLuaScriptPath);
float getServerVersion();
std::string getServerMessage();
@@ -88,6 +88,33 @@ namespace hg
return get182UrlEncoded(mLevelId) + get182MD5Hash(toEncrypt + serverKey182);
}
+ string get19Validator(const string& mPackPath, const string& mLevelId, const string& mLevelRootPath,
+ const string& mStyleRootPath, const string& mLuaScriptPath, float mDifficultyMultiplier)
+ {
+ string luaScriptContents{ssvu::FileSystem::getFileContents(mLuaScriptPath)};
+ unordered_set<string> luaScriptNames;
+ recursiveFillIncludedLuaFileNames(luaScriptNames, mPackPath, luaScriptContents);
+
+ string toEncrypt{""};
+ toEncrypt.append(mLevelId);
+ toEncrypt.append(toStr(mDifficultyMultiplier));
+ toEncrypt.append(ssvu::FileSystem::getFileContents(mLevelRootPath));
+ toEncrypt.append(ssvu::FileSystem::getFileContents(mStyleRootPath));
+ toEncrypt.append(luaScriptContents);
+
+ for(auto& luaScriptName : luaScriptNames)
+ {
+ string path{mPackPath + "/Scripts/" + luaScriptName};
+ string contents{ssvu::FileSystem::getFileContents(path)};
+ toEncrypt.append(contents);
+ }
+
+ toEncrypt = get182ControlStripped(toEncrypt);
+
+ string result{get182UrlEncoded(mLevelId) + get182MD5Hash(toEncrypt + HG_SKEY1 + HG_SKEY2 + HG_SKEY3)};
+ return result;
+ }
+
void convert181to183Hashes(const string& mSourceJsonPath, const string& mTargetJsonPath)
{
string scores{get184FileContents(mSourceJsonPath)};
@@ -105,7 +132,7 @@ namespace hg
log("");
log("computing new validator for " + levelData.getId() + ", difficulty multiplier " + toStr(difficultyMult) + "...");
- string newValidator{Online::getValidator(levelData.getPackPath(), levelData.getId(), levelData.getLevelRootPath(), levelData.getStyleRootPath(), levelData.getLuaScriptPath(), difficultyMult)};
+ string newValidator{get19Validator(levelData.getPackPath(), levelData.getId(), levelData.getLevelRootPath(), levelData.getStyleRootPath(), levelData.getLuaScriptPath(), difficultyMult)};
log("\"" + newValidator + "\"");
newValidators.push_back("\"" + newValidator + "\"");
log("");
@@ -138,7 +165,7 @@ namespace hg
log("");
log("computing new validator for " + levelData.getId() + ", difficulty multiplier " + toStr(difficultyMult) + "...");
- string newValidator{Online::getValidator(levelData.getPackPath(), levelData.getId(), levelData.getLevelRootPath(), levelData.getStyleRootPath(), levelData.getLuaScriptPath(), difficultyMult)};
+ string newValidator{get19Validator(levelData.getPackPath(), levelData.getId(), levelData.getLevelRootPath(), levelData.getStyleRootPath(), levelData.getLuaScriptPath(), difficultyMult)};
log("\"" + newValidator + "\"");
newValidators.push_back("\"" + newValidator + "\"");
log("");
@@ -172,31 +199,31 @@ namespace hg
{
log("Result has alerady validator <" + validator + ">", "Merge");
Json::Value& lvl = result[validator];
-
+
for(auto nsPairItr = (*itr).begin(); nsPairItr != (*itr).end(); ++nsPairItr)
{
Json::Value& nsPair = (*nsPairItr);
-
+
bool found{false};
for(auto lvlPairItr = lvl.begin(); lvlPairItr != lvl.end(); ++lvlPairItr)
{
Json::Value& lvlPair = (*lvlPairItr);
- if(lvlPair["n"] == nsPair["n"] && nsPair["s"].asFloat() > lvlPair["s"].asFloat())
+ if(lvlPair["n"] == nsPair["n"] && nsPair["s"].asFloat() > lvlPair["s"].asFloat())
{
lvlPair["s"] = nsPair["s"].asFloat();
found = true;
break;
}
}
-
+
if(!found) lvl.append(nsPair);
}
}
else log("Result has not validator <" + validator + ">, creating", "Merge");
}
-
+
string resString{""};
- Json::FastWriter fw;
+ Json::FastWriter fw;
resString = fw.write(result);
ofstream o; o.open(mTargetJsonPath); o << resString; o.flush(); o.close();
}
@@ -163,8 +163,8 @@ namespace hg
if(status.scoreInvalid || !isEligibleForScore()) return;
- string validator{Online::getValidator(levelData.getPackPath(), levelData.getId(), levelData.getLevelRootPath(), levelData.getStyleRootPath(), levelData.getLuaScriptPath(), difficultyMult)};
- Online::startSendScore(getCurrentProfile().getName(), validator, status.currentTime);
+ string validator{Online::getValidator(levelData.getPackPath(), levelData.getId(), levelData.getLevelRootPath(), levelData.getStyleRootPath(), levelData.getLuaScriptPath())};
+ Online::startSendScore(getCurrentProfile().getName(), validator, difficultyMult, status.currentTime);
}
void HexagonGame::goToMenu(bool mSendScores)
{
@@ -180,9 +180,10 @@ namespace hg
void MenuGame::refreshScores()
{
+ if(state != States::MAIN) return;
float difficultyMult{difficultyMultipliers[difficultyMultIndex % difficultyMultipliers.size()]};
- string validator{Online::getValidator(levelData.getPackPath(), levelData.getId(), levelData.getLevelRootPath(), levelData.getStyleRootPath(), levelData.getLuaScriptPath(), difficultyMult)};
- Online::startGetScores(currentScores, validator);
+ string validator{Online::getValidator(levelData.getPackPath(), levelData.getId(), levelData.getLevelRootPath(), levelData.getStyleRootPath(), levelData.getLuaScriptPath())};
+ Online::startGetScores(currentScores, getCurrentProfile().getName(), validator, difficultyMult);
}
string MenuGame::getLeaderboard()
{
@@ -101,7 +101,7 @@ namespace hg
thread.launch();
}
- void startSendScore(const string& mName, const string& mValidator, float mScore)
+ void startSendScore(const string& mName, const string& mValidator, float mDifficulty, float mScore)
{
if(!getOnline()) { log("Online disabled, aborting", "Online"); return; }
@@ -110,14 +110,32 @@ namespace hg
log("Sending score to server...", "Online");
string scoreString{toStr(mScore)};
- string body{"n=" + mName + "&v=" + mValidator + "&s=" + scoreString + "&k=" + HG_ENCRYPTIONKEY};
- Response response{getPostResponse(host, folder, sendScoreFile, body)};
- Status status{response.getStatus()};
+ //string body{"n=" + mName + "&v=" + mValidator + "&s=" + scoreString + "&k=" + HG_ENCRYPTIONKEY};
+
+ Packet packet0x00, packet0x10;
+ packet0x00 << int8_t{0x00} << (string)mValidator << (float)mDifficulty << (string)mName << (float)mScore << (string)HG_ENCRYPTIONKEY;
+ TcpSocket socket;
+ socket.connect("209.236.124.147", 27272);
+ socket.send(packet0x00);
+ socket.receive(packet0x10);
+ uint8_t packetID, pass;
+ if(packet0x10 >> packetID >> pass)
+ {
+ if(packetID == 0x10)
+ {
+ if(pass == 0) cout << "Successfully submitted score!";
+ else cout << "Oops! Something went wrong!";
+ }
+ }
+ socket.disconnect();
- if(status == Response::Ok) log("Score sent successfully: " + mName + ", " + scoreString, "Online");
- else log("Send score error: " + status, "Online");
+ //Response response{getPostResponse(host, folder, sendScoreFile, body)};
+ //Status status{response.getStatus()};
- log("Finished sending score", "Online"); log(""); log(response.getBody(), "Server Message");
+ //if(status == Response::Ok) log("Score sent successfully: " + mName + ", " + scoreString, "Online");
+ //else log("Send score error: " + status, "Online");
+
+ //log("Finished sending score", "Online"); log(""); log(response.getBody(), "Server Message");
startCheckScores();
cleanUp();
});
@@ -141,7 +159,7 @@ namespace hg
checkThread.launch();
}
- void startGetScores(string& mTargetString, const string& mValidator)
+ void startGetScores(string& mTargetString, const string& mName, const string& mValidator, float mDifficulty)
{
if(!getOnline()) { log("Online disabled, aborting", "Online"); return; }
@@ -151,16 +169,38 @@ namespace hg
log("Getting scores from server...", "Online");
- string body{"v=" + mValidator};
- Response response{getPostResponse(host, folder, getScoresFile, body)};
- Status status{response.getStatus()};
+ Packet packet0x01, packet0x11;
+ packet0x01 << int8_t{0x01} << (string)mValidator << (float)mDifficulty << (string)mName;
+ TcpSocket socket;
+ socket.connect("209.236.124.147", 27272);
+ socket.send(packet0x01);
+ socket.receive(packet0x11);
+ uint8_t packetID, pass;
+ string response[2];
- if(status == Response::Ok)
+ if(packet0x11 >> packetID >> pass)
{
- log("Scores got successfully", "Online");
- mTargetString = response.getBody();
+ if(pass == 0)
+ {
+ if(packet0x11 >> response[0] >> response[1]) cout << "Received scores!" << endl << response[0]<< endl << response[1] << endl;
+ else cout<<"Oops! Something went wrong with getting the scores!";
+ }
+ else cout<<"Oops! Something went wrong with getting the scores!";
}
- else log("Get scores error: " + status, "Online");
+ socket.disconnect();
+
+
+
+ //string body{"v=" + mValidator};
+ //Response response{getPostResponse(host, folder, getScoresFile, body)};
+ //Status status{response.getStatus()};
+
+ //if(status == Response::Ok)
+ //{
+ // log("Scores got successfully", "Online");
+ // mTargetString = response.getBody();
+ //}
+ //else log("Get scores error: " + status, "Online");
log("Finished getting scores", "Online");
cleanUp();
@@ -173,15 +213,14 @@ namespace hg
void terminateAll() { for(auto& t : memoryManager.getItems()) t->terminate(); memoryManager.cleanUp(); }
string getValidator(const string& mPackPath, const string& mLevelId, const string& mLevelRootPath,
- const string& mStyleRootPath, const string& mLuaScriptPath, float mDifficultyMultiplier)
+ const string& mStyleRootPath, const string& mLuaScriptPath)
{
string luaScriptContents{getFileContents(mLuaScriptPath)};
unordered_set<string> luaScriptNames;
recursiveFillIncludedLuaFileNames(luaScriptNames, mPackPath, luaScriptContents);
string toEncrypt{""};
toEncrypt.append(mLevelId);
- toEncrypt.append(toStr(mDifficultyMultiplier));
toEncrypt.append(getFileContents(mLevelRootPath));
toEncrypt.append(getFileContents(mStyleRootPath));
toEncrypt.append(luaScriptContents);
@@ -201,11 +240,11 @@ namespace hg
float getServerVersion() { return serverVersion; }
string getServerMessage() { return serverMessage; }
- Json::Value getScores(const std::string& mValidator) { return scoresRoot[mValidator]; }
+ Json::Value getScores(const string& mValidator) { return scoresRoot[mValidator]; }
string getMD5Hash(const string& mString) { MD5 key{mString}; return key.GetHash(); }
string getUrlEncoded(const string& mString) { string result{""}; for(auto c : mString) if(isalnum(c)) result += c; return result; }
string getControlStripped(const string& mString) { string result{""}; for(auto c : mString) if(!iscntrl(c)) result += c; return result; }
-
+
}
}

0 comments on commit 8935ddd

Please sign in to comment.