17h of work

This commit is contained in:
Anonymous275
2020-05-26 19:13:27 +03:00
parent 49c38a0f00
commit 375c4a4952
21 changed files with 2095 additions and 65 deletions

View File

@@ -59,9 +59,28 @@ void Client::DeleteCar(int ident){
}
}
}
int Client::GetOpenCarID(){
int OpenID = 0;
bool found;
do {
found = true;
for (const std::pair<int, std::string> &a : VehicleData) {
if (a.first == OpenID){
OpenID++;
found = false;
}
}
}while (!found);
return OpenID;
}
void Client::AddNewCar(int ident,const std::string& Data){
VehicleData.insert(std::make_pair(ident,Data));
}
std::set<std::pair<int,std::string>> Client::GetAllCars(){
return VehicleData;
}
std::string Client::GetCarData(int ident){
for(const std::pair<int,std::string>& a : VehicleData){
if(a.first == ident){

View File

@@ -22,6 +22,7 @@ private:
public:
std::set<std::pair<int,std::string>> GetAllCars();
void AddNewCar(int ident,const std::string& Data);
void SetName(const std::string& name);
void SetRole(const std::string& role);
@@ -40,6 +41,7 @@ public:
bool isConnected();
void SetID(int ID);
int GetCarCount();
int GetOpenCarID();
int GetStatus();
int GetID();
};

View File

@@ -4,6 +4,7 @@
#include "Client.hpp"
#include "../logger.h"
#include "../Settings.hpp"
#include "../Lua System/LuaSystem.hpp"
void UDPSend(Client*c,const std::string&Data);
void TCPSend(Client*c,const std::string&Data);
@@ -35,7 +36,7 @@ void SendToAll(Client*c, const std::string& Data, bool Self, bool Rel){
for(Client*client : Clients){
if(Self || client != c){
if(Rel){
if(Data.length() > 1000)TCPSendLarge(client,Data);
if(Data.length() > 1000 || Data.substr(0,2) == "Od")TCPSendLarge(client,Data);
else TCPSend(client,Data);
}
else UDPSend(client,Data);
@@ -52,19 +53,25 @@ void UpdatePlayers(){
SendToAll(nullptr, Packet,true,true);
}
void OnDisconnect(Client*c,bool Timed){
std::string Packet = "Od:" + std::to_string(c->GetID());
SendToAll(c, Packet,false,true);
//if(Timed)Packet = "L"+c->GetName()+" Timed out!";
void OnDisconnect(Client*c,bool kicked){
std::string Packet;
for(const std::pair<int,std::string>&a : c->GetAllCars()){
Packet = "Od:" + std::to_string(c->GetID()) + "-" + std::to_string(a.first);
SendToAll(c, Packet,false,true);
}
if(kicked)Packet = "L"+c->GetName()+" was kicked!";
Packet = "L"+c->GetName()+" Left the server!";
SendToAll(c, Packet,false,true);
Packet.clear();
Clients.erase(c); ///Removes the Client from the list
Clients.erase(c); ///Removes the Client from the existence
}
int TriggerLuaEvent(const std::string& Event,bool local,Lua*Caller);
void OnConnect(Client*c){
c->SetID(OpenID());
std::cout << "New Client Created! ID : " << c->GetID() << std::endl;
Respond(c,"NR",true);
Respond(c,"M"+MapName,true); //Send the Map on connect
TriggerLuaEvent("onPlayerJoining",false,nullptr);
}

View File

@@ -6,68 +6,82 @@
#include "Client.hpp"
#include "../logger.h"
#include "../Settings.hpp"
#include "../Lua System/LuaSystem.hpp"
void SendToAll(Client*c, const std::string& Data, bool Self, bool Rel);
std::string HTTP_REQUEST(const std::string& IP,int port);
void Respond(Client*c, const std::string& MSG, bool Rel);
void UpdatePlayers();
void FindAndSync(Client*c,int VehID){
/*void FindAndSync(Client*c,int ClientID){
for (Client*client : Clients) {
if (client != c){
if(client->GetID() == VehID){ /////mark
Respond(client,c->GetCarData(VehID),true);
if(client->GetID() == ClientID){ /////mark
Respond(client,c->GetCarData(ClientID),true);
}
}
}
}
}*/
int TriggerLuaEvent(const std::string& Event,bool local,Lua*Caller);
void VehicleParser(Client*c, std::string Packet){
char Code = Packet.at(1);
int ID = -1;
std::string Data = Packet.substr(3);
int PID = -1;
int VID = -1;
std::string Data = Packet.substr(3),pid,vid;
switch(Code){ //Spawned Destroyed Switched/Moved NotFound Reset
case 's':
if(Data.at(0) == '0'){
if(TriggerLuaEvent("onVehicleSpawn",false,nullptr))break;
int CarID = c->GetOpenCarID();
std::cout << c->GetName() << " CarID : " << CarID << std::endl;
Packet = "Os:"+c->GetRole()+":"+c->GetName()+":"+std::to_string(c->GetID())+"-"+std::to_string(CarID)+Packet.substr(4);
if(c->GetCarCount() >= MaxCars){
std::string Destroy = "Od:" + std::to_string(c->GetID()); ///to revise
SendToAll(c,Destroy,true,true);
c->DeleteCar(c->GetID());
Respond(c,Packet,true);
std::string Destroy = "Od:" + std::to_string(c->GetID())+"-"+std::to_string(CarID);
Respond(c,Destroy,true);
}else{
c->AddNewCar(CarID,Packet);
SendToAll(nullptr, Packet,true,true);
}
Packet = "Os:"+c->GetRole()+":"+c->GetName()+":"+std::to_string(c->GetID())+Packet.substr(4);
c->AddNewCar(c->GetID(),Packet); ///revise later
}
SendToAll(nullptr,Packet,true,true);
break;
case 'd':
if(Data.find_first_not_of("0123456789") == std::string::npos){
ID = stoi(Data);
pid = Data.substr(0,Data.find('-'));
vid = Data.substr(Data.find('-')+1);
if(pid.find_first_not_of("0123456789") == std::string::npos && vid.find_first_not_of("0123456789") == std::string::npos){
PID = stoi(pid);
VID = stoi(vid);
}
if(ID != -1 && ID == c->GetID()){
if(PID != -1 && VID != -1 && PID == c->GetID()){
SendToAll(nullptr,Packet,true,true);
c->DeleteCar(c->GetID());
///std::cout << "Delete Requested from " << c->GetName() << std::endl;
c->DeleteCar(VID);
}
break;
case 'm':
break;
case 'n':
/*case 'n':
if(Packet.substr(3).find_first_not_of("0123456789") == std::string::npos){
ID = stoi(Packet.substr(3));
PID = stoi(Packet.substr(3));
}
FindAndSync(c,ID);
break;
FindAndSync(c,PID); //ACK System
break;*/
case 'r':
SendToAll(c,Packet,false,true);
break;
}
}
void SyncVehicles(Client*c){
Respond(c,"Sn"+c->GetName(),true);
SendToAll(c,"JWelcome "+c->GetName()+"!",false,true);
TriggerLuaEvent("onPlayerJoin",false,nullptr);
for (Client*client : Clients) {
if (client != c) {
Respond(c,client->GetCarData(client->GetID()),true); ///revise later
for(const std::pair<int,std::string>&a : client->GetAllCars()){
Respond(c,a.second,true);
}
}
}
}
@@ -119,6 +133,7 @@ void GlobalParser(Client*c, const std::string&Packet){
SendToAll(c,Packet,false,true);
break;
case 'C':
if(TriggerLuaEvent("onChatMessage",false,nullptr))break;
SendToAll(nullptr,Packet,true,true);
break;
case 'E':

View File

@@ -17,7 +17,7 @@ void TCPSend(Client*c,const std::string&Data){
else if (BytesSent < 0) {
std::cout << "(TCP) send failed with error: " << WSAGetLastError() << std::endl;
closesocket(c->GetTCPSock());
c->SetStatus(-2);
c->SetStatus(-1);
}
}
@@ -35,7 +35,7 @@ void TCPRcv(Client*c){
else if (BytesRcv < 0) {
std::cout << "(TCP) recv failed with error: " << WSAGetLastError() << std::endl;
closesocket(c->GetTCPSock());
c->SetStatus(-2);
c->SetStatus(-1);
}
GlobalParser(c, std::string(buf));
}

View File

@@ -7,7 +7,6 @@
#include <iostream>
#include <vector>
#include <tuple>
#include "../logger.h"
#include "../Settings.hpp"