@@ -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,37 @@ 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+ if (
67+ patternRelationship (
68+ pattern ,
69+ colorPalette ,
70+ this . userMeResponse ,
71+ this . affiliateCode ,
72+ ) === "blocked"
73+ ) {
6374 continue ;
6475 }
76+ buttons . push ( html `
77+ < pattern-button
78+ .pattern =${ pattern }
79+ .colorPalette =${ this . cosmetics ?. colorPalettes ?. [
80+ colorPalette ?. name ?? ""
81+ ] ?? null }
82+ .requiresPurchase=${ patternRelationship (
83+ pattern ,
84+ colorPalette ,
85+ this . userMeResponse ,
86+ this . affiliateCode ,
87+ ) === "purchasable" }
88+ .onSelect=${ ( p : PlayerPattern | null ) => this . selectPattern ( p ) }
89+ .onPurchase=${ ( p : Pattern , colorPalette : ColorPalette | null ) =>
90+ handlePurchase ( p , colorPalette ) }
91+ > </ pattern-button >
92+ ` ) ;
6593 }
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- ` ) ;
7494 }
7595
7696 return html `
@@ -90,6 +110,20 @@ export class TerritoryPatternsModal extends LitElement {
90110 </ div >
91111 ` ;
92112 }
113+ patternRelationship (
114+ pattern : {
115+ name : string ;
116+ pattern : string ;
117+ affiliateCode : string | null ;
118+ product : { productId : string ; priceId : string ; price : string } | null ;
119+ colorPalettes ?:
120+ | { name : string ; isArchived ?: boolean | undefined } [ ]
121+ | undefined ;
122+ } ,
123+ colorPalette : { name : string ; isArchived ?: boolean | undefined } | null ,
124+ ) {
125+ throw new Error ( "Method not implemented." ) ;
126+ }
93127
94128 render ( ) {
95129 if ( ! this . isActive ) return html `` ;
@@ -115,19 +149,24 @@ export class TerritoryPatternsModal extends LitElement {
115149 this . modalEl ?. close ( ) ;
116150 }
117151
118- private selectPattern ( pattern : Pattern | null ) {
119- this . userSettings . setSelectedPatternName ( pattern ?. name ) ;
152+ private selectPattern ( pattern : PlayerPattern | null ) {
153+ if ( pattern === null ) {
154+ this . userSettings . setSelectedPatternName ( undefined ) ;
155+ return ;
156+ }
157+ const name =
158+ pattern . colorPalette ?. name === undefined
159+ ? pattern . name
160+ : `${ pattern . name } :${ pattern . colorPalette . name } ` ;
161+
162+ this . userSettings . setSelectedPatternName ( `pattern:${ name } ` ) ;
120163 this . selectedPattern = pattern ;
121164 this . refresh ( ) ;
122165 this . close ( ) ;
123166 }
124167
125168 public async refresh ( ) {
126- const preview = renderPatternPreview (
127- this . selectedPattern ?. pattern ?? null ,
128- 48 ,
129- 48 ,
130- ) ;
169+ const preview = renderPatternPreview ( this . selectedPattern ?? null , 48 , 48 ) ;
131170 this . requestUpdate ( ) ;
132171
133172 // Wait for the DOM to be updated and the o-modal element to be available
0 commit comments