www.pudn.com > 天堂1服务端模拟程序.rar > LoginMap.h


/* 
Copyright (C) 2004  freeplay.dk 
 
This program is free software; you can redistribute it and/or 
modify it under the terms of the GNU General Public License 
as published by the Free Software Foundation; either version 2 
of the License, or (at your option) any later version. 
 
This program is distributed in the hope that it will be useful, 
but WITHOUT ANY WARRANTY; without even the implied warranty of 
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
GNU General Public License for more details. 
 
You should have received a copy of the GNU General Public License 
along with this program; if not, write to the Free Software 
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. 
 
You may contact me for further information on admin@freeplay.dk 
*/ 
#define WIN32_LEAN_AND_MEAN 
 
// disable map warnings for std::map 
#pragma warning (disable: 4786) 
#pragma warning (disable: 4788) 
 
#include  
#include  
#include  
#include  
#include "Types.h" 
 
using namespace std; 
 
struct usersonline 
{ 
	DWORD ptr; 
	unsigned int socket; 
	int accountid; 
	string username; 
}; 
 
class LoginMap 
{ 
private: 
	map m; 
	struct usersonline uo; 
 
public: 
	//************************ 
	// std::map remove a map 
	//************************ 
	void removeMap(unsigned int keyid) 
	{ 
		m.erase(keyid); 
	} 
	//************************ 
	// std::map setters / getters 
	//************************ 
	//** Set Pointer ** 
    void setPtr(unsigned int key, DWORD ptr) 
    { 
		map::iterator it = m.find(key); 
		if(it != m.end()) 
			it->second.ptr = ptr; 
		else 
		{ 
			uo.ptr = ptr; 
			m.insert(make_pair(key, uo)); 
		} 
    } 
	//** Get Pointer ** 
	DWORD getPtr(unsigned int key) 
    { 
        unsigned int ptr = m[key].ptr; 
        return ptr; 
    } 
	//** Find pointer based on account id ** 
	DWORD findPtr(int accountid) 
	{ 
		map::iterator it; 
		for(it = m.begin() ; it != m.end() ; it++ ) 
		{ 
			unsigned int cp = m[it->first].socket; 
			int account = getAccountid(cp); 
			DWORD otherptr = getPtr(cp); 
 
			if(accountid == account) 
			{ 
				return otherptr; 
				break; 
			} 
		} 
		return 0; 
	} 
	//** Socket ** 
    void setSocket(unsigned int key, unsigned int socket) 
    { 
		map::iterator it = m.find(key); 
		if(it != m.end()) 
			it->second.socket = socket; 
		else 
		{ 
			uo.socket = socket; 
			m.insert(make_pair(key, uo)); 
		} 
    } 
	//** Socket ** 
	int getSocket(unsigned int key) 
    { 
        int socket = m[key].socket; 
        return socket; 
    } 
	//** Account ID ** 
    void setAccountid(unsigned int key, int accountid) 
    { 
		map::iterator it = m.find(key); 
		if(it != m.end()) 
			it->second.accountid = accountid; 
		else 
		{ 
			uo.accountid = accountid; 
			m.insert(make_pair(key, uo)); 
		} 
    } 
	//** Account ID ** 
	int getAccountid(unsigned int key) 
    { 
        int accountid = m[key].accountid; 
        return accountid; 
    } 
   //** User name ** 
	void setUsername(unsigned int key, string username) 
    { 
		map::iterator it = m.find(key); 
		if(it != m.end()) 
			it->second.username = username; 
		else 
		{ 
			uo.username = username; 
			m.insert(make_pair(key, uo)); 
		} 
    } 
	//** User name ** 
	string getUsername(unsigned int key) 
	{ 
		string username = m[key].username; 
		return username; 
	} 
};