本文整理汇总了C++中shared_ptr::AddPlayer方法的典型用法代码示例。如果您正苦于以下问题:C++ shared_ptr::AddPlayer方法的具体用法?C++ shared_ptr::AddPlayer怎么用?C++ shared_ptr::AddPlayer使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类shared_ptr
的用法示例。
在下文中一共展示了shared_ptr::AddPlayer方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: handle_client
void handle_client(shared_ptr<Socket> client) // this function runs in a separate thread
{
try {
client->write("Welcome to Server 1.0! To quit, type 'quit'.\r\n");
if (m_g->isGameStarted()) {
client->write("Game already in progress, please try again later.\r\n");
}
else {
client->write("What's your name?\r\n");
client->write(machiavelli::prompt);
string name{ client->readline() };
if (m_g->getCurrentPlayers().size() >= 2) {
client->write("Lobby full, please try again later.\r\n");
}
else {
shared_ptr<Player> player{ new Player {name, m_g, client} };
m_g->AddPlayer(player);
*client << machiavelli::prompt << "Welcome, " << name << ", have fun playing our game!\r\n";
m_g->handleCommand(player, "join");
while (true) { // game loop
try {
// read first line of request
string cmd{ client->readline() };
cerr << '[' << client->get_dotted_ip() << " (" << client->get_socket() << ") " << player->get_name() << "] " << cmd << '\n';
std::transform(cmd.begin(), cmd.end(), cmd.begin(), ::tolower);
if (cmd == "quit") {
m_g->RemovePlayer(player);
m_g->handleCommand(player, "quit");
client->write("Bye!\r\n");
break; // out of game loop, will end this thread and close connection
}
ClientCommand command{ cmd, client, player };
queue.put(command);
}
catch (const exception& ex) {
cerr << "*** exception in client handler thread for player " << player->get_name() << ": " << ex.what() << '\n';
if (client->is_open()) {
*client << "ERROR: " << ex.what() << "\r\n";
}
}
catch (...) {
cerr << "*** exception in client handler thread for player " << player->get_name() << '\n';
if (client->is_open()) {
client->write("ERROR: something went wrong during handling of your request. Sorry!\r\n");
}
}
}
if (client->is_open()) client->close();
}
}
}
catch (...) {
cerr << "handle_client crashed\n";
}
}