Cocos2d Save most recent login to multiple accounts for up to one months

Source: Internet
Author: User

Using a single case to manage, the data is present in the local XML file in the format below

<?XML version= "1.0" encoding = "Utf-8"?><RootNode>    <UserInfo Time= "1449905923">        < Account>abc002</ Account>        <Password>a123456</Password>    </UserInfo>    <UserInfo Time= "1430905758">        < Account>abc001</ Account>        <Password>a123456</Password>    </UserInfo></RootNode>

First define some of the number of information to be used

#include"AccountManager.h"#include"external/tinyxml2/tinyxml2.h"#defineAccount_file_name "Useraccounts.xml"#defineAccount_save_count 5//number of accounts saved#defineSeconds_of_day 86400//number of seconds in a day#defineLimit_day 30LL//Effective DaysAccountmanager* Accountmanager::m_pself =nullptr;structLoginaccountinfo {stringStraccount;//Account Number     stringstrpassword;//Password     stringstrdate;//Last Login Date};


Gets the previously saved account information when constructing the object

Accountmanager::accountmanager () {stringstrpath = Fileutils::getinstance ()->getwritablepath () +Account_file_name; if(Fileutils::getinstance ()isfileexist (strpath)) {        //extract account information to memoryTinyxml2::xmldocument *pxmldocument =Newtinyxml2::xmldocument; Pxmldocument-LoadFile (Strpath.c_str ()); Auto Pxmlrootnode= pxmldocument->rootelement (); Auto Pxmlnodeuinfo= pxmlrootnode->firstchildelement ();  while(pxmlnodeuinfo) {Loginaccountinfo accinfo; Accinfo.strdate= Pxmlnodeuinfo->attribute (" Time"); Auto Pxmlnodeaccunt= pxmlnodeuinfo->firstchildelement (); Accinfo.straccount= pxmlnodeaccunt->GetText (); Auto Pxmlnodepassword= pxmlnodeaccunt->nextsiblingelement (); Accinfo.strpassword= pxmlnodepassword->GetText ();            M_vectoraccountsinfo.push_back (Accinfo); Pxmlnodeuinfo= pxmlnodeuinfo->nextsiblingelement (); }    }}

Save account information to file

//Save account to filevoidAccountmanager::saveaccountinfotofile (Const string&straccounts,Const string&strpassword) {    //Sort AccountsAuto Iteratorstr =M_vectoraccountsinfo.begin ();  for(; Iteratorstr! = M_vectoraccountsinfo.end (); + +iteratorstr) {        if((*iteratorstr). Straccount = =straccounts)            {m_vectoraccountsinfo.erase (ITERATORSTR);  Break; }    }    //determine if there are more than 5 saved accounts    if(M_vectoraccountsinfo.size () >=account_save_count) M_vectoraccountsinfo.pop_back ();    Loginaccountinfo Accinfo; Accinfo.straccount=straccounts; Accinfo.strpassword=strpassword; Accinfo.strdate=getcurrentdate ();    M_vectoraccountsinfo.insert (M_vectoraccountsinfo.begin (), accinfo); Tinyxml2::xmldocument*pxmldoc =Newtinyxml2::xmldocument (); //StatementTinyxml2::xmldeclaration *pxmldeclare = pxmldoc->newdeclaration ("XML version=\ "1.0\" encoding = \ "Utf-8\"");    ASSERT (Pxmldeclare); if(Pxmldeclare = = nullptr)return; Pxmldoc-Linkendchild (Pxmldeclare); //root nodeTinyxml2::xmlelement *pxmlrootnode = pxmldoc->newelement ("RootNode"); //Add Account sub-node     for(size_t i =0; I < m_vectoraccountsinfo.size (); i++) {Auto Pxmlnodeuinfo= Pxmldoc->newelement ("UserInfo"); Pxmlnodeuinfo->setattribute (" Time", m_vectoraccountsinfo.at (i). STRDATE.C_STR ()); Auto Pxmlnodeaccount= Pxmldoc->newelement (" Account"); Pxmlnodeaccount->linkendchild (pxmldoc->NewText (m_vectoraccountsinfo.at (i). STRACCOUNT.C_STR ())); Pxmlnodeuinfo-Linkendchild (Pxmlnodeaccount); Auto Pxmlnodepassword= Pxmldoc->newelement ("Password"); Pxmlnodepassword->linkendchild (pxmldoc->NewText (m_vectoraccountsinfo.at (i). STRPASSWORD.C_STR ())); Pxmlnodeuinfo-Linkendchild (Pxmlnodepassword); Pxmlrootnode-Linkendchild (Pxmlnodeuinfo); } Pxmldoc-Linkendchild (Pxmlrootnode); //Save to file    if(pxmldoc) {stringstrpath = Fileutils::getinstance ()->getwritablepath () +Account_file_name; Pxmldoc->savefile (Fileutils::getinstance ()Getsuitablefopen (strpath). C_STR ()); DeletePxmldoc; }}

// Gets the current time total seconds value string accountmanager::getcurrentdate () {    = time (nullptr);     string strdate;     = Stringutils::format ("%lld", Lltimestamp);     return strdate;}

//determine if the account is saved more than the limit daysBOOLAccountmanager::isacccountoutofdate (Const string&Straccount) {     for(Auto accinfo:m_vectoraccountsinfo) {if(Accinfo.straccount = =Straccount) {            Long LongLllogintime =Atoll (Accinfo.strDate.c_str ()); Long LongLlnowtime =Atoll (Getcurrentdate (). C_STR ()); Long LongLlday = (llnowtime-lllogintime)/Seconds_of_day; if(Llday >limit_day) {                return true; }            return false; }    }    return false;}

Here is the data capture that the UI section uses, and the UI section uses the TableView

// get all accounts vector<string> accountmanager::getallaccount () {    vector<string > vectoraccounts;      for (auto Accinfo:m_vectoraccountsinfo)    {        vectoraccounts.push_back (accinfo.straccount);    }     return vectoraccounts;}


The above is only the data management, the UI part of the code, the idea is that the login interface by default load fill account password to determine whether the account is more than a given number of days (here is 30 days, the top macro definition), get the data list when all the accounts to fill define the UI with TableView, refresh the account information when logging in, put the most recent landing in front (the code is written), and then save to local.

Cocos2d Save most recent login to multiple accounts for up to one months

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.