package com.mgr.group;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import com.mgr.group.data.User;
import com.taurus.core.util.json.JSONUtils;
import com.taurus.permanent.data.Session;

/**
 * 当前节点  玩家session管理类
 * 
 *
 */
public class SessionManager {

	private ConcurrentMap<Session, User> sessionMap = null;

	public SessionManager() {
		this.sessionMap = new ConcurrentHashMap<Session, User>();
	}

	/**
	 * 通过sessionid获取User
	 * @param sessionid
	 * @return
	 */
	public User getUser(Session sessionid) {
		return this.sessionMap.get(sessionid);
	}

	/**
	 * 增加session
	 * @param session
	 * @param player
	 */
	public void putUser(Session session, User user) {
		this.sessionMap.put(session, user);
	}

	/**
	 * 删除session
	 * @param session
	 */
	public User delSession(Session session) {
		Global.logger.info("delSession----------------清理session:"+session.toString());
		if(session == null)return null;
		session.setHashId(null);
		User user =this.sessionMap.remove(session);
		return user;
	}

	/**
	 * 当前圈子在线人数
	 * @return
	 */
	public int size() {
		return this.sessionMap.size();
	}
	
	/**
	 * 断线
	 * @param sender
	 */
	public void disconnect(Session sender) {
		User user = this.delSession(sender);
		if (user == null) {
			return;
		}
		user.group.enqueueRunnable(new Runnable() {
			
			@Override
			public void run() {
				if (user.sender == sender) {
					user.isConnect = false;
				}
			}
		});
	}

}
