简体   繁体   English

如何处理时间表冲突事件?

[英]How to deal with conflicting schedule events?

I have a question that that has more to do with logic/algorithm rather than the actual code (PHP/JS). 我有一个问题,与逻辑/算法而不是实际代码(PHP / JS)有更多关系。

Say you have a bunch of events, happening within a week. 假设您在一周内发生了很多事件。 Each event's metadata is stored in the DB. 每个事件的元数据都存储在数据库中。 (Wordpress + Event Espresso) (Wordpress + Event Espresso)

Ie, Event A, Mon-Fri 8-5. 即事件A,周一至周五8-5。 Event B, Tue-Fri 11-4. 赛事B,周二至周五11-4。 Etc. 等等。

Users can go on the website and purchase these events. 用户可以在网站上购买这些事件。 I need to make sure they can't purchase at least 2 events that have conflicting schedule. 我需要确保他们不能购买至少2个时间表相冲突的活动。

I wanna avoid trips to the DB as much as possible. 我想尽可能避免去数据库。 Someone suggested that I check the conflicting event IDs in the shopping cart at checkout and throw the error. 有人建议我在结帐时检查购物车中有冲突的事件ID,并抛出错误。 That works, but what if I have about 12 events within the week? 那行得通,但是如果我一周内大约有12个活动怎么办? I can't deal with (12 choose 2, that 66, I think) cases, worst case. 我无法处理(12个选择2个,我认为是66个)最坏的情况。

What's the quickest and dirtiest way to solve this? 解决这个问题的最快,最肮脏的方法是什么? Or rather, what's a good way to solve this? 或者说,解决此问题的好方法是什么?

I'm on a time crunch, so client-side-only solution maybe acceptable for now. 我时间紧迫,因此目前仅接受客户端解决方案。

Thank you! 谢谢!

EDIT: This may be a better question: Say you have a list of event metadata, consisting of start date/time, and end date/time. 编辑:这可能是一个更好的问题:说您有一个事件元数据列表,包括开始日期/时间和结束日期/时间。 How do you check for conflict from each of the event? 您如何检查每个事件的冲突?

I've never done this kind of time/schedule conflict checking, so whatever you have in mind, please fire away! 我从来没有做过这样的时间/时间表冲突检查,所以无论您有什么想法,请开除!

Thank you! 谢谢!

Sort events by start time 按开始时间对事件进行排序

Your problem can be answered with a very simple algorithm --- but as I am not familiar with your DB , you will have to translate this algorithm to code yourself: 您可以使用非常简单的算法来解决您的问题---但由于我对您的数据库不熟悉,您将不得不将此算法转换为自己编写代码:

Let's say your events are accessible as objects in a list: event[0], event[1],... with members envent[i].start and event[i].end for start and end time (always assuming event[i].start <= event[i].end). 假设您的事件可以作为列表中的对象访问:event [0],event [1],...,成员envent [i] .start和event [i] .end为开始和结束时间(始终假设event [ i] .start <= event [i] .end)。 In order to check for any overlapping events, you first sort the events by their start time so that 为了检查是否有重叠的事件,您首先要按照事件的开始时间对其进行排序,以便

event[i].start <= event[i+1].start

Now, checking for overlaps means just comparing the end time of each event with the next start time: 现在,检查重叠意味着仅将每个事件的结束时间与下一个开始时间进行比较:

event[i].end <= event[i+1].start

If the above relation holds for all indices i, then (obviously) no events overlap each other. 如果上述关系适用于所有索引i,则(显然)没有事件相互重叠。 But if for some index i this relation doesn't hold then you have an overlap: 但是,如果对于某些索引我这种关系不成立,那么您有一个重叠:

event[i].start <= event[i+1].start < event[i].end

Note regarding equal times: 关于相等时间的注意事项:

I formulated the algorithm under the assumption that it is ok for one event to start exactly when the previous event stopped. 我在假设一个事件可以在上一个事件停止时准确开始的前提下制定算法。 But if you would consider this as an overlap, you just need to change the condition to check: 但是,如果您认为这是重叠的,则只需更改条件以进行检查:

event[i].end < event[i+1].start    (use < instead of <=)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM