@@ -2,12 +2,17 @@ import type { TemplateResult } from "lit";
22import { html , LitElement , render } from "lit" ;
33import { customElement , query , state } from "lit/decorators.js" ;
44import { UserMeResponse } from "../core/ApiSchemas" ;
5- import { Pattern } from "../core/CosmeticSchemas" ;
5+ import { ColorPalette , Cosmetics , Pattern } from "../core/CosmeticSchemas" ;
66import { UserSettings } from "../core/game/UserSettings" ;
7+ import { PlayerPattern } from "../core/Schemas" ;
78import "./components/Difficulties" ;
89import "./components/PatternButton" ;
910import { renderPatternPreview } from "./components/PatternButton" ;
10- import { fetchPatterns , handlePurchase } from "./Cosmetics" ;
11+ import {
12+ fetchCosmetics ,
13+ handlePurchase ,
14+ patternRelationship ,
15+ } from "./Cosmetics" ;
1116import { translateText } from "./Utils" ;
1217
1318@customElement ( "territory-patterns-modal" )
@@ -19,16 +24,18 @@ export class TerritoryPatternsModal extends LitElement {
1924
2025 public previewButton : HTMLElement | null = null ;
2126
22- @state ( ) private selectedPattern : Pattern | null ;
27+ @state ( ) private selectedPattern : PlayerPattern | null ;
2328
24- private patterns : Map < string , Pattern > = new Map ( ) ;
29+ private cosmetics : Cosmetics | null = null ;
2530
2631 private userSettings : UserSettings = new UserSettings ( ) ;
2732
2833 private isActive = false ;
2934
3035 private affiliateCode : string | null = null ;
3136
37+ private userMeResponse : UserMeResponse | null = null ;
38+
3239 constructor ( ) {
3340 super ( ) ;
3441 }
@@ -38,11 +45,12 @@ export class TerritoryPatternsModal extends LitElement {
3845 this . userSettings . setSelectedPatternName ( undefined ) ;
3946 this . selectedPattern = null ;
4047 }
41- this . patterns = await fetchPatterns ( userMeResponse ) ;
42- const storedPatternName = this . userSettings . getSelectedPatternName ( ) ;
43- if ( storedPatternName ) {
44- this . selectedPattern = this . patterns . get ( storedPatternName ) ?? null ;
45- }
48+ this . userMeResponse = userMeResponse ;
49+ this . cosmetics = await fetchCosmetics ( ) ;
50+ this . selectedPattern =
51+ this . cosmetics !== null
52+ ? this . userSettings . getSelectedPatternName ( this . cosmetics )
53+ : null ;
4654 this . refresh ( ) ;
4755 }
4856
@@ -52,25 +60,31 @@ export class TerritoryPatternsModal extends LitElement {
5260
5361 private renderPatternGrid ( ) : TemplateResult {
5462 const buttons : TemplateResult [ ] = [ ] ;
55- for ( const [ name , pattern ] of this . patterns ) {
56- if ( this . affiliateCode === null ) {
57- if ( pattern . affiliateCode !== null && pattern . product !== null ) {
58- // Patterns with affiliate code are not for sale by default.
59- continue ;
60- }
61- } else {
62- if ( pattern . affiliateCode !== this . affiliateCode ) {
63+ for ( const pattern of Object . values ( this . cosmetics ?. patterns ?? { } ) ) {
64+ const colorPalettes = [ ...( pattern . colorPalettes ?? [ ] ) , null ] ;
65+ for ( const colorPalette of colorPalettes ) {
66+ const rel = patternRelationship (
67+ pattern ,
68+ colorPalette ,
69+ this . userMeResponse ,
70+ this . affiliateCode ,
71+ ) ;
72+ if ( rel === "blocked" ) {
6373 continue ;
6474 }
75+ buttons . push ( html `
76+ < pattern-button
77+ .pattern =${ pattern }
78+ .colorPalette =${ this . cosmetics ?. colorPalettes ?. [
79+ colorPalette ?. name ?? ""
80+ ] ?? null }
81+ .requiresPurchase=${ rel === "purchasable" }
82+ .onSelect=${ ( p : PlayerPattern | null ) => this . selectPattern ( p ) }
83+ .onPurchase=${ ( p : Pattern , colorPalette : ColorPalette | null ) =>
84+ handlePurchase ( p , colorPalette ) }
85+ > </ pattern-button >
86+ ` ) ;
6587 }
66-
67- buttons . push ( html `
68- < pattern-button
69- .pattern =${ pattern }
70- .onSelect =${ ( p : Pattern | null ) => this . selectPattern ( p ) }
71- .onPurchase=${ ( p : Pattern ) => handlePurchase ( p ) }
72- > </ pattern-button >
73- ` ) ;
7488 }
7589
7690 return html `
@@ -115,19 +129,24 @@ export class TerritoryPatternsModal extends LitElement {
115129 this . modalEl ?. close ( ) ;
116130 }
117131
118- private selectPattern ( pattern : Pattern | null ) {
119- this . userSettings . setSelectedPatternName ( pattern ?. name ) ;
132+ private selectPattern ( pattern : PlayerPattern | null ) {
133+ if ( pattern === null ) {
134+ this . userSettings . setSelectedPatternName ( undefined ) ;
135+ } else {
136+ const name =
137+ pattern . colorPalette ?. name === undefined
138+ ? pattern . name
139+ : `${ pattern . name } :${ pattern . colorPalette . name } ` ;
140+
141+ this . userSettings . setSelectedPatternName ( `pattern:${ name } ` ) ;
142+ }
120143 this . selectedPattern = pattern ;
121144 this . refresh ( ) ;
122145 this . close ( ) ;
123146 }
124147
125148 public async refresh ( ) {
126- const preview = renderPatternPreview (
127- this . selectedPattern ?. pattern ?? null ,
128- 48 ,
129- 48 ,
130- ) ;
149+ const preview = renderPatternPreview ( this . selectedPattern ?? null , 48 , 48 ) ;
131150 this . requestUpdate ( ) ;
132151
133152 // Wait for the DOM to be updated and the o-modal element to be available
0 commit comments