@opndev/tennis-match-center 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -9,6 +9,8 @@ import {
9
9
  import { competitionHue, divisionHues } from '../division-hues.mjs'
10
10
  import { setNameText } from '../flags.mjs'
11
11
  import { rowsForDate } from '../day-rows.mjs'
12
+ import { courtCanTake, toMinutes } from '../lighting.mjs'
13
+ import { PX_PER_MINUTE, TennisDayGridElement } from './day-grid.mjs'
12
14
  import { withBusyButton } from '../busy-button.mjs'
13
15
 
14
16
  const KNOCKOUT_ROUND_ORDER = ['R256', 'R128', 'R64', 'R32', 'R16', 'QF', 'SF', 'F']
@@ -63,6 +65,12 @@ export class TennisScheduleBoardElement extends HTMLElementSugar {
63
65
  'clear-api': {},
64
66
  'csrf-token': {},
65
67
  'flag-svg': { default: 'windows' },
68
+
69
+ // The same two Planning takes: an unlit court stops taking play
70
+ // before the light goes, and a board that does not know when that
71
+ // is would happily put a match on one at nine in the evening.
72
+ 'dark-from': {},
73
+ 'light-buffer': {},
66
74
  }
67
75
 
68
76
  static HtmlTemplate = ['tennis-schedule-board-template', function () {
@@ -76,6 +84,10 @@ export class TennisScheduleBoardElement extends HTMLElementSugar {
76
84
  <input data-show-data type="checkbox">
77
85
  Show data
78
86
  </label>
87
+ <label data-narrow-toggle hidden>
88
+ <input data-narrow-empty type="checkbox">
89
+ Narrow empty courts
90
+ </label>
79
91
  <pre data-schedule-data hidden></pre>
80
92
  <div data-schedule hidden></div>
81
93
  <button data-save type="button" hidden>Save schedule</button>
@@ -106,6 +118,11 @@ export class TennisScheduleBoardElement extends HTMLElementSugar {
106
118
  )
107
119
  })
108
120
  this.$('[data-show-data]').addEventListener('change', () => this.render())
121
+ this.$('[data-narrow-empty]').addEventListener('change', () => this.render())
122
+
123
+ // Bound on window so letting go outside the grid still ends the
124
+ // drag rather than leaving one hanging.
125
+ window.addEventListener('mouseup', () => this.drop())
109
126
  }
110
127
 
111
128
  const shellApplied = await this.fetchShell()
@@ -359,7 +376,7 @@ export class TennisScheduleBoardElement extends HTMLElementSugar {
359
376
  * appear, so a half-built bracket doesn't produce empty rounds.
360
377
  */
361
378
  bracketsFor(competition) {
362
- return (competition.brackets ?? []).map(bracket => ({
379
+ return this.plannedWork(competition).brackets.map(bracket => ({
363
380
  name: bracket.division_name,
364
381
  rounds: KNOCKOUT_ROUND_ORDER
365
382
  .filter(round => bracket.matches.some(match => match.round === round))
@@ -397,9 +414,11 @@ export class TennisScheduleBoardElement extends HTMLElementSugar {
397
414
  return false
398
415
  }
399
416
 
417
+ const planned = this.plannedWork(competition)
418
+
400
419
  const { matches, unplaced } = placeGroupsIntoCells({
401
420
  cells: competition.cells,
402
- groups: competition.groups.map(group => ({
421
+ groups: planned.groups.map(group => ({
403
422
  id: group.id,
404
423
  divisionId: group.division_id,
405
424
  teamIds: group.team_ids,
@@ -522,15 +541,38 @@ export class TennisScheduleBoardElement extends HTMLElementSugar {
522
541
  * the other way round had a competition report more matches
523
542
  * scheduled than it has.
524
543
  */
544
+ /**
545
+ * The groups and brackets this board is answerable for.
546
+ *
547
+ * A division Planning does not cover is still played - Singles Pro
548
+ * is generated and seeded - but it reserves no court time, so
549
+ * nothing here can schedule it and counting it makes a full day
550
+ * report matches that could not fit. A payload that does not say
551
+ * either way is taken as covered, which is what it always was.
552
+ *
553
+ * @param {Object} competition
554
+ * @returns {Object}
555
+ */
556
+ plannedWork(competition) {
557
+ return {
558
+ groups: (competition.groups ?? [])
559
+ .filter(group => group.available_for_planning !== false),
560
+ brackets: (competition.brackets ?? [])
561
+ .filter(bracket => bracket.available_for_planning !== false),
562
+ }
563
+ }
564
+
525
565
  expectedMatchCount(competition) {
526
- const groups = competition.groups.reduce(
566
+ const planned = this.plannedWork(competition)
567
+
568
+ const groups = planned.groups.reduce(
527
569
  (total, group) => total + groupFixtures({
528
570
  teamIds: group.team_ids, groupSize: group.pool_size,
529
571
  }).filter(fixture => fixture.playable).length,
530
572
  0
531
573
  )
532
574
 
533
- const knockout = (competition.brackets ?? []).reduce(
575
+ const knockout = planned.brackets.reduce(
534
576
  (total, bracket) =>
535
577
  total + bracket.matches.filter(match => !isBye(match)).length,
536
578
  0
@@ -663,11 +705,42 @@ export class TennisScheduleBoardElement extends HTMLElementSugar {
663
705
  * where nothing was planned.
664
706
  */
665
707
  hoursFor(date) {
708
+ return this.rowsFor(date).map(row => row.key)
709
+ }
710
+
711
+ /**
712
+ * The rows of one day, with the minutes the grid draws them from.
713
+ *
714
+ * @param {string} date
715
+ * @returns {Array}
716
+ */
717
+ rowsFor(date) {
666
718
  return rowsForDate({
667
719
  date,
668
720
  competitions: this.state.competitions ?? [],
669
721
  cells: this.allCellsWithCompetition(),
670
- }).map(row => row.key)
722
+ darkFromMinutes: toMinutes(this.getConfig()['dark-from']),
723
+ lightBufferMinutes: this.lightBuffer() ?? 0,
724
+ })
725
+ }
726
+
727
+ /**
728
+ * Minutes before dark an unlit court stops taking new play. Left
729
+ * off the element, the planner's own default stands; "0" is a real
730
+ * answer, hence no `|| undefined`.
731
+ *
732
+ * @returns {?number}
733
+ */
734
+ lightBuffer() {
735
+ const raw = this.getConfig()['light-buffer']
736
+
737
+ if (raw == null || String(raw).trim() === '') {
738
+ return undefined
739
+ }
740
+
741
+ const minutes = Number(raw)
742
+
743
+ return Number.isFinite(minutes) && minutes >= 0 ? minutes : undefined
671
744
  }
672
745
 
673
746
  /**
@@ -701,6 +774,7 @@ export class TennisScheduleBoardElement extends HTMLElementSugar {
701
774
  this.$('[data-legend]').hidden = false
702
775
  this.$('[data-save]').hidden = false
703
776
  this.$('[data-view-toggle]').hidden = false
777
+ this.$('[data-narrow-toggle]').hidden = false
704
778
 
705
779
  // The schedule as data, for reading rather than looking at - the
706
780
  // same toggle Planning has.
@@ -849,7 +923,7 @@ export class TennisScheduleBoardElement extends HTMLElementSugar {
849
923
  return
850
924
  }
851
925
 
852
- tab.replaceChildren(this.tableForDay(date))
926
+ tab.replaceChildren(this.dayForDate(date))
853
927
 
854
928
  if (this.loadedDates.has(date)) {
855
929
  return
@@ -857,69 +931,160 @@ export class TennisScheduleBoardElement extends HTMLElementSugar {
857
931
 
858
932
  this.fetchDay(date).then(applied => {
859
933
  if (applied && tab.isConnected) {
860
- tab.replaceChildren(this.tableForDay(date))
934
+ tab.replaceChildren(this.dayForDate(date))
861
935
  }
862
936
  })
863
937
  }
864
938
 
865
- tableForDay(date) {
866
- const hours = this.hoursFor(date)
867
- const courts = this.state.courts
868
-
869
- const table = document.createElement('table')
870
- table.dataset.day = date
871
-
872
- const thead = document.createElement('thead')
873
- const headRow = document.createElement('tr')
874
- headRow.appendChild(document.createElement('th'))
875
- for (const court of courts) {
876
- const th = document.createElement('th')
877
- th.dataset.courtId = court.id
878
- th.textContent = court.name
879
- headRow.appendChild(th)
939
+ dayForDate(date) {
940
+ const grid = document.createElement(TennisDayGridElement.tag)
941
+
942
+ grid.addEventListener('day-grid:press', event => this.pressed(event.detail))
943
+ grid.addEventListener('day-grid:enter', event => this.entered(event.detail))
944
+
945
+ grid.draw({
946
+ date,
947
+ courts: this.state.courts,
948
+ rows: this.rowsFor(date),
949
+ cellFor: (court, courtIndex, slot) => this.cellFor(court, date, slot),
950
+ compact: this.$('[data-narrow-empty]')?.checked ?? false,
951
+ // A scheduled cell carries four names and what is being played,
952
+ // where a planned one carries a division and a group.
953
+ scale: PX_PER_MINUTE * 2,
954
+ })
955
+
956
+ return grid
957
+ }
958
+
959
+ /**
960
+ * Takes hold of the match under the cursor. An empty cell has
961
+ * nothing to move, and there is no selection here to drag: one
962
+ * match at a time is the whole gesture.
963
+ *
964
+ * @param {Object} where
965
+ */
966
+ pressed({ date, row, col, onContent, button, ctrlKey }) {
967
+ if (button === 2 || ctrlKey || !onContent) {
968
+ return
880
969
  }
881
- thead.appendChild(headRow)
882
- table.appendChild(thead)
883
970
 
884
- const tbody = document.createElement('tbody')
885
- for (const hour of hours) {
886
- tbody.appendChild(this.gridRow(date, hour, courts))
971
+ const at = this.cellAt(date, row, col)
972
+
973
+ this.drag = this.matchAt(at.courtId, date, at.hour)
974
+ ? { date, ...at, key: `${row},${col}`, over: null }
975
+ : null
976
+
977
+ this.showDrag()
978
+ }
979
+
980
+ entered({ date, row, col }) {
981
+ if (this.drag?.date === date) {
982
+ this.drag.over = { ...this.cellAt(date, row, col), key: `${row},${col}` }
983
+ this.showDrag()
887
984
  }
888
- table.appendChild(tbody)
985
+ }
889
986
 
890
- return table
987
+ showDrag() {
988
+ this.gridFor(this.drag?.date)?.showDrag(
989
+ this.drag ? new Set([this.drag.key]) : null,
990
+ this.drag?.over?.key ?? null
991
+ )
891
992
  }
892
993
 
893
- gridRow(date, hour, courts) {
894
- const row = document.createElement('tr')
895
- row.dataset.slot = hour
994
+ gridFor(date) {
995
+ return date ? this.$(`tennis-day-grid[data-day="${date}"]`) : null
996
+ }
896
997
 
897
- const heading = document.createElement('th')
898
- heading.dataset.slotLabel = ''
899
- heading.textContent = hour
900
- row.appendChild(heading)
998
+ cellAt(date, row, col) {
999
+ return {
1000
+ courtId: this.state.courts[col]?.id,
1001
+ hour: this.rowsFor(date)[row]?.key,
1002
+ }
1003
+ }
901
1004
 
902
- const availableCourts = new Set(this.courtsFor(date, hour).map(court => court.id))
1005
+ /**
1006
+ * Drops what is being dragged. An empty cell takes the match; one
1007
+ * that is taken trades with it, which is how two matches change
1008
+ * places. Scheduling never leaves a match without a cell, so there
1009
+ * is nothing to replace and nothing to lose.
1010
+ */
1011
+ drop() {
1012
+ const drag = this.drag
903
1013
 
904
- for (const court of courts) {
905
- const cell = document.createElement('td')
1014
+ this.drag = null
1015
+ this.gridFor(drag?.date)?.showDrag(null, null)
906
1016
 
907
- if (!availableCourts.has(court.id)) {
908
- cell.dataset.blocked = ''
909
- row.appendChild(cell)
910
- continue
911
- }
1017
+ if (!drag?.over || !drag.over.courtId || !drag.over.hour) {
1018
+ return
1019
+ }
1020
+
1021
+ const moving = this.matchAt(drag.courtId, drag.date, drag.hour)
912
1022
 
913
- cell.dataset.courtId = court.id
1023
+ if (!moving || (drag.over.courtId === drag.courtId && drag.over.hour === drag.hour)) {
1024
+ return
1025
+ }
914
1026
 
915
- const match = this.matchAt(court.id, date, hour)
1027
+ const sitting = this.matchAt(drag.over.courtId, drag.date, drag.over.hour)
916
1028
 
917
- cell.appendChild(match ? this.matchLabel(match) : this.emptySlotLabel())
1029
+ moving.court_id = drag.over.courtId
1030
+ moving.hour = drag.over.hour
918
1031
 
919
- row.appendChild(cell)
1032
+ if (sitting) {
1033
+ sitting.court_id = drag.courtId
1034
+ sitting.hour = drag.hour
920
1035
  }
921
1036
 
922
- return row
1037
+ this.dispatchEvent(new CustomEvent('tennis-schedule:changed', {
1038
+ bubbles: true,
1039
+ detail: { matches: this.matches },
1040
+ }))
1041
+
1042
+ this.redraw(drag.date)
1043
+ }
1044
+
1045
+ /**
1046
+ * Draws one day again, leaving the rest of the board alone.
1047
+ *
1048
+ * @param {string} date
1049
+ */
1050
+ redraw(date) {
1051
+ const tab = this.$(`[data-schedule] semtic-tab[tab="${date}"]`)
1052
+
1053
+ tab?.replaceChildren(this.dayForDate(date))
1054
+ }
1055
+
1056
+ /**
1057
+ * What Scheduling puts in a cell: the match played there, or an
1058
+ * empty slot waiting for one.
1059
+ *
1060
+ * A court and hour Planning never planned is drawn as open rather
1061
+ * than blocked. The plan is where a day starts, not a fence around
1062
+ * it: a match that has to move somewhere the plan did not think of
1063
+ * can be dragged there, and the cell says it is off-plan.
1064
+ *
1065
+ * @param {Object} court
1066
+ * @param {string} date
1067
+ * @param {Object} slot
1068
+ * @returns {Object}
1069
+ */
1070
+ cellFor(court, date, slot) {
1071
+ // Dark is not a matter of planning: a match cannot be played on
1072
+ // an unlit court once the light has gone, whoever wants it there.
1073
+ if (!courtCanTake(court, slot)) {
1074
+ return { blocked: true }
1075
+ }
1076
+
1077
+ const match = this.matchAt(court.id, date, slot.key)
1078
+
1079
+ if (match) {
1080
+ return { content: this.matchLabel(match), filled: true }
1081
+ }
1082
+
1083
+ const planned = this.courtsFor(date, slot.key).some(one => one.id === court.id)
1084
+
1085
+ // A planned cell waiting for a match draws its [empty], which is
1086
+ // something to look at and nothing to play.
1087
+ return { content: this.emptySlotLabel(planned), filled: false }
923
1088
  }
924
1089
 
925
1090
  /**
@@ -1074,11 +1239,15 @@ export class TennisScheduleBoardElement extends HTMLElementSugar {
1074
1239
  * capacity while Scheduling fills actual matches, so the leftover
1075
1240
  * cells are expected, not an error.
1076
1241
  */
1077
- emptySlotLabel() {
1242
+ emptySlotLabel(planned = true) {
1078
1243
  const container = document.createElement('div')
1079
1244
  container.dataset.cellMatch = ''
1080
1245
  container.dataset.cellEmpty = ''
1081
1246
 
1247
+ if (!planned) {
1248
+ container.dataset.cellOffPlan = ''
1249
+ }
1250
+
1082
1251
  const teams = document.createElement('div')
1083
1252
  teams.dataset.cellTeams = ''
1084
1253
  teams.dataset.rows = 1
package/lib/day-rows.mjs CHANGED
@@ -3,6 +3,7 @@
3
3
  // SPDX-License-Identifier: AGPL-3.0-or-later
4
4
 
5
5
  import { slotsForDate } from './auto-planner.mjs'
6
+ import { litRequired } from './lighting.mjs'
6
7
 
7
8
  /** What an hour is worth when nothing says otherwise. */
8
9
  const FALLBACK_SLOT_MINUTES = 45
@@ -33,10 +34,17 @@ function minutesToTime(minutes) {
33
34
  * cell is never stranded on a row the sessions no longer describe.
34
35
  * @param {Array} [params.fallbackSlots] Used only where the day has no
35
36
  * session at all, so there is still something to drag onto.
37
+ * @param {?number} [params.darkFromMinutes] When the light goes, so a
38
+ * row can say whether it needs a lit court.
39
+ * @param {number} [params.lightBufferMinutes] How long before that an
40
+ * unlit court stops taking new play.
36
41
  * @returns {Array<{key: string, label: string, start_minutes: number,
37
42
  * end_minutes: number}>}
38
43
  */
39
- export function rowsForDate({ date, competitions, cells, fallbackSlots = [] }) {
44
+ export function rowsForDate({
45
+ date, competitions, cells, fallbackSlots = [],
46
+ darkFromMinutes = null, lightBufferMinutes = 0,
47
+ }) {
40
48
 
41
49
  const byKey = new Map()
42
50
 
@@ -61,7 +69,7 @@ export function rowsForDate({ date, competitions, cells, fallbackSlots = [] }) {
61
69
  .map(cell => cell.slot)
62
70
  .sort()
63
71
 
64
- for (const slot of slotsForDate(sessions, date, null)) {
72
+ for (const slot of slotsForDate(sessions, date, darkFromMinutes)) {
65
73
  if (mine.length > 0 && slot.key < mine[0]) {
66
74
  continue
67
75
  }
@@ -112,5 +120,13 @@ export function rowsForDate({ date, competitions, cells, fallbackSlots = [] }) {
112
120
  }
113
121
  }
114
122
 
115
- return [...byKey.values()].sort((a, b) => a.key.localeCompare(b.key))
123
+ // Every row, however it was arrived at, answers the same question
124
+ // about the light: a session's own slot already carries it, a
125
+ // fallback or a stranded cell's row does not.
126
+ return [...byKey.values()]
127
+ .map(row => ({
128
+ ...row,
129
+ lit_required: litRequired(row, darkFromMinutes, lightBufferMinutes),
130
+ }))
131
+ .sort((a, b) => a.key.localeCompare(b.key))
116
132
  }
@@ -0,0 +1,93 @@
1
+ // SPDX-FileCopyrightText: 2026 Wesley Schwengle <wesley@opndev.io>
2
+ //
3
+ // SPDX-License-Identifier: AGPL-3.0-or-later
4
+
5
+ /**
6
+ * One dialog on the desk, and which match it is about.
7
+ *
8
+ * The desk has four of these and they open, close and are answered in
9
+ * exactly the same way. Written once, because the way they close is
10
+ * not obvious and getting it wrong in one of four places is how it
11
+ * came to be written down:
12
+ *
13
+ * `close()` is us putting the dialog away, so it asks semtic to close.
14
+ * `forget()` is the dialog having closed, however that happened, so it
15
+ * asks nothing: semtic answers close() by dispatching closed, and
16
+ * closed is what calls forget(), so a forget() that closed again would
17
+ * be a loop.
18
+ *
19
+ * <semtic-modal> brings its own cancel and confirm, so nothing here
20
+ * draws either. Cancel, the ✕ and Escape are one answer as far as the
21
+ * desk is concerned, and they all arrive as closed.
22
+ */
23
+ export class GamedayDialog {
24
+ /**
25
+ * @param {HTMLElement} modal - the <semtic-modal>
26
+ * @param {object} spec
27
+ * @param {Function} spec.fill - draw the dialog for this match
28
+ * @param {Function} spec.focus - what the finger lands on, given the modal
29
+ * @param {Function} spec.confirmed - the confirm button was pressed
30
+ */
31
+ constructor(modal, { fill, focus, confirmed }) {
32
+ this.modal = modal
33
+ this.match = null
34
+ this._fill = fill
35
+ this._focus = focus
36
+
37
+ modal.addEventListener('semtic-modal:confirmed', () => confirmed(this.match))
38
+ modal.addEventListener('semtic-modal:closed', () => this.forget())
39
+ }
40
+
41
+ /**
42
+ * An official is reading the line they came from, so focus that
43
+ * scrolls the board out from under them is worse than focus they
44
+ * have to find.
45
+ */
46
+ static NO_SCROLL = { preventScroll: true }
47
+
48
+ /**
49
+ * @param {object|null} match
50
+ */
51
+ open(match) {
52
+ if (!match) {
53
+ return
54
+ }
55
+
56
+ this.match = match
57
+ this._fill(match)
58
+
59
+ // <semtic-modal> is authored by tag and upgraded by the host, so
60
+ // it may still be un-upgraded here. `hidden` keeps it closed either
61
+ // way; semtic adds the dialog chrome on top.
62
+ this.modal.hidden = false
63
+
64
+ if (typeof this.modal.open === 'function') {
65
+ this.modal.open()
66
+ }
67
+
68
+ this._focus(this.modal)?.focus(GamedayDialog.NO_SCROLL)
69
+ }
70
+
71
+ /**
72
+ * Put the dialog away, because we are done with it.
73
+ */
74
+ close() {
75
+ if (this.modal.hidden) {
76
+ return
77
+ }
78
+
79
+ this.forget()
80
+
81
+ if (typeof this.modal.close === 'function') {
82
+ this.modal.close()
83
+ }
84
+ }
85
+
86
+ /**
87
+ * The dialog is closed. See the note above: this asks nothing of it.
88
+ */
89
+ forget() {
90
+ this.modal.hidden = true
91
+ this.match = null
92
+ }
93
+ }