Fimbulwinter Project  Pre-Alpha
An Ragnarok Online Emulator
AuthServer/AccountDB.h
00001 /*==================================================================*
00002 *     ___ _           _           _          _       _                          *
00003 *    / __(_)_ __ ___ | |__  _   _| |_      _(_)_ __ | |_ ___ _ __       *
00004 *   / _\ | | '_ ` _ \| '_ \| | | | \ \ /\ / / | '_ \| __/ _ \ '__|      *
00005 *  / /   | | | | | | | |_) | |_| | |\ V  V /| | | | | ||  __/ |         *
00006 *  \/    |_|_| |_| |_|_.__/ \__,_|_| \_/\_/ |_|_| |_|\__\___|_|         *
00007 *                                                                                                                                       *
00008 * ------------------------------------------------------------------*
00009 *                                                        Emulator                                       *
00010 * ------------------------------------------------------------------*
00011 *                     Licenced under GNU GPL v3                     *
00012 * ----------------------------------------------------------------- *
00013 *                        Account Databases                              *
00014 * ==================================================================*/
00015 
00016 #pragma once
00017 
00018 #include <soci/soci.h>
00019 #include <database_helper.h>
00020 
00021 using namespace soci;
00022 
00023 
00046 class Account
00047 {
00048 public:
00049         int account_id;
00050         string userid;
00051         string user_pass;      
00052         char sex;              
00053         string email;         
00054         int level;              
00055         unsigned int state;     
00056         time_t unban_time;      
00057         time_t expiration_time; 
00058         unsigned int logincount;
00059         string lastlogin;     
00060         string last_ip;       
00061         string birthdate;  
00062 };
00063 
00072 class AccountDB
00073 {
00074 public:
00075         AccountDB(soci::session *db) // AccountDB Constructor
00076         {
00077                 db_ = db;
00078         }
00079 
00085         bool load_account(int accid, Account &acc)
00086         {
00087                 soci::statement s =     (db_->prepare << "SELECT `account_id`, `userid`, `user_pass`, `sex`, `email`, `level`, \
00088                         `state`, `unban_time`, `expiration_time`, `logincount`, `lastlogin`, \
00089                         `last_ip`, `birthdate` FROM `login` WHERE `account_id`=:id LIMIT 1", use(accid),
00090                         into(acc.account_id),
00091                         into(acc.userid),
00092                         into(acc.user_pass),
00093                         into(acc.sex),
00094                         into(acc.email),
00095                         into(acc.level),
00096                         into(acc.state),
00097                         into(acc.unban_time),
00098                         into(acc.expiration_time),
00099                         into(acc.logincount),
00100                         into(acc.lastlogin),
00101                         into(acc.last_ip),
00102                         into(acc.birthdate));
00103 
00104                 s.execute(true);
00105 
00106                 return s.get_affected_rows() == 1;
00107         }
00108 
00114         bool load_account(string name, Account &acc)
00115         {
00116                 soci::statement s =     (db_->prepare << "SELECT `account_id`, `userid`, `user_pass`, `sex`, `email`, `level`, \
00117                                                         `state`, `unban_time`, `expiration_time`, `logincount`, `lastlogin`, \
00118                                                         `last_ip`, `birthdate` FROM `login` WHERE `userid`=:name LIMIT 1", use(name),
00119                                                         into(acc.account_id),
00120                                                         into(acc.userid),
00121                                                         into(acc.user_pass),
00122                                                         into(acc.sex),
00123                                                         into(acc.email),
00124                                                         into(acc.level),
00125                                                         into(acc.state),
00126                                                         into(acc.unban_time),
00127                                                         into(acc.expiration_time),
00128                                                         into(acc.logincount),
00129                                                         into(acc.lastlogin),
00130                                                         into(acc.last_ip),
00131                                                         into(acc.birthdate));
00132                 
00133                 s.execute(true);
00134 
00135                 return s.get_affected_rows() == 1;
00136         }
00137 
00142         void save_account(Account &acc, bool nw = false) // Save the Account into the database.
00143         {
00144                 if (nw)
00145                 {
00146                         *db_ << "INSERT INTO `login` (`account_id`, `userid`, `user_pass`, `sex`, `email`, `level`, \
00147                                         `state`, `unban_time`, `expiration_time`, `logincount`, `lastlogin`, \
00148                                         `last_ip`, `birthdate`) VALUES (\
00149                                         :id, :uid, :pwd, :sex, :email, :lvl, :state, :unban, :expr, :loginc, \
00150                                         :lip, :birth)",
00151                                         use(acc.account_id),
00152                                         use(acc.userid),
00153                                         use(acc.user_pass),
00154                                         use(acc.sex),
00155                                         use(acc.email),
00156                                         use(acc.level),
00157                                         use(acc.state),
00158                                         use(acc.unban_time),
00159                                         use(acc.expiration_time),
00160                                         use(acc.logincount),
00161                                         use(acc.lastlogin),
00162                                         use(acc.last_ip),
00163                                         use(acc.birthdate);
00164                 }
00165                 else
00166                 {
00167                         *db_ << "UPDATE `login` SET `userid`=:uid, `user_pass`=:pw, `sex`=:s, `email`=:email, `level`=:lvl, \
00168                                         `state`=:state, `unban_time`=:unban, `expiration_time`=:expr, `logincount`=:loginc, `lastlogin`=:llogin, \
00169                                         `last_ip`=:lip, `birthdate`=:birth WHERE `account_id`=:id",
00170                                         use(acc.userid),
00171                                         use(acc.user_pass),
00172                                         use(acc.sex),
00173                                         use(acc.email),
00174                                         use(acc.level),
00175                                         use(acc.state),
00176                                         use(acc.unban_time),
00177                                         use(acc.expiration_time),
00178                                         use(acc.logincount),
00179                                         use(acc.lastlogin),
00180                                         use(acc.last_ip),
00181                                         use(acc.birthdate),
00182                                         use(acc.account_id);
00183                 }
00184         }
00185 
00186 private:
00187         soci::session *db_;
00188 };
 All Classes Functions