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"); 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; if (currentProfile.selectSeatFirst) { //席别优先 seat = _.find(st, function (s) { 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) { seat = _.find(st, function (s) { 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); data.auto = { train: train, seat: seat, passengers: _.first(currentProfile.passengers || [], pcount), 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) + "!"); //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 init = function () { query.events.on("processTrains", function (e, d) { processTrains(d); if (inAutoRefresh) trainSuggest.setQueryResult(d, refreshCount); 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) { 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"; trainSuggest.clearQueryResultCache(); //track port.track(expdata.trackTypes.START_AUTOREFRESH); } 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; trainSuggest.clearQueryResultCache(); $("tr.train-needauto").show(); //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.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 () { 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; });