Advertisement
Rochet2

Connection to a new database test

Sep 25th, 2012
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.38 KB | None | 0 0
  1. #include "ScriptMgr.h"
  2.  
  3. const char* DBInfo[] =
  4. {"127.0.0.1", "account", "passwd", "Webwow"};
  5. #define PORT    3306
  6. #define REWARD  25 // reward item
  7.  
  8. class example_votepoint_gossip : public CreatureScript
  9. {
  10. public:
  11.     example_votepoint_gossip() : CreatureScript("example_votepoint_gossip") { }
  12.  
  13.     bool OnGossipHello(Player* player, Creature* creature)
  14.     {
  15.         player->ADD_GOSSIP_ITEM(GOSSIP_ICON_CHAT, "Claim vote points", GOSSIP_SENDER_MAIN, GOSSIP_ACTION_INFO_DEF+1);
  16.         player->ADD_GOSSIP_ITEM(GOSSIP_ICON_TALK, "Nevermind", GOSSIP_SENDER_MAIN, GOSSIP_ACTION_INFO_DEF+2);
  17.  
  18.         player->PlayerTalkClass->SendGossipMenu(DEFAULT_GOSSIP_MESSAGE, creature->GetGUID());
  19.  
  20.         return true;
  21.     }
  22.  
  23.     bool OnGossipSelect(Player* player, Creature* creature, uint32 sender, uint32 action)
  24.     {
  25.         player->PlayerTalkClass->ClearMenus();
  26.  
  27.         switch(action)
  28.         {
  29.         case GOSSIP_ACTION_INFO_DEF+1:
  30.             {
  31.                 uint32 votePoints = GetVotePoints(player->GetSession()->GetAccountId());
  32.                 if(votePoints < 1) // vote point amount smaller than 1, send error
  33.                     player->GetSession()->SendNotification("Not enough vote points");
  34.                 else if(player->AddItem(REWARD, votePoints))
  35.                 {
  36.                     ResetVotePoints(player->GetSession()->GetAccountId());
  37.                     player->GetSession()->SendAreaTriggerMessage("Vote claimed succesfully");
  38.                 }
  39.                 else
  40.                     player->GetSession()->SendNotification("Not enough space for reward");
  41.                 OnGossipHello(player, creature);
  42.             } break;
  43.  
  44.         case GOSSIP_ACTION_INFO_DEF+2: player->CLOSE_GOSSIP_MENU(); break;
  45.         }
  46.  
  47.         return true;
  48.     }
  49.  
  50. private:
  51.  
  52.     void ResetVotePoints(uint32 accountID)
  53.     {
  54.         MYSQL *conn;
  55.  
  56.         char sql[256];
  57.         snprintf(sql, 256, "UPDATE Account_more SET vp = 0 WHERE account = %u", accountID);
  58.  
  59.         conn = mysql_init(NULL);
  60.         mysql_real_connect(conn, DBInfo[0], DBInfo[1], DBInfo[2], DBInfo[3], PORT, NULL, 0);
  61.         mysql_query(conn, sql);
  62.         mysql_close(conn);
  63.     }
  64.  
  65.     uint32 GetVotePoints(uint32 accountID)
  66.     {
  67.         MYSQL *conn;
  68.         MYSQL_RES *result1;
  69.         MYSQL_FIELD *field;
  70.         uint64 rowCount;
  71.         uint32 fieldCount;
  72.  
  73.         char sql[256];
  74.         snprintf(sql, 256, "SELECT vp FROM Account_more WHERE account = %u", accountID);
  75.  
  76.         conn = mysql_init(NULL);
  77.         mysql_real_connect(conn, DBInfo[0], DBInfo[1], DBInfo[2], DBInfo[3], PORT, NULL, 0);
  78.         if(!conn)
  79.             return 0;
  80.         mysql_query(conn, sql);
  81.  
  82.         result1 = mysql_store_result(conn);
  83.         rowCount = mysql_affected_rows(conn);
  84.         fieldCount = mysql_field_count(conn);
  85.         mysql_close(conn);
  86.  
  87.         if (!result1)
  88.             return 0;
  89.         if (!rowCount)
  90.         {
  91.             mysql_free_result(result1);
  92.             return 0;
  93.         }
  94.  
  95.         field = mysql_fetch_fields(result1);
  96.         ResultSet* result2 =  new ResultSet(result1, field, rowCount, fieldCount);
  97.         if(!result2)
  98.             return 0;
  99.         if(!result2->NextRow())
  100.             return 0;
  101.         return result2->Fetch()[0].GetUInt32(); // return mysql_fetch_row(result)[0];
  102.     }
  103. };
  104.  
  105. void AddSC_example_votepoint_gossip()
  106. {
  107.     new example_votepoint_gossip();
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement