Compare commits

...

3 Commits

Author SHA1 Message Date
c107ce732c start ChessServer and remove basic redundant examples 2025-05-20 17:25:50 -05:00
ad658d914c improve ChatVoice 2025-05-20 16:06:11 -05:00
f6ad110880 add ^ 2025-05-19 14:13:55 -05:00
12 changed files with 236 additions and 179 deletions

View File

@@ -114,6 +114,8 @@ double Calculator::evaluate(std::string equation, std::unordered_map<char, std::
return std::stod(equation); return std::stod(equation);
} }
break; break;
default:
break;
} }
} }
//op is now the appropriate operator //op is now the appropriate operator
@@ -146,6 +148,8 @@ double Calculator::evaluate(std::string equation, std::unordered_map<char, std::
return a + b; return a + b;
case '-': case '-':
return a - b; return a - b;
default:
break;
} }
} }
@@ -155,6 +159,12 @@ std::string Calculator::calculate(std::string equation) {
while(equation.find(' ') != std::string::npos) { while(equation.find(' ') != std::string::npos) {
equation.erase(equation.find(' '), 1); equation.erase(equation.find(' '), 1);
} }
while(equation.find('\t') != std::string::npos) {
equation.erase(equation.find('\t'), 1);
}
while(equation.find('\n') != std::string::npos) {
equation.erase(equation.find('\n'), 1);
}
std::unordered_map<unsigned int, std::string> nodes; std::unordered_map<unsigned int, std::string> nodes;
std::unordered_map<char, std::string> evalStr; std::unordered_map<char, std::string> evalStr;
@@ -257,19 +267,16 @@ void Calculator::basicCalculator() {
s.clear(); s.clear();
} }
ImGui::SameLine(); ImGui::SameLine();
if(ImGui::Button("()")) { if(ImGui::Button("(")) {
if(parenthesis) {
s += " ( "; s += " ( ";
} else {
s += " ) ";
}
parenthesis = !parenthesis;
} }
ImGui::SameLine(); ImGui::SameLine();
if(ImGui::Button("Gr")) { if(ImGui::Button(")")) {
graphing = true; s += " ) ";
}
ImGui::SameLine();
if(ImGui::Button("^")) {
s += " ^ ";
} }
ImGui::SameLine(); ImGui::SameLine();
if(ImGui::Button("=")) { if(ImGui::Button("=")) {

View File

@@ -84,7 +84,8 @@ void ChatClientVoice::run() {
if(cm->isRunning() && cm->isConnected()) { if(cm->isRunning() && cm->isConnected()) {
if(ImGui::Button("Disconnect") && cm->isRunning()) { if(ImGui::Button("Disconnect") && cm->isRunning()) {
cm->stopClient(); cm->sendReliable("srvcmd1", strlen("srvcmd1"));
//cm->stopClient();
} }
} else { } else {
@@ -105,11 +106,16 @@ void ChatClientVoice::run() {
ImGui::SameLine(); ImGui::SameLine();
if(ImGui::Button("send")) { if(ImGui::Button("send")) {
s.insert(s.begin(), 'o'); if(s == "/quit") {
s.insert(s.begin(), ' '); cm->sendReliable("srvcmd1", strlen("srvcmd1"));
s.insert(s.begin(), '0'); cm->stopClient();
s.insert(s.begin(), 'a'); } else if(s == "/shutdown") {
cm->sendReliable("srvcmd2", strlen("srvcmd2"));
//cm->stopClient();
} else {
s.insert(0, "client");
cm->sendReliable(s.c_str(), (uint32) s.length()); cm->sendReliable(s.c_str(), (uint32) s.length());
}
s.clear(); s.clear();
} }
} }
@@ -130,9 +136,6 @@ void ChatClientVoice::run() {
SDL_ClearAudioStream(mic); SDL_ClearAudioStream(mic);
SDL_ResumeAudioStreamDevice(mic); SDL_ResumeAudioStreamDevice(mic);
SDL_SetAudioStreamGain(mic, micVol); SDL_SetAudioStreamGain(mic, micVol);
} else if(!cm->isConnected()) {
SDL_PauseAudioStreamDevice(mic);
SDL_ClearAudioStream(mic);
} }
if(!SDL_AudioStreamDevicePaused(speaker)) { if(!SDL_AudioStreamDevicePaused(speaker)) {
@@ -141,10 +144,6 @@ void ChatClientVoice::run() {
SDL_ClearAudioStream(speaker); SDL_ClearAudioStream(speaker);
SDL_ResumeAudioStreamDevice(speaker); SDL_ResumeAudioStreamDevice(speaker);
SDL_SetAudioStreamGain(speaker, speakerVol); SDL_SetAudioStreamGain(speaker, speakerVol);
} else if(!cm->isConnected()) {
SDL_PauseAudioStreamDevice(speaker);
SDL_ClearAudioStream(speaker);
} }
static int avail = 0; static int avail = 0;
@@ -152,9 +151,10 @@ void ChatClientVoice::run() {
if(micTest) { if(micTest) {
avail = SDL_min(len, SDL_GetAudioStreamAvailable(mic)); avail = SDL_min(len, SDL_GetAudioStreamAvailable(mic));
if(avail > 10) {
avail = SDL_GetAudioStreamData(mic, buf, avail); avail = SDL_GetAudioStreamData(mic, buf, avail);
SDL_PutAudioStreamData(speaker, buf, avail); SDL_PutAudioStreamData(speaker, buf, avail);
}
} else if(cm->isConnected()){ } else if(cm->isConnected()){
// if not testing, send to server. // if not testing, send to server.
@@ -176,6 +176,7 @@ void ChatClientVoice::run() {
bool ChatClientVoice::onEvent(const Archimedes::Event& event) { bool ChatClientVoice::onEvent(const Archimedes::Event& event) {
unsigned int type = app->getEventType(event); unsigned int type = app->getEventType(event);
static ClientModule* cm;
/*if(type == app->getEventType("DataSentEvent")) { /*if(type == app->getEventType("DataSentEvent")) {
//we did this? //we did this?
@@ -186,19 +187,49 @@ bool ChatClientVoice::onEvent(const Archimedes::Event& event) {
Archimedes::DataRecievedEvent& e = (Archimedes::DataRecievedEvent&) event; Archimedes::DataRecievedEvent& e = (Archimedes::DataRecievedEvent&) event;
{ cm = (ClientModule*) moduleInstances[ClientModule()]; }
static std::string s; static std::string s;
if(((char*)e.msg->m_pData)[0] == 'a' && ((char*)e.msg->m_pData)[1] == '0' && ((char*)e.msg->m_pData)[2] == ' ' && ((char*)e.msg->m_pData)[3] == 'o') { if(std::string((const char*)e.msg->m_pData, 6) == "client") {
s = std::string((const char*)e.msg->m_pData + 4, e.msg->m_cbSize - 4); s = std::string((const char*)e.msg->m_pData + 6, e.msg->m_cbSize - 6);
messages += "\n\n" + s; messages += "\n\n" + s;
} else { } else if(std::string((const char*)e.msg->m_pData, 6) == "server") {
s = std::string((const char*)e.msg->m_pData + 6, e.msg->m_cbSize - 6);
int serverCode = stoi(s);
switch(serverCode) {
case 0:
//start voice
SDL_ClearAudioStream(mic);
SDL_ClearAudioStream(speaker);
SDL_ResumeAudioStreamDevice(mic);
SDL_ResumeAudioStreamDevice(speaker);
break;
case 1:
//stop voice and clear
SDL_PauseAudioStreamDevice(mic);
SDL_PauseAudioStreamDevice(speaker);
SDL_ClearAudioStream(mic);
SDL_ClearAudioStream(speaker);
break;
case 2:
//disconnect
cm->stopClient();
break;
default:
break;
}
} else /*if(std::string((const char*)e.msg->m_pData, 6) == "audio:")*/ {
s = "audio data"; s = "audio data";
SDL_PutAudioStreamData(speaker, e.msg->m_pData, e.msg->m_cbSize); SDL_PutAudioStreamData(speaker, e.msg->m_pData, e.msg->m_cbSize);
} }
std::cerr << "Client Recieved: " << s << std::endl; //std::cerr << "Client Recieved: " << s << std::endl;
return true; return true;
} else if(type == app->getEventType("ConnectionStatusChangedEvent")) { } else if(type == app->getEventType("ConnectionStatusChangedEvent")) {
@@ -230,11 +261,6 @@ bool ChatClientVoice::onEvent(const Archimedes::Event& event) {
case k_ESteamNetworkingConnectionState_Connected: case k_ESteamNetworkingConnectionState_Connected:
//OnConnect //OnConnect
SDL_ClearAudioStream(mic);
SDL_ClearAudioStream(speaker);
SDL_ResumeAudioStreamDevice(mic);
SDL_ResumeAudioStreamDevice(speaker);
break; break;
default: default:

View File

@@ -12,7 +12,7 @@ bool ChatServerVoice::onEvent(const Archimedes::Event& event) {
unsigned int type = app->getEventType(event); unsigned int type = app->getEventType(event);
static ServerModule* sm;
/*if(type == app->getEventType("DataSentEvent")) { /*if(type == app->getEventType("DataSentEvent")) {
//we did this? //we did this?
return true; return true;
@@ -20,30 +20,48 @@ bool ChatServerVoice::onEvent(const Archimedes::Event& event) {
if(type == app->getEventType(Archimedes::DataRecievedEvent())) { if(type == app->getEventType(Archimedes::DataRecievedEvent())) {
static ServerModule* sm; { sm = (ServerModule*) moduleInstances[ServerModule()]; } { sm = (ServerModule*) moduleInstances[ServerModule()]; }
Archimedes::DataRecievedEvent& e = (Archimedes::DataRecievedEvent&) event; Archimedes::DataRecievedEvent& e = (Archimedes::DataRecievedEvent&) event;
static std::string s; static std::string s;
if(((char*)e.msg->m_pData)[0] == 'a' && ((char*)e.msg->m_pData)[1] == '0' && ((char*)e.msg->m_pData)[2] == ' ' && ((char*)e.msg->m_pData)[3] == 'o') { if(std::string((const char*)e.msg->m_pData, 6) == "client") {
s = std::string((const char*)e.msg->m_pData + 4, e.msg->m_cbSize - 4); s = std::string((const char*)e.msg->m_pData + 6, e.msg->m_cbSize - 6);
if(s == "/quit") {
sm->disconnectClient(e.msg->m_conn);
} else if(s == "/shutdown") {
sm->stopServer();
app->end();
}
for(auto& it : sm->getClients()) { for(auto& it : sm->getClients()) {
if(it.first != e.msg->m_conn) if(it.first != e.msg->m_conn)
sm->sendReliable(it.first, e.msg->m_pData, e.msg->m_cbSize); sm->sendReliable(it.first, e.msg->m_pData, e.msg->m_cbSize);
else else
sm->sendReliable(e.msg->m_conn, "a0 o\nMessage sent\n", strlen("a0 o\nMessage sent\n")); sm->sendReliable(e.msg->m_conn, "client\nMessage sent\n", strlen("client\nMessage sent\n"));
} }
} else { } else if(std::string((const char*)e.msg->m_pData, 6) == "srvcmd") {
s = std::string((const char*)e.msg->m_pData + 6, e.msg->m_cbSize - 6);
int cmdCode = stoi(s);
switch(cmdCode) {
case 0:
break;
case 1:
//disconnect
sm->disconnectClient(e.msg->m_conn);
break;
case 2:
for(auto& it : sm->getClients()) {
sm->sendReliable(it.first, "server2", strlen("server2"));
}
sm->stopServer();
app->end();
break;
default:
break;
}
} else /*if(std::string((const char*)e.msg->m_pData, 6) == "audio:")*/ {
s = "audio data"; s = "audio data";
for(auto& it : sm->getClients()) { for(auto& it : sm->getClients()) {
@@ -52,17 +70,62 @@ bool ChatServerVoice::onEvent(const Archimedes::Event& event) {
} }
} }
std::cerr << "Server Recieved: " << s << std::endl; //std::cerr << "Server Recieved: " << s << std::endl;
return true; return true;
} /*else if(type == app->getEventType("ConnectionStatusChangedEvent")) { } else if(type == app->getEventType("ConnectionStatusChangedEvent")) {
//Archimedes::ConnectionStatusChangedEvent& e = (Archimedes::ConnectionStatusChangedEvent&) event; Archimedes::ConnectionStatusChangedEvent& e = (Archimedes::ConnectionStatusChangedEvent&) event;
{ sm = (ServerModule*) moduleInstances[ServerModule()]; }
unsigned int numClients;
(void)sm->getClients(&numClients);
switch(e.info->m_info.m_eState) {
case k_ESteamNetworkingConnectionState_None:
// NOTE: We will get callbacks here when we destroy connections. You can ignore these.
break;
case k_ESteamNetworkingConnectionState_ClosedByPeer:
case k_ESteamNetworkingConnectionState_ProblemDetectedLocally:
{
if(numClients < 2) {
for(auto& it : sm->getClients()) {
sm->sendReliable(it.first, "server1", strlen("server1"));
}
}
break;
}
case k_ESteamNetworkingConnectionState_Connecting:
{
break;
}
case k_ESteamNetworkingConnectionState_Connected:
//OnConnect
if(numClients == 2) {
for(auto& it : sm->getClients()) {
sm->sendReliable(it.first, "server0", strlen("server0"));
}
} else if(numClients > 2) {
sm->sendReliable(e.info->m_hConn, "server0", strlen("server0"));
}
break;
default:
// Silences -Wswitch
break;
}
return false; return false;
}*/ }
return false; return false;
} }

View File

@@ -0,0 +1,46 @@
#include "ChessServer.h"
void ChessServer::onLoad() {
ServerModule* sm = (ServerModule*) moduleInstances[ServerModule()];
sm->startServer(9932);
}
//void ChessServer::run() {}
bool ChessServer::onEvent(const Archimedes::Event& event) {
unsigned int type = app->getEventType(event);
/*if(type == app->getEventType("DataSentEvent")) {
//we did this?
return true;
} else */
if(type == app->getEventType(Archimedes::DataRecievedEvent())) {
static ServerModule* sm; { sm = (ServerModule*) moduleInstances[ServerModule()]; }
Archimedes::DataRecievedEvent& e = (Archimedes::DataRecievedEvent&) event;
static std::string s; s = std::string((const char*)e.msg->m_pData, e.msg->m_cbSize);
for(auto& it : sm->getClients()) {
if(it.first != e.msg->m_conn)
sm->sendReliable(it.first, s.c_str(), s.length());
}
return true;
} /*else if(type == app->getEventType("ConnectionStatusChangedEvent")) {
//Archimedes::ConnectionStatusChangedEvent& e = (Archimedes::ConnectionStatusChangedEvent&) event;
return false;
}*/
return false;
}

View File

@@ -0,0 +1,34 @@
#include "Archimedes.h"
#include "modules/ServerModule/ServerModule.h"
class ChessServer : public Archimedes::Module {
public:
ChessServer(Archimedes::App* a, void* h) : Module(a, h) {
name = "ChessServer";
ServerModule* sm = new ServerModule(a, h);
deps[*sm] = sm;
sm->shouldHandleEvents(ServerModule::SMEventEnum::ConnectionStatusChanged | ServerModule::SMEventEnum::DataSent);
}
ChessServer() { name = "ChessServer"; }
~ChessServer() {
if(app) {}
}
void onLoad();
//void run();
bool onEvent(const Archimedes::Event&);
};
#ifdef CHESSSERVER_DYNAMIC
typedef ChessServer mtype;
#include "endModule.h"
#endif

View File

@@ -1,16 +0,0 @@
#include "DependsOnPrint.h"
DependsOnPrint::DependsOnPrint(Archimedes::App* a, void* h) : Module(a, h) {
name = "DependsOnPrint";
deps["Print"] = "/home/nathan/Projects/Archimedes/result-1/bin/Print";
}
DependsOnPrint::~DependsOnPrint() {
std::cout << "DependsOnPrint Destroyed!\n";
}
void DependsOnPrint::run() {
std::cout << "DependsOnPrint lib loaded and run!\n";
app->emitEvent(new Archimedes::DoUnloadModuleEvent(name));
}

View File

@@ -1,16 +0,0 @@
#include "Archimedes.h"
class DependsOnPrint : public Archimedes::Module {
public:
DependsOnPrint(Archimedes::App*, void*);
~DependsOnPrint();
void run();
void onLoad() {}
};
#ifdef DEPENDSONPRINT_DYNAMIC
typedef DependsOnPrint mtype;
#include "endModule.h"
#endif

View File

@@ -1,19 +0,0 @@
#include "DependsOnPrintStatic.h"
#include "modules/Archimedes-Modules/Print/Print.h"
DependsOnPrintStatic::DependsOnPrintStatic(Archimedes::App* a, void* h) : Module(a, h) {
name = "DependsOnPrintStatic";
Print* p = new Print(a, h);
deps[*p] = p;
}
DependsOnPrintStatic::~DependsOnPrintStatic() {
std::cout << "DependsOnPrintStatic Destroyed!\n";
}
void DependsOnPrintStatic::run() {
std::cout << "DependsOnPrintStatic lib loaded and run!\n";
app->emitEvent(new Archimedes::DoUnloadModuleEvent(name));
}

View File

@@ -1,16 +0,0 @@
#include "Archimedes.h"
class DependsOnPrintStatic : public Archimedes::Module {
public:
DependsOnPrintStatic(Archimedes::App*, void*);
~DependsOnPrintStatic();
void run();
void onLoad() {}
};
#ifdef DEPENDSONPRINTSTATIC_DYNAMIC
typedef DependsOnPrintStatic mtype;
#include "endModule.h"
#endif

View File

@@ -1,37 +0,0 @@
#include "TestMenu.h"
TestMenu::TestMenu(Archimedes::App* a, void* h) : Module(a, h) {
name = "TestMenu";
}
TestMenu::~TestMenu() {
std::cout << "TestMenu Destroyed!\n";
}
void TestMenu::run() {
std::cout << "Your number is: " << num << "\n"
<< "1. Add 1\n"
<< "2. Subtract 1\n"
<< "3. Unload Module\n\n"
<< "4. Quit\n\n";
std::cin >> choice;
switch(choice) {
case 1:
num++;
break;
case 2:
num--;
break;
case 3:
app->emitEvent(new Archimedes::DoUnloadModuleEvent(name));
break;
case 4:
app->end();
break;
default:
break;
}
}

View File

@@ -1,20 +0,0 @@
#include "Archimedes.h"
class TestMenu : public Archimedes::Module {
public:
TestMenu(Archimedes::App*, void*);
TestMenu() { name = "TestMenu"; }
~TestMenu();
void run();
void onLoad() {}
private:
int choice;
int num = 5;
};
#ifdef TESTMENU_DYNAMIC
typedef TestMenu mtype;
#include "endModule.h"
#endif

View File

@@ -52,7 +52,12 @@ class ServerModule : public Archimedes::Module {
} }
} }
std::map<HSteamNetConnection, unsigned int> getClients() const { return clients; } std::map<HSteamNetConnection, unsigned int> getClients(unsigned int* num = nullptr) const {
if(num) {
*num = numClients;
}
return clients;
}
void pollIncomingData(); void pollIncomingData();