📄 game.cpp
字号:
((*pWonPile)[--i]->signal_event ().connect (mem_fun (*this, (&Game::wonCardsSelected)))); return false;}//-----------------------------------------------------------------------------/// Disables the won cards//-----------------------------------------------------------------------------void Game::disableWonCards () { TRACE8 ("Game::disableWonCards () - Disabling " << wonCards.size () << " cards"); for (int i (wonCards.size ()); i > 0;) wonCards[--i].disconnect (); wonCards.clear ();}//-----------------------------------------------------------------------------/// Changes the names of the playing people/// \param newNames: Array holding the new names of the players//-----------------------------------------------------------------------------void Game::changeNames (const std::vector<Player*>& newPlayer) { const_cast<std::vector<Player*>&> (actPlayers) = newPlayer;}//----------------------------------------------------------------------------/// Callback to inform a controller about status changes/// \param status: New status of the game//----------------------------------------------------------------------------void Game::control (unsigned int status) const {}//----------------------------------------------------------------------------/// Writes a message to all partners/// \param msg: Message to write//----------------------------------------------------------------------------void Game::broadcastMessage (const std::string& msg) const { TRACE3 ("Game::broadcastMessage (const std::string&) - " << msg); const YGP::ConnectionMgr& cmgr (getConnectionMgr ()); if (getConnectionMgr ().getMode () == YGP::ConnectionMgr::SERVER) for (std::vector<YGP::Socket*>::const_iterator i (cmgr.getClients ().begin ()); i != cmgr.getClients ().end (); ++i) writeMessage (**i, msg); else writeMessage (*cmgr.getSocket (), msg);}//----------------------------------------------------------------------------/// Writes a set-startplayer message to all clients/// \param startplayer: Startplayer//----------------------------------------------------------------------------void Game::broadcastStartPlayer (unsigned int startplayer) { // Send startplayer to the clients const YGP::ConnectionMgr& cmgr (getConnectionMgr ()); if (cmgr.getMode () == YGP::ConnectionMgr::SERVER) { TRACE3 ("Game::broadcastStartPlayer (unsigned int) - " << startplayer); const std::vector<YGP::Socket*>& clients (cmgr.getClients ()); unsigned int player ((startplayer - 1) & 0x3); for (std::vector<YGP::Socket*>::const_iterator i (clients.begin ()); i != clients.end (); ++i) { std::ostringstream msg; msg << "ActPlayer=" << player; writeMessage (**i, msg.str ()); player = (player - 1) & 0x3; } }}//----------------------------------------------------------------------------/// Writes a message to the partner/// \param socket: Socket to write message to/// \param msg: Message to write//----------------------------------------------------------------------------void Game::writeMessage (YGP::Socket& socket, const std::string& msg) { try { socket.write (msg); socket.write ("\0", 1); } catch (YGP::CommError& error) { std::string err (_("Can't write message!\n\nReason: %1")); err.replace (err.find ("%1"), 2, error.what ()); Gtk::MessageDialog dlg (msg, false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK); dlg.set_title (PACKAGE); dlg.run (); }}//----------------------------------------------------------------------------/// Writes a status message to the partner/// \param socket: Socket to write message to/// \param rc: Error code to send/// \param msg: Message to write//----------------------------------------------------------------------------void Game::writeError (YGP::Socket& socket, unsigned int rc, const std::string& msg) { std::ostringstream error; error << "Error=" << rc << ";Msg=\"" + msg << "\"\0"; writeMessage (socket, error.str ());}//----------------------------------------------------------------------------/// Handles a message send from the server/// \param player: ID of player sending the message/// \param msg: Message to handle/// \returns bool: True, if the message has been processed completely; else/// (if message is still pending) false/// \throw YGP::ParseError, YGP::CommError: In case of an error an describing string//----------------------------------------------------------------------------bool Game::handleMessage (unsigned int player, const std::string& msg) throw (YGP::ParseError, YGP::CommError) { TRACE1 ("Game::handleMessage (unsigned int player, const std::string&) - " << msg << " (" << player << ')'); Check1 (msg.size ()); Check2 (!data); bool rc (true); Check2 (!ignoreNextMsg); switch (statGame) { case NONE: statGame = INITIALIZING; data = msg.c_str (); start (); data = NULL; break; case INITIALIZING: break; default: // Playing (and game specific stati) rc = performCommand (player, msg); } return rc;}//----------------------------------------------------------------------------/// Sets the player performing the next turn/// \param player: Number identifying player (starting with 0)//----------------------------------------------------------------------------void Game::setNextPlayer (unsigned int player) { TRACE8 ("Game::setNextPlayer (unsigned int) - " << player); actPlayer = player;}//----------------------------------------------------------------------------/// Handles a command the server sent in playing mode/// \param player: ID of player sending the message/// \param msg: Command to perform/// \returns bool: Flag, if command has been performed completely/// \throws YGP::ParseError, YGP::CommError: Describing the error//----------------------------------------------------------------------------bool Game::performCommand (unsigned int player, const std::string& msg) throw (YGP::ParseError, YGP::CommError) { TRACE8 ("Game::performCommand (unsigned int player, const std::string&) - " << msg << " (" << player << ')'); Check1 (msg.size ()); YGP::Tokenize command (msg); std::string cmd (command.getNextNode ('=')); TRACE2 ("Game::performCommand (unsigned int player, const std::string&) - " << cmd); if (cmd == "Play") { cmd = command.getNextNode (';'); std::string playTo (command.getNextNode ('=')); std::string strTarget (command.getNextNode (';')); unsigned long target (-1UL); if (stringToNumber (target, strTarget.c_str ()) || (playTo != "Target")) throw YGP::ParseError (N_("Invalid target!")); Check3 (actPlayer >= 0); ICardPile* pile (getPileOfPlayer (actPlayer, target)); if (!pile) throw YGP::ParseError (N_("Invalid target!")); flipCards2Play (*pile, cmd); // Inform clients about cards to play if (getConnectionMgr ().getMode () == YGP::ConnectionMgr::SERVER) broadcastMessage (msg); if (executeRemoteMove (*pile, target)) { Glib::signal_timeout ().connect (bind (mem_fun (*this, &Game::endRemoteMove), actPlayer), ComputerPlayer::TIMEOUT); stati.pendingTurn = 1; return false; } else makeNextMoves (); } else if (cmd == "ActPlayer") { cmd = command.getNextNode (';'); TRACE8 ("Game::performCommand (unsigned int player, const std::string&) - " "Next player: " << cmd); unsigned long player; if (stringToNumber (player, cmd.c_str ())) throw YGP::ParseError (N_("Invalid number")); actPlayer = player; if (statGame == PLAYING) displayTurn (player); } else if (cmd == "End") { end (false); if (getConnectionMgr ().getMode () == YGP::ConnectionMgr::SERVER) broadcastMessage ("End"); } else throw YGP::ParseError (N_("Unknown command!")); return true;}//----------------------------------------------------------------------------/// Converts a string into a number/// \param number: Target of conversion/// \param text: String to convert/// \returns bool: False, if conversion succeeded (\c text contained a number)//----------------------------------------------------------------------------bool Game::stringToNumber (unsigned long& number, const char* text) { Check1 (text); char* pTail = NULL; errno = 0; number = strtoul (text, &pTail, 0); return (errno || (pTail && *pTail));}//----------------------------------------------------------------------------/// Executes the remote move locally/// \param pile: Pile to move to/from/// \param card: ID of target, where to play the card/// \returns bool: True, if the timer to execute the move should be set/// \throw YGP::ParseError: In case of an invalid value//----------------------------------------------------------------------------bool Game::executeRemoteMove (ICardPile& pile, unsigned int card) throw (YGP::ParseError) { TRACE8 ("Game::executeRemoteMove (ICardPile&, unsigned int) - " << pos2Play); return true;}//----------------------------------------------------------------------------/// Returns if the game can be stopped imediately. This is true, if there is no/// timer activated./// \returns bool: True, if the game can be stopped imediately//----------------------------------------------------------------------------bool Game::canBeStopped () const { return !(actPlayer && stati.pendingTurn);}//----------------------------------------------------------------------------/// Checks if the game should ignore a message. If so, the count of messages/// to ignore is reduced by 1./// \returns bool: True, if a message should be ignored//----------------------------------------------------------------------------bool Game::ignoreMessage () { if (ignoreNextMsg) { TRACE9 ("Game::ignoreMessage () - Ignoring " << ignoreNextMsg); --ignoreNextMsg; return true; } return false;}//-----------------------------------------------------------------------------/// Adds game-specific menus/// \param mgrUI: UIManager to add to//-----------------------------------------------------------------------------void Game::addMenus (Glib::RefPtr<Gtk::UIManager> mgrUI) {}//-----------------------------------------------------------------------------/// Removes the game-specific menus/// \param mgrUI: UIManager to add to//-----------------------------------------------------------------------------void Game::removeMenus (Glib::RefPtr<Gtk::UIManager> mgrUI) {}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -