320 lines
9.8 KiB
JavaScript
320 lines
9.8 KiB
JavaScript
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");
|
||
var trainSuggest = require("../otn/trainstationsuggest.js");
|
||
var port = require("../platform/extensionPort.js");
|
||
var utility = require("../utility.js");
|
||
var onceTransit = require("./ui-oncetrainsitquery.js");
|
||
|
||
sessMgr.on("sessionChanged", function () {
|
||
session = sessMgr.current;
|
||
});
|
||
sessMgr.on("currentProfileChanged", function () {
|
||
currentProfile = sessMgr.currentProfile;
|
||
});
|
||
|
||
var processTrains = function (data) {
|
||
if (!currentProfile || (!session.options.showMoreOpt && !inAutoRefresh))
|
||
return;
|
||
|
||
//预置参数
|
||
data.enableAuto = session.options.showMoreOpt;
|
||
data.inAuto = inAutoRefresh;
|
||
|
||
var count = Math.max(!currentProfile.partialSubmitEnabled && currentProfile.passengers ? currentProfile.passengers.length : 0, 1);
|
||
var seat = null, train = null, entry = null;
|
||
|
||
if (data.original.length && !data.include.length && currentProfile.selectedTrain && currentProfile.selectedTrain.length && !currentProfile.byAuto) {
|
||
//TODO 没有查到任何车次
|
||
}
|
||
|
||
var trainRegCache = _.map(expdata.translateTrain(currentProfile.selectedTrain || []), function (s) { return new RegExp("^" + s + "$", "i"); });
|
||
if (!trainRegCache || !trainRegCache.length) {
|
||
trainRegCache = [/.*/];
|
||
}
|
||
|
||
//搜索车次
|
||
var st = currentProfile.selectedSeatType;
|
||
if (!st || !st.length)
|
||
st = expdata.seatDisplayOrder;
|
||
var selectResult = utility.selectTrain(data.available, trainRegCache, st, currentProfile.selectSeatFirst, count);
|
||
if (selectResult) {
|
||
seat = selectResult.seat;
|
||
train = selectResult.train.code;
|
||
entry = selectResult.train;
|
||
}
|
||
|
||
//查找结果
|
||
if (seat) {
|
||
//乘客
|
||
var ticketCount = _.findWhere(entry.tickets, { code: seat }).count;
|
||
var pcount = Math.min(ticketCount, currentProfile.passengers ? currentProfile.passengers.length : 0);
|
||
|
||
data.auto = {
|
||
train: train,
|
||
seat: seat === "0" ? (entry.ticketMap['1'] ? "1" : "O") : seat,
|
||
passengers: _.first(currentProfile.passengers || [], pcount),
|
||
data: entry
|
||
};
|
||
data.auto.passengers.forEach(function (p) { p.seat = data.auto.seat; });
|
||
if (currentProfile.submitStuAsCommon && !currentProfile.studentTicket) {
|
||
data.auto.passengers.forEach(function (p) { if (p.passenger_type == "3") p.passenger_type = "1"; });
|
||
}
|
||
} else {
|
||
data.auto = null;
|
||
}
|
||
|
||
if (data.noAction)
|
||
return;
|
||
|
||
//判断是否需要继续刷新
|
||
if (data.auto !== null) {
|
||
//找到车次了
|
||
data.nextTime = null;
|
||
if (inAutoRefresh) {
|
||
media.play();
|
||
media.notify("可以订票了!", "终于等到可以订的 " + data.auto.train + "次列车" + expdata.toSeatTypeName(data.auto.seat) + "!");
|
||
|
||
//track
|
||
port.track(expdata.trackTypes.VALID_AUTOREFRESH);
|
||
|
||
if (currentProfile.autoSubmitEnabled && data.enableAuto && expdata.isAutoBookEnabled(currentProfile)) {
|
||
auto.fireEvent("performAutoSubmit", data.auto);
|
||
|
||
//track
|
||
port.track(expdata.trackTypes.SUBMIT_AUTO);
|
||
}
|
||
}
|
||
} 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;
|
||
var isAutoRefreshBlock = false;
|
||
|
||
//下一次刷新时间
|
||
var nextTick = null;
|
||
|
||
var init = function () {
|
||
query.events.on("processTrains", function (e, d) {
|
||
isAutoRefreshBlock = false;
|
||
processTrains(d);
|
||
|
||
if (inAutoRefresh) {
|
||
trainSuggest.setQueryResult(d, refreshCount);
|
||
d.showTransit = onceTransit.setQueryResult(d, refreshCount);
|
||
}
|
||
|
||
if (!d.noAction) {
|
||
if (!d.auto && (d.nextTime && inAutoRefresh)) {
|
||
countdownTime = d.nextTime;
|
||
that.start();
|
||
d.inAutoRefresh = inAutoRefresh;
|
||
} else if (d.auto) {
|
||
that.stop();
|
||
}
|
||
}
|
||
}).on("requestFailed", function () {
|
||
isAutoRefreshBlock = false;
|
||
//失败?不做任何处理
|
||
if (inAutoRefresh) {
|
||
that.start();
|
||
}
|
||
});
|
||
that.on("performAutoSubmit", function (e, d) {
|
||
that.dispatchEvent("requireSubmitOrder", d);
|
||
});
|
||
};
|
||
this.start = function (force) {
|
||
if (isAutoRefreshBlock) {
|
||
return;
|
||
}
|
||
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";
|
||
trainSuggest.clearQueryResultCache();
|
||
onceTransit.clearQueryResultCache();
|
||
|
||
//set bd
|
||
that.fireEvent("requestAutoRefresh");
|
||
|
||
//track
|
||
var cp = sessMgr.currentProfile;
|
||
port.track(expdata.trackTypes.START_AUTOREFRESH, [
|
||
cp.fromCode,
|
||
cp.fromText,
|
||
cp.toCode,
|
||
cp.toText,
|
||
cp.depDate,
|
||
cp.studentTicket + '',
|
||
(cp.selectedSeatType && cp.selectedSeatType.length) || 0,
|
||
(cp.selectedTrain && cp.selectedTrain.length) || 0,
|
||
(cp.passengers && cp.passengers.length) || 0,
|
||
(cp.dateloop && cp.dateloop.length) || 0,
|
||
cp.hideNoTicket ? 1 : 0,
|
||
cp.hideNotInListTrain ? 1 : 0,
|
||
cp.autoWaitToSell ? 1 : 0,
|
||
cp.tryBottomSeat ? 1 : 0,
|
||
cp.selectSeatFirst ? 1 : 0,
|
||
cp.autoSubmitEnabled ? 1 : 0
|
||
]);
|
||
}
|
||
|
||
inAutoRefresh = true;
|
||
if (countdownTime <= 1)
|
||
countdownTime = currentProfile.autoRefreshDelay;
|
||
if (!refreshTimer) {
|
||
nextTick = force ? 0 : new Date().getTime() + countdownTime * 1000;
|
||
refreshTimer = setInterval(that.countdown, 100);
|
||
}
|
||
refreshInfoP.eq(2).hide();
|
||
refreshInfoP.eq(1).show();
|
||
that.showCountDownInfo();
|
||
};
|
||
this.stop = function () {
|
||
if (!inAutoRefresh)
|
||
return;
|
||
|
||
nextTick = null;
|
||
isAutoRefreshBlock = false;
|
||
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);
|
||
sessMgr.currentProfile.depDate = $("#dep_date").val();
|
||
//currentSelectedDate = null;
|
||
trainSuggest.clearQueryResultCache();
|
||
onceTransit.clearQueryResultCache();
|
||
|
||
$("tr.train-needauto").show();
|
||
//隐藏监控
|
||
$("#suggestion td.train-action").hide();
|
||
|
||
//track
|
||
port.track(expdata.trackTypes.STOP_AUTOREFRESH);
|
||
};
|
||
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.increaseQueryCount = function(value) {
|
||
if (!inAutoRefresh)
|
||
return;
|
||
|
||
refreshCount++;
|
||
that.updateDisplay();
|
||
};
|
||
this.countdown = function () {
|
||
countdownTime -= 0.1;
|
||
that.showCountDownInfo();
|
||
|
||
if (new Date().getTime() >= nextTick) {
|
||
clearInterval(refreshTimer);
|
||
refreshTimer = null;
|
||
refreshInfoP.eq(1).hide();
|
||
refreshInfoP.eq(2).show();
|
||
refreshCount++;
|
||
that.updateDisplay();
|
||
|
||
//已经在刷新状态了?日期轮询
|
||
if (sessMgr.currentProfile.dateloop && sessMgr.currentProfile.dateloop && sessMgr.currentProfile.dateloop.length) {
|
||
currentDateLoopIndex++;
|
||
|
||
$("#date_loop_editor>span.selected").removeClass("selected");
|
||
if (currentDateLoopIndex > sessMgr.currentProfile.dateloop.length - 1) {
|
||
sessMgr.currentProfile.depDate = $("#dep_date").val();
|
||
currentDateLoopIndex = -1;
|
||
} else {
|
||
sessMgr.currentProfile.depDate = sessMgr.currentProfile.dateloop[currentDateLoopIndex];
|
||
$("#date_loop_editor>span:eq(" + currentDateLoopIndex + ")").addClass("selected");
|
||
}
|
||
}
|
||
isAutoRefreshBlock = true;
|
||
that.fireEvent("requestQueryTicket");
|
||
}
|
||
};
|
||
this.showCountDownInfo = function () {
|
||
var str = (Math.round(countdownTime * 10) / 10) + "";
|
||
if (str.indexOf(".") === -1)
|
||
str += ".0";
|
||
refreshInfoSpan.eq(3).html(str);
|
||
};
|
||
|
||
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);
|
||
init();
|
||
|
||
return this;
|
||
};
|
||
AutoRefresh.prototype = Object.create(ev);
|
||
AutoRefresh.constructor = AutoRefresh;
|
||
|
||
var auto = new AutoRefresh();
|
||
return auto;
|
||
});
|