Light12306/Web12306/js/ui/ui-autorefresh.js

257 lines
8.1 KiB
JavaScript
Raw Normal View History

2014-07-11 18:34:59 +08:00
define(function (require, exports, module) {
var sessMgr = require("../account/sessionMgr.js");
var session = sessMgr.current;
var currentProfile = sessMgr.currentProfile;
var expdata = require("../data.js");
var inAutoRefresh = false;
var media = require("../platform/media.js");
var query = require("../otn/queryticket.js");
2014-07-31 21:30:48 +08:00
var trainSuggest = require("../otn/trainstationsuggest.js");
2014-07-11 18:34:59 +08:00
sessMgr.on("sessionChanged", function () {
session = sessMgr.current;
});
sessMgr.on("currentProfileChanged", function () {
currentProfile = sessMgr.currentProfile;
});
var processTrains = function (data) {
if (!currentProfile || (!session.options.showMoreOpt && !inAutoRefresh))
2014-07-11 18:34:59 +08:00
return;
//预置参数
data.enableAuto = session.options.showMoreOpt;
data.inAuto = inAutoRefresh;
2014-07-31 21:30:48 +08:00
var count = Math.max(!currentProfile.partialSubmitEnabled && currentProfile.passengers ? currentProfile.passengers.length : 0, 1);
2014-07-11 18:34:59 +08:00
var seat = null, train = null, entry = null;
if (data.original.length && !data.include.length && currentProfile.selectedTrain && currentProfile.selectedTrain.length && !currentProfile.byAuto) {
//TODO 没有查到任何车次
}
2014-07-31 21:30:48 +08:00
var trainRegCache = _.map(expdata.translateTrain(currentProfile.selectedTrain || []), function (s) { return new RegExp("^" + s + "$", "i"); });
2014-07-11 18:34:59 +08:00
if (!trainRegCache || !trainRegCache.length) {
trainRegCache = [/.*/];
}
//搜索车次
2014-07-29 21:19:06 +08:00
var st = currentProfile.selectedSeatType;
if (!st || !st.length)
st = expdata.seatDisplayOrder;
2014-07-11 18:34:59 +08:00
if (currentProfile.selectSeatFirst) {
//席别优先
2014-07-29 21:19:06 +08:00
seat = _.find(st, function (s) {
2014-07-11 18:34:59 +08:00
var _treg = _.find(trainRegCache, function (t) {
entry = _.find(data.available, function (item) {
return t.test(item.code) && (_.findWhere(item.tickets, { code: s }) || {}).count >= count;
});
return typeof (entry) != 'undefined';
});
return typeof (_treg) != 'undefined';
});
} else {
//车次优先
_.find(trainRegCache, function (t) {
2014-07-29 21:19:06 +08:00
seat = _.find(st, function (s) {
2014-07-11 18:34:59 +08:00
entry = _.find(data.available, function (item) {
return t.test(item.code) && (_.findWhere(item.tickets, { code: s }) || {}).count >= count;
});
return typeof (entry) != 'undefined';
});
return typeof (seat) != 'undefined';
});
}
//查找结果
if (seat) {
train = entry.code;
//乘客
var ticketCount = _.findWhere(entry.tickets, { code: seat }).count;
var pcount = Math.min(ticketCount, currentProfile.passengers ? currentProfile.passengers.length : 0);
2014-07-11 18:34:59 +08:00
data.auto = {
train: train,
seat: seat,
passengers: _.first(currentProfile.passengers || [], pcount),
2014-07-11 18:34:59 +08:00
data: entry
};
data.auto.passengers.forEach(function (p) { p.seat = seat; });
} else {
data.auto = null;
}
//判断是否需要继续刷新
if (data.auto !== null) {
//找到车次了
data.nextTime = null;
if (inAutoRefresh) {
media.play();
media.notify("可以订票了!", "终于等到可以订的 " + data.auto.train + "次列车" + expdata.toSeatTypeName(data.auto.seat) + "");
2014-07-11 18:34:59 +08:00
}
2014-07-31 21:30:48 +08:00
if (currentProfile.autoSubmitEnabled && data.enableAuto && expdata.isAutoBookEnabled(currentProfile)) {
auto.fireEvent("performAutoSubmit", data.auto);
2014-07-11 18:34:59 +08:00
}
} else {
//规则:如果只有待售的,那么等到指定的时间
//如果没有且在整点附近的,那么等到整点
//否则就按常规时间
var now = new Date();
if (data.available.length && currentProfile.autoWaitToSell && data.include.length && _.every(data.include, function (x) { return x.available === -1; })) {
var next = _.min(_.map(data.include, function (x) {
return x.selltime;
}));
data.nextTime = now.getDate() === next.getDate() && next > now ? (next - now) / 1000 + 15 : currentProfile.autoRefreshDelay;
} else if (now.getMinutes() >= 59) {
data.nextTime = 61 - now.getSeconds();
} else {
data.nextTime = currentProfile.autoRefreshDelay;
}
}
};
var ev = require("../platform/EventObject.js");
var AutoRefresh = function () {
ev.apply(this);
var that = this;
var refreshDiv = $("#auto_refresh");
var refreshInfoSpan = $("section.auto-refresh-info span");
var refreshInfoP = $("section.auto-refresh-info>p");
var refreshTimer = null;
var countdownTime = null;
var refreshStartTime = null;
var refreshElapseTimer = null;
var refreshCount = 0;
var currentSelectedDate;
var currentDateLoopIndex = -1;
this.init = function () {
2014-07-29 21:19:06 +08:00
query.events.on("processTrains", function (e, d) {
2014-07-11 18:34:59 +08:00
processTrains(d);
2014-07-31 21:30:48 +08:00
if (inAutoRefresh)
trainSuggest.setQueryResult(d, refreshCount);
2014-07-11 18:34:59 +08:00
if (!d.auto && (d.nextTime && inAutoRefresh)) {
countdownTime = d.nextTime;
that.start();
d.inAutoRefresh = inAutoRefresh;
} else if (d.auto) {
that.stop();
}
}).on("requestFailed", function () {
//失败?不做任何处理
});
that.on("performAutoSubmit", function (e, d) {
2014-07-11 18:34:59 +08:00
that.dispatchEvent("requireSubmitOrder", d);
});
};
this.start = function () {
if (!inAutoRefresh) {
currentSelectedDate = sessMgr.currentProfile.depDate;
refreshCount = 0;
refreshStartTime = new Date();
inAutoRefresh = true;
refreshDiv.height();
refreshDiv.addClass("final");
refreshElapseTimer = setInterval(that.showElapsedTime, 1000);
that.showElapsedTime();
that.updateDisplay();
$("#result>table").addClass("auto-refresh");
location.hash = "#result";
2014-07-31 21:30:48 +08:00
trainSuggest.clearQueryResultCache();
2014-07-11 18:34:59 +08:00
}
inAutoRefresh = true;
if (!countdownTime)
countdownTime = currentProfile.autoRefreshDelay;
refreshTimer = setInterval(that.countdown, 100);
refreshInfoP.eq(2).hide();
refreshInfoP.eq(1).show();
that.showCountDownInfo();
};
this.stop = function () {
if (!inAutoRefresh)
return;
inAutoRefresh = false;
refreshTimer && (clearInterval(refreshTimer), refreshTimer = null);
refreshElapseTimer && (clearInterval(refreshElapseTimer), refreshElapseTimer = null);
refreshStartTime = null;
refreshDiv.removeClass("final");
$("#date_loop_editor>span.selected").removeClass("selected");
$("#result>table").removeClass("auto-refresh");
currentDateLoopIndex = -1;
currentSelectedDate && (sessMgr.currentProfile.depDate = currentSelectedDate);
currentSelectedDate = null;
2014-07-31 21:30:48 +08:00
trainSuggest.clearQueryResultCache();
$("tr.train-needauto").show();
2014-07-11 18:34:59 +08:00
};
this.updateDisplay = function () {
refreshInfoSpan.eq(0).html(refreshCount);
};
this.showElapsedTime = function () {
var ed = (new Date() - refreshStartTime) / 1000;
var m = Math.floor(ed / 60);
var s = Math.floor(ed % 60);
refreshInfoSpan.eq(1).html(m);
refreshInfoSpan.eq(2).html(s);
};
this.countdown = function () {
countdownTime -= 0.1;
that.showCountDownInfo();
if (countdownTime <= 0) {
clearInterval(refreshTimer);
refreshTimer = null;
refreshInfoP.eq(1).hide();
refreshInfoP.eq(2).show();
refreshCount++;
that.updateDisplay();
//已经在刷新状态了?日期轮询
if (sessMgr.currentProfile.dateloop && sessMgr.currentProfile.dateloop) {
currentDateLoopIndex++;
$("#date_loop_editor>span.selected").removeClass("selected");
if (currentDateLoopIndex > sessMgr.currentProfile.dateloop.length - 1) {
sessMgr.currentProfile.depDate = currentSelectedDate;
currentDateLoopIndex = -1;
} else {
sessMgr.currentProfile.depDate = sessMgr.currentProfile.dateloop[currentDateLoopIndex];
$("#date_loop_editor>span:eq(" + currentDateLoopIndex + ")").addClass("selected");
}
}
that.fireEvent("requestQueryTicket");
}
};
this.showCountDownInfo = function () {
refreshInfoSpan.eq(3).html((Math.round(countdownTime * 10) / 10) + "");
};
Object.defineProperties(this, {
inAutoRefresh: {
get: function () {
return inAutoRefresh;
}
}
});
$(document).on("click", "#btn_start_autorefresh", function () {
$(this).closest("tr").hide();
that.start();
});
$("#btn_stop_refresh").click(this.stop);
return this;
};
AutoRefresh.prototype = Object.create(ev);
AutoRefresh.constructor = AutoRefresh;
var auto = new AutoRefresh();
return auto;
2014-07-11 18:34:59 +08:00
});