package com.qiwu.chatbotbackend.entity; import com.alibaba.fastjson.JSONObject; import com.qiwu.chatbotbackend.GameEnvironment.GameEnv; import com.qiwu.chatbotbackend.processing.SimpleUtil; import org.apache.http.message.BasicNameValuePair; import org.springframework.beans.factory.DisposableBean; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; public class ChatCache implements DisposableBean { public String ENV_ERROR = "游戏平台初始化失败"; // private String chatKey = "不知道"; private String userKey = ""; private String randKey = ""; private String botAccount = ""; private Map infoPoint = null; private GameEnv gameEnv = null; private Boolean gameStart = false; private Boolean reload = false; private Boolean initial = false; public Boolean getInitial() { return initial; } public void setInitial(Boolean initial) { this.initial = initial; } public String getBotAccount() { return botAccount; } public void setBotAccount(String botAccount) { this.botAccount = botAccount; } public String getUserKey() { return userKey; } public void setUserKey(String userKey) { this.userKey = userKey; } public String getRandKey() { return randKey; } public void setRandKey(String randKey) { this.randKey = randKey; } public Map getInfoPoint() { return infoPoint; } public void setInfoPoint(Map infoPoint) { this.infoPoint = infoPoint; } public boolean isGameStart() { return gameStart; } public void setGameStart(boolean gameStart) { this.gameStart = gameStart; } public Boolean getReload() { return reload; } public void setReload(Boolean reload) { this.reload = reload; } public GameEnv getGameEnv() { return gameEnv; } public void setGameEnv(GameEnv gameEnv) { this.gameEnv = gameEnv; } public ChatCache(String userKey, String randKey, GameEnv gameEnv, Map infoPoint) { this.userKey = userKey; this.randKey = randKey; this.gameEnv = gameEnv; this.infoPoint = infoPoint; } @Override public void destroy() throws IOException { } public ChatCache(UserInfo userInfo, String userKey, String randKey, String botType) throws Exception { String k; this.botAccount = userInfo.botAccount; this.infoPoint = new HashMap(); if(botType.equals("game")) { try { this.gameEnv = new GameEnv(userInfo.botAccount); System.out.println("初始化时:" + this.gameEnv); } catch (Exception e) { System.out.println(ENV_ERROR); this.infoPoint.put("error_message", ENV_ERROR); e.printStackTrace(); } } Iterator iit = userInfo.varInitialMap.keySet().iterator(); while (iit.hasNext()) { k = iit.next(); this.infoPoint.put(k, userInfo.varInitialMap.get(k)); } this.infoPoint.put("lastUpdateTime", String.valueOf(System.currentTimeMillis())); this.randKey = randKey; this.userKey = userKey; this.initial = true; } public ChatCache(String userKey, String randKey) { this.userKey = userKey; this.randKey = randKey; } public Boolean sendPost(MapcurInfoPoint) { if(initial == false) return false; initial = false; String postURL = infoPoint.get("postURL"); if(postURL == null || postURL.trim().equals("")) return false; List pairList = new ArrayList(); Set> entrySet = curInfoPoint.entrySet(); String _key = null, _value = null; for(Map.Entry entry : entrySet) { _key = entry.getKey(); _value = entry.getValue().trim(); if(_value == null || _value.equals("")) { continue; } pairList.add(new BasicNameValuePair(_key, _value)); } entrySet = infoPoint.entrySet(); for(Map.Entry entry : entrySet) { _key = entry.getKey(); _value = entry.getValue().trim(); if(_value == null || _value.equals("")) { continue; } pairList.add(new BasicNameValuePair(_key, _value)); } JSONObject result = null; try { result = SimpleUtil.postFORM(postURL, pairList); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } System.out.println("initial post:" + result.toJSONString()); Set> entrySet2 = result.entrySet(); for(Entry entry : entrySet2) { _key = entry.getKey(); if(_key == "post_status") continue; infoPoint.put(_key, entry.getValue().toString()); } return true; } }