快捷链接
下载地址
相关随笔
正文
朋友 : 这I;P的成就咋搞的?
我 : 不是我干的, 都是Google Play Service搞定的, 要看API文档不~
朋友 : 知道你没个服务器搞这个, 我就想问问你怎么跟踪成就事件的? 就是你怎么判断成就完成了呢?
我 : 啊, 这个啊...简单的怕你骂我...
开说 ---- 我觉得呢, 每个问题都有N多的解决方案, 但基于人性考虑呢, 简单,好敲, 省力气的, 才是好方案...
成就事件跟踪呢, 简单考虑就是实时/定时事件点上更新成就所需数据, 并在指定的事件点上(也可以实时)进行检查判断, 以决定是否解锁.
因此, 我用了如下的类型存放成就所需数据:
private Type(int id, int target, boolean inc, boolean global) {
this.id = id;
this.target = target;
this.inc = inc;
this.global = global;
}
id: 成就标识;
target: 成就目标阈值;
inc: 递增类型(用于区别大于/小于);
global: 全局成就类型(就是全关卡通用, 反之每次running后清零);
有了上面的结构, 实例化各个成就数据, 然后在running过程中的每个事件中, 比如, Tary移动, 指令调用, 执行成功等等地方, 累计相应的成就值就好; 当执行成功时(这个是I;P成就的前提条件), 检查每一个成就实例, 哪个满足条件了, 就解锁哪个. 很简单不是? 下面是代码:
package jie.android.ip.playservice;
import java.util.HashMap;
import jie.android.ip.PlayEventListener;
import jie.android.ip.utils.Utils;
public class PlayServiceTracker {
private static final String Tag = PlayServiceTracker.class.getSimpleName();
private static final int ID_MOVE_LEFT = 10;
private static final int ID_MOVE_RIGHT = 11;
private static final int ID_ACTION_EMPTY = 12;
private static final int ID_ACTION_MAX = 13;
private static final int ID_CALL_MAX = 14;
private static final int ID_CHECK_MAX = 15;
private static final int ID_STEP_MAX = 20;
private static final int ID_EXECUTE_MAX_EXCEPTIN = 30;
private static final int ID_EXECUTE_MAX_OVERFLOW = 31;
private static final int ID_EXECUTE_MAX_FINISHED = 32;
private static final int ID_EXECUTE_MAX_RESET = 33;
private static final int ID_EXECUTE_MIN_SUCC = 34;
private static final int ID_EVENT_NO_UNSOLVED = 40;
public enum Type {
MOVE_LEFT(ID_MOVE_LEFT, 50, true, false), MOVE_RIGHT(ID_MOVE_RIGHT, 50, true, false), ACTION_EMPTY(ID_ACTION_EMPTY, 30, true, false),
ACTION_MAX(ID_ACTION_MAX, 180, true, false), CALL_MAX(ID_CALL_MAX, 30, true, false), CHECK_MAX(ID_CHECK_MAX, 50, true, false),
STEP_MAX(ID_STEP_MAX, 200, true, false),
EXECUTE_MAX_EXCEPTION(ID_EXECUTE_MAX_EXCEPTIN, 10, true, true), EXECUTE_MAX_OVERFLOW(ID_EXECUTE_MAX_OVERFLOW, 10, true, true), EXECUTE_MAX_FINISHED(ID_EXECUTE_MAX_FINISHED, 10, true, true),
EXECUTE_MAX_RESET(ID_EXECUTE_MAX_RESET, 10, true, true), EXECUTE_MIN_SUCC(ID_EXECUTE_MIN_SUCC, 1, false, true),
EVENT_NO_UNSOLVED(ID_EVENT_NO_UNSOLVED, 1, true, true);
private final int id;
private final int target;
private final boolean inc;
private final boolean global;
private Type(int id, int target, boolean inc, boolean global) {
this.id = id;
this.target = target;
this.inc = inc;
this.global = global;
}
public int getId() {
return id;
}
public int getTarget() {
return target;
}
public boolean isInc() {
return inc;
}
public boolean isGlobal() {
return global;
}
}
public class Data {
public int value;
public int target;
public boolean check() {
return value >= target;
}
}
private HashMap<Integer, Integer> mapData = new HashMap<Integer, Integer>();
public PlayServiceTracker() {
init();
}
private void init() {
for (final Type type : Type.values()) {
mapData.put(type.getId(), new Integer(0));
}
}
public void update(final Type type) {
mapData.put(type.getId(), mapData.get(type.getId()) + 1);
}
public void refresh(boolean all) {
for (final Type type : Type.values()) {
if (all || !type.isGlobal()) {
mapData.put(type.getId(), new Integer(0));
}
}
}
public boolean check(final Type type) {
final Integer val = mapData.get(type.getId());
if (type.isInc()) {
return (val >= type.getTarget());
} else {
return (val <= type.getTarget());
}
}
public void check(final PlayEventListener playEventListener) {
for (final Type type : Type.values()) {
if (check(type)) {
Utils.log(Tag, "PlayEvent is triggered - id = " + type.id);
playEventListener.unlockTrackerAchievement(type.getId());
}
}
}
}
对了, 最后还好, 朋友只是鄙视了下, 没打没骂...