Skip to content

Commit

Permalink
Fix: se pueden unir varios jugadores a un mismo server despues de fin…
Browse files Browse the repository at this point in the history
…alizar
  • Loading branch information
TomasGalian committed Jun 27, 2023
1 parent 03d8c5f commit d18790f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
9 changes: 6 additions & 3 deletions src/server/player_receiver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
#include <arpa/inet.h>

PlayerReceiver::PlayerReceiver(Socket *socket, std::atomic<bool> &stay_in_match,
std::atomic<bool> &keep_playing, int32_t& player_id)
: player_id(player_id), stay_in_match(stay_in_match), keep_playing(keep_playing),
server_deserializer(socket) {
std::atomic<bool> &keep_playing, int32_t& player_id,
Queue<GameDTO> &queue_sender) : queue_sender(queue_sender),
player_id(player_id), stay_in_match(stay_in_match), keep_playing(keep_playing),
server_deserializer(socket) {
queue_receiver = nullptr; //Comienza en nullptr. El player_sender le va a setear la queue
}

Expand All @@ -32,7 +33,9 @@ void PlayerReceiver::run() {
break;
}
}
keep_playing = false;
stay_in_match = false;
queue_sender.close();
}

void PlayerReceiver::setQueueReceiver(Queue<InstructionsDTO*> *queue_receiver){
Expand Down
5 changes: 4 additions & 1 deletion src/server/player_receiver.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,15 @@ class PlayerReceiver : public Thread{
std::atomic<bool>& keep_playing;

ServerDeserializer server_deserializer;

Queue<GameDTO> &queue_sender;

int32_t player_id;

public:
PlayerReceiver(Socket *socket, std::atomic<bool> &stay_in_match,
std::atomic<bool> &keep_playing, int32_t& player_id);
std::atomic<bool> &keep_playing, int32_t& player_id,
Queue<GameDTO> &queue_sender);

void run() override;

Expand Down
5 changes: 4 additions & 1 deletion src/server/player_sender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
PlayerSender::PlayerSender(Socket *socket, std::atomic<bool> &stay_in_match,
std::atomic<bool> &keep_playing, MatchMananger *match_mananger, int32_t& player_id) :
stay_in_match(stay_in_match), keep_playing(keep_playing), queue_sender(10000),
player_receiver(socket,stay_in_match, keep_playing, player_id),match_mananger(match_mananger),
player_receiver(socket,stay_in_match, keep_playing, player_id,queue_sender),match_mananger(match_mananger),
server_deserializer(socket), server_serializer(socket), player_id(player_id) {
queue_receiver = nullptr; //Al inicio es un nullptr ya que no tiene ninguna partida asociada.
stay_in_match = true;
Expand All @@ -31,10 +31,13 @@ void PlayerSender::run() {
std::cout<<"Se cerro la cola correctamente"<<std::endl;
}
stay_in_match = false; //En el caso de que se haya cerrado el socket pongo stay_in_match=false.
keep_playing = false;
//Si esta asociada a una partida lo elimino de la partida
if (queue_receiver != nullptr)
match_mananger->delete_player(player_id); //Elimino al jugador de la partida en la que estaba
}
std::cout<<"Salgo"<<std::endl;

}

void PlayerSender::join_player_receiver(){
Expand Down

0 comments on commit d18790f

Please sign in to comment.