Skip to content

Commit

Permalink
Merge pull request #23 from Jignacio14/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
Jignacio14 authored Jun 11, 2024
2 parents 759f30e + b742b66 commit 3c4bbb2
Show file tree
Hide file tree
Showing 812 changed files with 9,656 additions and 5,437 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,5 @@ process.logs

AtoB.dump
BtoA.dump

.Trash-1000
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ file(GLOB_RECURSE SERVER_SOURCES CONFIGURE_DEPENDS
"${PROJECT_SOURCE_DIR}/src/server/*.cpp"
"${PROJECT_SOURCE_DIR}/src/engine/*.h"
"${PROJECT_SOURCE_DIR}/src/engine/*.cpp"
"${PROJECT_SOURCE_DIR}/src/server/placeholder.cpp"
"${PROJECT_SOURCE_DIR}/src/engine/weapons/*.h"
"${PROJECT_SOURCE_DIR}/src/engine/weapons/*.cpp"
"${PROJECT_SOURCE_DIR}/src/engine/states/*.h"
Expand All @@ -75,6 +74,9 @@ file(GLOB_RECURSE SERVER_SOURCES CONFIGURE_DEPENDS
file(GLOB_RECURSE COMMON_SOURCES CONFIGURE_DEPENDS
"${PROJECT_SOURCE_DIR}/src/common/*.h"
"${PROJECT_SOURCE_DIR}/src/common/*.cpp"
"${PROJECT_SOURCE_DIR}/src/data/"
"${PROJECT_SOURCE_DIR}/src/data/*.h"
"${PROJECT_SOURCE_DIR}/src/data/*.cpp"
)

# The client executable will be called "client". The main entry point is defined with all the other sources
Expand Down
8 changes: 7 additions & 1 deletion games_config.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
max_players_per_game: 5
max_players_per_game: 2 # CANNOT EXEED 10
max_game_duration: 90
max_username_length: 29
min_number_of_players: 1
Expand All @@ -13,3 +13,9 @@ target_fps: 60
# Client exclusive debug values
debug_hostname: "localhost"
debug_port: 1002

# Player Parameters
starting_points: 0
max_life: 100
starting_ammo: 20

71 changes: 61 additions & 10 deletions src/client/client.cpp
Original file line number Diff line number Diff line change
@@ -1,40 +1,91 @@

#include "client.h"
#include "client_receiver.h"
#include "client_sender.h"
#include "./client.h"
#include "../data/convention.h"
#include "./client_receiver.h"
#include "./client_sender.h"

Client::Client(Socket &&socket, int id)
: client_id(id), protocol(std::move(socket)), keep_talking(true),
sender(keep_talking, sender_queue, protocol),
sender(keep_talking, sender_queue, protocol, id),
receiver(keep_talking, protocol, receiver_queue) {
receiver.start();
sender.start();
}

std::optional<Snapshot> Client::get_current_snapshot() {
std::unique_ptr<Snapshot> Client::get_current_snapshot() {
Snapshot snapshot;
if (receiver_queue.try_pop(snapshot)) {
return std::optional<Snapshot>(snapshot);
return std::make_unique<Snapshot>(snapshot);
} else {
return std::optional<Snapshot>();
return nullptr;
}
}

void Client::move_right() {
std::vector<uint8_t> command;
command.push_back(MOVE_RIGHT);
uint8_t command_to_push = PlayerCommands::MOVE_RIGHT;
command.push_back(command_to_push);
std::cout << "Moving right"
<< "\n";
sender_queue.try_push(command);
}

void Client::move_left() {
std::vector<uint8_t> command;
command.push_back(MOVE_LEFT);
uint8_t command_to_push = PlayerCommands::MOVE_LEFT;
command.push_back(command_to_push);
std::cout << "Moving Left"
<< "\n";
sender_queue.try_push(command);
}

void Client::stop_moving() {
std::vector<uint8_t> command;
uint8_t command_to_push = PlayerCommands::STOP_MOVING;
command.push_back(command_to_push);
sender_queue.try_push(command);
}

void Client::jump() {
std::vector<uint8_t> command;
command.push_back(JUMP);
uint8_t command_to_push = PlayerCommands::JUMP;
command.push_back(command_to_push);
sender_queue.try_push(command);
}

void Client::shoot() {
std::vector<uint8_t> command;
uint8_t command_to_push = PlayerCommands::SHOOT;
command.push_back(command_to_push);
sender_queue.try_push(command);
}

void Client::run() {
std::vector<uint8_t> command;
uint8_t command_to_push = PlayerCommands::RUN;
command.push_back(command_to_push);
sender_queue.try_push(command);
}

void Client::stop_running() {
std::vector<uint8_t> command;
uint8_t command_to_push = PlayerCommands::STOP_RUNNING;
command.push_back(command_to_push);
sender_queue.try_push(command);
}

void Client::special_attack() {
std::vector<uint8_t> command;
uint8_t command_to_push = PlayerCommands::SPECIAL_ATTACK;
command.push_back(command_to_push);
sender_queue.try_push(command);
}

void Client::change_weapon(uint8_t weapon_number) {
std::vector<uint8_t> command;
uint8_t command_to_push = PlayerCommands::CHANGE_WEAPON;
command.push_back(command_to_push);
command.push_back(weapon_number);
sender_queue.try_push(command);
}

Expand Down
21 changes: 17 additions & 4 deletions src/client/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
#ifndef JAZZJACKRABBIT_CLIENT_H
#define JAZZJACKRABBIT_CLIENT_H

#include "../common/player_commands.h"
#include "../common/queue.h"
#include "../common/snapshot_DTO.h"
#include "../data/convention.h"
#include "../data/snapshot_dto.h"
#include "client_protocol.h"
#include "client_receiver.h"
#include "client_sender.h"
#include <atomic>
#include <memory>
#include <optional>

class Client {
Expand All @@ -25,13 +26,25 @@ class Client {
public:
Client(Socket &&socket, int id);

std::optional<Snapshot> get_current_snapshot();
std::unique_ptr<Snapshot> get_current_snapshot();

void move_right();

void move_left();

void jump();
void stop_moving();

void jump(); // up

void shoot(); // space

void run(); // shift izquierdo

void stop_running();

void special_attack(); // ctrl izq

void change_weapon(uint8_t weapon_number);

/*
* Kills the client´s back-end, joining receiver and sender thread, and closes
Expand Down
35 changes: 21 additions & 14 deletions src/client/client_main.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#include "../data/convention.h"
#include "../data/snapshot_dto.h"
#include "./game_configs.h"
#include "./graphics/graphic_engine.h"
#include "./player.h"
#include "./ui/startup_screen.h"
#include "lobby.h"
#include "renderer.h"
Expand All @@ -18,15 +21,10 @@ static GlobalConfigs &globalConfigs = GlobalConfigs::getInstance();
const static int EXIT_SUCCESS_CODE = 0;
const static int EXIT_ERROR_CODE = -1;

const static char CHARACTER_NOT_SELECTED = '0';
const static char JAZZ_SELECTED = 'J';
const static char SPAZ_SELECTED = 'S';
const static char LORI_SELECTED = 'L';

const static bool TEST_ONLY_SDL_MODE = true;
const static bool TEST_ONLY_SDL_MODE = false;

void debugPrint(std::string &hostname, uint32_t &port, std::string &username,
char &userCharacter, GameConfigs &gameConfig) {
uint8_t &userCharacter, GameConfigs &gameConfig) {
std::cout << "username: " << username << "\n";
std::cout << "character selected: " << userCharacter << "\n";
std::cout << "Owner name: " << gameConfig.getOwnerName() << "|"
Expand All @@ -47,12 +45,16 @@ int main(int argc, char *argv[]) {
}

std::string hostname("");
// cppcheck-suppress unreadVariable
uint32_t port(0);
std::string username("");
char userCharacter = CHARACTER_NOT_SELECTED;
uint8_t userCharacter = PlayableCharactersIds::NoneSelected;

Snapshot initialSnapshotDto;
Snapshot *initialSnapshotDtoPtr = &initialSnapshotDto;

GameConfigs gameConfig;
GameConfigs *gamePtr = &gameConfig;

std::unique_ptr<Lobby> lobby = nullptr;

int exitCode = 0;
Expand All @@ -61,19 +63,19 @@ int main(int argc, char *argv[]) {

if (TEST_ONLY_SDL_MODE == false) {
StartupScreen startupScreen(argc, argv, hostname, port, username, gamePtr,
userCharacter);
initialSnapshotDtoPtr, userCharacter);

exitCode = startupScreen.show();
lobby = startupScreen.getLobby();

if (exitCode != EXIT_SUCCESS_CODE) {
return EXIT_ERROR_CODE;
}
} else {
} else /* DEBUG MODE ON */ {
hostname = globalConfigs.getDebugHostname();
port = globalConfigs.getDebugPort();
username = "testUsername";
userCharacter = JAZZ_SELECTED;
userCharacter = PlayableCharactersIds::Jazz;
lobby = std::make_unique<Lobby>(hostname.c_str(),
std::to_string(port).c_str());
}
Expand All @@ -83,9 +85,14 @@ int main(int argc, char *argv[]) {

debugPrint(hostname, port, username, userCharacter, gameConfig);

int client_id = 1;
SnapshotWrapper initialSnapshot(initialSnapshotDto);

uint8_t playerId = lobby->get_player_id();
Player player(username, userCharacter, graphicEngine, initialSnapshot,
playerId);
Socket skt = lobby->transfer_socket();
Renderer renderer(graphicEngine, client_id, std::move(skt));
Renderer renderer(graphicEngine, playerId, std::move(skt), player,
initialSnapshot);
renderer.run();

return exitCode;
Expand Down
26 changes: 20 additions & 6 deletions src/client/client_protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,29 @@

ClientProtocol::ClientProtocol(Socket &&socket) : skt(std::move(socket)) {}

void ClientProtocol::send_status(bool &was_closed,
const std::vector<uint8_t> &command) {
skt.sendall_bytewise(command.data(), command.size(), &was_closed);
void ClientProtocol::send_commands(bool &was_closed,
const CommandCodeDto command_code_dto) {
try {
skt.sendall_bytewise(&command_code_dto, sizeof(CommandCodeDto),
&was_closed);
} catch (const LibError &skt_err) {
std::cout
<< "Some error ocurred while trying to send a message to the server."
<< std::endl;
}
}

Snapshot ClientProtocol::receive_snapshot(bool &was_closed) {
Snapshot status;
skt.recvall_bytewise(&status, sizeof(Snapshot), &was_closed);
return status;
try {
Snapshot status;
skt.recvall_bytewise(&status, sizeof(Snapshot), &was_closed);
return status;
} catch (const LibError &skt_err) {
throw LibError(errno, "Some error ocurred while trying to receive a "
"message from the server.");
// throw std::runtime_error("Some error ocurred while trying to receive a
// message from the server.");
}
}

void ClientProtocol::close_and_shutdown() {
Expand Down
7 changes: 4 additions & 3 deletions src/client/client_protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@
#ifndef JAZZJACKRABBIT_CLIENT_PROTOCOL_H
#define JAZZJACKRABBIT_CLIENT_PROTOCOL_H

#include "../common/liberror.h"
#include "../common/player_status_DTO.h"
#include "../common/queue.h"
#include "../common/snapshot_DTO.h"
#include "../common/socket.h"
#include "../data/command_code_dto.h"
#include "../data/snapshot_dto.h"
#include <atomic>

class ClientProtocol {
private:
Socket skt;
std::atomic<bool> was_closed{false};

public:
explicit ClientProtocol(Socket &&socket);

void send_status(bool &was_closed, const std::vector<uint8_t> &command);
void send_commands(bool &was_closed, const CommandCodeDto command_code_dto);

Snapshot receive_snapshot(bool &was_closed);

Expand Down
2 changes: 1 addition & 1 deletion src/client/client_receiver.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
#define JAZZJACKRABBIT_CLIENT_RECEIVER_H

#include "../common/queue.h"
#include "../common/snapshot_DTO.h"
#include "../common/thread.h"
#include "../data/snapshot_dto.h"
#include "client_protocol.h"
#include <atomic>
#include <iostream>
Expand Down
14 changes: 11 additions & 3 deletions src/client/client_sender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,23 @@

ClientSender::ClientSender(std::atomic<bool> &keep_talking,
Queue<std::vector<uint8_t>> &q,
ClientProtocol &protocol)
: keep_talking((keep_talking)), sender_queue(q), protocol(protocol) {}
ClientProtocol &protocol, uint8_t id)
: keep_talking((keep_talking)), sender_queue(q), protocol(protocol),
id(id) {}

void ClientSender::run() {
try {
bool was_closed = false;
while (!was_closed && keep_talking) {
std::vector<uint8_t> command = sender_queue.pop();
protocol.send_status(was_closed, command);
CommandCodeDto command_code_dto;
command_code_dto.player_id = id;
command_code_dto.code = command[0];
if (command.size() > 1)
command_code_dto.data = command[1];
else
command_code_dto.data = 0;
protocol.send_commands(was_closed, command_code_dto);
}
} catch (const std::runtime_error &e) {
std::cout << "Sender queue was closed." << std::endl;
Expand Down
5 changes: 3 additions & 2 deletions src/client/client_sender.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ class ClientSender : public Thread {

private:
std::atomic<bool> &keep_talking;
// O quizas una queue de punteros al DTO?
Queue<std::vector<uint8_t>> &sender_queue;
ClientProtocol &protocol;
// cppcheck-suppress unusedStructMember
uint8_t id;

public:
ClientSender(std::atomic<bool> &keep_talking, Queue<std::vector<uint8_t>> &q,
ClientProtocol &protocol);
ClientProtocol &protocol, uint8_t id);
void run() override;

void kill();
Expand Down
Loading

0 comments on commit 3c4bbb2

Please sign in to comment.