/*
 * 由SharpDevelop创建。
 * 用户： Administrator
 * 日期: 2018-11-15
 * 时间: 10:33
 * 
 * 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件
 */
using System;
using System.Collections.Generic;
using Server.Data;
using StackExchange.Redis;
using Server.DB.Redis;
using Server.Pack;
using Server.Module;
using MrWu.Debug;
using System.Threading;
using Server.Data.Module;
using GameData;

namespace Server.Module
{
	/// <summary>
	/// 游戏信息表   所有开放的游戏
	/// </summary>
	public partial class GameInfoTab
	{
		/// <summary>
		/// 
		/// </summary>
		public class game{
			/// <summary>
			/// 游戏服务器id
			/// </summary>
			public int gameid;
			
			/// <summary>
			/// 是否开启朋友房模式
			/// </summary>
			public int openFriend;
			
			/// <summary>
			/// 是否开启金币模式
			/// </summary>
			public int openGlod;
			
			/// <summary>
			/// 
			/// </summary>
			public game(){}
			
			/// <summary>
			/// 
			/// </summary>
			/// <param name="gameid"></param>
			/// <param name="openFriend"></param>
			/// <param name="openGlod"></param>
			public game(int gameid,int openFriend,int openGlod){
				this.gameid = gameid;
				this.openFriend = openFriend;
				this.openGlod = openGlod;
			}
		}
		
		/// <summary>
		/// 游戏信息数据
		/// </summary>
		private readonly List<GameInfo> datas = new List<GameInfo>();
		
		private ModuleUtile myUtil;
		
		private readonly ReaderWriterLockSlim rwls = new ReaderWriterLockSlim();
		
		/// <summary>
		/// 游戏信息表
		/// </summary>
		/// <param name="util"></param>
		/// <param name="info"></param>
		public GameInfoTab(ModuleUtile util,GameInfo info){
			myUtil = util;
			if(info != null)
				AddGameInfo(info);
			LoadData();
		}
		
		/// <summary>
		/// 加载数据
		/// </summary>
		private void LoadData(){
			//读取redis数据
			HashEntry[] values = redisManager.db.HashGetAll(RedisDictionary.GameInfoTabName);
			foreach(var value in values){
				game gm = JsonPack.GetData<game>(value.Value);
				GameInfo info = new GameInfo(gm.gameid,gm.openFriend,gm.openGlod,int.Parse(value.Name));
				if(!AddData(info)){
					if(info.moduleId != myUtil.id){
						Debug.Error("有重复的游戏信息:" + info.ToString());
					}
				}
			}
		}
		
		/// <summary>
		/// 添加自身游戏信息 负责添加到内存和redis中
		/// </summary>
		/// <param name="info">自身游戏信息</param>
		/// <returns></returns>
		private bool AddGameInfo(GameInfo info){
			if(AddData(info)){
				if(info.moduleId == myUtil.id ||
                    !redisManager.db.HashExists(RedisDictionary.GameInfoTabName,info.moduleId)){
					game gm = new game(info.gameId,info.openFriend,info.openGlod);
					Debug.Info("添加信息:" + info.openFriend);
					redisManager.db.HashSet(RedisDictionary.GameInfoTabName,
                        info.moduleId,JsonPack.GetJson(gm));
				}
			}
			return false;
		}
		
		/// <summary>
		/// 添加数据 只负责添加到data中 内存数据 不负责redis
		/// </summary>
		/// <param name="info">游戏信息</param>
		public bool AddData(GameInfo info){
			rwls.EnterWriteLock();
			try{
				GameInfo CurrInfo = FindData_gameid_(info.gameId);
				if(CurrInfo == null){
					GameInfo temp = FindData_moduleId_(info.moduleId);
					if(temp == null){
						datas.Add(info);
						return true;
					}else{
						Debug.Error("添加游戏数据信息错误:" + info.gameId + "," + temp.ToString() );
					}
					return false;
				}
				return false;
			}finally{
				rwls.ExitWriteLock();
			}
		}
		
		/// <summary>
		/// 查找游戏信息
		/// </summary>
		/// <param name="gameid">服务id</param>
		/// <returns></returns>
		public GameInfo FindData_gameid(int gameid){
			rwls.EnterReadLock();
			try{
				return FindData_gameid_(gameid);
			}finally{
				rwls.ExitReadLock();
			}
		}
		
		private GameInfo FindData_gameid_(int gameid){
			foreach(var item in datas){
				if(gameid == item.gameId){
					return item.Clone();
				}
			}
			return null;
		}
		
		/// <summary>
		/// 查找数据
		/// </summary>
		/// <param name="moduleId"></param>
		/// <returns></returns>
		public GameInfo FindData_moduleId(int moduleId){
			rwls.EnterReadLock();
			try{
				return FindData_moduleId_(moduleId);
			}finally{
				rwls.ExitReadLock();
			}
		}
		
		private GameInfo FindData_moduleId_(int moduleId){
			foreach(var item in datas){
				if(item.moduleId == moduleId){
					return item.Clone();
				}
			}
			return null;
		}
	}
}
