package com.pack.service;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.pack.Utils;
import com.taurus.core.entity.ITArray;
import com.taurus.core.entity.ITObject;
import com.taurus.core.entity.TArray;
import com.taurus.core.plugin.redis.Redis;
import com.taurus.core.util.StringUtil;

import redis.clients.jedis.Jedis;

public class ServletAllGame extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	
	
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String _svr_type = request.getParameter("svr_type");
		int svr_type = 1;
		if(StringUtil.isNotEmpty(_svr_type)) {
			svr_type = Integer.parseInt(_svr_type);
		}
		String svr = Utils.svr_list[svr_type];
		ITArray gameArray = TArray.newInstance();
		Jedis jedis1 = Redis.use(svr+"_db1").getJedis();
		try {
			Set<String> all = jedis1.keys("game:[0-9]*");
			for(String key : all) {
				key = key.replaceAll("game:", "");
				ITObject gameObj = Utils.getGameData(jedis1, Integer.parseInt(key),false);
				gameArray.addTObject(gameObj);
			}
		}finally {
			jedis1.close();
		}
		
		OutputStream os =response.getOutputStream();
		os.write(StringUtil.getBytes(gameArray.toJson()));
		os.flush();
		os.close();
	}
}
