1
1
var _conflicts = [ ]
2
+ var _claims = [ ]
3
+
4
+ /**
5
+ * Check if a value is within the designated range (inclusive)
6
+ * @param {number } s1 start of first range
7
+ * @param {number } e1 end of first range
8
+ * @param {number } s2 start of first range
9
+ * @param {number } e2 end of first range
10
+ */
11
+ const _isOverlap = ( s1 , e1 , s2 , e2 ) => {
12
+ return Math . max ( s1 , e1 ) <= Math . min ( s2 , e2 )
13
+ }
14
+
15
+ /**
16
+ * Compares claims to see if they overlap
17
+ * @param {Object } c1 claim to compare
18
+ * @param {Object* } c2 claim to compare
19
+ */
20
+ const _isOverlappingClaim = ( c1 , c2 ) => {
21
+ // x range overlaps
22
+ const x = _isOverlap (
23
+ c1 . x ,
24
+ c1 . x + c1 . width ,
25
+ c2 . x ,
26
+ c2 . x + c2 . width
27
+ )
28
+ // y range overlaps
29
+ const y = _isOverlap (
30
+ c1 . y ,
31
+ c1 . y + c1 . height ,
32
+ c2 . y ,
33
+ c2 . y + c2 . height
34
+ )
35
+ return ( x && y )
36
+ }
37
+
38
+ /**
39
+ * Iterates through the list of claims and checks to see if any cover the specified point
40
+ * @param {number } x point x value
41
+ * @param {number } y point y value
42
+ * @returns {Boolean }
43
+ */
44
+ const isClaimed = ( x , y ) => {
45
+ const matches = _claims . filter ( ( claim ) =>
46
+ x >= claim . x &&
47
+ x <= claim . x + claim . width &&
48
+ y >= claim . y &&
49
+ y <= claim . y + claim . height
50
+ )
51
+ return ( matches . length > 0 )
52
+ }
2
53
3
54
/**
4
55
* Logs a conflict about multiple claims for a singel point
@@ -17,6 +68,23 @@ const logConflict = (x, y, claims) => {
17
68
}
18
69
}
19
70
71
+ const makeClaim = ( claim ) => {
72
+ // Check if there's any overlap with an exisiting claim
73
+ const overlaps = _claims . filter ( ( existing ) => _isOverlappingClaim ( existing , claim ) )
74
+ if ( overlaps . length > 0 ) {
75
+ console . log ( `Claim ${ claim . id } overlaps ${ overlaps . length } already existing claims.` )
76
+ // step through individual points to log conflicts
77
+ for ( let x = claim . x ; x < claim . x + claim . width ; x ++ ) {
78
+ for ( let y = claim . y ; y < claim . y + claim . height ; y ++ ) {
79
+ if ( isClaimed ( x , y ) ) {
80
+ logConflict ( x , y , [ claim . id ] )
81
+ }
82
+ }
83
+ }
84
+ }
85
+ _claims . push ( claim )
86
+ }
87
+
20
88
/**
21
89
* Parses a claim into a usable object
22
90
* @param {String } str formatted claim
@@ -43,6 +111,9 @@ const parseClaim = (str) => {
43
111
44
112
module . exports = {
45
113
_conflicts,
114
+ _claims,
115
+ isClaimed,
46
116
logConflict,
117
+ makeClaim,
47
118
parseClaim
48
119
}
0 commit comments