From 9bdaca2ca9bf7e62ffe95900c4360d02114b41ea Mon Sep 17 00:00:00 2001 From: Egor Savkin Date: Fri, 8 Dec 2023 17:36:12 +0800 Subject: [PATCH] Start adding the warnings Signed-off-by: Egor Savkin --- static/js/shop/CrateList.jsx | 8 +++--- static/js/shop/Shop.jsx | 56 ++++++++++++++++++++---------------- static/js/shop/warnings.js | 33 ++++++++++++++++++++- static/js/shop_data.js | 3 +- 4 files changed, 69 insertions(+), 31 deletions(-) diff --git a/static/js/shop/CrateList.jsx b/static/js/shop/CrateList.jsx index f559519..369dae7 100644 --- a/static/js/shop/CrateList.jsx +++ b/static/js/shop/CrateList.jsx @@ -2,16 +2,16 @@ import React from 'react' import {Accordion} from "react-bootstrap"; import {Crate} from "./Crate.jsx"; -export function CrateList({crates, isMobile, isTouch, onAddCrate, onDeleteCrate, onModeChange}) { +export function CrateList({crates, active_crate, isMobile, isTouch, onAddCrate, onDeleteCrate, onModeChange, onCrateSelect}) { const onClickAdd = (_) => { onAddCrate("crate" + Object.entries(crates).length); } return ( - + {Object.entries(crates).map(([crate_id, crate], index) => - - Crate #{`${index}`} + + onCrateSelect(crate_id)}>Crate #{`${index}`} { if (!(idx in itemsCloned)) itemsCloned[idx] = elem; @@ -473,27 +477,20 @@ export class Shop extends PureComponent { itemsCloned = FillResources(itemsCloned); itemsCloned = TriggerWarnings(itemsCloned); - // check number of slot in crate - const nbrOccupied = nbrOccupiedSlotsInCrate(newItems); - if (nbrOccupied > crateModeSlots[currentMode]) { - rules[crateRules.maxSlot.type] = {...crateRules.maxSlot}; - } else if (crateModeSlots[currentMode] === 21 && nbrOccupied <= 10) { - rules[crateRules.compactSlot.type] = {...crateRules.compactSlot}; - } - // update state with rules this.setState({ ...this.state, columns: { ...this.state.columns, - cart: { - ...this.state.columns.cart, - itemsData: itemsCloned, + crates: { + ...this.state.columns.crates, + [crate_id]: { + ...this.state.columns.crates[crate_id], + items: itemsCloned, + warnings: crate_warnings + }, } - }, - rules: { - ...rules, - }, + } }); } @@ -525,6 +522,13 @@ export class Shop extends PureComponent { this.setState(new_state); } + onCrateSelected(id) { + console.log(id) + this.setState({ + active_crate: id + }) + } + onCrateModeChange(crate_id, new_mode) { let new_state = { ...this.state, @@ -604,6 +608,8 @@ export class Shop extends PureComponent { onAddCrate={this.onAddCrate} onDeleteCrate={this.onDeleteCrate} onModeChange={this.onCrateModeChange} + onCrateSelect={this.onCrateSelected} + active_crate={this.state.active_crate} /> } summaryPrice={ diff --git a/static/js/shop/warnings.js b/static/js/shop/warnings.js index 9a86157..0f21c82 100644 --- a/static/js/shop/warnings.js +++ b/static/js/shop/warnings.js @@ -5,7 +5,8 @@ * Second - resources indicator should be separate component */ -import {item_occupied_counters} from "./count_resources"; +import {crate_type_to_hp, item_occupied_counters, resource_counters} from "./count_resources"; +import {data as shared_data} from "./utils"; const Levels = { "reminder": {priority: 1, icon: '/images/shop/icon-reminder.svg'}, @@ -120,4 +121,34 @@ export function MaxLevel(warnings) { if (Levels[warning.level].priority > mx.priority) mx = Levels[warning.level]; } return mx; +} + + + +const crate_warnings = { + "overfit": { + message: "You have reached the maximum number of slots allowed for this crate. Consider removing cards.", + level: "warning", + trigger: (crate, occupied) => { + const nbrHP = crate_type_to_hp(crate.crate_type); + return occupied > nbrHP; + } + }, + "underfit_rack": { + message: "The selected cards fit in a 42hp desktop crate, consider switching to it for a more compact system", + level: "reminder", + trigger: (crate, occupied) => { + const nbrHPDesktop = shared_data.crateModes.desktop.hp; + return crate.crate_type === shared_data.crateModes.rack.id && occupied < nbrHPDesktop; + } + } +} + +export function TriggerCrateWarnings(crate) { + const nbrOccupied = resource_counters.hp(crate.items, -1); + let warnings = []; + Object.entries(crate_warnings).forEach(([id, warning], _) => { + if (warning.trigger(crate, nbrOccupied)) warnings.push({...warning, id: id, trigger: undefined}); + }) + return warnings; } \ No newline at end of file diff --git a/static/js/shop_data.js b/static/js/shop_data.js index da6c7a4..1be11d0 100644 --- a/static/js/shop_data.js +++ b/static/js/shop_data.js @@ -1188,7 +1188,8 @@ const shop_data = { "crates": { "crate0": { crate_type: "rack", - items: [] + items: [], + warnings: [] } }