@@ -25,7 +25,8 @@ class DeckEncoder {
2525 throw new TypeError ( 'The provided code requires a higher version of this library; please update.' )
2626 }
2727
28- for ( let i = 3 ; i > 0 ; i -- ) {
28+ // first encoded cards are grouped by 3, 2, 1 pieces
29+ DeckEncoder . GROUPS . forEach ( i => {
2930 const numGroupOfs = VarInt . pop ( bytes )
3031
3132 for ( let j = 0 ; j < numGroupOfs ; j ++ ) {
@@ -43,7 +44,7 @@ class DeckEncoder {
4344 result . push ( Card . from ( setString , factionString , cardString , i ) )
4445 }
4546 }
46- }
47+ } )
4748
4849 while ( bytes . length > 0 ) {
4950 const fourPlusCount = VarInt . pop ( bytes )
@@ -66,46 +67,37 @@ class DeckEncoder {
6667 throw new TypeError ( 'The deck provided contains invalid card codes' )
6768 }
6869
69- const grouped3 = this . groupByFactionAndSetSorted ( cards . filter ( c => c . count === 3 ) )
70- const grouped2 = this . groupByFactionAndSetSorted ( cards . filter ( c => c . count === 2 ) )
71- const grouped1 = this . groupByFactionAndSetSorted ( cards . filter ( c => c . count === 1 ) )
72- const nOfs = cards . filter ( c => c . count > 3 )
73-
70+ const grouped = DeckEncoder . GROUPS . map ( i => this . groupByFactionAndSetSorted ( cards . filter ( c => c . count === i ) ) )
7471 return Base32 . encode ( [
7572 DeckEncoder . FORMAT << 4 | cards . reduce ( ( p , c ) => Math . max ( p , c . version ) , 0 ) & 0xF ,
76- ...this . encodeGroup ( grouped3 ) ,
77- ...this . encodeGroup ( grouped2 ) ,
78- ...this . encodeGroup ( grouped1 ) ,
79- ...this . encodeNofs ( nOfs )
73+ ...grouped . map ( group => this . encodeGroup ( group ) ) . reduce ( ( prev , curr ) => [ ...prev , ...curr ] , [ ] ) ,
74+ ...this . encodeNofs ( cards . filter ( c => c . count > DeckEncoder . GROUPS [ 0 ] ) )
8075 ] )
8176 }
8277
8378 static encodeNofs ( nOfs ) {
8479 return nOfs
8580 . sort ( ( a , b ) => a . code . localeCompare ( b . code ) )
86- . reduce ( ( result , card ) => {
87- result . push ( ... VarInt . get ( card . count ) )
88- result . push ( ... VarInt . get ( card . set ) )
89- result . push ( ... VarInt . get ( card . faction . id ) )
90- result . push ( ... VarInt . get ( card . id ) )
91- return result
92- } , [ ] )
81+ . reduce ( ( prev , card ) => [
82+ ... prev ,
83+ ... VarInt . get ( card . count ) ,
84+ ... VarInt . get ( card . set ) ,
85+ ... VarInt . get ( card . faction . id ) ,
86+ ... VarInt . get ( card . id )
87+ ] , [ ] )
9388 }
9489
9590 static encodeGroup ( group ) {
96- return group . reduce ( ( result , list ) => {
97- result . push ( ...VarInt . get ( list . length ) )
98-
99- const first = list [ 0 ]
100- result . push ( ...VarInt . get ( first . set ) )
101- result . push ( ...VarInt . get ( first . faction . id ) )
102-
103- for ( const card of list ) {
104- result . push ( ...VarInt . get ( card . id ) )
105- }
106-
107- return result
108- } , VarInt . get ( group . length ) )
91+ return group . reduce (
92+ ( prev , list ) => [
93+ ...prev ,
94+ ...VarInt . get ( list . length ) ,
95+ ...VarInt . get ( list [ 0 ] . set ) ,
96+ ...VarInt . get ( list [ 0 ] . faction . id ) ,
97+ ...list . map ( card => VarInt . get ( card . id ) ) . reduce ( ( prev , curr ) => [ ...prev , ...curr ] , [ ] )
98+ ] ,
99+ VarInt . get ( group . length )
100+ )
109101 }
110102
111103 static isValidDeck ( cards ) {
@@ -145,5 +137,6 @@ class DeckEncoder {
145137DeckEncoder . MAX_KNOWN_VERSION = 5
146138DeckEncoder . FORMAT = 1
147139DeckEncoder . INITIAL_VERSION = 1
140+ DeckEncoder . GROUPS = [ 3 , 2 , 1 ]
148141
149142module . exports = DeckEncoder
0 commit comments