package com.data.bean;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import com.taurus.core.entity.ITObject;
import com.taurus.core.entity.TObject;
import com.taurus.core.plugin.redis.Redis;



/**
 * game bean class.
 *
 */
public class GameBean extends BaseBean{
	
	/**
	 * name
	 */
	public String name;
	/**
	 * 最大人数
	 */
	public int maxPlayers;
	/**
	 * 体力值类型
	 */
	public int hpType;
	/**
	 * 包名
	 */
	public String bundle;
	/**
	 * 局数设置
	 */
	public Map<Integer,Integer> opt;
	/**
	 * 版本
	 */
	public String version;

	public int gameType;

	/**
	 * 服务器列表
	 */
	public Set<String> svr_list;
	/**
	 * 支付设置
	 */
	public Map<String,Integer> pay;
	/**不可负分*/
	public int isNonnegative ;
	
	
	
	public void fillData(Map<String,String> redis_map) {
//		Map<String,String> redis_map = jedis.hgetAll(redis_key);
		if(redis_map.isEmpty()) {
			this.del = true;
			return;
		}
		this.id = Integer.parseInt(redis_map.get("id"));
		this.name = redis_map.get("name");
		this.bundle = redis_map.get("bundle");
		this.maxPlayers =Integer.parseInt(redis_map.get("maxPlayers"));
		this.hpType =Integer.parseInt(redis_map.get("hpType"));
		this.opt = new HashMap<>();
		for(int i=1;i<=5;++i) {
			String key = "opt"+i;
			this.opt.put(i, Integer.parseInt(redis_map.get(key)));
		}
		this.version = redis_map.get("version");
		this.gameType =Integer.parseInt( redis_map.get("gameType"));
		this.isNonnegative = Integer.parseInt(redis_map.get("isNonnegative"));
		this.svr_list = Redis.use("group1_db1").smembers("game_svr:" + id);
		
		this.pay = new HashMap<>();
		for (Entry<String, String> entry : redis_map.entrySet()) {
			String key = entry.getKey();
			if(key.startsWith("pay")) {
				this.pay.put(key, Integer.parseInt(entry.getValue()));
			}
		}
	}
	
	public ITObject getTObject() {
		ITObject obj = TObject.newInstance();
		obj.putInt("game_id", id);
		obj.putString("name", name);
		obj.putString("bundle", bundle);
		obj.putInt("hpType", hpType);
		obj.putString("version", version);
		obj.putInt("gameType", gameType);
		obj.putInt("isNonnegative", isNonnegative);
		return obj;
	}
	
	public static String genKey(int gid) {
		return "game:" + gid;
	}
}
