Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Starting migration of server from NodeJS to C++.
- Loading branch information
Showing
152 changed files
with
10,962 additions
and
439 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,4 @@ | ||
# Logs | ||
logs | ||
*.log | ||
npm-debug.log* | ||
build/ | ||
web/avatars/ | ||
lib/ | ||
|
||
# Runtime data | ||
pids | ||
*.pid | ||
*.seed | ||
|
||
# Directory for instrumented libs generated by jscoverage/JSCover | ||
lib-cov | ||
|
||
# Coverage directory used by tools like istanbul | ||
coverage | ||
|
||
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) | ||
.grunt | ||
|
||
# node-waf configuration | ||
.lock-wscript | ||
|
||
# Compiled binary addons (http://nodejs.org/api/addons.html) | ||
build/Release | ||
|
||
# Dependency directory | ||
node_modules | ||
|
||
# Optional npm cache directory | ||
.npm | ||
|
||
# Optional REPL history | ||
.node_repl_history |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
cmake_minimum_required(VERSION 2.6) | ||
project(TrachOnline) | ||
|
||
include_directories("${PROJECT_SOURCE_DIR}/../seasocks/src/main/c/") | ||
include_directories("${PROJECT_SOURCE_DIR}") | ||
|
||
set(CMAKE_CXX_FLAGS "-g -std=c++11 ${CMAKE_CXX_FLAGS} -pthread") | ||
|
||
set(SOURCES | ||
main.cpp | ||
Message.cpp | ||
GameServer.cpp | ||
) | ||
|
||
# Define sources and executable | ||
set(EXECUTABLE_NAME "trachserver") | ||
add_executable(${EXECUTABLE_NAME} ${SOURCES}) | ||
|
||
#link_directories("${PROJECT_SOURCE_DIR}/lib") | ||
|
||
target_link_libraries(${EXECUTABLE_NAME} "${PROJECT_SOURCE_DIR}/lib/libseasocks.so") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
#include "GameServer.hpp" | ||
|
||
GameServer::User::User(WebSocket* ws) : ws(ws) {} | ||
GameServer::User::User(){} | ||
|
||
void GameServer::onConnect(WebSocket *socket) | ||
{ | ||
if(state == PLAYING){ | ||
send(socket,Message("error","Game in progress. Spectating not supported").toText()); | ||
socket->close(); | ||
return; | ||
} | ||
connections.insert(make_pair(socket,User(socket))); | ||
updateUsers(); | ||
} | ||
void GameServer::onData(WebSocket * conn, const char *data) | ||
{ | ||
Message m(data); | ||
if(handlers[m.name]){ | ||
handlers[m.name](conn,m.data); | ||
} | ||
} | ||
void GameServer::onDisconnect(WebSocket *socket) | ||
{ | ||
if(state == WAITING) | ||
connections.erase(socket); | ||
else if(connections.find(socket)!=connections.end()){ | ||
connections[socket].nickname = connections[socket].nickname + " (disconnected)"; | ||
connections[socket].ws = nullptr; | ||
afkCount++; | ||
if(afkCount==connections.size()){ | ||
cout<<"All players disconnected. Resetting game state."<<endl; | ||
connections.clear(); | ||
afkCount = 0; | ||
resetGame(); | ||
} | ||
} | ||
updateUsers(); | ||
} | ||
|
||
void GameServer::broadcast(const Message& m){ | ||
string data = m.toText(); | ||
for (auto c : connections){ | ||
if(c.second.ws!=nullptr) | ||
c.second.ws->send(data.c_str()); | ||
} | ||
} | ||
void GameServer::send(WebSocket* c, const Message& m){ | ||
if(c==nullptr) return; | ||
c->send(m.toText().c_str()); | ||
} | ||
|
||
GameServer::GameServer(){ | ||
handlers["login"] = [&](WebSocket* conn, json data){ | ||
connections[conn].nickname = data["nickname"]; | ||
updateUsers(); | ||
}; | ||
handlers["requestStart"] = [&](WebSocket* conn, json data){ | ||
startPlaying(); | ||
}; | ||
} | ||
void GameServer::updateUsers(){ | ||
Message m; | ||
m.name = "updateUsers"; | ||
for(auto c : connections){ | ||
json u; | ||
u["id"] = c.second.playerId; | ||
u["name"] = c.second.nickname; | ||
m.data.push_back(u); | ||
} | ||
broadcast(m); | ||
} | ||
|
||
void GameServer::resetGame(){ | ||
state = WAITING; | ||
} | ||
void GameServer::startPlaying(){ | ||
cout<<"Starting the game"<<endl; | ||
state = PLAYING; | ||
int count = 0; | ||
for(auto c : connections){ | ||
if(c.second.nickname!="???") count++; | ||
} | ||
//players.resize(count); | ||
int pid = 0; | ||
for(auto c : connections){ | ||
if(c.second.nickname!="???") c.second.playerId = pid++; | ||
} | ||
updateUsers(); | ||
broadcast(Message("start",json())); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#ifndef GAMESERVER_HPP | ||
#define GAMESERVER_HPP | ||
|
||
#include "seasocks/PrintfLogger.h" | ||
#include "seasocks/Server.h" | ||
#include "seasocks/WebSocket.h" | ||
#include "json.hpp" | ||
#include <set> | ||
#include <string> | ||
|
||
#include "Message.hpp" | ||
|
||
using namespace std; | ||
using namespace seasocks; | ||
using namespace nlohmann; | ||
|
||
struct GameServer : WebSocket::Handler { | ||
struct User{ | ||
User(WebSocket* ws); | ||
User(); | ||
string nickname = "???"; | ||
WebSocket* ws; | ||
int playerId=-1; | ||
}; | ||
|
||
//helper functions | ||
void onConnect(WebSocket *socket) override; | ||
void onData(WebSocket * conn, const char *data) override; | ||
void onDisconnect(WebSocket *socket) override; | ||
|
||
void broadcast(const Message& m); | ||
void send(WebSocket* c, const Message& m); | ||
|
||
//logic implementation | ||
map<WebSocket*,User> connections; | ||
int afkCount; | ||
map<string,function<void(WebSocket*,json)>> handlers; | ||
|
||
enum State{ | ||
WAITING, PLAYING | ||
} state = WAITING; | ||
GameServer(); | ||
void updateUsers(); | ||
|
||
void resetGame(); | ||
void startPlaying(); | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#include "Message.hpp" | ||
|
||
Message::Message(){} | ||
Message::Message(string name, json data) : name(name), data(data) {} | ||
Message::Message(string text){ | ||
json msg = json::parse(text); | ||
name = msg["name"]; | ||
data = msg["data"]; | ||
} | ||
string Message::toText() const{ | ||
json msg; | ||
msg["name"] = name; | ||
msg["data"] = data; | ||
return msg.dump(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#ifndef MESSAGE_HPP | ||
#define MESSAGE_HPP | ||
|
||
#include "json.hpp" | ||
|
||
using namespace nlohmann; | ||
using namespace std; | ||
|
||
struct Message{ | ||
Message(); | ||
Message(string name, json data); | ||
Message(string text); | ||
string name; | ||
json data; | ||
string toText() const; | ||
}; | ||
|
||
#endif |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Oops, something went wrong.