每当在屏幕点击宠物的时候,金钱的方法就会额外增加,同时心跳包会带上点击的宠物ID和点击次数发给服务器。 所以,可以使用HttpClient来模拟心跳包来实现金钱的额外增长,实际加的很少,但也好过没有。

代码地址: https://github.com/lixize/wx-game-dogbian.git

核心代码

发包代码

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
    /**
     * 模拟心跳
     * @param dogId 宠物狗的内部ID
     * @param count 点击次数(100以内)
     * @return 心跳结果
     */
    public HeartPacket doHeart(int dogId, int count) {
        String param = "?cmd=sync";
        param += "&token=" + token;
        param += "&a=[]";
        param += "&e1_p=0";
        param += "&now=" + Calendar.getInstance().getTimeInMillis();
        param += "&seq=" + (seq++);
        param += "&c=[["+dogId+","+count+"]]";
        param += "&e1=[]&e2_mer=[]&e2_eat=[]";
        System.out.println(host + param);
        Request request = new Request(method, host, path + param, 2000);
        Map<String, String> headers = new HashMap<>();
        Map<String, String> querys = new HashMap<>();
        headers.put("content-type", ContentType.CONTENT_TYPE_JSON);
        headers.put("charset", "utf-8");
        request.setHeaders(headers);
        request.setQuerys(null);

        try {
            Response response = Client.execute(request);
            System.out.println(response.getStatusCode());
            //System.out.println(response.getBody());
            ObjectMapper mapper = new ObjectMapper();
            mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); // 忽略Bean中不存在的字段
            HeartPacket hp = mapper.readValue(response.getBody(), HeartPacket.class);
            System.out.println(hp);
            setChanged();
            notifyObservers(hp);
            return hp;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

定时器代码:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
public class HeartService {
    Timer timer;
    HeartController c;
    private String token;
    private List<Integer> dogId;
    private static int index = 0;
    private Integer count;

    public HeartService() {
        c = new HeartController();
    }

    public void run() {
        index = 0;
        timer = new Timer();
        if (token != null && !token.isEmpty()) {
            System.out.println(c);
            System.out.println(token);
            c.setToken(token);
        }

        if (count != null) {
            c.setCount(count);
        } else {
            c.setCount(100);
        }
        TimerTask task = new HeartTimerTask(c);
        timer.schedule(task, 0L, 2000L);
        System.out.println("ok");
    }

    public void addObserver(Observer o) {
        if (o != null && c != null) {
            c.addObserver(o);
        }
    }

    public void cancel() {
        timer.cancel();
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public List<Integer> getDogId() {
        return dogId;
    }

    public void setDogId(List<Integer> dogId) {
        this.dogId = dogId;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    class HeartTimerTask extends TimerTask {
        HeartController c;
        public HeartTimerTask(HeartController c) {
            this.c = c;
        }

        @Override
        public void run() {
            if (dogId != null && dogId.size() > 0) {
                c.setDogId(dogId.get(index++));
                index %= dogId.size();
                c.doHeart();
            } else {
                System.out.println("DogId Null");
            }
        }
    }
}