Skip to content

Commit

Permalink
Starting migration of server from NodeJS to C++.
Browse files Browse the repository at this point in the history
  • Loading branch information
radeusgd committed Jun 6, 2016
1 parent c514697 commit 8acfa91
Show file tree
Hide file tree
Showing 152 changed files with 10,962 additions and 439 deletions.
35 changes: 3 additions & 32 deletions .gitignore
@@ -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
21 changes: 21 additions & 0 deletions CMakeLists.txt
@@ -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")
92 changes: 92 additions & 0 deletions GameServer.cpp
@@ -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()));
}

49 changes: 49 additions & 0 deletions GameServer.hpp
@@ -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
15 changes: 15 additions & 0 deletions Message.cpp
@@ -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();
}
18 changes: 18 additions & 0 deletions Message.hpp
@@ -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
13 changes: 0 additions & 13 deletions cards/atak.js

This file was deleted.

13 changes: 0 additions & 13 deletions cards/pustak.js

This file was deleted.

11 changes: 0 additions & 11 deletions deck.js

This file was deleted.

110 changes: 0 additions & 110 deletions game.js

This file was deleted.

Binary file removed html/avatars/0.jpg
Binary file not shown.
Binary file removed html/avatars/1.jpg
Binary file not shown.
Binary file removed html/avatars/10.jpg
Binary file not shown.
Binary file removed html/avatars/11.jpg
Binary file not shown.
Binary file removed html/avatars/12.jpg
Binary file not shown.
Binary file removed html/avatars/13.jpg
Binary file not shown.
Binary file removed html/avatars/14.jpg
Binary file not shown.
Binary file removed html/avatars/15.jpg
Binary file not shown.
Binary file removed html/avatars/16.jpg
Binary file not shown.
Binary file removed html/avatars/17.jpg
Binary file not shown.
Binary file removed html/avatars/18.jpg
Binary file not shown.
Binary file removed html/avatars/19.jpg
Binary file not shown.
Binary file removed html/avatars/2.jpg
Binary file not shown.
Binary file removed html/avatars/20.jpg
Binary file not shown.
Binary file removed html/avatars/21.jpg
Binary file not shown.
Binary file removed html/avatars/22.jpg
Binary file not shown.
Binary file removed html/avatars/23.jpg
Binary file not shown.
Binary file removed html/avatars/3.jpg
Binary file not shown.
Binary file removed html/avatars/4.jpg
Binary file not shown.
Binary file removed html/avatars/5.jpg
Binary file not shown.
Binary file removed html/avatars/6.jpg
Binary file not shown.
Binary file removed html/avatars/7.jpg
Binary file not shown.
Binary file removed html/avatars/8.jpg
Binary file not shown.
Binary file removed html/avatars/9.jpg
Binary file not shown.

0 comments on commit 8acfa91

Please sign in to comment.