Skip to content

Commit

Permalink
[ui] Support for skipping turn when playing #18 #19
Browse files Browse the repository at this point in the history
  • Loading branch information
gbaudic committed Apr 19, 2020
1 parent 8f68d97 commit c12b87e
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/protocol.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ const Uint8 REWARD_MSG = 116;
const Uint8 PENALTY_MSG = 117;
const Uint8 SERVER_MSG = 118;
const Uint8 SHOT_MSG = 120; //! Shot coordinates: angle, strength, weapon and item used
const Uint8 SKIP_TURN_MSG = 121;
const Uint8 SWITCH_MOBILE_MSG = 122; //! only used in DUO mode
const Uint8 DMG_MSG = 125; //! Result of a shot: impact on players, on the map...
const Uint8 ITEMS_MSG = 135; //! Item choice when playing (for server consistency checks)
const Uint8 COORDS_MSG = 130; //! Coordinates (for players landing, at the beginning or when respawning, and objects)
Expand Down
26 changes: 26 additions & 0 deletions src/views/lobbyview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,40 @@ void LobbyView::addMessage(const std::string &user, const std::string &message,
}
}

/**
* \brief Convenience function to get currently selected game mode
* \return the GameMode
* \see GameMode
*/
GameMode LobbyView::getMode() const {
return roomBasicInfo.mode;
}

/**
* \brief Convenience function to get currently selected map
* \return the name of the map
*/
std::string LobbyView::getMap() const {
return roomBasicInfo.mapName;
}

/**
* \brief Convenience function to get the name of the player currently connected with this client
* \return the name
*/
std::string LobbyView::getCurrentPlayerName() const {
return currentPlayerName;
}

/**
* \brief Convenience function to get currently selected sudden death type
* \return the type of sudden death
* \see SuddenDeathType
*/
SuddenDeathType LobbyView::getSuddenDeathType() const {
return sdType;
}

void LobbyView::addWidgets() {
addWidget(&lbl_number, 5, 5);
addWidget(&lbl_name, 20, 5);
Expand Down
6 changes: 6 additions & 0 deletions src/views/lobbyview.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ class LobbyView : public Context, public gcn::ActionListener {

GameMode getMode() const;
std::string getMap() const;
std::string getCurrentPlayerName() const;
SuddenDeathType getSuddenDeathType() const;

virtual void drawBackground(SDL_Renderer* screen) override;
virtual void processMessage(const Uint8 code, const std::string& message) override;
Expand All @@ -66,6 +68,10 @@ class LobbyView : public Context, public gcn::ActionListener {

bool isCurrentPlayerAdmin = false;
RoomBasicInfo roomBasicInfo;
SuddenDeathType sdType = SuddenDeathType::BIGBOMB;

// List of players
std::string currentPlayerName; // TODO: replace by proper structure

void addWidgets();
};
Expand Down
25 changes: 23 additions & 2 deletions src/views/roomview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

#include <algorithm>
#include <sstream>
#include <regex>
#include "../utils.hpp"
#include "../sound.hpp"
#include "../protocol.hpp"
Expand Down Expand Up @@ -145,7 +146,7 @@ void RoomView::processEvent(SDL_Event &event) {
currentMode = InteractionMode::IDLE;

stringstream msg;
// user name should go here
msg << origin->getCurrentPlayerName();
msg << ";" << static_cast<int>(getSelectedType());
msg << ";" << pb_power.getValue();
msg << ";" << lbl_currentAngle.getCaption();
Expand All @@ -154,6 +155,17 @@ void RoomView::processEvent(SDL_Event &event) {
lbl_lastAngle.setCaption(lbl_currentAngle.getCaption());
}
// Support here arrow up/down (angle), left/right (motion), F7 (mobile), F8 (pass turn), F1-6 (item use)...
if (event.key.keysym.sym == SDLK_F8 && currentMode == InteractionMode::TURN) {
// Player skipped turn
currentMode = InteractionMode::IDLE;
// Stop the countdown to 0 here
network.send(SKIP_TURN_MSG, origin->getCurrentPlayerName());
}
if (event.key.keysym.sym == SDLK_F7 && currentMode == InteractionMode::TURN && gameMode == GameMode::DUO) {
// Switch mobile
// Update view too
network.send(SWITCH_MOBILE_MSG, origin->getCurrentPlayerName());
}
} else if (event.type == SDL_MOUSEMOTION) {
SDL_MouseMotionEvent mouseEvent = event.motion;

Expand Down Expand Up @@ -183,7 +195,16 @@ void RoomView::processEvent(SDL_Event &event) {
*/
void RoomView::addMessage(const std::string& sender, const std::string& message, const Uint8 type) {
msgLog.addMessage(sender, message, type);
if (!sender.empty()) {
if (type == REWARD_MSG || type == PENALTY_MSG) {
if (sender == origin->getCurrentPlayerName()) {
// Also add to the current gold balance widget
regex pattern{ "(-?\\d+)G$" };
smatch matches;
if (regex_search(message, matches, pattern)) {
goldDisplay.updateValue(stoi(matches[1], nullptr, 10));
}
}
} else if (type == TEAM_CHAT_MSG || type == ROOM_CHAT_MSG) {
// TODO show in chatballoon

// and add to the chat channel of the room lobby
Expand Down

0 comments on commit c12b87e

Please sign in to comment.