Compare commits
20 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| e0f369d322 | |||
| ea98774ccb | |||
| ea9dd848fd | |||
| a328059933 | |||
| 18659d19d1 | |||
| 7840ef9eb2 | |||
| 6d061d23a1 | |||
| 25f22231a6 | |||
| c66ff26d1d | |||
| cd792b20b2 | |||
| 73c7f50f74 | |||
| 83c40116af | |||
| 347d5a4b4f | |||
| 93f2ceaabe | |||
| e390b72222 | |||
| 3650788dc5 | |||
| 39cf8dcd6c | |||
| 456b4d42e3 | |||
| e1c8ae0743 | |||
| 8f86d66ffe |
@@ -0,0 +1,7 @@
|
|||||||
|
# Claude Flow runtime files
|
||||||
|
data/
|
||||||
|
logs/
|
||||||
|
sessions/
|
||||||
|
neural/
|
||||||
|
*.log
|
||||||
|
*.tmp
|
||||||
@@ -0,0 +1,403 @@
|
|||||||
|
# RuFlo V3 - Complete Capabilities Reference
|
||||||
|
> Generated: 2026-05-19T00:18:20.864Z
|
||||||
|
> Full documentation: https://github.com/ruvnet/claude-flow
|
||||||
|
|
||||||
|
## 📋 Table of Contents
|
||||||
|
|
||||||
|
1. [Overview](#overview)
|
||||||
|
2. [Swarm Orchestration](#swarm-orchestration)
|
||||||
|
3. [Available Agents (60+)](#available-agents)
|
||||||
|
4. [CLI Commands (26 Commands, 140+ Subcommands)](#cli-commands)
|
||||||
|
5. [Hooks System (27 Hooks + 12 Workers)](#hooks-system)
|
||||||
|
6. [Memory & Intelligence (RuVector)](#memory--intelligence)
|
||||||
|
7. [Hive-Mind Consensus](#hive-mind-consensus)
|
||||||
|
8. [Performance Targets](#performance-targets)
|
||||||
|
9. [Integration Ecosystem](#integration-ecosystem)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Overview
|
||||||
|
|
||||||
|
RuFlo V3 is a domain-driven design architecture for multi-agent AI coordination with:
|
||||||
|
|
||||||
|
- **15-Agent Swarm Coordination** with hierarchical and mesh topologies
|
||||||
|
- **HNSW Vector Search** - 150x-12,500x faster pattern retrieval
|
||||||
|
- **SONA Neural Learning** - Self-optimizing with <0.05ms adaptation
|
||||||
|
- **Byzantine Fault Tolerance** - Queen-led consensus mechanisms
|
||||||
|
- **MCP Server Integration** - Model Context Protocol support
|
||||||
|
|
||||||
|
### Current Configuration
|
||||||
|
| Setting | Value |
|
||||||
|
|---------|-------|
|
||||||
|
| Topology | hierarchical-mesh |
|
||||||
|
| Max Agents | 15 |
|
||||||
|
| Memory Backend | hybrid |
|
||||||
|
| HNSW Indexing | Enabled |
|
||||||
|
| Neural Learning | Enabled |
|
||||||
|
| LearningBridge | Enabled (SONA + ReasoningBank) |
|
||||||
|
| Knowledge Graph | Enabled (PageRank + Communities) |
|
||||||
|
| Agent Scopes | Enabled (project/local/user) |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Swarm Orchestration
|
||||||
|
|
||||||
|
### Topologies
|
||||||
|
| Topology | Description | Best For |
|
||||||
|
|----------|-------------|----------|
|
||||||
|
| `hierarchical` | Queen controls workers directly | Anti-drift, tight control |
|
||||||
|
| `mesh` | Fully connected peer network | Distributed tasks |
|
||||||
|
| `hierarchical-mesh` | V3 hybrid (recommended) | 10+ agents |
|
||||||
|
| `ring` | Circular communication | Sequential workflows |
|
||||||
|
| `star` | Central coordinator | Simple coordination |
|
||||||
|
| `adaptive` | Dynamic based on load | Variable workloads |
|
||||||
|
|
||||||
|
### Strategies
|
||||||
|
- `balanced` - Even distribution across agents
|
||||||
|
- `specialized` - Clear roles, no overlap (anti-drift)
|
||||||
|
- `adaptive` - Dynamic task routing
|
||||||
|
|
||||||
|
### Quick Commands
|
||||||
|
```bash
|
||||||
|
# Initialize swarm
|
||||||
|
npx @claude-flow/cli@latest swarm init --topology hierarchical --max-agents 8 --strategy specialized
|
||||||
|
|
||||||
|
# Check status
|
||||||
|
npx @claude-flow/cli@latest swarm status
|
||||||
|
|
||||||
|
# Monitor activity
|
||||||
|
npx @claude-flow/cli@latest swarm monitor
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Available Agents
|
||||||
|
|
||||||
|
### Core Development (5)
|
||||||
|
`coder`, `reviewer`, `tester`, `planner`, `researcher`
|
||||||
|
|
||||||
|
### V3 Specialized (4)
|
||||||
|
`security-architect`, `security-auditor`, `memory-specialist`, `performance-engineer`
|
||||||
|
|
||||||
|
### Swarm Coordination (5)
|
||||||
|
`hierarchical-coordinator`, `mesh-coordinator`, `adaptive-coordinator`, `collective-intelligence-coordinator`, `swarm-memory-manager`
|
||||||
|
|
||||||
|
### Consensus & Distributed (7)
|
||||||
|
`byzantine-coordinator`, `raft-manager`, `gossip-coordinator`, `consensus-builder`, `crdt-synchronizer`, `quorum-manager`, `security-manager`
|
||||||
|
|
||||||
|
### Performance & Optimization (5)
|
||||||
|
`perf-analyzer`, `performance-benchmarker`, `task-orchestrator`, `memory-coordinator`, `smart-agent`
|
||||||
|
|
||||||
|
### GitHub & Repository (9)
|
||||||
|
`github-modes`, `pr-manager`, `code-review-swarm`, `issue-tracker`, `release-manager`, `workflow-automation`, `project-board-sync`, `repo-architect`, `multi-repo-swarm`
|
||||||
|
|
||||||
|
### SPARC Methodology (6)
|
||||||
|
`sparc-coord`, `sparc-coder`, `specification`, `pseudocode`, `architecture`, `refinement`
|
||||||
|
|
||||||
|
### Specialized Development (8)
|
||||||
|
`backend-dev`, `mobile-dev`, `ml-developer`, `cicd-engineer`, `api-docs`, `system-architect`, `code-analyzer`, `base-template-generator`
|
||||||
|
|
||||||
|
### Testing & Validation (2)
|
||||||
|
`tdd-london-swarm`, `production-validator`
|
||||||
|
|
||||||
|
### Agent Routing by Task
|
||||||
|
| Task Type | Recommended Agents | Topology |
|
||||||
|
|-----------|-------------------|----------|
|
||||||
|
| Bug Fix | researcher, coder, tester | mesh |
|
||||||
|
| New Feature | coordinator, architect, coder, tester, reviewer | hierarchical |
|
||||||
|
| Refactoring | architect, coder, reviewer | mesh |
|
||||||
|
| Performance | researcher, perf-engineer, coder | hierarchical |
|
||||||
|
| Security | security-architect, auditor, reviewer | hierarchical |
|
||||||
|
| Docs | researcher, api-docs | mesh |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## CLI Commands
|
||||||
|
|
||||||
|
### Core Commands (12)
|
||||||
|
| Command | Subcommands | Description |
|
||||||
|
|---------|-------------|-------------|
|
||||||
|
| `init` | 4 | Project initialization |
|
||||||
|
| `agent` | 8 | Agent lifecycle management |
|
||||||
|
| `swarm` | 6 | Multi-agent coordination |
|
||||||
|
| `memory` | 11 | AgentDB with HNSW search |
|
||||||
|
| `mcp` | 9 | MCP server management |
|
||||||
|
| `task` | 6 | Task assignment |
|
||||||
|
| `session` | 7 | Session persistence |
|
||||||
|
| `config` | 7 | Configuration |
|
||||||
|
| `status` | 3 | System monitoring |
|
||||||
|
| `workflow` | 6 | Workflow templates |
|
||||||
|
| `hooks` | 17 | Self-learning hooks |
|
||||||
|
| `hive-mind` | 6 | Consensus coordination |
|
||||||
|
|
||||||
|
### Advanced Commands (14)
|
||||||
|
| Command | Subcommands | Description |
|
||||||
|
|---------|-------------|-------------|
|
||||||
|
| `daemon` | 5 | Background workers |
|
||||||
|
| `neural` | 5 | Pattern training |
|
||||||
|
| `security` | 6 | Security scanning |
|
||||||
|
| `performance` | 5 | Profiling & benchmarks |
|
||||||
|
| `providers` | 5 | AI provider config |
|
||||||
|
| `plugins` | 5 | Plugin management |
|
||||||
|
| `deployment` | 5 | Deploy management |
|
||||||
|
| `embeddings` | 4 | Vector embeddings |
|
||||||
|
| `claims` | 4 | Authorization |
|
||||||
|
| `migrate` | 5 | V2→V3 migration |
|
||||||
|
| `process` | 4 | Process management |
|
||||||
|
| `doctor` | 1 | Health diagnostics |
|
||||||
|
| `completions` | 4 | Shell completions |
|
||||||
|
|
||||||
|
### Example Commands
|
||||||
|
```bash
|
||||||
|
# Initialize
|
||||||
|
npx @claude-flow/cli@latest init --wizard
|
||||||
|
|
||||||
|
# Spawn agent
|
||||||
|
npx @claude-flow/cli@latest agent spawn -t coder --name my-coder
|
||||||
|
|
||||||
|
# Memory operations
|
||||||
|
npx @claude-flow/cli@latest memory store --key "pattern" --value "data" --namespace patterns
|
||||||
|
npx @claude-flow/cli@latest memory search --query "authentication"
|
||||||
|
|
||||||
|
# Diagnostics
|
||||||
|
npx @claude-flow/cli@latest doctor --fix
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Hooks System
|
||||||
|
|
||||||
|
### 27 Available Hooks
|
||||||
|
|
||||||
|
#### Core Hooks (6)
|
||||||
|
| Hook | Description |
|
||||||
|
|------|-------------|
|
||||||
|
| `pre-edit` | Context before file edits |
|
||||||
|
| `post-edit` | Record edit outcomes |
|
||||||
|
| `pre-command` | Risk assessment |
|
||||||
|
| `post-command` | Command metrics |
|
||||||
|
| `pre-task` | Task start + agent suggestions |
|
||||||
|
| `post-task` | Task completion learning |
|
||||||
|
|
||||||
|
#### Session Hooks (4)
|
||||||
|
| Hook | Description |
|
||||||
|
|------|-------------|
|
||||||
|
| `session-start` | Start/restore session |
|
||||||
|
| `session-end` | Persist state |
|
||||||
|
| `session-restore` | Restore previous |
|
||||||
|
| `notify` | Cross-agent notifications |
|
||||||
|
|
||||||
|
#### Intelligence Hooks (5)
|
||||||
|
| Hook | Description |
|
||||||
|
|------|-------------|
|
||||||
|
| `route` | Optimal agent routing |
|
||||||
|
| `explain` | Routing decisions |
|
||||||
|
| `pretrain` | Bootstrap intelligence |
|
||||||
|
| `build-agents` | Generate configs |
|
||||||
|
| `transfer` | Pattern transfer |
|
||||||
|
|
||||||
|
#### Coverage Hooks (3)
|
||||||
|
| Hook | Description |
|
||||||
|
|------|-------------|
|
||||||
|
| `coverage-route` | Coverage-based routing |
|
||||||
|
| `coverage-suggest` | Improvement suggestions |
|
||||||
|
| `coverage-gaps` | Gap analysis |
|
||||||
|
|
||||||
|
### 12 Background Workers
|
||||||
|
| Worker | Priority | Purpose |
|
||||||
|
|--------|----------|---------|
|
||||||
|
| `ultralearn` | normal | Deep knowledge |
|
||||||
|
| `optimize` | high | Performance |
|
||||||
|
| `consolidate` | low | Memory consolidation |
|
||||||
|
| `predict` | normal | Predictive preload |
|
||||||
|
| `audit` | critical | Security |
|
||||||
|
| `map` | normal | Codebase mapping |
|
||||||
|
| `preload` | low | Resource preload |
|
||||||
|
| `deepdive` | normal | Deep analysis |
|
||||||
|
| `document` | normal | Auto-docs |
|
||||||
|
| `refactor` | normal | Suggestions |
|
||||||
|
| `benchmark` | normal | Benchmarking |
|
||||||
|
| `testgaps` | normal | Coverage gaps |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Memory & Intelligence
|
||||||
|
|
||||||
|
### RuVector Intelligence System
|
||||||
|
- **SONA**: Self-Optimizing Neural Architecture (<0.05ms)
|
||||||
|
- **MoE**: Mixture of Experts routing
|
||||||
|
- **HNSW**: 150x-12,500x faster search
|
||||||
|
- **EWC++**: Prevents catastrophic forgetting
|
||||||
|
- **Flash Attention**: 2.49x-7.47x speedup
|
||||||
|
- **Int8 Quantization**: 3.92x memory reduction
|
||||||
|
|
||||||
|
### 4-Step Intelligence Pipeline
|
||||||
|
1. **RETRIEVE** - HNSW pattern search
|
||||||
|
2. **JUDGE** - Success/failure verdicts
|
||||||
|
3. **DISTILL** - LoRA learning extraction
|
||||||
|
4. **CONSOLIDATE** - EWC++ preservation
|
||||||
|
|
||||||
|
### Self-Learning Memory (ADR-049)
|
||||||
|
|
||||||
|
| Component | Status | Description |
|
||||||
|
|-----------|--------|-------------|
|
||||||
|
| **LearningBridge** | ✅ Enabled | Connects insights to SONA/ReasoningBank neural pipeline |
|
||||||
|
| **MemoryGraph** | ✅ Enabled | PageRank knowledge graph + community detection |
|
||||||
|
| **AgentMemoryScope** | ✅ Enabled | 3-scope agent memory (project/local/user) |
|
||||||
|
|
||||||
|
**LearningBridge** - Insights trigger learning trajectories. Confidence evolves: +0.03 on access, -0.005/hour decay. Consolidation runs the JUDGE/DISTILL/CONSOLIDATE pipeline.
|
||||||
|
|
||||||
|
**MemoryGraph** - Builds a knowledge graph from entry references. PageRank identifies influential insights. Communities group related knowledge. Graph-aware ranking blends vector + structural scores.
|
||||||
|
|
||||||
|
**AgentMemoryScope** - Maps Claude Code 3-scope directories:
|
||||||
|
- `project`: `<gitRoot>/.claude/agent-memory/<agent>/`
|
||||||
|
- `local`: `<gitRoot>/.claude/agent-memory-local/<agent>/`
|
||||||
|
- `user`: `~/.claude/agent-memory/<agent>/`
|
||||||
|
|
||||||
|
High-confidence insights (>0.8) can transfer between agents.
|
||||||
|
|
||||||
|
### Memory Commands
|
||||||
|
```bash
|
||||||
|
# Store pattern
|
||||||
|
npx @claude-flow/cli@latest memory store --key "name" --value "data" --namespace patterns
|
||||||
|
|
||||||
|
# Semantic search
|
||||||
|
npx @claude-flow/cli@latest memory search --query "authentication"
|
||||||
|
|
||||||
|
# List entries
|
||||||
|
npx @claude-flow/cli@latest memory list --namespace patterns
|
||||||
|
|
||||||
|
# Initialize database
|
||||||
|
npx @claude-flow/cli@latest memory init --force
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Hive-Mind Consensus
|
||||||
|
|
||||||
|
### Queen Types
|
||||||
|
| Type | Role |
|
||||||
|
|------|------|
|
||||||
|
| Strategic Queen | Long-term planning |
|
||||||
|
| Tactical Queen | Execution coordination |
|
||||||
|
| Adaptive Queen | Dynamic optimization |
|
||||||
|
|
||||||
|
### Worker Types (8)
|
||||||
|
`researcher`, `coder`, `analyst`, `tester`, `architect`, `reviewer`, `optimizer`, `documenter`
|
||||||
|
|
||||||
|
### Consensus Mechanisms
|
||||||
|
| Mechanism | Fault Tolerance | Use Case |
|
||||||
|
|-----------|-----------------|----------|
|
||||||
|
| `byzantine` | f < n/3 faulty | Adversarial |
|
||||||
|
| `raft` | f < n/2 failed | Leader-based |
|
||||||
|
| `gossip` | Eventually consistent | Large scale |
|
||||||
|
| `crdt` | Conflict-free | Distributed |
|
||||||
|
| `quorum` | Configurable | Flexible |
|
||||||
|
|
||||||
|
### Hive-Mind Commands
|
||||||
|
```bash
|
||||||
|
# Initialize
|
||||||
|
npx @claude-flow/cli@latest hive-mind init --queen-type strategic
|
||||||
|
|
||||||
|
# Status
|
||||||
|
npx @claude-flow/cli@latest hive-mind status
|
||||||
|
|
||||||
|
# Spawn workers
|
||||||
|
npx @claude-flow/cli@latest hive-mind spawn --count 5 --type worker
|
||||||
|
|
||||||
|
# Consensus
|
||||||
|
npx @claude-flow/cli@latest hive-mind consensus --propose "task"
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Performance Targets
|
||||||
|
|
||||||
|
| Metric | Target | Status |
|
||||||
|
|--------|--------|--------|
|
||||||
|
| HNSW Search | 150x-12,500x faster | ✅ Implemented |
|
||||||
|
| Memory Reduction | 50-75% | ✅ Implemented (3.92x) |
|
||||||
|
| SONA Integration | Pattern learning | ✅ Implemented |
|
||||||
|
| Flash Attention | 2.49x-7.47x | 🔄 In Progress |
|
||||||
|
| MCP Response | <100ms | ✅ Achieved |
|
||||||
|
| CLI Startup | <500ms | ✅ Achieved |
|
||||||
|
| SONA Adaptation | <0.05ms | 🔄 In Progress |
|
||||||
|
| Graph Build (1k) | <200ms | ✅ 2.78ms (71.9x headroom) |
|
||||||
|
| PageRank (1k) | <100ms | ✅ 12.21ms (8.2x headroom) |
|
||||||
|
| Insight Recording | <5ms/each | ✅ 0.12ms (41x headroom) |
|
||||||
|
| Consolidation | <500ms | ✅ 0.26ms (1,955x headroom) |
|
||||||
|
| Knowledge Transfer | <100ms | ✅ 1.25ms (80x headroom) |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Integration Ecosystem
|
||||||
|
|
||||||
|
### Integrated Packages
|
||||||
|
| Package | Version | Purpose |
|
||||||
|
|---------|---------|---------|
|
||||||
|
| agentic-flow | 3.0.0-alpha.1 | Core coordination + ReasoningBank + Router |
|
||||||
|
| agentdb | 3.0.0-alpha.10 | Vector database + 8 controllers |
|
||||||
|
| @ruvector/attention | 0.1.3 | Flash attention |
|
||||||
|
| @ruvector/sona | 0.1.5 | Neural learning |
|
||||||
|
|
||||||
|
### Optional Integrations
|
||||||
|
| Package | Command |
|
||||||
|
|---------|---------|
|
||||||
|
| ruv-swarm | `npx ruv-swarm mcp start` |
|
||||||
|
| flow-nexus | `npx flow-nexus@latest mcp start` |
|
||||||
|
| agentic-jujutsu | `npx agentic-jujutsu@latest` |
|
||||||
|
|
||||||
|
### MCP Server Setup
|
||||||
|
```bash
|
||||||
|
# Add Ruflo MCP
|
||||||
|
claude mcp add ruflo -- npx -y ruflo@latest
|
||||||
|
|
||||||
|
# Optional servers
|
||||||
|
claude mcp add ruv-swarm -- npx -y ruv-swarm mcp start
|
||||||
|
claude mcp add flow-nexus -- npx -y flow-nexus@latest mcp start
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Quick Reference
|
||||||
|
|
||||||
|
### Essential Commands
|
||||||
|
```bash
|
||||||
|
# Setup
|
||||||
|
npx ruflo@latest init --wizard
|
||||||
|
npx ruflo@latest daemon start
|
||||||
|
npx ruflo@latest doctor --fix
|
||||||
|
|
||||||
|
# Swarm
|
||||||
|
npx ruflo@latest swarm init --topology hierarchical --max-agents 8
|
||||||
|
npx ruflo@latest swarm status
|
||||||
|
|
||||||
|
# Agents
|
||||||
|
npx ruflo@latest agent spawn -t coder
|
||||||
|
npx ruflo@latest agent list
|
||||||
|
|
||||||
|
# Memory
|
||||||
|
npx ruflo@latest memory search --query "patterns"
|
||||||
|
|
||||||
|
# Hooks
|
||||||
|
npx ruflo@latest hooks pre-task --description "task"
|
||||||
|
npx ruflo@latest hooks worker dispatch --trigger optimize
|
||||||
|
```
|
||||||
|
|
||||||
|
### File Structure
|
||||||
|
```
|
||||||
|
.claude-flow/
|
||||||
|
├── config.yaml # Runtime configuration
|
||||||
|
├── CAPABILITIES.md # This file
|
||||||
|
├── data/ # Memory storage
|
||||||
|
├── logs/ # Operation logs
|
||||||
|
├── sessions/ # Session state
|
||||||
|
├── hooks/ # Custom hooks
|
||||||
|
├── agents/ # Agent configs
|
||||||
|
└── workflows/ # Workflow templates
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
**Full Documentation**: https://github.com/ruvnet/claude-flow
|
||||||
|
**Issues**: https://github.com/ruvnet/claude-flow/issues
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
# RuFlo V3 Runtime Configuration
|
||||||
|
# Generated: 2026-05-19T00:18:20.863Z
|
||||||
|
|
||||||
|
version: "3.0.0"
|
||||||
|
|
||||||
|
swarm:
|
||||||
|
topology: hierarchical-mesh
|
||||||
|
maxAgents: 15
|
||||||
|
autoScale: true
|
||||||
|
coordinationStrategy: consensus
|
||||||
|
|
||||||
|
memory:
|
||||||
|
backend: hybrid
|
||||||
|
enableHNSW: true
|
||||||
|
persistPath: .claude-flow/data
|
||||||
|
cacheSize: 100
|
||||||
|
# ADR-049: Self-Learning Memory
|
||||||
|
learningBridge:
|
||||||
|
enabled: true
|
||||||
|
sonaMode: balanced
|
||||||
|
confidenceDecayRate: 0.005
|
||||||
|
accessBoostAmount: 0.03
|
||||||
|
consolidationThreshold: 10
|
||||||
|
memoryGraph:
|
||||||
|
enabled: true
|
||||||
|
pageRankDamping: 0.85
|
||||||
|
maxNodes: 5000
|
||||||
|
similarityThreshold: 0.8
|
||||||
|
agentScopes:
|
||||||
|
enabled: true
|
||||||
|
defaultScope: project
|
||||||
|
|
||||||
|
neural:
|
||||||
|
enabled: true
|
||||||
|
modelPath: .claude-flow/neural
|
||||||
|
|
||||||
|
hooks:
|
||||||
|
enabled: true
|
||||||
|
autoExecute: true
|
||||||
|
|
||||||
|
mcp:
|
||||||
|
autoStart: false
|
||||||
|
port: 3000
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
{
|
||||||
|
"initialized": "2026-05-19T00:18:20.864Z",
|
||||||
|
"routing": {
|
||||||
|
"accuracy": 0,
|
||||||
|
"decisions": 0
|
||||||
|
},
|
||||||
|
"patterns": {
|
||||||
|
"shortTerm": 0,
|
||||||
|
"longTerm": 0,
|
||||||
|
"quality": 0
|
||||||
|
},
|
||||||
|
"sessions": {
|
||||||
|
"total": 0,
|
||||||
|
"current": null
|
||||||
|
},
|
||||||
|
"_note": "Intelligence grows as you use Ruflo"
|
||||||
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
{
|
||||||
|
"timestamp": "2026-05-19T00:18:20.864Z",
|
||||||
|
"processes": {
|
||||||
|
"agentic_flow": 0,
|
||||||
|
"mcp_server": 0,
|
||||||
|
"estimated_agents": 0
|
||||||
|
},
|
||||||
|
"swarm": {
|
||||||
|
"active": false,
|
||||||
|
"agent_count": 0,
|
||||||
|
"coordination_active": false
|
||||||
|
},
|
||||||
|
"integration": {
|
||||||
|
"agentic_flow_active": false,
|
||||||
|
"mcp_active": false
|
||||||
|
},
|
||||||
|
"_initialized": true
|
||||||
|
}
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
{
|
||||||
|
"version": "3.0.0",
|
||||||
|
"initialized": "2026-05-19T00:18:20.864Z",
|
||||||
|
"domains": {
|
||||||
|
"completed": 0,
|
||||||
|
"total": 5,
|
||||||
|
"status": "INITIALIZING"
|
||||||
|
},
|
||||||
|
"ddd": {
|
||||||
|
"progress": 0,
|
||||||
|
"modules": 0,
|
||||||
|
"totalFiles": 0,
|
||||||
|
"totalLines": 0
|
||||||
|
},
|
||||||
|
"swarm": {
|
||||||
|
"activeAgents": 0,
|
||||||
|
"maxAgents": 15,
|
||||||
|
"topology": "hierarchical-mesh"
|
||||||
|
},
|
||||||
|
"learning": {
|
||||||
|
"status": "READY",
|
||||||
|
"patternsLearned": 0,
|
||||||
|
"sessionsCompleted": 0
|
||||||
|
},
|
||||||
|
"_note": "Metrics will update as you use Ruflo. Run: npx ruflo@latest daemon start"
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
{
|
||||||
|
"initialized": "2026-05-19T00:18:20.864Z",
|
||||||
|
"status": "PENDING",
|
||||||
|
"cvesFixed": 0,
|
||||||
|
"totalCves": 3,
|
||||||
|
"lastScan": null,
|
||||||
|
"_note": "Run: npx @claude-flow/cli@latest security scan"
|
||||||
|
}
|
||||||
@@ -4,6 +4,12 @@ on:
|
|||||||
push:
|
push:
|
||||||
tags:
|
tags:
|
||||||
- 'v*'
|
- 'v*'
|
||||||
|
workflow_dispatch:
|
||||||
|
inputs:
|
||||||
|
tag:
|
||||||
|
description: 'Release tag (e.g. v0.36.2)'
|
||||||
|
required: true
|
||||||
|
default: 'v0.36.2'
|
||||||
|
|
||||||
env:
|
env:
|
||||||
APK_OUT: target/release/apk/ferrous-solitaire.apk
|
APK_OUT: target/release/apk/ferrous-solitaire.apk
|
||||||
@@ -42,7 +48,12 @@ jobs:
|
|||||||
|
|
||||||
- name: Get tag name
|
- name: Get tag name
|
||||||
id: tag
|
id: tag
|
||||||
run: echo "name=${GITHUB_REF#refs/tags/}" >> "$GITHUB_OUTPUT"
|
run: |
|
||||||
|
if [ -n "${{ github.event.inputs.tag }}" ]; then
|
||||||
|
echo "name=${{ github.event.inputs.tag }}" >> "$GITHUB_OUTPUT"
|
||||||
|
else
|
||||||
|
echo "name=${GITHUB_REF#refs/tags/}" >> "$GITHUB_OUTPUT"
|
||||||
|
fi
|
||||||
|
|
||||||
- name: Decode release keystore
|
- name: Decode release keystore
|
||||||
run: echo "${{ secrets.RELEASE_KEYSTORE_B64 }}" | base64 -d > release.jks
|
run: echo "${{ secrets.RELEASE_KEYSTORE_B64 }}" | base64 -d > release.jks
|
||||||
|
|||||||
@@ -8,6 +8,10 @@
|
|||||||
data/
|
data/
|
||||||
.claude/
|
.claude/
|
||||||
|
|
||||||
|
# ruflo runtime state
|
||||||
|
agentdb.rvf
|
||||||
|
agentdb.rvf.lock
|
||||||
|
|
||||||
# IDE project files
|
# IDE project files
|
||||||
.idea/
|
.idea/
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,22 @@
|
|||||||
|
{
|
||||||
|
"mcpServers": {
|
||||||
|
"ruflo": {
|
||||||
|
"command": "npx",
|
||||||
|
"args": [
|
||||||
|
"-y",
|
||||||
|
"ruflo@latest",
|
||||||
|
"mcp",
|
||||||
|
"start"
|
||||||
|
],
|
||||||
|
"env": {
|
||||||
|
"npm_config_update_notifier": "false",
|
||||||
|
"CLAUDE_FLOW_MODE": "v3",
|
||||||
|
"CLAUDE_FLOW_HOOKS_ENABLED": "true",
|
||||||
|
"CLAUDE_FLOW_TOPOLOGY": "hierarchical-mesh",
|
||||||
|
"CLAUDE_FLOW_MAX_AGENTS": "15",
|
||||||
|
"CLAUDE_FLOW_MEMORY_BACKEND": "hybrid"
|
||||||
|
},
|
||||||
|
"autoStart": false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,130 @@
|
|||||||
|
# Ferrous Solitaire — Session Handoff
|
||||||
|
|
||||||
|
**Last updated:** 2026-05-18 — Three leaderboard bugs fixed, tagged v0.35.1. All commits on origin/master.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Current state
|
||||||
|
|
||||||
|
- **HEAD on origin/master:** `8f86d66` (fix: three leaderboard bugs)
|
||||||
|
- **Latest tag:** `v0.35.1`
|
||||||
|
- **Working tree:** clean
|
||||||
|
- **Build:** `cargo clippy --workspace -- -D warnings` clean
|
||||||
|
- **Tests:** 1277 passing / 0 failing across the workspace
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## What shipped since the last handoff (v0.23.0 → v0.35.1)
|
||||||
|
|
||||||
|
### v0.34.0 — Android polish + code-quality sweep (2026-05-16/17)
|
||||||
|
|
||||||
|
| Commit | Summary |
|
||||||
|
|--------|---------|
|
||||||
|
| `9623bde` | Wire FiraMono to Android corner label; CardImageSet load tests |
|
||||||
|
| `980312c` | Fix wrong bottom-right suit symbol on JS/QS/KS card assets |
|
||||||
|
| `04e99a8` | Correct Android waste fan overlap and resume layout desync |
|
||||||
|
| `3bb3ddb` | Eliminate panics, fix dismiss hit-test scope, guard home respawn |
|
||||||
|
| `f8f1f26` | Adaptive drop zones, touch event correctness, modal lifecycle guards |
|
||||||
|
| `1eb4043` | Auth-guard avatar serving; atomic write; user_id assertion in merge |
|
||||||
|
| `69c6e88` | Deterministic pile serialization, undo skip, url-encode bytes, merge_at |
|
||||||
|
| `aa7b0f6` | Gate frame-hot ECS systems on resource changes (perf) |
|
||||||
|
| `6727126` | Consolidate APP_DIR_NAME; add `#[must_use]` on pure fns |
|
||||||
|
| `a4dfb0c` | Differentiate leaderboard opt-in vs opt-out error toasts (M-12) |
|
||||||
|
| `7fc98f8` | WASM: state() and step() return Result, errors throw JS exceptions (CR-6) |
|
||||||
|
| `ffed6b2` | Share Tokio runtime across all network tasks (M-16) |
|
||||||
|
| `fa84152` | Correct Android help hint label `→` to `!` (M-17) |
|
||||||
|
| `18d7937` | Derive Copy for DrawMode; drop redundant .clone() calls (M-18) |
|
||||||
|
| `132fea9` | Use saturating_add for move_count increments (M-19) |
|
||||||
|
| `0ecc1a9` | Add missing derives to AchievementContext (M-20) |
|
||||||
|
| `2301cc6` | Align android_keystore temp extension with cleanup glob (M-21) |
|
||||||
|
| `2e52f54` | Enforce 32-char display_name limit at sync client boundary (M-22) |
|
||||||
|
| `c8878d6` | Fix stale FOCUS_RING colour comment (M-23) |
|
||||||
|
| `4aafc0a` | Name HUD popover Z-layers; replace raw Z arithmetic (M-24) |
|
||||||
|
|
||||||
|
### v0.35.0 — Accessibility + sync reliability (2026-05-18)
|
||||||
|
|
||||||
|
| Commit | Summary |
|
||||||
|
|--------|---------|
|
||||||
|
| `eb6c93f` | Silence B0004 by adding Transform to ModalScrim |
|
||||||
|
| `6f5cebd` | Fire WarningToastEvent on sync pull failure (was InfoToastEvent) |
|
||||||
|
| `87aec5b` | Gate all decorative motion animations under `reduce_motion_mode` |
|
||||||
|
|
||||||
|
`reduce_motion_mode` now gates: score pulse, score floater, streak flourish
|
||||||
|
(hud_plugin), card-shake on rejected move, foundation completion flourish
|
||||||
|
(feedback_anim_plugin). Pattern: gate at the trigger/start system, never at
|
||||||
|
the tick system — if the component isn't inserted, the tick path never runs.
|
||||||
|
|
||||||
|
### v0.35.1 — Leaderboard bug fixes (2026-05-18)
|
||||||
|
|
||||||
|
| Commit | Summary |
|
||||||
|
|--------|---------|
|
||||||
|
| `8f86d66` | Fix three leaderboard bugs: wrong toast type, stale label, name not synced |
|
||||||
|
|
||||||
|
Three bugs fixed:
|
||||||
|
|
||||||
|
1. **Wrong toast type on error** — `poll_opt_in_task` / `poll_opt_out_task` error
|
||||||
|
branches now fire `WarningToastEvent` instead of `InfoToastEvent`.
|
||||||
|
|
||||||
|
2. **Display name not pushed to server on change** — `Settings` gains
|
||||||
|
`leaderboard_opted_in: bool` (serde-defaulted `false`). Set `true`/`false` when
|
||||||
|
opt-in/out tasks succeed and persisted to `settings.json`. `handle_display_name_confirm`
|
||||||
|
now spawns an `opt_in_leaderboard` task when already opted in — the server's upsert
|
||||||
|
endpoint updates only `display_name` without re-opting-in.
|
||||||
|
|
||||||
|
3. **"Public name" label stale after name change** — `LeaderboardPublicNameText` marker
|
||||||
|
component added to the label node. `update_leaderboard_public_name_label` system
|
||||||
|
rewrites the text each frame the panel is open; O(0) cost when panel is closed.
|
||||||
|
|
||||||
|
5 new regression tests cover all three bugs.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Open punch list
|
||||||
|
|
||||||
|
### 1. CHANGELOG documentation debt
|
||||||
|
|
||||||
|
CHANGELOG.md currently ends at v0.33.0. Entries for v0.34.0, v0.35.0, and v0.35.1
|
||||||
|
are missing. Low priority (git log is authoritative) but worth closing before the
|
||||||
|
next release.
|
||||||
|
|
||||||
|
### 2. Android APK launch verification (Option A)
|
||||||
|
|
||||||
|
Physical device test: install the latest APK on a real Android device (not AVD),
|
||||||
|
confirm:
|
||||||
|
- App launches without crash
|
||||||
|
- Safe area insets arrive and shift HUD correctly after ~3 frames
|
||||||
|
- All modal Done buttons are above the gesture bar
|
||||||
|
- Drag-and-drop works on all pile types
|
||||||
|
- Leaderboard panel opens and the "Public name" label updates correctly after
|
||||||
|
using "Set Name"
|
||||||
|
|
||||||
|
This has never been gated in CI. AVD `adb shell input tap` doesn't deliver real
|
||||||
|
touch events, so physical-device smoke testing is the only gate.
|
||||||
|
|
||||||
|
### 3. Matomo analytics wiring
|
||||||
|
|
||||||
|
`Settings` has `analytics_enabled: bool` and `matomo_url: Option<String>` but no
|
||||||
|
engine code consumes them — the analytics toggle in Settings is a no-op. If
|
||||||
|
analytics are ever needed, the Matomo HTTP Tracking API client needs to be written
|
||||||
|
and wired to `GameStateResource` events.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Architectural notes for next session
|
||||||
|
|
||||||
|
- **Reduce-motion pattern:** always gate in the `start_*` / `detect_*` system
|
||||||
|
(the trigger), not the `tick_*` system. If the component is never inserted, the
|
||||||
|
tick path never runs. See `hud_plugin.rs::detect_score_change` and
|
||||||
|
`feedback_anim_plugin.rs::start_shake_anim` for the canonical pattern.
|
||||||
|
|
||||||
|
- **Leaderboard server upsert:** `POST /api/leaderboard/opt-in` is idempotent —
|
||||||
|
calling it when already opted in just updates `display_name`. Safe to call from
|
||||||
|
`handle_display_name_confirm` without tracking a separate "needs update" flag.
|
||||||
|
|
||||||
|
- **`Messages<T>` API (Bevy 0.18.1):** write with
|
||||||
|
`resource_mut::<Messages<T>>().write(value)`; read in tests with
|
||||||
|
`msgs.get_cursor()` + `cursor.read(msgs).next()`.
|
||||||
|
|
||||||
|
- **Test input-state pitfall:** `MinimalPlugins` has no input-tick system, so
|
||||||
|
`ButtonInput::just_pressed` state persists across frames unless explicitly cleared
|
||||||
|
with `input.release(key); input.clear()` between updates.
|
||||||
@@ -20,4 +20,4 @@ resources:
|
|||||||
images:
|
images:
|
||||||
- name: solitaire-server
|
- name: solitaire-server
|
||||||
newName: git.aleshym.co/funman300/solitaire-server
|
newName: git.aleshym.co/funman300/solitaire-server
|
||||||
newTag: eb6c93fb
|
newTag: ea9dd848
|
||||||
|
|||||||
+88
-33
@@ -10,6 +10,9 @@ pub enum Suit {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Suit {
|
impl Suit {
|
||||||
|
/// All four suits in declaration order.
|
||||||
|
pub const SUITS: [Self; 4] = [Self::Clubs, Self::Diamonds, Self::Hearts, Self::Spades];
|
||||||
|
|
||||||
/// Returns `true` for red suits (Diamonds, Hearts).
|
/// Returns `true` for red suits (Diamonds, Hearts).
|
||||||
pub fn is_red(self) -> bool {
|
pub fn is_red(self) -> bool {
|
||||||
matches!(self, Suit::Diamonds | Suit::Hearts)
|
matches!(self, Suit::Diamonds | Suit::Hearts)
|
||||||
@@ -24,38 +27,63 @@ impl Suit {
|
|||||||
/// Card rank, Ace through King.
|
/// Card rank, Ace through King.
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
||||||
pub enum Rank {
|
pub enum Rank {
|
||||||
Ace,
|
Ace = 1,
|
||||||
Two,
|
Two = 2,
|
||||||
Three,
|
Three = 3,
|
||||||
Four,
|
Four = 4,
|
||||||
Five,
|
Five = 5,
|
||||||
Six,
|
Six = 6,
|
||||||
Seven,
|
Seven = 7,
|
||||||
Eight,
|
Eight = 8,
|
||||||
Nine,
|
Nine = 9,
|
||||||
Ten,
|
Ten = 10,
|
||||||
Jack,
|
Jack = 11,
|
||||||
Queen,
|
Queen = 12,
|
||||||
King,
|
King = 13,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Rank {
|
impl Rank {
|
||||||
|
/// All thirteen ranks in ascending order.
|
||||||
|
pub const RANKS: [Self; 13] = [
|
||||||
|
Self::Ace, Self::Two, Self::Three, Self::Four, Self::Five,
|
||||||
|
Self::Six, Self::Seven, Self::Eight, Self::Nine, Self::Ten,
|
||||||
|
Self::Jack, Self::Queen, Self::King,
|
||||||
|
];
|
||||||
|
|
||||||
/// Numeric value: Ace = 1, King = 13.
|
/// Numeric value: Ace = 1, King = 13.
|
||||||
pub fn value(self) -> u8 {
|
pub fn value(self) -> u8 {
|
||||||
match self {
|
self as u8
|
||||||
Rank::Ace => 1,
|
}
|
||||||
Rank::Two => 2,
|
|
||||||
Rank::Three => 3,
|
const fn new(n: u8) -> Option<Self> {
|
||||||
Rank::Four => 4,
|
match n {
|
||||||
Rank::Five => 5,
|
1 => Some(Self::Ace),
|
||||||
Rank::Six => 6,
|
2 => Some(Self::Two),
|
||||||
Rank::Seven => 7,
|
3 => Some(Self::Three),
|
||||||
Rank::Eight => 8,
|
4 => Some(Self::Four),
|
||||||
Rank::Nine => 9,
|
5 => Some(Self::Five),
|
||||||
Rank::Ten => 10,
|
6 => Some(Self::Six),
|
||||||
Rank::Jack => 11,
|
7 => Some(Self::Seven),
|
||||||
Rank::Queen => 12,
|
8 => Some(Self::Eight),
|
||||||
Rank::King => 13,
|
9 => Some(Self::Nine),
|
||||||
|
10 => Some(Self::Ten),
|
||||||
|
11 => Some(Self::Jack),
|
||||||
|
12 => Some(Self::Queen),
|
||||||
|
13 => Some(Self::King),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the rank `n` steps above `self`, or `None` if it would exceed King.
|
||||||
|
pub const fn checked_add(self, n: u8) -> Option<Self> {
|
||||||
|
Self::new((self as u8).saturating_add(n))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the rank `n` steps below `self`, or `None` if it would go below Ace.
|
||||||
|
pub const fn checked_sub(self, n: u8) -> Option<Self> {
|
||||||
|
match (self as u8).checked_sub(n) {
|
||||||
|
Some(v) => Self::new(v),
|
||||||
|
None => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -79,16 +107,43 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn rank_values_are_sequential() {
|
fn rank_values_are_sequential() {
|
||||||
let ranks = [
|
for (i, r) in Rank::RANKS.iter().enumerate() {
|
||||||
Rank::Ace, Rank::Two, Rank::Three, Rank::Four, Rank::Five,
|
|
||||||
Rank::Six, Rank::Seven, Rank::Eight, Rank::Nine, Rank::Ten,
|
|
||||||
Rank::Jack, Rank::Queen, Rank::King,
|
|
||||||
];
|
|
||||||
for (i, r) in ranks.iter().enumerate() {
|
|
||||||
assert_eq!(r.value(), (i + 1) as u8);
|
assert_eq!(r.value(), (i + 1) as u8);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn rank_as_u8_matches_value() {
|
||||||
|
for r in Rank::RANKS {
|
||||||
|
assert_eq!(r as u8, r.value());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn rank_checked_add_boundary() {
|
||||||
|
assert_eq!(Rank::King.checked_add(1), None);
|
||||||
|
assert_eq!(Rank::Queen.checked_add(1), Some(Rank::King));
|
||||||
|
assert_eq!(Rank::Ace.checked_add(1), Some(Rank::Two));
|
||||||
|
assert_eq!(Rank::Five.checked_add(3), Some(Rank::Eight));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn rank_checked_sub_boundary() {
|
||||||
|
assert_eq!(Rank::Ace.checked_sub(1), None);
|
||||||
|
assert_eq!(Rank::Two.checked_sub(1), Some(Rank::Ace));
|
||||||
|
assert_eq!(Rank::King.checked_sub(1), Some(Rank::Queen));
|
||||||
|
assert_eq!(Rank::Five.checked_sub(3), Some(Rank::Two));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn suit_suits_contains_all_four() {
|
||||||
|
assert_eq!(Suit::SUITS.len(), 4);
|
||||||
|
assert!(Suit::SUITS.contains(&Suit::Clubs));
|
||||||
|
assert!(Suit::SUITS.contains(&Suit::Diamonds));
|
||||||
|
assert!(Suit::SUITS.contains(&Suit::Hearts));
|
||||||
|
assert!(Suit::SUITS.contains(&Suit::Spades));
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn suit_red_and_black_are_complementary() {
|
fn suit_red_and_black_are_complementary() {
|
||||||
for suit in [Suit::Clubs, Suit::Diamonds, Suit::Hearts, Suit::Spades] {
|
for suit in [Suit::Clubs, Suit::Diamonds, Suit::Hearts, Suit::Spades] {
|
||||||
|
|||||||
@@ -193,7 +193,7 @@ impl GameState {
|
|||||||
is_auto_completable: false,
|
is_auto_completable: false,
|
||||||
undo_count: 0,
|
undo_count: 0,
|
||||||
recycle_count: 0,
|
recycle_count: 0,
|
||||||
take_from_foundation: true,
|
take_from_foundation: false,
|
||||||
schema_version: GAME_STATE_SCHEMA_VERSION,
|
schema_version: GAME_STATE_SCHEMA_VERSION,
|
||||||
undo_stack: VecDeque::new(),
|
undo_stack: VecDeque::new(),
|
||||||
}
|
}
|
||||||
@@ -407,7 +407,7 @@ impl GameState {
|
|||||||
self.score = if self.mode == GameMode::Zen {
|
self.score = if self.mode == GameMode::Zen {
|
||||||
0
|
0
|
||||||
} else {
|
} else {
|
||||||
(snapshot.score + scoring_undo()).max(0)
|
(self.score + scoring_undo()).max(0)
|
||||||
};
|
};
|
||||||
self.move_count = snapshot.move_count;
|
self.move_count = snapshot.move_count;
|
||||||
self.is_won = false;
|
self.is_won = false;
|
||||||
@@ -416,12 +416,25 @@ impl GameState {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns `true` when all four foundation slots each contain 13 cards.
|
/// Returns `true` when all four foundation slots each contain a valid A→K
|
||||||
|
/// sequence of a single suit.
|
||||||
|
///
|
||||||
|
/// Counting 13 cards is not sufficient — a corrupt save could produce 13
|
||||||
|
/// arbitrary cards per pile and permanently lock the game via `GameAlreadyWon`.
|
||||||
pub fn check_win(&self) -> bool {
|
pub fn check_win(&self) -> bool {
|
||||||
(0..4_u8).all(|slot| {
|
(0..4_u8).all(|slot| self.is_valid_foundation_pile(slot))
|
||||||
self.piles
|
}
|
||||||
.get(&PileType::Foundation(slot))
|
|
||||||
.is_some_and(|p| p.cards.len() == 13)
|
fn is_valid_foundation_pile(&self, slot: u8) -> bool {
|
||||||
|
let Some(pile) = self.piles.get(&PileType::Foundation(slot)) else {
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
if pile.cards.len() != 13 {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
let suit = pile.cards[0].suit;
|
||||||
|
pile.cards.iter().enumerate().all(|(i, card)| {
|
||||||
|
card.suit == suit && card.rank.value() == (i as u8 + 1)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -440,6 +453,91 @@ impl GameState {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns all currently valid `move_cards` calls as `(from, to, count)` triples.
|
||||||
|
///
|
||||||
|
/// Does not include stock draws — callers check `piles[&PileType::Stock]` directly.
|
||||||
|
/// Every returned triple is guaranteed to succeed when passed to `move_cards`.
|
||||||
|
pub fn possible_instructions(&self) -> Vec<(PileType, PileType, usize)> {
|
||||||
|
if self.is_won {
|
||||||
|
return Vec::new();
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut moves = Vec::new();
|
||||||
|
|
||||||
|
// Waste top card → foundation or tableau
|
||||||
|
if let Some(waste_top) = self.piles.get(&PileType::Waste).and_then(|p| p.cards.last()) {
|
||||||
|
for slot in 0..4_u8 {
|
||||||
|
if let Some(f) = self.piles.get(&PileType::Foundation(slot))
|
||||||
|
&& can_place_on_foundation(waste_top, f)
|
||||||
|
{
|
||||||
|
moves.push((PileType::Waste, PileType::Foundation(slot), 1));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for dst in 0..7_usize {
|
||||||
|
if let Some(t) = self.piles.get(&PileType::Tableau(dst))
|
||||||
|
&& can_place_on_tableau(waste_top, t)
|
||||||
|
{
|
||||||
|
moves.push((PileType::Waste, PileType::Tableau(dst), 1));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Tableau sources
|
||||||
|
for src in 0..7_usize {
|
||||||
|
let Some(src_pile) = self.piles.get(&PileType::Tableau(src)) else { continue };
|
||||||
|
if src_pile.cards.is_empty() {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let run_len = src_pile.cards.iter().rev().take_while(|c| c.face_up).count();
|
||||||
|
if run_len == 0 {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
for count in 1..=run_len {
|
||||||
|
let seq_start = src_pile.cards.len() - count;
|
||||||
|
if !is_valid_tableau_sequence(&src_pile.cards[seq_start..]) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let bottom = &src_pile.cards[seq_start];
|
||||||
|
if count == 1 {
|
||||||
|
for slot in 0..4_u8 {
|
||||||
|
if let Some(f) = self.piles.get(&PileType::Foundation(slot))
|
||||||
|
&& can_place_on_foundation(bottom, f)
|
||||||
|
{
|
||||||
|
moves.push((PileType::Tableau(src), PileType::Foundation(slot), 1));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for dst in 0..7_usize {
|
||||||
|
if dst == src {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if let Some(t) = self.piles.get(&PileType::Tableau(dst))
|
||||||
|
&& can_place_on_tableau(bottom, t)
|
||||||
|
{
|
||||||
|
moves.push((PileType::Tableau(src), PileType::Tableau(dst), count));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Foundation top → tableau (only when house rule is enabled)
|
||||||
|
if self.take_from_foundation {
|
||||||
|
for slot in 0..4_u8 {
|
||||||
|
let Some(f) = self.piles.get(&PileType::Foundation(slot)) else { continue };
|
||||||
|
let Some(top) = f.cards.last() else { continue };
|
||||||
|
for dst in 0..7_usize {
|
||||||
|
if let Some(t) = self.piles.get(&PileType::Tableau(dst))
|
||||||
|
&& can_place_on_tableau(top, t)
|
||||||
|
{
|
||||||
|
moves.push((PileType::Foundation(slot), PileType::Tableau(dst), 1));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
moves
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns the next `(from, to)` move that advances auto-complete, or
|
/// Returns the next `(from, to)` move that advances auto-complete, or
|
||||||
/// `None` if no such move exists (or `is_auto_completable` is not set).
|
/// `None` if no such move exists (or `is_auto_completable` is not set).
|
||||||
///
|
///
|
||||||
@@ -1310,12 +1408,9 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn take_from_foundation_allowed_by_default() {
|
fn take_from_foundation_disabled_by_default() {
|
||||||
let mut g = setup_take_from_foundation_game();
|
let g = setup_take_from_foundation_game();
|
||||||
assert!(g.take_from_foundation, "standard Klondike allows take-from-foundation by default");
|
assert!(!g.take_from_foundation, "take_from_foundation is off by default (non-standard rule)");
|
||||||
g.move_cards(PileType::Foundation(0), PileType::Tableau(0), 1).unwrap();
|
|
||||||
assert_eq!(g.piles[&PileType::Foundation(0)].cards.len(), 1);
|
|
||||||
assert_eq!(g.piles[&PileType::Tableau(0)].cards.len(), 2);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -1366,4 +1461,78 @@ mod tests {
|
|||||||
.unwrap_err();
|
.unwrap_err();
|
||||||
assert!(matches!(err, MoveError::RuleViolation(_)));
|
assert!(matches!(err, MoveError::RuleViolation(_)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// --- possible_instructions ---
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn possible_instructions_empty_when_won() {
|
||||||
|
let mut g = new_game();
|
||||||
|
g.is_won = true;
|
||||||
|
assert!(g.possible_instructions().is_empty());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn possible_instructions_includes_ace_to_foundation() {
|
||||||
|
let mut g = new_game();
|
||||||
|
g.piles.get_mut(&PileType::Stock).unwrap().cards.clear();
|
||||||
|
g.piles.get_mut(&PileType::Waste).unwrap().cards.clear();
|
||||||
|
for i in 0..7 {
|
||||||
|
g.piles.get_mut(&PileType::Tableau(i)).unwrap().cards.clear();
|
||||||
|
}
|
||||||
|
g.piles.get_mut(&PileType::Tableau(0)).unwrap().cards.push(Card {
|
||||||
|
id: 1, suit: Suit::Clubs, rank: Rank::Ace, face_up: true,
|
||||||
|
});
|
||||||
|
let moves = g.possible_instructions();
|
||||||
|
assert!(
|
||||||
|
moves.contains(&(PileType::Tableau(0), PileType::Foundation(0), 1)),
|
||||||
|
"Ace must be moveable to empty foundation slot 0; got {moves:?}"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn possible_instructions_all_valid_on_fresh_game() {
|
||||||
|
// Every triple returned must actually succeed when applied to a clone of the state.
|
||||||
|
let g = new_game();
|
||||||
|
for (from, to, count) in g.possible_instructions() {
|
||||||
|
let mut clone = g.clone();
|
||||||
|
assert!(
|
||||||
|
clone.move_cards(from.clone(), to.clone(), count).is_ok(),
|
||||||
|
"instruction ({from:?}, {to:?}, {count}) from possible_instructions must succeed"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn possible_instructions_no_face_down_sources() {
|
||||||
|
let g = new_game();
|
||||||
|
for (from, _, count) in g.possible_instructions() {
|
||||||
|
if let PileType::Tableau(i) = from {
|
||||||
|
let pile = &g.piles[&PileType::Tableau(i)];
|
||||||
|
let run_len = pile.cards.iter().rev().take_while(|c| c.face_up).count();
|
||||||
|
assert!(
|
||||||
|
count <= run_len,
|
||||||
|
"count {count} exceeds face-up run {run_len} for Tableau({i})"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn possible_instructions_waste_top_included() {
|
||||||
|
let mut g = new_game();
|
||||||
|
// Clear board, put a King on waste, and an empty tableau pile — waste→tableau must appear.
|
||||||
|
g.piles.get_mut(&PileType::Stock).unwrap().cards.clear();
|
||||||
|
for i in 0..7 {
|
||||||
|
g.piles.get_mut(&PileType::Tableau(i)).unwrap().cards.clear();
|
||||||
|
}
|
||||||
|
g.piles.get_mut(&PileType::Waste).unwrap().cards.push(Card {
|
||||||
|
id: 99, suit: Suit::Spades, rank: Rank::King, face_up: true,
|
||||||
|
});
|
||||||
|
let moves = g.possible_instructions();
|
||||||
|
// King goes on any of the 7 empty tableau piles
|
||||||
|
assert!(
|
||||||
|
(0..7).any(|dst| moves.contains(&(PileType::Waste, PileType::Tableau(dst), 1))),
|
||||||
|
"King on waste must be moveable to an empty tableau column"
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
use crate::card::Card;
|
use crate::card::{Card, Rank};
|
||||||
use crate::pile::Pile;
|
use crate::pile::Pile;
|
||||||
|
|
||||||
/// Returns `true` if `card` can be placed on the foundation `pile`.
|
/// Returns `true` if `card` can be placed on the foundation `pile`.
|
||||||
@@ -12,8 +12,8 @@ use crate::pile::Pile;
|
|||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn can_place_on_foundation(card: &Card, pile: &Pile) -> bool {
|
pub fn can_place_on_foundation(card: &Card, pile: &Pile) -> bool {
|
||||||
match pile.cards.last() {
|
match pile.cards.last() {
|
||||||
None => card.rank.value() == 1,
|
None => card.rank == Rank::Ace,
|
||||||
Some(top) => card.suit == top.suit && card.rank.value() == top.rank.value() + 1,
|
Some(top) => card.suit == top.suit && card.rank.checked_sub(1) == Some(top.rank),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -23,10 +23,10 @@ pub fn can_place_on_foundation(card: &Card, pile: &Pile) -> bool {
|
|||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn can_place_on_tableau(card: &Card, pile: &Pile) -> bool {
|
pub fn can_place_on_tableau(card: &Card, pile: &Pile) -> bool {
|
||||||
match pile.cards.last() {
|
match pile.cards.last() {
|
||||||
None => card.rank.value() == 13,
|
None => card.rank == Rank::King,
|
||||||
Some(top) => {
|
Some(top) => {
|
||||||
top.face_up
|
top.face_up
|
||||||
&& card.rank.value() + 1 == top.rank.value()
|
&& card.rank.checked_add(1) == Some(top.rank)
|
||||||
&& card.suit.is_red() != top.suit.is_red()
|
&& card.suit.is_red() != top.suit.is_red()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -41,7 +41,7 @@ pub fn can_place_on_tableau(card: &Card, pile: &Pile) -> bool {
|
|||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn is_valid_tableau_sequence(cards: &[Card]) -> bool {
|
pub fn is_valid_tableau_sequence(cards: &[Card]) -> bool {
|
||||||
cards.windows(2).all(|w| {
|
cards.windows(2).all(|w| {
|
||||||
w[0].rank.value() == w[1].rank.value() + 1 && w[0].suit.is_red() != w[1].suit.is_red()
|
w[0].rank.checked_sub(1) == Some(w[1].rank) && w[0].suit.is_red() != w[1].suit.is_red()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,9 +9,11 @@ use crate::pile::PileType;
|
|||||||
pub fn score_move(from: &PileType, to: &PileType) -> i32 {
|
pub fn score_move(from: &PileType, to: &PileType) -> i32 {
|
||||||
match to {
|
match to {
|
||||||
PileType::Foundation(_) => 10,
|
PileType::Foundation(_) => 10,
|
||||||
PileType::Tableau(_) => {
|
PileType::Tableau(_) => match from {
|
||||||
if matches!(from, PileType::Waste) { 5 } else { 0 }
|
PileType::Waste => 5,
|
||||||
}
|
PileType::Foundation(_) => -15,
|
||||||
|
_ => 0,
|
||||||
|
},
|
||||||
_ => 0,
|
_ => 0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -71,13 +73,12 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn non_waste_to_tableau_scores_zero() {
|
fn foundation_to_tableau_penalises_fifteen() {
|
||||||
// Foundation → Tableau is impossible in practice but must score 0.
|
// Moving a card back off a foundation (take_from_foundation rule) costs -15.
|
||||||
assert_eq!(score_move(&PileType::Foundation(0), &PileType::Tableau(0)), 0);
|
assert_eq!(score_move(&PileType::Foundation(0), &PileType::Tableau(0)), -15);
|
||||||
// Tableau → Tableau (restack) scores 0.
|
|
||||||
assert_eq!(score_move(&PileType::Tableau(1), &PileType::Tableau(2)), 0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn move_to_stock_or_waste_scores_zero() {
|
fn move_to_stock_or_waste_scores_zero() {
|
||||||
// These destinations are illegal moves in practice, but the function
|
// These destinations are illegal moves in practice, but the function
|
||||||
|
|||||||
@@ -298,9 +298,16 @@ impl SolverState {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// True when every foundation slot has 13 cards.
|
/// True when every foundation slot holds a complete Ace-through-King sequence.
|
||||||
fn is_won(&self) -> bool {
|
fn is_won(&self) -> bool {
|
||||||
self.foundation.iter().all(|f| f.len() == 13)
|
self.foundation.iter().all(|pile| {
|
||||||
|
pile.len() == 13
|
||||||
|
&& pile[0].rank == crate::card::Rank::Ace
|
||||||
|
&& pile.windows(2).all(|w| {
|
||||||
|
w[0].suit == w[1].suit
|
||||||
|
&& w[1].rank.value() == w[0].rank.value() + 1
|
||||||
|
})
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the foundation slot that already claims `suit`, or the
|
/// Returns the foundation slot that already claims `suit`, or the
|
||||||
|
|||||||
@@ -238,6 +238,12 @@ pub struct Settings {
|
|||||||
/// field existed deserialize cleanly to `None` via `#[serde(default)]`.
|
/// field existed deserialize cleanly to `None` via `#[serde(default)]`.
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub leaderboard_display_name: Option<String>,
|
pub leaderboard_display_name: Option<String>,
|
||||||
|
/// `true` once the player has successfully opted in to the leaderboard on
|
||||||
|
/// the server. Used to decide whether a display-name change should also
|
||||||
|
/// push an update via `opt_in_leaderboard`. Older `settings.json` files
|
||||||
|
/// deserialize cleanly to `false` via `#[serde(default)]`.
|
||||||
|
#[serde(default)]
|
||||||
|
pub leaderboard_opted_in: bool,
|
||||||
/// When `true`, the player may drag the top card of a foundation pile back
|
/// When `true`, the player may drag the top card of a foundation pile back
|
||||||
/// onto a compatible tableau column. Enabled by default (standard Klondike
|
/// onto a compatible tableau column. Enabled by default (standard Klondike
|
||||||
/// rules). Older `settings.json` files without this key deserialize to
|
/// rules). Older `settings.json` files without this key deserialize to
|
||||||
@@ -387,6 +393,7 @@ impl Default for Settings {
|
|||||||
replay_move_interval_secs: default_replay_move_interval_secs(),
|
replay_move_interval_secs: default_replay_move_interval_secs(),
|
||||||
last_difficulty: None,
|
last_difficulty: None,
|
||||||
leaderboard_display_name: None,
|
leaderboard_display_name: None,
|
||||||
|
leaderboard_opted_in: false,
|
||||||
take_from_foundation: true,
|
take_from_foundation: true,
|
||||||
analytics_enabled: false,
|
analytics_enabled: false,
|
||||||
matomo_url: None,
|
matomo_url: None,
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ use crate::settings_plugin::{SettingsResource, SettingsStoragePath};
|
|||||||
use crate::stats_plugin::{StatsResource, StatsUpdate};
|
use crate::stats_plugin::{StatsResource, StatsUpdate};
|
||||||
use crate::ui_modal::{
|
use crate::ui_modal::{
|
||||||
spawn_modal, spawn_modal_actions, spawn_modal_button, spawn_modal_header, ButtonVariant,
|
spawn_modal, spawn_modal_actions, spawn_modal_button, spawn_modal_header, ButtonVariant,
|
||||||
ScrimDismissible,
|
ModalScrim, ScrimDismissible,
|
||||||
};
|
};
|
||||||
use crate::ui_theme::{
|
use crate::ui_theme::{
|
||||||
ACCENT_PRIMARY, BORDER_SUBTLE, STATE_SUCCESS, TEXT_DISABLED, TEXT_PRIMARY, TEXT_SECONDARY,
|
ACCENT_PRIMARY, BORDER_SUBTLE, STATE_SUCCESS, TEXT_DISABLED, TEXT_PRIMARY, TEXT_SECONDARY,
|
||||||
@@ -162,93 +162,91 @@ fn evaluate_on_win(
|
|||||||
mut achievements: ResMut<AchievementsResource>,
|
mut achievements: ResMut<AchievementsResource>,
|
||||||
mut progress: ResMut<ProgressResource>,
|
mut progress: ResMut<ProgressResource>,
|
||||||
) {
|
) {
|
||||||
let Some(ev) = wins.read().last() else {
|
for ev in wins.read() {
|
||||||
return;
|
let ctx = AchievementContext {
|
||||||
};
|
games_played: stats.0.games_played,
|
||||||
|
games_won: stats.0.games_won,
|
||||||
let ctx = AchievementContext {
|
win_streak_current: stats.0.win_streak_current,
|
||||||
games_played: stats.0.games_played,
|
best_single_score: stats.0.best_single_score,
|
||||||
games_won: stats.0.games_won,
|
lifetime_score: stats.0.lifetime_score,
|
||||||
win_streak_current: stats.0.win_streak_current,
|
draw_three_wins: stats.0.draw_three_wins,
|
||||||
best_single_score: stats.0.best_single_score,
|
daily_challenge_streak: progress.0.daily_challenge_streak,
|
||||||
lifetime_score: stats.0.lifetime_score,
|
last_win_score: ev.score,
|
||||||
draw_three_wins: stats.0.draw_three_wins,
|
last_win_time_seconds: ev.time_seconds,
|
||||||
daily_challenge_streak: progress.0.daily_challenge_streak,
|
last_win_used_undo: game.0.undo_count > 0,
|
||||||
last_win_score: ev.score,
|
wall_clock_hour: Some(Local::now().hour()),
|
||||||
last_win_time_seconds: ev.time_seconds,
|
last_win_recycle_count: game.0.recycle_count,
|
||||||
last_win_used_undo: game.0.undo_count > 0,
|
last_win_is_zen: game.0.mode == solitaire_core::game_state::GameMode::Zen,
|
||||||
wall_clock_hour: Some(Local::now().hour()),
|
|
||||||
last_win_recycle_count: game.0.recycle_count,
|
|
||||||
last_win_is_zen: game.0.mode == solitaire_core::game_state::GameMode::Zen,
|
|
||||||
};
|
|
||||||
|
|
||||||
let hits = check_achievements(&ctx);
|
|
||||||
if hits.is_empty() {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
let now = Utc::now();
|
|
||||||
let mut achievements_changed = false;
|
|
||||||
let mut progress_changed = false;
|
|
||||||
|
|
||||||
for def in hits {
|
|
||||||
let Some(record) = achievements.0.iter_mut().find(|r| r.id == def.id) else {
|
|
||||||
continue;
|
|
||||||
};
|
};
|
||||||
if record.unlocked {
|
|
||||||
|
let hits = check_achievements(&ctx);
|
||||||
|
if hits.is_empty() {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
record.unlock(now);
|
|
||||||
achievements_changed = true;
|
|
||||||
|
|
||||||
// Grant the reward on first unlock.
|
let now = Utc::now();
|
||||||
if !record.reward_granted {
|
let mut achievements_changed = false;
|
||||||
if let Some(reward) = def.reward {
|
let mut progress_changed = false;
|
||||||
match reward {
|
|
||||||
Reward::CardBack(idx) => {
|
for def in hits {
|
||||||
if !progress.0.unlocked_card_backs.contains(&idx) {
|
let Some(record) = achievements.0.iter_mut().find(|r| r.id == def.id) else {
|
||||||
progress.0.unlocked_card_backs.push(idx);
|
continue;
|
||||||
progress_changed = true;
|
};
|
||||||
}
|
if record.unlocked {
|
||||||
}
|
continue;
|
||||||
Reward::Background(idx) => {
|
|
||||||
if !progress.0.unlocked_backgrounds.contains(&idx) {
|
|
||||||
progress.0.unlocked_backgrounds.push(idx);
|
|
||||||
progress_changed = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reward::BonusXp(amount) => {
|
|
||||||
xp_awarded.write(XpAwardedEvent { amount });
|
|
||||||
let prev_level = progress.0.add_xp(amount);
|
|
||||||
if progress.0.leveled_up_from(prev_level) {
|
|
||||||
levelups.write(LevelUpEvent {
|
|
||||||
previous_level: prev_level,
|
|
||||||
new_level: progress.0.level,
|
|
||||||
total_xp: progress.0.total_xp,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
progress_changed = true;
|
|
||||||
}
|
|
||||||
Reward::Badge => {}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
record.reward_granted = true;
|
record.unlock(now);
|
||||||
|
achievements_changed = true;
|
||||||
|
|
||||||
|
// Grant the reward on first unlock.
|
||||||
|
if !record.reward_granted {
|
||||||
|
if let Some(reward) = def.reward {
|
||||||
|
match reward {
|
||||||
|
Reward::CardBack(idx) => {
|
||||||
|
if !progress.0.unlocked_card_backs.contains(&idx) {
|
||||||
|
progress.0.unlocked_card_backs.push(idx);
|
||||||
|
progress_changed = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Reward::Background(idx) => {
|
||||||
|
if !progress.0.unlocked_backgrounds.contains(&idx) {
|
||||||
|
progress.0.unlocked_backgrounds.push(idx);
|
||||||
|
progress_changed = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Reward::BonusXp(amount) => {
|
||||||
|
xp_awarded.write(XpAwardedEvent { amount });
|
||||||
|
let prev_level = progress.0.add_xp(amount);
|
||||||
|
if progress.0.leveled_up_from(prev_level) {
|
||||||
|
levelups.write(LevelUpEvent {
|
||||||
|
previous_level: prev_level,
|
||||||
|
new_level: progress.0.level,
|
||||||
|
total_xp: progress.0.total_xp,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
progress_changed = true;
|
||||||
|
}
|
||||||
|
Reward::Badge => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
record.reward_granted = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
unlocks.write(AchievementUnlockedEvent(record.clone()));
|
||||||
}
|
}
|
||||||
|
|
||||||
unlocks.write(AchievementUnlockedEvent(record.clone()));
|
if achievements_changed
|
||||||
|
&& let Some(target) = &path.0
|
||||||
|
&& let Err(e) = save_achievements_to(target, &achievements.0) {
|
||||||
|
warn!("failed to save achievements: {e}");
|
||||||
|
}
|
||||||
|
|
||||||
|
if progress_changed
|
||||||
|
&& let Some(target) = &progress_path.0
|
||||||
|
&& let Err(e) = save_progress_to(target, &progress.0) {
|
||||||
|
warn!("failed to save progress after reward: {e}");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if achievements_changed
|
|
||||||
&& let Some(target) = &path.0
|
|
||||||
&& let Err(e) = save_achievements_to(target, &achievements.0) {
|
|
||||||
warn!("failed to save achievements: {e}");
|
|
||||||
}
|
|
||||||
|
|
||||||
if progress_changed
|
|
||||||
&& let Some(target) = &progress_path.0
|
|
||||||
&& let Err(e) = save_progress_to(target, &progress.0) {
|
|
||||||
warn!("failed to save progress after reward: {e}");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Cinephile unlock observer.
|
/// Cinephile unlock observer.
|
||||||
@@ -391,6 +389,7 @@ fn toggle_achievements_screen(
|
|||||||
achievements: Res<AchievementsResource>,
|
achievements: Res<AchievementsResource>,
|
||||||
font_res: Option<Res<FontResource>>,
|
font_res: Option<Res<FontResource>>,
|
||||||
screens: Query<Entity, With<AchievementsScreen>>,
|
screens: Query<Entity, With<AchievementsScreen>>,
|
||||||
|
other_modal_scrims: Query<(), (With<ModalScrim>, Without<AchievementsScreen>)>,
|
||||||
) {
|
) {
|
||||||
let button_clicked = requests.read().count() > 0;
|
let button_clicked = requests.read().count() > 0;
|
||||||
if !keys.just_pressed(KeyCode::KeyA) && !button_clicked {
|
if !keys.just_pressed(KeyCode::KeyA) && !button_clicked {
|
||||||
@@ -398,7 +397,7 @@ fn toggle_achievements_screen(
|
|||||||
}
|
}
|
||||||
if let Ok(entity) = screens.single() {
|
if let Ok(entity) = screens.single() {
|
||||||
commands.entity(entity).despawn();
|
commands.entity(entity).despawn();
|
||||||
} else {
|
} else if other_modal_scrims.is_empty() {
|
||||||
spawn_achievements_screen(&mut commands, &achievements.0, font_res.as_deref());
|
spawn_achievements_screen(&mut commands, &achievements.0, font_res.as_deref());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -72,6 +72,17 @@ const TIME_ATTACK_TOAST_SECS: f32 = 5.0;
|
|||||||
const CHALLENGE_TOAST_SECS: f32 = 3.0;
|
const CHALLENGE_TOAST_SECS: f32 = 3.0;
|
||||||
const VOLUME_TOAST_SECS: f32 = 1.4;
|
const VOLUME_TOAST_SECS: f32 = 1.4;
|
||||||
|
|
||||||
|
/// Z added to a card's render depth while its `CardAnim` is in-flight.
|
||||||
|
///
|
||||||
|
/// Foundation and tableau cards share x,y during the slide (destination equals
|
||||||
|
/// a slot that already holds a card). Without this lift the incoming card's
|
||||||
|
/// bottom-right corner overlaps the stationary card's top-left, which the
|
||||||
|
/// player perceives as a single card with mismatched rank/suit indices.
|
||||||
|
///
|
||||||
|
/// 50.0 sits comfortably above the highest pile depth (~1.04) and well below
|
||||||
|
/// `DRAG_Z` (500), so a dragged card always renders above an animated one.
|
||||||
|
const CARD_ANIM_Z_LIFT: f32 = 50.0;
|
||||||
|
|
||||||
/// Per-card stagger interval for the win cascade at Normal speed (seconds).
|
/// Per-card stagger interval for the win cascade at Normal speed (seconds).
|
||||||
///
|
///
|
||||||
/// Sourced from `ui_theme::MOTION_CASCADE_STAGGER_SECS` so all motion timing
|
/// Sourced from `ui_theme::MOTION_CASCADE_STAGGER_SECS` so all motion timing
|
||||||
@@ -247,6 +258,11 @@ fn advance_card_anims(
|
|||||||
anim.delay = (anim.delay - dt).max(0.0);
|
anim.delay = (anim.delay - dt).max(0.0);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
if anim.duration <= 0.0 {
|
||||||
|
transform.translation = anim.target;
|
||||||
|
commands.entity(entity).remove::<CardAnim>();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
anim.elapsed += dt;
|
anim.elapsed += dt;
|
||||||
let t = (anim.elapsed / anim.duration).min(1.0);
|
let t = (anim.elapsed / anim.duration).min(1.0);
|
||||||
// Curved interpolation using `MotionCurve::SmoothSnap` (cubic ease-out
|
// Curved interpolation using `MotionCurve::SmoothSnap` (cubic ease-out
|
||||||
@@ -254,7 +270,11 @@ fn advance_card_anims(
|
|||||||
// shared `CardAnim` struct stays a simple linear-tween container — the
|
// shared `CardAnim` struct stays a simple linear-tween container — the
|
||||||
// upgrade is one extra `sample_curve` call per advancing animation.
|
// upgrade is one extra `sample_curve` call per advancing animation.
|
||||||
let s = sample_curve(MotionCurve::SmoothSnap, t);
|
let s = sample_curve(MotionCurve::SmoothSnap, t);
|
||||||
transform.translation = anim.start.lerp(anim.target, s);
|
let mut pos = anim.start.lerp(anim.target, s);
|
||||||
|
// Elevate z during transit so the moving card always renders in front
|
||||||
|
// of any card already resting at the destination position.
|
||||||
|
pos.z = anim.target.z + CARD_ANIM_Z_LIFT;
|
||||||
|
transform.translation = pos;
|
||||||
if t >= 1.0 {
|
if t >= 1.0 {
|
||||||
transform.translation = anim.target;
|
transform.translation = anim.target;
|
||||||
commands.entity(entity).remove::<CardAnim>();
|
commands.entity(entity).remove::<CardAnim>();
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ use bevy::prelude::*;
|
|||||||
use crate::audio_plugin::{AudioState, SoundLibrary};
|
use crate::audio_plugin::{AudioState, SoundLibrary};
|
||||||
use crate::events::{MoveRequestEvent, StateChangedEvent};
|
use crate::events::{MoveRequestEvent, StateChangedEvent};
|
||||||
use crate::game_plugin::GameMutation;
|
use crate::game_plugin::GameMutation;
|
||||||
|
use crate::pause_plugin::PausedResource;
|
||||||
use crate::resources::GameStateResource;
|
use crate::resources::GameStateResource;
|
||||||
|
|
||||||
/// Volume amplitude used for the auto-complete activation chime.
|
/// Volume amplitude used for the auto-complete activation chime.
|
||||||
@@ -72,9 +73,14 @@ fn detect_auto_complete(
|
|||||||
if game.0.is_auto_completable && !state.active {
|
if game.0.is_auto_completable && !state.active {
|
||||||
state.active = true;
|
state.active = true;
|
||||||
state.cooldown = 0.0; // fire first move immediately
|
state.cooldown = 0.0; // fire first move immediately
|
||||||
} else if !game.0.is_auto_completable {
|
|
||||||
state.active = false;
|
|
||||||
}
|
}
|
||||||
|
// Intentionally no `else if !is_auto_completable` branch here.
|
||||||
|
// Deactivating on every frame where `is_auto_completable` is false
|
||||||
|
// would hard-stop the sequence mid-flight whenever `next_auto_complete_move`
|
||||||
|
// transiently returns `None` (e.g. while the previous move is still
|
||||||
|
// in-flight). The `is_won` check above already handles the definitive
|
||||||
|
// end-of-game case; `drive_auto_complete` simply retries next tick
|
||||||
|
// when no move is available yet.
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Plays a distinct chime the moment auto-complete first activates.
|
/// Plays a distinct chime the moment auto-complete first activates.
|
||||||
@@ -106,11 +112,15 @@ fn drive_auto_complete(
|
|||||||
mut state: ResMut<AutoCompleteState>,
|
mut state: ResMut<AutoCompleteState>,
|
||||||
game: Res<GameStateResource>,
|
game: Res<GameStateResource>,
|
||||||
time: Res<Time>,
|
time: Res<Time>,
|
||||||
|
paused: Option<Res<PausedResource>>,
|
||||||
mut moves: MessageWriter<MoveRequestEvent>,
|
mut moves: MessageWriter<MoveRequestEvent>,
|
||||||
) {
|
) {
|
||||||
if !state.active {
|
if !state.active {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if paused.is_some_and(|p| p.0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
state.cooldown -= time.delta_secs();
|
state.cooldown -= time.delta_secs();
|
||||||
if state.cooldown > 0.0 {
|
if state.cooldown > 0.0 {
|
||||||
|
|||||||
@@ -142,6 +142,13 @@ impl Plugin for CardAnimationPlugin {
|
|||||||
update_frame_time_diagnostics,
|
update_frame_time_diagnostics,
|
||||||
// Advance active animations.
|
// Advance active animations.
|
||||||
advance_card_animations,
|
advance_card_animations,
|
||||||
|
// Flush deferred commands so `CardAnimation` removals from
|
||||||
|
// `advance_card_animations` are visible before the chain
|
||||||
|
// system runs. Without this, the chain sees the component
|
||||||
|
// still present in the same frame it was removed (deferred
|
||||||
|
// commands aren't applied until the next ApplyDeferred
|
||||||
|
// point), causing a 1-frame gap between every chain step.
|
||||||
|
ApplyDeferred,
|
||||||
// After each animation finishes, pop the next chain segment.
|
// After each animation finishes, pop the next chain segment.
|
||||||
advance_animation_chains,
|
advance_animation_chains,
|
||||||
// Interaction visuals (run after animation for final positions).
|
// Interaction visuals (run after animation for final positions).
|
||||||
|
|||||||
@@ -41,7 +41,9 @@ use crate::ui_theme::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
/// Fraction of card height used as vertical offset between face-up tableau cards.
|
/// Fraction of card height used as vertical offset between face-up tableau cards.
|
||||||
pub const TABLEAU_FAN_FRAC: f32 = 0.25;
|
/// Must match `layout::TABLEAU_FAN_FRAC` so the initial layout and the first
|
||||||
|
/// dynamic update from `update_tableau_fan_frac` produce identical spacing.
|
||||||
|
pub const TABLEAU_FAN_FRAC: f32 = 0.18;
|
||||||
|
|
||||||
/// Per-card vertical step for face-down tableau cards, as a fraction of
|
/// Per-card vertical step for face-down tableau cards, as a fraction of
|
||||||
/// card height. Smaller than [`TABLEAU_FAN_FRAC`] because face-down cards
|
/// card height. Smaller than [`TABLEAU_FAN_FRAC`] because face-down cards
|
||||||
@@ -51,18 +53,22 @@ pub const TABLEAU_FAN_FRAC: f32 = 0.25;
|
|||||||
/// renderer creates a visible offset between the card face and where
|
/// renderer creates a visible offset between the card face and where
|
||||||
/// clicks land.
|
/// clicks land.
|
||||||
///
|
///
|
||||||
/// Matches `layout::TABLEAU_FACEDOWN_FAN_FRAC` (0.20). Both constants must
|
/// Matches `layout::TABLEAU_FACEDOWN_FAN_FRAC` (0.14). Both constants must
|
||||||
/// stay in sync; the layout constant drives the adaptive LayoutResource value
|
/// stay in sync; the layout constant drives the adaptive LayoutResource value
|
||||||
/// used at runtime, while this one is the minimum floor used by
|
/// used at runtime, while this one is the minimum floor used by
|
||||||
/// `update_tableau_fan_frac` when computing proportional updates.
|
/// `update_tableau_fan_frac` when computing proportional updates.
|
||||||
pub const TABLEAU_FACEDOWN_FAN_FRAC: f32 = 0.20;
|
pub const TABLEAU_FACEDOWN_FAN_FRAC: f32 = 0.14;
|
||||||
|
|
||||||
/// Fraction of card height used as a tiny offset between stacked cards in
|
/// Fraction of card height used as a tiny offset between stacked cards in
|
||||||
/// non-tableau piles, so stacking is visible. Public so other plugins
|
/// non-tableau piles, so stacking is visible. Public so other plugins
|
||||||
/// (e.g. input_plugin's drag-rejection tween) can compute the resting
|
/// (e.g. input_plugin's drag-rejection tween) can compute the resting
|
||||||
/// `Transform.translation.z` for a card at a given stack index without
|
/// `Transform.translation.z` for a card at a given stack index without
|
||||||
/// drifting from the value used by [`card_positions`].
|
/// drifting from the value used by [`card_positions`].
|
||||||
pub const STACK_FAN_FRAC: f32 = 0.003;
|
// Must exceed the highest child local-z of any card entity (0.02 for the
|
||||||
|
// Android corner label) so every card's sprite covers all children of the
|
||||||
|
// card below it. Raising from 0.003 → 0.025 fixes corner labels on
|
||||||
|
// foundation piles bleeding through when a 2 sits on an Ace.
|
||||||
|
pub const STACK_FAN_FRAC: f32 = 0.025;
|
||||||
|
|
||||||
/// Font size as a fraction of card width.
|
/// Font size as a fraction of card width.
|
||||||
const FONT_SIZE_FRAC: f32 = 0.28;
|
const FONT_SIZE_FRAC: f32 = 0.28;
|
||||||
@@ -178,8 +184,8 @@ pub struct CardLabel;
|
|||||||
/// readable at phone scale. Only exists when `CardImageSet` is present
|
/// readable at phone scale. Only exists when `CardImageSet` is present
|
||||||
/// (the fallback solid-colour path uses a plain `CardLabel` instead).
|
/// (the fallback solid-colour path uses a plain `CardLabel` instead).
|
||||||
#[cfg(target_os = "android")]
|
#[cfg(target_os = "android")]
|
||||||
#[derive(Component, Debug, Clone, Copy)]
|
#[derive(Component, Debug, Clone)]
|
||||||
struct AndroidCornerLabel;
|
struct AndroidCornerLabel(pub String);
|
||||||
|
|
||||||
/// Solid-colour background sprite behind [`AndroidCornerLabel`].
|
/// Solid-colour background sprite behind [`AndroidCornerLabel`].
|
||||||
///
|
///
|
||||||
@@ -691,15 +697,36 @@ fn sync_cards(
|
|||||||
) {
|
) {
|
||||||
let positions = card_positions(game, layout);
|
let positions = card_positions(game, layout);
|
||||||
|
|
||||||
// Map card_id -> (Entity, current_translation, has_card_animation) for
|
// The waste buffer card exists only to keep its entity alive while the new
|
||||||
// in-place updates. The `has_card_animation` flag lets `update_card_entity`
|
// top card's slide animation plays — it must never be visible to the player.
|
||||||
// skip the snap/slide path on cards that are already being driven by a
|
// Without this, the buffer sits at waste_base uncovered during the animation
|
||||||
// curve-based `CardAnimation` tween (e.g. the drag-rejection return tween
|
// and its rank/suit peek behind the incoming card.
|
||||||
// — see `input_plugin::end_drag`). Otherwise the StateChangedEvent that
|
let waste_buffer_id: Option<u32> = {
|
||||||
// accompanies a rejection would race the tween and the card would jump.
|
let visible = match game.draw_mode {
|
||||||
let mut existing: HashMap<u32, (Entity, Vec3, bool)> = HashMap::new();
|
DrawMode::DrawOne => 1_usize,
|
||||||
|
DrawMode::DrawThree => 3_usize,
|
||||||
|
};
|
||||||
|
game.piles
|
||||||
|
.get(&PileType::Waste)
|
||||||
|
.filter(|w| w.cards.len() > visible)
|
||||||
|
.and_then(|w| w.cards.get(w.cards.len().saturating_sub(visible + 1)))
|
||||||
|
.map(|c| c.id)
|
||||||
|
};
|
||||||
|
|
||||||
|
// Map card_id -> (Entity, current_translation, anim_end) for in-place
|
||||||
|
// updates. `anim_end` is `Some(end_xy)` when a curve-based `CardAnimation`
|
||||||
|
// is currently driving the card (e.g. a drag-rejection return tween).
|
||||||
|
//
|
||||||
|
// In the position loop below we compare `anim_end` against the new game-
|
||||||
|
// state target position to decide whether to honour or cancel the tween:
|
||||||
|
// • end ≈ target → animation is still heading to the right place; let
|
||||||
|
// it finish (skip the snap/slide path).
|
||||||
|
// • end ≠ target → the game state has changed (e.g. a new game started
|
||||||
|
// while the win-cascade was mid-flight); cancel the
|
||||||
|
// stale `CardAnimation` and apply the new position.
|
||||||
|
let mut existing: HashMap<u32, (Entity, Vec3, Option<Vec2>)> = HashMap::new();
|
||||||
for (entity, marker, transform, anim) in entities.iter() {
|
for (entity, marker, transform, anim) in entities.iter() {
|
||||||
existing.insert(marker.card_id, (entity, transform.translation, anim.is_some()));
|
existing.insert(marker.card_id, (entity, transform.translation, anim.map(|a| a.end)));
|
||||||
}
|
}
|
||||||
|
|
||||||
let live_ids: HashSet<u32> = positions.iter().map(|(c, _, _)| c.id).collect();
|
let live_ids: HashSet<u32> = positions.iter().map(|(c, _, _)| c.id).collect();
|
||||||
@@ -711,17 +738,38 @@ fn sync_cards(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// For each card in the current state: spawn or update its entity.
|
// For each card in the current state: spawn or update its entity, then
|
||||||
|
// apply visibility. The waste buffer card is hidden so it cannot peek
|
||||||
|
// behind the incoming top card during the draw slide animation.
|
||||||
for (card, position, z) in positions {
|
for (card, position, z) in positions {
|
||||||
match existing.get(&card.id) {
|
let entity = match existing.get(&card.id) {
|
||||||
Some(&(entity, cur, has_anim)) => {
|
Some(&(entity, cur, anim_end)) => {
|
||||||
|
// If a CardAnimation is in flight, check whether its destination
|
||||||
|
// still matches the game-state target. If the game moved the card
|
||||||
|
// elsewhere (e.g. new game started during a win-cascade scatter),
|
||||||
|
// cancel the stale tween so the card snaps/slides to its new home.
|
||||||
|
let has_anim = match anim_end {
|
||||||
|
Some(end_xy) if (end_xy - position).length() > 2.0 => {
|
||||||
|
commands.entity(entity).remove::<CardAnimation>();
|
||||||
|
false
|
||||||
|
}
|
||||||
|
Some(_) => true,
|
||||||
|
None => false,
|
||||||
|
};
|
||||||
update_card_entity(
|
update_card_entity(
|
||||||
&mut commands, entity, card, position, z, layout,
|
&mut commands, entity, card, position, z, layout,
|
||||||
slide_secs, back_colour, color_blind, high_contrast, cur, has_anim, card_images, selected_back, font_handle,
|
slide_secs, back_colour, color_blind, high_contrast, cur, has_anim, card_images, selected_back, font_handle,
|
||||||
)
|
);
|
||||||
|
entity
|
||||||
}
|
}
|
||||||
None => spawn_card_entity(&mut commands, card, position, z, layout, back_colour, color_blind, high_contrast, card_images, selected_back, font_handle),
|
None => spawn_card_entity(&mut commands, card, position, z, layout, back_colour, color_blind, high_contrast, card_images, selected_back, font_handle),
|
||||||
}
|
};
|
||||||
|
let visibility = if waste_buffer_id == Some(card.id) {
|
||||||
|
Visibility::Hidden
|
||||||
|
} else {
|
||||||
|
Visibility::Inherited
|
||||||
|
};
|
||||||
|
commands.entity(entity).insert(visibility);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -831,7 +879,7 @@ fn spawn_card_entity(
|
|||||||
card_images: Option<&CardImageSet>,
|
card_images: Option<&CardImageSet>,
|
||||||
selected_back: usize,
|
selected_back: usize,
|
||||||
font_handle: Option<&Handle<Font>>,
|
font_handle: Option<&Handle<Font>>,
|
||||||
) {
|
) -> Entity {
|
||||||
let sprite = card_sprite(card, layout.card_size, back_colour, card_images, selected_back);
|
let sprite = card_sprite(card, layout.card_size, back_colour, card_images, selected_back);
|
||||||
|
|
||||||
let mut entity = commands.spawn((
|
let mut entity = commands.spawn((
|
||||||
@@ -840,6 +888,7 @@ fn spawn_card_entity(
|
|||||||
Transform::from_xyz(pos.x, pos.y, z),
|
Transform::from_xyz(pos.x, pos.y, z),
|
||||||
Visibility::default(),
|
Visibility::default(),
|
||||||
));
|
));
|
||||||
|
let entity_id = entity.id();
|
||||||
// Every card gets a subtle drop-shadow child so the play surface reads
|
// Every card gets a subtle drop-shadow child so the play surface reads
|
||||||
// as physical instead of flat. Spawned in idle state; the drag-tracking
|
// as physical instead of flat. Spawned in idle state; the drag-tracking
|
||||||
// system retunes its offset / alpha when this card joins the dragged
|
// system retunes its offset / alpha when this card joins the dragged
|
||||||
@@ -880,6 +929,7 @@ fn spawn_card_entity(
|
|||||||
// Suppress unused-variable warning when not building for Android.
|
// Suppress unused-variable warning when not building for Android.
|
||||||
#[cfg(not(target_os = "android"))]
|
#[cfg(not(target_os = "android"))]
|
||||||
let _ = font_handle;
|
let _ = font_handle;
|
||||||
|
entity_id
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(clippy::too_many_arguments)]
|
#[allow(clippy::too_many_arguments)]
|
||||||
@@ -1115,10 +1165,11 @@ fn add_android_corner_label(
|
|||||||
// Large rank+suit text drawn on top of the background. FiraMono must be
|
// Large rank+suit text drawn on top of the background. FiraMono must be
|
||||||
// wired here explicitly — the suit glyphs (U+2660–U+2666) are not in
|
// wired here explicitly — the suit glyphs (U+2660–U+2666) are not in
|
||||||
// Bevy's built-in font and render as a coloured rectangle without it.
|
// Bevy's built-in font and render as a coloured rectangle without it.
|
||||||
|
let label_text = mobile_label_for(card);
|
||||||
parent.spawn((
|
parent.spawn((
|
||||||
AndroidCornerLabel,
|
AndroidCornerLabel(label_text.clone()),
|
||||||
CardLabel,
|
CardLabel,
|
||||||
Text2d::new(mobile_label_for(card)),
|
Text2d::new(label_text),
|
||||||
TextFont {
|
TextFont {
|
||||||
font: font_handle.cloned().unwrap_or_default(),
|
font: font_handle.cloned().unwrap_or_default(),
|
||||||
font_size,
|
font_size,
|
||||||
@@ -2062,7 +2113,7 @@ fn resize_cards_in_place(
|
|||||||
fn resize_android_corner_labels(
|
fn resize_android_corner_labels(
|
||||||
layout: Res<LayoutResource>,
|
layout: Res<LayoutResource>,
|
||||||
card_images: Option<Res<CardImageSet>>,
|
card_images: Option<Res<CardImageSet>>,
|
||||||
mut text_query: Query<(&mut TextFont, &mut Transform), With<AndroidCornerLabel>>,
|
mut text_query: Query<(&AndroidCornerLabel, &mut Text2d, &mut TextFont, &mut Transform)>,
|
||||||
mut bg_query: Query<
|
mut bg_query: Query<
|
||||||
(&mut Sprite, &mut Transform),
|
(&mut Sprite, &mut Transform),
|
||||||
(With<AndroidCornerBg>, Without<AndroidCornerLabel>),
|
(With<AndroidCornerBg>, Without<AndroidCornerLabel>),
|
||||||
@@ -2078,7 +2129,8 @@ fn resize_android_corner_labels(
|
|||||||
let text_x = -layout.0.card_size.x / 2.0 + inset;
|
let text_x = -layout.0.card_size.x / 2.0 + inset;
|
||||||
let text_y = layout.0.card_size.y / 2.0 - inset;
|
let text_y = layout.0.card_size.y / 2.0 - inset;
|
||||||
|
|
||||||
for (mut font, mut transform) in text_query.iter_mut() {
|
for (label, mut text2d, mut font, mut transform) in text_query.iter_mut() {
|
||||||
|
text2d.0 = label.0.clone();
|
||||||
font.font_size = font_size;
|
font.font_size = font_size;
|
||||||
transform.translation.x = text_x;
|
transform.translation.x = text_x;
|
||||||
transform.translation.y = text_y;
|
transform.translation.y = text_y;
|
||||||
@@ -2357,6 +2409,35 @@ mod tests {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The waste buffer card (slot below top) must be at the *same* XY as the
|
||||||
|
/// top card so that hiding it (`Visibility::Hidden`) leaves no visible gap.
|
||||||
|
#[test]
|
||||||
|
fn waste_draw_one_buffer_card_at_same_xy_as_top() {
|
||||||
|
use solitaire_core::game_state::DrawMode;
|
||||||
|
let mut g = GameState::new(42, DrawMode::DrawOne);
|
||||||
|
// Draw 3 times so the waste pile has 3 cards and the buffer exists.
|
||||||
|
for _ in 0..3 {
|
||||||
|
let _ = g.draw();
|
||||||
|
}
|
||||||
|
let waste_ids: std::collections::HashSet<u32> =
|
||||||
|
g.piles[&PileType::Waste].cards.iter().map(|c| c.id).collect();
|
||||||
|
let layout = crate::layout::compute_layout(Vec2::new(1280.0, 800.0), 0.0, 0.0, true);
|
||||||
|
let positions = card_positions(&g, &layout);
|
||||||
|
let waste_rendered: Vec<_> = positions
|
||||||
|
.iter()
|
||||||
|
.filter(|(card, _, _)| waste_ids.contains(&card.id))
|
||||||
|
.collect();
|
||||||
|
// Buffer (slot 0) + top (slot 1) = 2 rendered waste cards.
|
||||||
|
assert_eq!(waste_rendered.len(), 2, "Draw-One with 3 waste cards must render exactly 2");
|
||||||
|
// Both must share the same XY so that hiding the buffer leaves no gap.
|
||||||
|
let (_, pos0, _) = waste_rendered[0];
|
||||||
|
let (_, pos1, _) = waste_rendered[1];
|
||||||
|
assert!(
|
||||||
|
(pos0.x - pos1.x).abs() < 1e-3 && (pos0.y - pos1.y).abs() < 1e-3,
|
||||||
|
"buffer and top card must be at the same XY; got buffer={pos0:?} top={pos1:?}"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn card_positions_tableau_cards_are_fanned_downward() {
|
fn card_positions_tableau_cards_are_fanned_downward() {
|
||||||
let g = GameState::new(42, solitaire_core::game_state::DrawMode::DrawOne);
|
let g = GameState::new(42, solitaire_core::game_state::DrawMode::DrawOne);
|
||||||
|
|||||||
@@ -382,8 +382,8 @@ fn update_drop_target_overlays(
|
|||||||
/// for everything else it is card-sized. Replicated here rather than
|
/// for everything else it is card-sized. Replicated here rather than
|
||||||
/// imported because `pile_drop_rect` is private to `input_plugin` and
|
/// imported because `pile_drop_rect` is private to `input_plugin` and
|
||||||
/// this overlay is the only other consumer.
|
/// this overlay is the only other consumer.
|
||||||
fn drop_overlay_rect(pile: &PileType, layout: &Layout, game: &GameState) -> (Vec2, Vec2) {
|
fn drop_overlay_rect(pile: &PileType, layout: &Layout, game: &GameState) -> Option<(Vec2, Vec2)> {
|
||||||
let centre = layout.pile_positions[pile];
|
let centre = layout.pile_positions.get(pile).copied()?;
|
||||||
if matches!(pile, PileType::Tableau(_)) {
|
if matches!(pile, PileType::Tableau(_)) {
|
||||||
let card_count = game.piles.get(pile).map_or(0, |p| p.cards.len());
|
let card_count = game.piles.get(pile).map_or(0, |p| p.cards.len());
|
||||||
if card_count > 1 {
|
if card_count > 1 {
|
||||||
@@ -393,13 +393,13 @@ fn drop_overlay_rect(pile: &PileType, layout: &Layout, game: &GameState) -> (Vec
|
|||||||
let bottom_edge = bottom_card_centre_y - layout.card_size.y / 2.0;
|
let bottom_edge = bottom_card_centre_y - layout.card_size.y / 2.0;
|
||||||
let span_height = top_edge - bottom_edge;
|
let span_height = top_edge - bottom_edge;
|
||||||
let new_centre_y = (top_edge + bottom_edge) / 2.0;
|
let new_centre_y = (top_edge + bottom_edge) / 2.0;
|
||||||
return (
|
return Some((
|
||||||
Vec2::new(centre.x, new_centre_y),
|
Vec2::new(centre.x, new_centre_y),
|
||||||
Vec2::new(layout.card_size.x, span_height),
|
Vec2::new(layout.card_size.x, span_height),
|
||||||
);
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
(centre, layout.card_size)
|
Some((centre, layout.card_size))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Spawns one overlay parent (fill) plus four edge sprites (outline) at
|
/// Spawns one overlay parent (fill) plus four edge sprites (outline) at
|
||||||
@@ -410,7 +410,10 @@ fn spawn_drop_target_overlay(
|
|||||||
layout: &Layout,
|
layout: &Layout,
|
||||||
game: &GameState,
|
game: &GameState,
|
||||||
) {
|
) {
|
||||||
let (centre, size) = drop_overlay_rect(pile, layout, game);
|
let Some((centre, size)) = drop_overlay_rect(pile, layout, game) else {
|
||||||
|
warn!("drop_overlay_rect: pile {pile:?} not in layout, skipping overlay");
|
||||||
|
return;
|
||||||
|
};
|
||||||
let edge = DROP_TARGET_OUTLINE_PX;
|
let edge = DROP_TARGET_OUTLINE_PX;
|
||||||
|
|
||||||
commands
|
commands
|
||||||
|
|||||||
@@ -210,10 +210,15 @@ impl Plugin for FeedbackAnimPlugin {
|
|||||||
start_shake_anim.after(GameMutation),
|
start_shake_anim.after(GameMutation),
|
||||||
tick_shake_anim,
|
tick_shake_anim,
|
||||||
start_settle_anim.after(GameMutation),
|
start_settle_anim.after(GameMutation),
|
||||||
|
// tick_foundation_flourish writes the full Transform.scale
|
||||||
|
// (Vec3); tick_settle_anim writes only scale.y on top of
|
||||||
|
// it. Ordering ensures the settle's y-only write always
|
||||||
|
// applies last so it wins on the ~0.15 s overlap when both
|
||||||
|
// components are present on the same King entity.
|
||||||
|
tick_foundation_flourish.before(tick_settle_anim),
|
||||||
tick_settle_anim,
|
tick_settle_anim,
|
||||||
start_deal_anim.after(GameMutation),
|
start_deal_anim.after(GameMutation),
|
||||||
start_foundation_flourish.after(GameMutation),
|
start_foundation_flourish.after(GameMutation),
|
||||||
tick_foundation_flourish,
|
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,8 +31,15 @@ fn load_font(fonts: Option<ResMut<Assets<Font>>>, mut commands: Commands) {
|
|||||||
// Assets<Font>). FontPlugin in that context is a no-op — consumers
|
// Assets<Font>). FontPlugin in that context is a no-op — consumers
|
||||||
// already query `Option<Res<FontResource>>` and degrade cleanly.
|
// already query `Option<Res<FontResource>>` and degrade cleanly.
|
||||||
let Some(mut fonts) = fonts else { return };
|
let Some(mut fonts) = fonts else { return };
|
||||||
let font = Font::try_from_bytes(BUNDLED_FONT_BYTES.to_vec())
|
let font = match Font::try_from_bytes(BUNDLED_FONT_BYTES.to_vec()) {
|
||||||
.expect("bundled FiraMono failed to parse — binary is corrupt");
|
Ok(f) => f,
|
||||||
|
Err(e) => {
|
||||||
|
// A corrupt embedded font is unusual but should not crash the
|
||||||
|
// process — UI will render without glyphs rather than panicking.
|
||||||
|
warn!("bundled FiraMono failed to parse ({e}); UI text may be invisible");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
};
|
||||||
let handle = fonts.add(font);
|
let handle = fonts.add(font);
|
||||||
commands.insert_resource(FontResource(handle));
|
commands.insert_resource(FontResource(handle));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ use crate::font_plugin::FontResource;
|
|||||||
use crate::hud_plugin::ANDROID_HINT_LABEL;
|
use crate::hud_plugin::ANDROID_HINT_LABEL;
|
||||||
use crate::ui_modal::{
|
use crate::ui_modal::{
|
||||||
spawn_modal, spawn_modal_actions, spawn_modal_button, spawn_modal_header, ButtonVariant,
|
spawn_modal, spawn_modal_actions, spawn_modal_button, spawn_modal_header, ButtonVariant,
|
||||||
ScrimDismissible,
|
ModalScrim, ScrimDismissible,
|
||||||
};
|
};
|
||||||
use crate::ui_theme::{SPACE_2, TEXT_PRIMARY, TEXT_SECONDARY, TYPE_BODY, VAL_SPACE_2, VAL_SPACE_3, Z_MODAL_PANEL};
|
use crate::ui_theme::{SPACE_2, TEXT_PRIMARY, TEXT_SECONDARY, TYPE_BODY, VAL_SPACE_2, VAL_SPACE_3, Z_MODAL_PANEL};
|
||||||
#[cfg(not(target_os = "android"))]
|
#[cfg(not(target_os = "android"))]
|
||||||
@@ -67,6 +67,7 @@ fn toggle_help_screen(
|
|||||||
keys: Res<ButtonInput<KeyCode>>,
|
keys: Res<ButtonInput<KeyCode>>,
|
||||||
mut requests: MessageReader<HelpRequestEvent>,
|
mut requests: MessageReader<HelpRequestEvent>,
|
||||||
screens: Query<Entity, With<HelpScreen>>,
|
screens: Query<Entity, With<HelpScreen>>,
|
||||||
|
other_modal_scrims: Query<(), (With<ModalScrim>, Without<HelpScreen>)>,
|
||||||
font_res: Option<Res<FontResource>>,
|
font_res: Option<Res<FontResource>>,
|
||||||
) {
|
) {
|
||||||
// Either F1 or a click on the HUD "Help" button (which fires
|
// Either F1 or a click on the HUD "Help" button (which fires
|
||||||
@@ -77,7 +78,7 @@ fn toggle_help_screen(
|
|||||||
}
|
}
|
||||||
if let Ok(entity) = screens.single() {
|
if let Ok(entity) = screens.single() {
|
||||||
commands.entity(entity).despawn();
|
commands.entity(entity).despawn();
|
||||||
} else {
|
} else if other_modal_scrims.is_empty() {
|
||||||
spawn_help_screen(&mut commands, font_res.as_deref());
|
spawn_help_screen(&mut commands, font_res.as_deref());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ use crate::layout::LayoutSystem;
|
|||||||
use crate::pause_plugin::PausedResource;
|
use crate::pause_plugin::PausedResource;
|
||||||
use crate::resources::GameStateResource;
|
use crate::resources::GameStateResource;
|
||||||
#[cfg(target_os = "android")]
|
#[cfg(target_os = "android")]
|
||||||
use crate::resources::DragState;
|
use crate::resources::{DragState, GameInputConsumedResource};
|
||||||
use crate::selection_plugin::SelectionState;
|
use crate::selection_plugin::SelectionState;
|
||||||
use crate::time_attack_plugin::TimeAttackResource;
|
use crate::time_attack_plugin::TimeAttackResource;
|
||||||
use crate::ui_focus::{FocusGroup, Focusable};
|
use crate::ui_focus::{FocusGroup, Focusable};
|
||||||
@@ -870,12 +870,12 @@ fn spawn_action_buttons(
|
|||||||
);
|
);
|
||||||
#[cfg(not(target_os = "android"))]
|
#[cfg(not(target_os = "android"))]
|
||||||
let labels = (
|
let labels = (
|
||||||
"Menu \u{25BE}",
|
"Menu \u{2193}",
|
||||||
"Undo",
|
"Undo",
|
||||||
"Pause",
|
"Pause",
|
||||||
"Help",
|
"Help",
|
||||||
"Hint",
|
"Hint",
|
||||||
"Modes \u{25BE}",
|
"Modes \u{2193}",
|
||||||
"New Game",
|
"New Game",
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -2492,6 +2492,7 @@ fn toggle_hud_on_tap(
|
|||||||
mut tracker: ResMut<HudTapTracker>,
|
mut tracker: ResMut<HudTapTracker>,
|
||||||
mut hud_vis: ResMut<HudVisibility>,
|
mut hud_vis: ResMut<HudVisibility>,
|
||||||
buttons: Query<&Interaction, With<ActionButton>>,
|
buttons: Query<&Interaction, With<ActionButton>>,
|
||||||
|
mut game_consumed: ResMut<GameInputConsumedResource>,
|
||||||
) {
|
) {
|
||||||
use bevy::input::touch::TouchPhase;
|
use bevy::input::touch::TouchPhase;
|
||||||
if !scrims.is_empty() || paused.is_some_and(|p| p.0) {
|
if !scrims.is_empty() || paused.is_some_and(|p| p.0) {
|
||||||
@@ -2502,6 +2503,7 @@ fn toggle_hud_on_tap(
|
|||||||
for _ in touch_events.read() {}
|
for _ in touch_events.read() {}
|
||||||
tracker.start_pos = None;
|
tracker.start_pos = None;
|
||||||
tracker.started_on_button = false;
|
tracker.started_on_button = false;
|
||||||
|
game_consumed.0 = false;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
for event in touch_events.read() {
|
for event in touch_events.read() {
|
||||||
@@ -2515,7 +2517,13 @@ fn toggle_hud_on_tap(
|
|||||||
buttons.iter().any(|i| *i != Interaction::None);
|
buttons.iter().any(|i| *i != Interaction::None);
|
||||||
}
|
}
|
||||||
TouchPhase::Ended if drag.is_idle() => {
|
TouchPhase::Ended if drag.is_idle() => {
|
||||||
let on_button = tracker.started_on_button;
|
// Also treat taps where game logic consumed the touch (e.g.
|
||||||
|
// drawing from stock) as "on button" so they don't toggle
|
||||||
|
// the HUD. The flag is set on TouchPhase::Started by the
|
||||||
|
// input system that consumed the tap and must be cleared here
|
||||||
|
// regardless of whether we toggle.
|
||||||
|
let on_button = tracker.started_on_button || game_consumed.0;
|
||||||
|
game_consumed.0 = false;
|
||||||
if let Some(start) = tracker.start_pos.take() {
|
if let Some(start) = tracker.start_pos.take() {
|
||||||
if !on_button && (event.position - start).length() < HUD_TAP_SLOP_PX {
|
if !on_button && (event.position - start).length() < HUD_TAP_SLOP_PX {
|
||||||
*hud_vis = match *hud_vis {
|
*hud_vis = match *hud_vis {
|
||||||
@@ -2532,6 +2540,7 @@ fn toggle_hud_on_tap(
|
|||||||
TouchPhase::Canceled => {
|
TouchPhase::Canceled => {
|
||||||
tracker.start_pos = None;
|
tracker.start_pos = None;
|
||||||
tracker.started_on_button = false;
|
tracker.started_on_button = false;
|
||||||
|
game_consumed.0 = false;
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -47,7 +47,8 @@ use crate::game_plugin::{ConfirmNewGameScreen, GameMutation, RestorePromptScreen
|
|||||||
use crate::pause_plugin::PausedResource;
|
use crate::pause_plugin::PausedResource;
|
||||||
use crate::progress_plugin::ProgressResource;
|
use crate::progress_plugin::ProgressResource;
|
||||||
use crate::layout::{Layout, LayoutResource};
|
use crate::layout::{Layout, LayoutResource};
|
||||||
use crate::resources::{DragState, GameStateResource, HintCycleIndex};
|
use crate::replay_playback::ReplayPlaybackState;
|
||||||
|
use crate::resources::{DragState, GameInputConsumedResource, GameStateResource, HintCycleIndex};
|
||||||
use crate::selection_plugin::SelectionState;
|
use crate::selection_plugin::SelectionState;
|
||||||
use crate::time_attack_plugin::TimeAttackResource;
|
use crate::time_attack_plugin::TimeAttackResource;
|
||||||
|
|
||||||
@@ -95,6 +96,7 @@ impl Plugin for InputPlugin {
|
|||||||
app.init_resource::<HintCycleIndex>()
|
app.init_resource::<HintCycleIndex>()
|
||||||
.init_resource::<HintSolverConfig>()
|
.init_resource::<HintSolverConfig>()
|
||||||
.init_resource::<crate::pending_hint::PendingHintTask>()
|
.init_resource::<crate::pending_hint::PendingHintTask>()
|
||||||
|
.init_resource::<GameInputConsumedResource>()
|
||||||
.add_message::<StartZenRequestEvent>()
|
.add_message::<StartZenRequestEvent>()
|
||||||
.add_message::<InfoToastEvent>()
|
.add_message::<InfoToastEvent>()
|
||||||
.add_message::<ForfeitRequestEvent>()
|
.add_message::<ForfeitRequestEvent>()
|
||||||
@@ -174,11 +176,20 @@ fn handle_keyboard_core(
|
|||||||
mut zen_requests: MessageReader<StartZenRequestEvent>,
|
mut zen_requests: MessageReader<StartZenRequestEvent>,
|
||||||
confirm_screens: Query<(), With<ConfirmNewGameScreen>>,
|
confirm_screens: Query<(), With<ConfirmNewGameScreen>>,
|
||||||
restore_prompts: Query<(), With<RestorePromptScreen>>,
|
restore_prompts: Query<(), With<RestorePromptScreen>>,
|
||||||
|
replay_state: Option<Res<ReplayPlaybackState>>,
|
||||||
) {
|
) {
|
||||||
if paused.is_some_and(|p| p.0) {
|
if paused.is_some_and(|p| p.0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// During replay playback (Playing or Completed) all game-input shortcuts
|
||||||
|
// are suppressed. The replay overlay owns Space (pause/resume) and the
|
||||||
|
// arrow keys (step). Letting game input through would mutate
|
||||||
|
// `GameStateResource` and corrupt replay determinism.
|
||||||
|
if replay_state.is_some_and(|r| !matches!(*r, ReplayPlaybackState::Inactive)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if keys.just_pressed(KeyCode::KeyU) {
|
if keys.just_pressed(KeyCode::KeyU) {
|
||||||
ev.undo.write(UndoRequestEvent);
|
ev.undo.write(UndoRequestEvent);
|
||||||
}
|
}
|
||||||
@@ -501,6 +512,7 @@ fn handle_touch_stock_tap(
|
|||||||
layout: Option<Res<LayoutResource>>,
|
layout: Option<Res<LayoutResource>>,
|
||||||
drag: Res<DragState>,
|
drag: Res<DragState>,
|
||||||
mut draw: MessageWriter<DrawRequestEvent>,
|
mut draw: MessageWriter<DrawRequestEvent>,
|
||||||
|
mut game_consumed: ResMut<GameInputConsumedResource>,
|
||||||
) {
|
) {
|
||||||
if paused.is_some_and(|p| p.0) {
|
if paused.is_some_and(|p| p.0) {
|
||||||
return;
|
return;
|
||||||
@@ -522,6 +534,7 @@ fn handle_touch_stock_tap(
|
|||||||
};
|
};
|
||||||
if point_in_rect(world, stock_pos, layout.0.card_size) {
|
if point_in_rect(world, stock_pos, layout.0.card_size) {
|
||||||
draw.write(DrawRequestEvent);
|
draw.write(DrawRequestEvent);
|
||||||
|
game_consumed.0 = true;
|
||||||
break; // one draw per tap frame
|
break; // one draw per tap frame
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -123,7 +123,7 @@ pub struct Layout {
|
|||||||
pub pile_positions: HashMap<PileType, Vec2>,
|
pub pile_positions: HashMap<PileType, Vec2>,
|
||||||
/// Per-step vertical offset fraction for face-up tableau cards, as a
|
/// Per-step vertical offset fraction for face-up tableau cards, as a
|
||||||
/// fraction of `card_size.y`. On height-limited (desktop) windows this
|
/// fraction of `card_size.y`. On height-limited (desktop) windows this
|
||||||
/// equals `TABLEAU_FAN_FRAC` (0.25); on width-limited (portrait phone)
|
/// equals `TABLEAU_FAN_FRAC` (0.18); on width-limited (portrait phone)
|
||||||
/// windows it expands to fill the available vertical space so the tableau
|
/// windows it expands to fill the available vertical space so the tableau
|
||||||
/// stretches to the bottom of the screen. Card rendering (`card_plugin`)
|
/// stretches to the bottom of the screen. Card rendering (`card_plugin`)
|
||||||
/// and hit testing (`input_plugin`) both read from this field so they
|
/// and hit testing (`input_plugin`) both read from this field so they
|
||||||
|
|||||||
@@ -15,13 +15,13 @@ use bevy::tasks::{futures_lite::future, AsyncComputeTaskPool, Task};
|
|||||||
use solitaire_data::{save_settings_to, settings::SyncBackend};
|
use solitaire_data::{save_settings_to, settings::SyncBackend};
|
||||||
use solitaire_sync::LeaderboardEntry;
|
use solitaire_sync::LeaderboardEntry;
|
||||||
|
|
||||||
use crate::events::{InfoToastEvent, ToggleLeaderboardRequestEvent};
|
use crate::events::{InfoToastEvent, ToggleLeaderboardRequestEvent, WarningToastEvent};
|
||||||
use crate::font_plugin::FontResource;
|
use crate::font_plugin::FontResource;
|
||||||
use crate::settings_plugin::{SettingsResource, SettingsStoragePath};
|
use crate::settings_plugin::{SettingsResource, SettingsStoragePath};
|
||||||
use crate::sync_plugin::SyncProviderResource;
|
use crate::sync_plugin::SyncProviderResource;
|
||||||
use crate::ui_modal::{
|
use crate::ui_modal::{
|
||||||
spawn_modal, spawn_modal_actions, spawn_modal_button, spawn_modal_header, ButtonVariant,
|
spawn_modal, spawn_modal_actions, spawn_modal_button, spawn_modal_header, ButtonVariant,
|
||||||
ScrimDismissible,
|
ModalScrim, ScrimDismissible,
|
||||||
};
|
};
|
||||||
use crate::ui_theme::{
|
use crate::ui_theme::{
|
||||||
ACCENT_PRIMARY, BG_ELEVATED, BORDER_SUBTLE, RADIUS_SM, STATE_INFO,
|
ACCENT_PRIMARY, BG_ELEVATED, BORDER_SUBTLE, RADIUS_SM, STATE_INFO,
|
||||||
@@ -138,6 +138,8 @@ impl Plugin for LeaderboardPlugin {
|
|||||||
.init_resource::<OptOutTask>()
|
.init_resource::<OptOutTask>()
|
||||||
.init_resource::<DisplayNameBuffer>()
|
.init_resource::<DisplayNameBuffer>()
|
||||||
.add_message::<ToggleLeaderboardRequestEvent>()
|
.add_message::<ToggleLeaderboardRequestEvent>()
|
||||||
|
.add_message::<WarningToastEvent>()
|
||||||
|
.add_message::<InfoToastEvent>()
|
||||||
// `MouseWheel` and `KeyboardInput` are emitted by Bevy's input
|
// `MouseWheel` and `KeyboardInput` are emitted by Bevy's input
|
||||||
// plugin under `DefaultPlugins`; register them explicitly so all
|
// plugin under `DefaultPlugins`; register them explicitly so all
|
||||||
// leaderboard systems run cleanly under `MinimalPlugins` in tests.
|
// leaderboard systems run cleanly under `MinimalPlugins` in tests.
|
||||||
@@ -159,6 +161,7 @@ impl Plugin for LeaderboardPlugin {
|
|||||||
handle_display_name_text_input,
|
handle_display_name_text_input,
|
||||||
handle_display_name_confirm,
|
handle_display_name_confirm,
|
||||||
handle_display_name_cancel,
|
handle_display_name_cancel,
|
||||||
|
update_leaderboard_public_name_label,
|
||||||
)
|
)
|
||||||
.chain(),
|
.chain(),
|
||||||
)
|
)
|
||||||
@@ -361,10 +364,13 @@ fn handle_opt_in_button(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Polls the opt-in task; fires an `InfoToastEvent` on completion or failure.
|
/// Polls the opt-in task; fires a toast and persists opted-in state on completion.
|
||||||
fn poll_opt_in_task(
|
fn poll_opt_in_task(
|
||||||
mut task_res: ResMut<OptInTask>,
|
mut task_res: ResMut<OptInTask>,
|
||||||
mut toast: MessageWriter<InfoToastEvent>,
|
mut toast: MessageWriter<InfoToastEvent>,
|
||||||
|
mut warn_toast: MessageWriter<WarningToastEvent>,
|
||||||
|
settings: Option<ResMut<SettingsResource>>,
|
||||||
|
settings_path: Option<Res<SettingsStoragePath>>,
|
||||||
) {
|
) {
|
||||||
let Some(task) = task_res.0.as_mut() else { return };
|
let Some(task) = task_res.0.as_mut() else { return };
|
||||||
let Some(result) = future::block_on(future::poll_once(task)) else { return };
|
let Some(result) = future::block_on(future::poll_once(task)) else { return };
|
||||||
@@ -372,10 +378,18 @@ fn poll_opt_in_task(
|
|||||||
match result {
|
match result {
|
||||||
Ok(()) => {
|
Ok(()) => {
|
||||||
toast.write(InfoToastEvent("Opted in to leaderboard".to_string()));
|
toast.write(InfoToastEvent("Opted in to leaderboard".to_string()));
|
||||||
|
if let Some(mut s) = settings {
|
||||||
|
s.0.leaderboard_opted_in = true;
|
||||||
|
if let Some(path) = settings_path.as_ref().and_then(|p| p.0.as_ref())
|
||||||
|
&& let Err(e) = save_settings_to(path, &s.0)
|
||||||
|
{
|
||||||
|
warn!("failed to save settings after opt-in: {e}");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
warn!("leaderboard opt-in failed: {e}");
|
warn!("leaderboard opt-in failed: {e}");
|
||||||
toast.write(InfoToastEvent("Failed to join leaderboard".to_string()));
|
warn_toast.write(WarningToastEvent("Failed to join leaderboard".to_string()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -401,10 +415,13 @@ fn handle_opt_out_button(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Polls the opt-out task; fires an `InfoToastEvent` on completion or failure.
|
/// Polls the opt-out task; fires a toast and clears opted-in state on completion.
|
||||||
fn poll_opt_out_task(
|
fn poll_opt_out_task(
|
||||||
mut task_res: ResMut<OptOutTask>,
|
mut task_res: ResMut<OptOutTask>,
|
||||||
mut toast: MessageWriter<InfoToastEvent>,
|
mut toast: MessageWriter<InfoToastEvent>,
|
||||||
|
mut warn_toast: MessageWriter<WarningToastEvent>,
|
||||||
|
settings: Option<ResMut<SettingsResource>>,
|
||||||
|
settings_path: Option<Res<SettingsStoragePath>>,
|
||||||
) {
|
) {
|
||||||
let Some(task) = task_res.0.as_mut() else { return };
|
let Some(task) = task_res.0.as_mut() else { return };
|
||||||
let Some(result) = future::block_on(future::poll_once(task)) else { return };
|
let Some(result) = future::block_on(future::poll_once(task)) else { return };
|
||||||
@@ -412,10 +429,18 @@ fn poll_opt_out_task(
|
|||||||
match result {
|
match result {
|
||||||
Ok(()) => {
|
Ok(()) => {
|
||||||
toast.write(InfoToastEvent("Opted out of leaderboard".to_string()));
|
toast.write(InfoToastEvent("Opted out of leaderboard".to_string()));
|
||||||
|
if let Some(mut s) = settings {
|
||||||
|
s.0.leaderboard_opted_in = false;
|
||||||
|
if let Some(path) = settings_path.as_ref().and_then(|p| p.0.as_ref())
|
||||||
|
&& let Err(e) = save_settings_to(path, &s.0)
|
||||||
|
{
|
||||||
|
warn!("failed to save settings after opt-out: {e}");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
warn!("leaderboard opt-out failed: {e}");
|
warn!("leaderboard opt-out failed: {e}");
|
||||||
toast.write(InfoToastEvent("Failed to leave leaderboard".to_string()));
|
warn_toast.write(WarningToastEvent("Failed to leave leaderboard".to_string()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -428,6 +453,12 @@ fn poll_opt_out_task(
|
|||||||
#[derive(Component, Debug)]
|
#[derive(Component, Debug)]
|
||||||
pub struct LeaderboardCloseButton;
|
pub struct LeaderboardCloseButton;
|
||||||
|
|
||||||
|
/// Marker on the "Public name: …" label inside the leaderboard panel so it
|
||||||
|
/// can be updated reactively when the player changes their display name
|
||||||
|
/// without a full panel rebuild.
|
||||||
|
#[derive(Component, Debug)]
|
||||||
|
struct LeaderboardPublicNameText;
|
||||||
|
|
||||||
fn spawn_leaderboard_screen(
|
fn spawn_leaderboard_screen(
|
||||||
commands: &mut Commands,
|
commands: &mut Commands,
|
||||||
data: &LeaderboardResource,
|
data: &LeaderboardResource,
|
||||||
@@ -481,6 +512,7 @@ fn spawn_leaderboard_screen(
|
|||||||
None => "Public name: (same as username)".to_string(),
|
None => "Public name: (same as username)".to_string(),
|
||||||
};
|
};
|
||||||
row.spawn((
|
row.spawn((
|
||||||
|
LeaderboardPublicNameText,
|
||||||
Text::new(label),
|
Text::new(label),
|
||||||
font_caption.clone(),
|
font_caption.clone(),
|
||||||
TextColor(TEXT_SECONDARY),
|
TextColor(TEXT_SECONDARY),
|
||||||
@@ -683,6 +715,7 @@ fn data_cell(
|
|||||||
fn handle_set_display_name_button(
|
fn handle_set_display_name_button(
|
||||||
button_q: Query<&Interaction, (Changed<Interaction>, With<SetDisplayNameButton>)>,
|
button_q: Query<&Interaction, (Changed<Interaction>, With<SetDisplayNameButton>)>,
|
||||||
existing: Query<(), With<DisplayNameModal>>,
|
existing: Query<(), With<DisplayNameModal>>,
|
||||||
|
other_modal_scrims: Query<(), (With<ModalScrim>, Without<DisplayNameModal>)>,
|
||||||
mut commands: Commands,
|
mut commands: Commands,
|
||||||
settings: Option<Res<SettingsResource>>,
|
settings: Option<Res<SettingsResource>>,
|
||||||
font_res: Option<Res<FontResource>>,
|
font_res: Option<Res<FontResource>>,
|
||||||
@@ -694,6 +727,9 @@ fn handle_set_display_name_button(
|
|||||||
if !existing.is_empty() {
|
if !existing.is_empty() {
|
||||||
return; // already open
|
return; // already open
|
||||||
}
|
}
|
||||||
|
if !other_modal_scrims.is_empty() {
|
||||||
|
return; // Another modal is already visible.
|
||||||
|
}
|
||||||
buf.0 = settings
|
buf.0 = settings
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.and_then(|s| s.0.leaderboard_display_name.clone())
|
.and_then(|s| s.0.leaderboard_display_name.clone())
|
||||||
@@ -733,7 +769,9 @@ fn handle_display_name_text_input(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Saves the typed display name to `SettingsResource` and closes the modal.
|
/// Saves the typed display name to `SettingsResource`, closes the modal, and
|
||||||
|
/// pushes the new name to the server when the player is already opted in.
|
||||||
|
#[allow(clippy::too_many_arguments)]
|
||||||
fn handle_display_name_confirm(
|
fn handle_display_name_confirm(
|
||||||
button_q: Query<&Interaction, (Changed<Interaction>, With<DisplayNameConfirmButton>)>,
|
button_q: Query<&Interaction, (Changed<Interaction>, With<DisplayNameConfirmButton>)>,
|
||||||
screens: Query<Entity, With<DisplayNameModal>>,
|
screens: Query<Entity, With<DisplayNameModal>>,
|
||||||
@@ -741,6 +779,8 @@ fn handle_display_name_confirm(
|
|||||||
buf: Res<DisplayNameBuffer>,
|
buf: Res<DisplayNameBuffer>,
|
||||||
settings: Option<ResMut<SettingsResource>>,
|
settings: Option<ResMut<SettingsResource>>,
|
||||||
settings_path: Option<Res<SettingsStoragePath>>,
|
settings_path: Option<Res<SettingsStoragePath>>,
|
||||||
|
provider: Option<Res<SyncProviderResource>>,
|
||||||
|
mut task_res: ResMut<OptInTask>,
|
||||||
) {
|
) {
|
||||||
if !button_q.iter().any(|i| *i == Interaction::Pressed) {
|
if !button_q.iter().any(|i| *i == Interaction::Pressed) {
|
||||||
return;
|
return;
|
||||||
@@ -750,13 +790,47 @@ fn handle_display_name_confirm(
|
|||||||
settings.0.leaderboard_display_name = if trimmed.is_empty() {
|
settings.0.leaderboard_display_name = if trimmed.is_empty() {
|
||||||
None
|
None
|
||||||
} else {
|
} else {
|
||||||
Some(trimmed)
|
Some(trimmed.clone())
|
||||||
};
|
};
|
||||||
if let Some(path) = settings_path.as_ref().and_then(|p| p.0.as_ref())
|
if let Some(path) = settings_path.as_ref().and_then(|p| p.0.as_ref())
|
||||||
&& let Err(e) = save_settings_to(path, &settings.0)
|
&& let Err(e) = save_settings_to(path, &settings.0)
|
||||||
{
|
{
|
||||||
warn!("failed to save settings: {e}");
|
warn!("failed to save settings: {e}");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Push updated name to the server when already opted in and no task
|
||||||
|
// is in flight. The server's opt-in endpoint is an upsert, so calling
|
||||||
|
// it a second time only updates the display_name column.
|
||||||
|
let is_remote = provider
|
||||||
|
.as_ref()
|
||||||
|
.is_some_and(|p| p.0.backend_name() != "local");
|
||||||
|
if settings.0.leaderboard_opted_in && is_remote && task_res.0.is_none() {
|
||||||
|
let display_name = settings
|
||||||
|
.0
|
||||||
|
.leaderboard_display_name
|
||||||
|
.clone()
|
||||||
|
.unwrap_or_else(|| {
|
||||||
|
if let solitaire_data::settings::SyncBackend::SolitaireServer {
|
||||||
|
ref username,
|
||||||
|
..
|
||||||
|
} = settings.0.sync_backend
|
||||||
|
{
|
||||||
|
username.chars().take(32).collect()
|
||||||
|
} else {
|
||||||
|
"Player".to_string()
|
||||||
|
}
|
||||||
|
});
|
||||||
|
if let Some(p) = provider {
|
||||||
|
let provider = p.0.clone();
|
||||||
|
let task = AsyncComputeTaskPool::get().spawn(async move {
|
||||||
|
provider
|
||||||
|
.opt_in_leaderboard(&display_name)
|
||||||
|
.await
|
||||||
|
.map_err(|e| e.to_string())
|
||||||
|
});
|
||||||
|
task_res.0 = Some(task);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
for entity in &screens {
|
for entity in &screens {
|
||||||
commands.entity(entity).despawn();
|
commands.entity(entity).despawn();
|
||||||
@@ -857,6 +931,25 @@ fn spawn_display_name_modal(
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Keeps the "Public name: …" label in the leaderboard panel in sync with
|
||||||
|
/// `SettingsResource` after the player saves a new display name. No-op when
|
||||||
|
/// the panel is closed (`labels.is_empty()` exits immediately).
|
||||||
|
fn update_leaderboard_public_name_label(
|
||||||
|
settings: Option<Res<SettingsResource>>,
|
||||||
|
mut labels: Query<&mut Text, With<LeaderboardPublicNameText>>,
|
||||||
|
) {
|
||||||
|
if labels.is_empty() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let new_label = match settings.as_ref().and_then(|s| s.0.leaderboard_display_name.as_deref()) {
|
||||||
|
Some(n) => format!("Public name: {n}"),
|
||||||
|
None => "Public name: (same as username)".to_string(),
|
||||||
|
};
|
||||||
|
for mut text in &mut labels {
|
||||||
|
text.0 = new_label.clone();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Accepts printable ASCII characters (0x20–0x7e) for the display-name field.
|
/// Accepts printable ASCII characters (0x20–0x7e) for the display-name field.
|
||||||
fn printable_char_dn(text: &str) -> Option<char> {
|
fn printable_char_dn(text: &str) -> Option<char> {
|
||||||
let ch = text.chars().next()?;
|
let ch = text.chars().next()?;
|
||||||
@@ -1048,4 +1141,224 @@ mod tests {
|
|||||||
// 65 seconds = 1:05, not 1:5
|
// 65 seconds = 1:05, not 1:5
|
||||||
assert_eq!(format_secs(65), "1:05");
|
assert_eq!(format_secs(65), "1:05");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
// Bug-fix regression tests
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
|
||||||
|
fn headless_app_with_settings() -> App {
|
||||||
|
let mut app = headless_app();
|
||||||
|
app.insert_resource(SettingsResource(solitaire_data::settings::Settings::default()));
|
||||||
|
app
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Bug 1: opt-in errors must fire `WarningToastEvent`, not `InfoToastEvent`.
|
||||||
|
#[test]
|
||||||
|
fn opt_in_error_fires_warning_toast() {
|
||||||
|
use bevy::ecs::message::Messages;
|
||||||
|
|
||||||
|
let mut app = headless_app_with_settings();
|
||||||
|
|
||||||
|
// Inject a pre-resolved failed task directly into OptInTask.
|
||||||
|
let failed_task = AsyncComputeTaskPool::get()
|
||||||
|
.spawn(async { Err::<(), String>("network error".to_string()) });
|
||||||
|
app.world_mut().resource_mut::<OptInTask>().0 = Some(failed_task);
|
||||||
|
|
||||||
|
// Pump until the task is polled or a deadline elapses. A fixed
|
||||||
|
// update count is unreliable under parallel `cargo test --workspace`
|
||||||
|
// load — the AsyncComputeTaskPool background threads can be starved
|
||||||
|
// long enough that 5 updates finish before the task completes.
|
||||||
|
// Mirrors the deadline-loop pattern used in sync_plugin tests.
|
||||||
|
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(5);
|
||||||
|
loop {
|
||||||
|
app.update();
|
||||||
|
let msgs = app.world().resource::<Messages<WarningToastEvent>>();
|
||||||
|
let mut cursor = msgs.get_cursor();
|
||||||
|
if cursor.read(msgs).next().is_some() {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if std::time::Instant::now() >= deadline {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
std::thread::yield_now();
|
||||||
|
}
|
||||||
|
|
||||||
|
let msgs = app.world().resource::<Messages<WarningToastEvent>>();
|
||||||
|
let mut cursor = msgs.get_cursor();
|
||||||
|
assert!(
|
||||||
|
cursor.read(msgs).next().is_some(),
|
||||||
|
"WarningToastEvent must be fired when opt-in fails"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Bug 1: opt-out errors must fire `WarningToastEvent`, not `InfoToastEvent`.
|
||||||
|
#[test]
|
||||||
|
fn opt_out_error_fires_warning_toast() {
|
||||||
|
use bevy::ecs::message::Messages;
|
||||||
|
|
||||||
|
let mut app = headless_app_with_settings();
|
||||||
|
|
||||||
|
let failed_task = AsyncComputeTaskPool::get()
|
||||||
|
.spawn(async { Err::<(), String>("network error".to_string()) });
|
||||||
|
app.world_mut().resource_mut::<OptOutTask>().0 = Some(failed_task);
|
||||||
|
|
||||||
|
// Deadline-bounded pump — see opt_in_error_fires_warning_toast for rationale.
|
||||||
|
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(5);
|
||||||
|
loop {
|
||||||
|
app.update();
|
||||||
|
let msgs = app.world().resource::<Messages<WarningToastEvent>>();
|
||||||
|
let mut cursor = msgs.get_cursor();
|
||||||
|
if cursor.read(msgs).next().is_some() {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if std::time::Instant::now() >= deadline {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
std::thread::yield_now();
|
||||||
|
}
|
||||||
|
|
||||||
|
let msgs = app.world().resource::<Messages<WarningToastEvent>>();
|
||||||
|
let mut cursor = msgs.get_cursor();
|
||||||
|
assert!(
|
||||||
|
cursor.read(msgs).next().is_some(),
|
||||||
|
"WarningToastEvent must be fired when opt-out fails"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Bug 2: successful opt-in must set `leaderboard_opted_in = true` in Settings.
|
||||||
|
#[test]
|
||||||
|
fn opt_in_success_sets_opted_in_flag() {
|
||||||
|
let mut app = headless_app_with_settings();
|
||||||
|
|
||||||
|
// Confirm the flag starts false.
|
||||||
|
assert!(!app
|
||||||
|
.world()
|
||||||
|
.resource::<SettingsResource>()
|
||||||
|
.0
|
||||||
|
.leaderboard_opted_in);
|
||||||
|
|
||||||
|
let ok_task = AsyncComputeTaskPool::get().spawn(async { Ok::<(), String>(()) });
|
||||||
|
app.world_mut().resource_mut::<OptInTask>().0 = Some(ok_task);
|
||||||
|
|
||||||
|
// Deadline-bounded pump — see opt_in_error_fires_warning_toast for rationale.
|
||||||
|
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(5);
|
||||||
|
loop {
|
||||||
|
app.update();
|
||||||
|
if app
|
||||||
|
.world()
|
||||||
|
.resource::<SettingsResource>()
|
||||||
|
.0
|
||||||
|
.leaderboard_opted_in
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if std::time::Instant::now() >= deadline {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
std::thread::yield_now();
|
||||||
|
}
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
app.world()
|
||||||
|
.resource::<SettingsResource>()
|
||||||
|
.0
|
||||||
|
.leaderboard_opted_in,
|
||||||
|
"leaderboard_opted_in must be true after successful opt-in"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Bug 2: successful opt-out must clear `leaderboard_opted_in`.
|
||||||
|
#[test]
|
||||||
|
fn opt_out_success_clears_opted_in_flag() {
|
||||||
|
let mut app = headless_app_with_settings();
|
||||||
|
|
||||||
|
// Seed as opted in.
|
||||||
|
app.world_mut()
|
||||||
|
.resource_mut::<SettingsResource>()
|
||||||
|
.0
|
||||||
|
.leaderboard_opted_in = true;
|
||||||
|
|
||||||
|
let ok_task = AsyncComputeTaskPool::get().spawn(async { Ok::<(), String>(()) });
|
||||||
|
app.world_mut().resource_mut::<OptOutTask>().0 = Some(ok_task);
|
||||||
|
|
||||||
|
// Deadline-bounded pump — see opt_in_error_fires_warning_toast for rationale.
|
||||||
|
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(5);
|
||||||
|
loop {
|
||||||
|
app.update();
|
||||||
|
if !app
|
||||||
|
.world()
|
||||||
|
.resource::<SettingsResource>()
|
||||||
|
.0
|
||||||
|
.leaderboard_opted_in
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if std::time::Instant::now() >= deadline {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
std::thread::yield_now();
|
||||||
|
}
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
!app.world()
|
||||||
|
.resource::<SettingsResource>()
|
||||||
|
.0
|
||||||
|
.leaderboard_opted_in,
|
||||||
|
"leaderboard_opted_in must be false after successful opt-out"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Bug 3: `LeaderboardPublicNameText` label must reflect a display-name
|
||||||
|
/// change applied to `SettingsResource` without a panel rebuild.
|
||||||
|
#[test]
|
||||||
|
fn public_name_label_updates_reactively() {
|
||||||
|
let mut app = headless_app_with_settings();
|
||||||
|
|
||||||
|
// Open the panel.
|
||||||
|
press(&mut app, KeyCode::KeyL);
|
||||||
|
app.update();
|
||||||
|
|
||||||
|
// Verify the label starts with the default copy.
|
||||||
|
let initial: String = app
|
||||||
|
.world_mut()
|
||||||
|
.query_filtered::<&Text, With<LeaderboardPublicNameText>>()
|
||||||
|
.iter(app.world())
|
||||||
|
.next()
|
||||||
|
.expect("LeaderboardPublicNameText must exist while panel is open")
|
||||||
|
.0
|
||||||
|
.clone();
|
||||||
|
assert!(
|
||||||
|
initial.contains("same as username"),
|
||||||
|
"initial label should say '(same as username)' when no display name is set"
|
||||||
|
);
|
||||||
|
|
||||||
|
// Clear just-pressed state so `toggle_leaderboard_screen` doesn't
|
||||||
|
// re-fire in the next frame (MinimalPlugins has no input-tick system).
|
||||||
|
{
|
||||||
|
let mut input = app.world_mut().resource_mut::<ButtonInput<KeyCode>>();
|
||||||
|
input.release(KeyCode::KeyL);
|
||||||
|
input.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update the display name in SettingsResource.
|
||||||
|
app.world_mut()
|
||||||
|
.resource_mut::<SettingsResource>()
|
||||||
|
.0
|
||||||
|
.leaderboard_display_name = Some("TestPlayer".to_string());
|
||||||
|
|
||||||
|
app.update();
|
||||||
|
|
||||||
|
let updated: String = app
|
||||||
|
.world_mut()
|
||||||
|
.query_filtered::<&Text, With<LeaderboardPublicNameText>>()
|
||||||
|
.iter(app.world())
|
||||||
|
.next()
|
||||||
|
.expect("LeaderboardPublicNameText must still exist")
|
||||||
|
.0
|
||||||
|
.clone();
|
||||||
|
assert!(
|
||||||
|
updated.contains("TestPlayer"),
|
||||||
|
"label must reflect new display name after settings change"
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -138,12 +138,13 @@ fn handle_open_dialog(
|
|||||||
mut requests: MessageReader<StartPlayBySeedRequestEvent>,
|
mut requests: MessageReader<StartPlayBySeedRequestEvent>,
|
||||||
font_res: Option<Res<FontResource>>,
|
font_res: Option<Res<FontResource>>,
|
||||||
existing: Query<(), With<PlayBySeedScreen>>,
|
existing: Query<(), With<PlayBySeedScreen>>,
|
||||||
|
other_scrims: Query<(), (With<crate::ui_modal::ModalScrim>, Without<PlayBySeedScreen>)>,
|
||||||
) {
|
) {
|
||||||
if requests.read().count() == 0 {
|
if requests.read().count() == 0 {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// Guard against double-spawn (e.g. two events in one frame).
|
// Guard against double-spawn (e.g. two events in one frame) or stacking over another modal.
|
||||||
if !existing.is_empty() {
|
if !existing.is_empty() || !other_scrims.is_empty() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
let font = font_res.as_deref();
|
let font = font_res.as_deref();
|
||||||
@@ -411,7 +412,11 @@ fn handle_confirm(
|
|||||||
new_game.write(NewGameRequestEvent {
|
new_game.write(NewGameRequestEvent {
|
||||||
seed: Some(seed),
|
seed: Some(seed),
|
||||||
mode: None,
|
mode: None,
|
||||||
confirmed: false,
|
// The player explicitly clicked Play (or pressed Enter) after typing
|
||||||
|
// a seed — treat this as an affirmative confirmation so the
|
||||||
|
// abandon-current-game dialog is not shown on top of the already-
|
||||||
|
// dismissed seed dialog.
|
||||||
|
confirmed: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
for entity in &screen {
|
for entity in &screen {
|
||||||
@@ -566,7 +571,9 @@ mod tests {
|
|||||||
assert_eq!(fired.len(), 1);
|
assert_eq!(fired.len(), 1);
|
||||||
assert_eq!(fired[0].seed, Some(42));
|
assert_eq!(fired[0].seed, Some(42));
|
||||||
assert_eq!(fired[0].mode, None);
|
assert_eq!(fired[0].mode, None);
|
||||||
assert!(!fired[0].confirmed);
|
// confirmed: true — the player explicitly clicked Play, so no
|
||||||
|
// abandon-current-game dialog should appear.
|
||||||
|
assert!(fired[0].confirmed);
|
||||||
|
|
||||||
// Dialog should be gone.
|
// Dialog should be gone.
|
||||||
assert!(!dialog_present(&mut app));
|
assert!(!dialog_present(&mut app));
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ use chrono::Datelike;
|
|||||||
|
|
||||||
use crate::font_plugin::FontResource;
|
use crate::font_plugin::FontResource;
|
||||||
use crate::layout::LayoutResource;
|
use crate::layout::LayoutResource;
|
||||||
use crate::events::{DrawRequestEvent, MoveRequestEvent, UndoRequestEvent};
|
use crate::events::{DrawRequestEvent, MoveRequestEvent, StateChangedEvent, UndoRequestEvent};
|
||||||
use crate::replay_playback::{
|
use crate::replay_playback::{
|
||||||
step_backwards_replay_playback, step_replay_playback, stop_replay_playback,
|
step_backwards_replay_playback, step_replay_playback, stop_replay_playback,
|
||||||
toggle_pause_replay_playback, ReplayPlaybackState,
|
toggle_pause_replay_playback, ReplayPlaybackState,
|
||||||
@@ -476,6 +476,7 @@ impl Plugin for ReplayOverlayPlugin {
|
|||||||
.add_message::<MoveRequestEvent>()
|
.add_message::<MoveRequestEvent>()
|
||||||
.add_message::<DrawRequestEvent>()
|
.add_message::<DrawRequestEvent>()
|
||||||
.add_message::<UndoRequestEvent>()
|
.add_message::<UndoRequestEvent>()
|
||||||
|
.add_message::<StateChangedEvent>()
|
||||||
.add_systems(
|
.add_systems(
|
||||||
Update,
|
Update,
|
||||||
(
|
(
|
||||||
@@ -1884,6 +1885,7 @@ fn handle_pause_keyboard(
|
|||||||
/// resets to 0 on key release so the next fresh press fires
|
/// resets to 0 on key release so the next fresh press fires
|
||||||
/// immediately. This matches the mockup's `[← →] scrub`
|
/// immediately. This matches the mockup's `[← →] scrub`
|
||||||
/// terminology while keeping single-press = single-step semantics.
|
/// terminology while keeping single-press = single-step semantics.
|
||||||
|
#[allow(clippy::too_many_arguments)]
|
||||||
fn handle_arrow_keyboard(
|
fn handle_arrow_keyboard(
|
||||||
keys: Option<Res<ButtonInput<KeyCode>>>,
|
keys: Option<Res<ButtonInput<KeyCode>>>,
|
||||||
time: Res<Time>,
|
time: Res<Time>,
|
||||||
@@ -1892,10 +1894,22 @@ fn handle_arrow_keyboard(
|
|||||||
mut moves_writer: MessageWriter<MoveRequestEvent>,
|
mut moves_writer: MessageWriter<MoveRequestEvent>,
|
||||||
mut draws_writer: MessageWriter<DrawRequestEvent>,
|
mut draws_writer: MessageWriter<DrawRequestEvent>,
|
||||||
mut undo_writer: MessageWriter<UndoRequestEvent>,
|
mut undo_writer: MessageWriter<UndoRequestEvent>,
|
||||||
|
mut state_changed: MessageReader<StateChangedEvent>,
|
||||||
|
// `true` while a backward step is in-flight: cursor was decremented and
|
||||||
|
// `UndoRequestEvent` was written, but `handle_undo` hasn't applied it yet.
|
||||||
|
// Cleared when `StateChangedEvent` confirms the game state has caught up.
|
||||||
|
// Prevents rapid ← presses from accumulating multiple cursor decrements
|
||||||
|
// before any undo is applied (Bug #16).
|
||||||
|
mut back_pending: Local<bool>,
|
||||||
) {
|
) {
|
||||||
let Some(keys) = keys else { return };
|
let Some(keys) = keys else { return };
|
||||||
let dt = time.delta_secs();
|
let dt = time.delta_secs();
|
||||||
|
|
||||||
|
// Clear the in-flight flag once the game confirms the undo landed.
|
||||||
|
if state_changed.read().count() > 0 {
|
||||||
|
*back_pending = false;
|
||||||
|
}
|
||||||
|
|
||||||
// Right (forward step) — initial press fires immediately;
|
// Right (forward step) — initial press fires immediately;
|
||||||
// held repeats fire when the accumulator crosses the interval.
|
// held repeats fire when the accumulator crosses the interval.
|
||||||
if keys.just_pressed(KeyCode::ArrowRight) {
|
if keys.just_pressed(KeyCode::ArrowRight) {
|
||||||
@@ -1911,14 +1925,28 @@ fn handle_arrow_keyboard(
|
|||||||
hold.right_held_secs = 0.0;
|
hold.right_held_secs = 0.0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Left (backwards step) — symmetric to the right path.
|
// Left (backwards step) — gate on `back_pending` so at most one undo
|
||||||
|
// is in-flight at a time. The cursor is only decremented inside
|
||||||
|
// `step_backwards_replay_playback`, which also writes `UndoRequestEvent`.
|
||||||
|
// `back_pending` is set after a successful step and cleared above when
|
||||||
|
// `StateChangedEvent` confirms the undo was applied.
|
||||||
if keys.just_pressed(KeyCode::ArrowLeft) {
|
if keys.just_pressed(KeyCode::ArrowLeft) {
|
||||||
step_backwards_replay_playback(&mut state, &mut undo_writer);
|
if !*back_pending {
|
||||||
|
let fired = step_backwards_replay_playback(&mut state, &mut undo_writer);
|
||||||
|
if fired {
|
||||||
|
*back_pending = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
hold.left_held_secs = 0.0;
|
hold.left_held_secs = 0.0;
|
||||||
} else if keys.pressed(KeyCode::ArrowLeft) {
|
} else if keys.pressed(KeyCode::ArrowLeft) {
|
||||||
hold.left_held_secs += dt;
|
hold.left_held_secs += dt;
|
||||||
if hold.left_held_secs >= SCRUB_REPEAT_INTERVAL_SECS {
|
if hold.left_held_secs >= SCRUB_REPEAT_INTERVAL_SECS {
|
||||||
step_backwards_replay_playback(&mut state, &mut undo_writer);
|
if !*back_pending {
|
||||||
|
let fired = step_backwards_replay_playback(&mut state, &mut undo_writer);
|
||||||
|
if fired {
|
||||||
|
*back_pending = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
hold.left_held_secs = 0.0;
|
hold.left_held_secs = 0.0;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -512,6 +512,7 @@ pub struct ReplayPlaybackPlugin;
|
|||||||
impl Plugin for ReplayPlaybackPlugin {
|
impl Plugin for ReplayPlaybackPlugin {
|
||||||
fn build(&self, app: &mut App) {
|
fn build(&self, app: &mut App) {
|
||||||
app.init_resource::<ReplayPlaybackState>()
|
app.init_resource::<ReplayPlaybackState>()
|
||||||
|
.add_message::<StateChangedEvent>()
|
||||||
.add_systems(
|
.add_systems(
|
||||||
Update,
|
Update,
|
||||||
(
|
(
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use bevy::math::Vec2;
|
use bevy::math::Vec2;
|
||||||
use bevy::prelude::Resource;
|
use bevy::prelude::{warn, Resource};
|
||||||
use chrono::{DateTime, Utc};
|
use chrono::{DateTime, Utc};
|
||||||
use solitaire_core::game_state::GameState;
|
use solitaire_core::game_state::GameState;
|
||||||
use solitaire_core::pile::PileType;
|
use solitaire_core::pile::PileType;
|
||||||
@@ -114,6 +114,13 @@ pub struct HintCycleIndex(pub usize);
|
|||||||
#[derive(Resource, Debug, Clone, Default)]
|
#[derive(Resource, Debug, Clone, Default)]
|
||||||
pub struct SettingsScrollPos(pub f32);
|
pub struct SettingsScrollPos(pub f32);
|
||||||
|
|
||||||
|
/// Set to `true` by an input system when a touch tap is consumed by game logic
|
||||||
|
/// (e.g. drawing from stock). `toggle_hud_on_tap` checks this flag on
|
||||||
|
/// `TouchPhase::Ended` and skips the HUD visibility toggle when set, then
|
||||||
|
/// resets it to `false` so subsequent taps behave normally.
|
||||||
|
#[derive(Resource, Debug, Clone, Default)]
|
||||||
|
pub struct GameInputConsumedResource(pub bool);
|
||||||
|
|
||||||
/// Shared Tokio runtime used by all async-task closures that need HTTP I/O.
|
/// Shared Tokio runtime used by all async-task closures that need HTTP I/O.
|
||||||
///
|
///
|
||||||
/// Bevy's `AsyncComputeTaskPool` uses `async-executor` (not Tokio), so spawned
|
/// Bevy's `AsyncComputeTaskPool` uses `async-executor` (not Tokio), so spawned
|
||||||
@@ -124,15 +131,48 @@ pub struct SettingsScrollPos(pub f32);
|
|||||||
#[derive(Resource, Clone)]
|
#[derive(Resource, Clone)]
|
||||||
pub struct TokioRuntimeResource(pub Arc<tokio::runtime::Runtime>);
|
pub struct TokioRuntimeResource(pub Arc<tokio::runtime::Runtime>);
|
||||||
|
|
||||||
impl Default for TokioRuntimeResource {
|
impl TokioRuntimeResource {
|
||||||
fn default() -> Self {
|
/// Attempts to build the shared multi-threaded Tokio runtime.
|
||||||
// Building the Tokio runtime is startup-time initialization; failure
|
///
|
||||||
// here means the OS refused to create threads, which is unrecoverable.
|
/// Returns `Err` if the OS refuses to create worker threads (e.g. resource
|
||||||
|
/// limits on Android). Callers should log the error and disable sync
|
||||||
|
/// features rather than panicking.
|
||||||
|
pub fn new() -> Result<Self, tokio::io::Error> {
|
||||||
let rt = tokio::runtime::Builder::new_multi_thread()
|
let rt = tokio::runtime::Builder::new_multi_thread()
|
||||||
.worker_threads(2)
|
.worker_threads(2)
|
||||||
.enable_all()
|
.enable_all()
|
||||||
.build()
|
.build()?;
|
||||||
.expect("failed to build shared Tokio runtime");
|
Ok(Self(Arc::new(rt)))
|
||||||
Self(Arc::new(rt))
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for TokioRuntimeResource {
|
||||||
|
fn default() -> Self {
|
||||||
|
// Try multi-threaded first; fall back to current-thread (single
|
||||||
|
// worker) if the OS refuses to create additional threads. Neither
|
||||||
|
// path uses `.expect()` so this never panics at startup.
|
||||||
|
match tokio::runtime::Builder::new_multi_thread()
|
||||||
|
.worker_threads(2)
|
||||||
|
.enable_all()
|
||||||
|
.build()
|
||||||
|
{
|
||||||
|
Ok(rt) => Self(Arc::new(rt)),
|
||||||
|
Err(e) => {
|
||||||
|
warn!(
|
||||||
|
"sync: failed to build multi-thread Tokio runtime ({e}); \
|
||||||
|
falling back to current-thread runtime"
|
||||||
|
);
|
||||||
|
// current_thread runtime never spawns OS threads, so it
|
||||||
|
// succeeds even under tight sandboxing.
|
||||||
|
let rt = tokio::runtime::Builder::new_current_thread()
|
||||||
|
.enable_all()
|
||||||
|
.build()
|
||||||
|
.expect(
|
||||||
|
"current-thread Tokio runtime failed — \
|
||||||
|
the process cannot do any async I/O",
|
||||||
|
);
|
||||||
|
Self(Arc::new(rt))
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -510,6 +510,7 @@ fn toggle_settings_screen(
|
|||||||
fn sync_settings_panel_visibility(
|
fn sync_settings_panel_visibility(
|
||||||
screen: Res<SettingsScreen>,
|
screen: Res<SettingsScreen>,
|
||||||
panels: Query<Entity, With<SettingsPanel>>,
|
panels: Query<Entity, With<SettingsPanel>>,
|
||||||
|
other_modal_scrims: Query<(), (With<ModalScrim>, Without<SettingsPanel>)>,
|
||||||
scroll_nodes: Query<&ScrollPosition, With<SettingsScrollNode>>,
|
scroll_nodes: Query<&ScrollPosition, With<SettingsScrollNode>>,
|
||||||
mut scroll_pos: ResMut<SettingsScrollPos>,
|
mut scroll_pos: ResMut<SettingsScrollPos>,
|
||||||
mut commands: Commands,
|
mut commands: Commands,
|
||||||
@@ -525,7 +526,7 @@ fn sync_settings_panel_visibility(
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if screen.0 {
|
if screen.0 {
|
||||||
if panels.is_empty() {
|
if panels.is_empty() && other_modal_scrims.is_empty() {
|
||||||
let status_label = sync_status
|
let status_label = sync_status
|
||||||
.map_or_else(|| "Status: local only".to_string(), |s| sync_status_label(&s.0));
|
.map_or_else(|| "Status: local only".to_string(), |s| sync_status_label(&s.0));
|
||||||
let unlocked_backs = progress
|
let unlocked_backs = progress
|
||||||
|
|||||||
@@ -220,7 +220,13 @@ impl Plugin for StatsPlugin {
|
|||||||
)
|
)
|
||||||
.add_systems(
|
.add_systems(
|
||||||
Update,
|
Update,
|
||||||
handle_forfeit.before(GameMutation),
|
// handle_forfeit must run before update_stats_on_new_game so
|
||||||
|
// the NewGameRequestEvent it emits is not visible to
|
||||||
|
// update_stats_on_new_game in the same frame — otherwise
|
||||||
|
// record_abandoned() fires twice on every forfeit (#21).
|
||||||
|
handle_forfeit
|
||||||
|
.before(GameMutation)
|
||||||
|
.before(update_stats_on_new_game),
|
||||||
)
|
)
|
||||||
.add_systems(Update, toggle_stats_screen.after(GameMutation))
|
.add_systems(Update, toggle_stats_screen.after(GameMutation))
|
||||||
.add_systems(Update, handle_stats_close_button)
|
.add_systems(Update, handle_stats_close_button)
|
||||||
|
|||||||
@@ -101,7 +101,6 @@ impl SyncPlugin {
|
|||||||
impl Plugin for SyncPlugin {
|
impl Plugin for SyncPlugin {
|
||||||
fn build(&self, app: &mut App) {
|
fn build(&self, app: &mut App) {
|
||||||
app.insert_resource(SyncProviderResource(self.provider.clone()))
|
app.insert_resource(SyncProviderResource(self.provider.clone()))
|
||||||
.init_resource::<TokioRuntimeResource>()
|
|
||||||
.init_resource::<SyncStatusResource>()
|
.init_resource::<SyncStatusResource>()
|
||||||
.init_resource::<PullTaskResult>()
|
.init_resource::<PullTaskResult>()
|
||||||
.init_resource::<PullTask>()
|
.init_resource::<PullTask>()
|
||||||
@@ -109,18 +108,30 @@ impl Plugin for SyncPlugin {
|
|||||||
.add_message::<ManualSyncRequestEvent>()
|
.add_message::<ManualSyncRequestEvent>()
|
||||||
.add_message::<SyncCompleteEvent>()
|
.add_message::<SyncCompleteEvent>()
|
||||||
.add_message::<SyncConfigureRequestEvent>()
|
.add_message::<SyncConfigureRequestEvent>()
|
||||||
.add_message::<WarningToastEvent>()
|
.add_message::<WarningToastEvent>();
|
||||||
.add_systems(Startup, start_pull)
|
|
||||||
.add_systems(
|
// Build the shared Tokio runtime; disable all network sync if the OS
|
||||||
Update,
|
// refuses to create threads (resource-limited environments, sandboxed
|
||||||
(
|
// Android builds, etc.).
|
||||||
poll_pull_result,
|
match TokioRuntimeResource::new() {
|
||||||
handle_manual_sync_request,
|
Ok(rt) => {
|
||||||
push_replay_on_win,
|
app.insert_resource(rt)
|
||||||
poll_replay_upload_result,
|
.add_systems(Startup, start_pull)
|
||||||
),
|
.add_systems(
|
||||||
)
|
Update,
|
||||||
.add_systems(Last, push_on_exit);
|
(
|
||||||
|
poll_pull_result,
|
||||||
|
handle_manual_sync_request,
|
||||||
|
push_replay_on_win,
|
||||||
|
poll_replay_upload_result,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.add_systems(Last, push_on_exit);
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
warn!("sync: failed to create Tokio runtime — network sync disabled: {e}");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -55,7 +55,7 @@ use crate::font_plugin::FontResource;
|
|||||||
use crate::settings_plugin::{SettingsResource, SettingsScreen, SettingsStoragePath};
|
use crate::settings_plugin::{SettingsResource, SettingsScreen, SettingsStoragePath};
|
||||||
use crate::resources::TokioRuntimeResource;
|
use crate::resources::TokioRuntimeResource;
|
||||||
use crate::sync_plugin::SyncProviderResource;
|
use crate::sync_plugin::SyncProviderResource;
|
||||||
use crate::ui_modal::spawn_modal;
|
use crate::ui_modal::{spawn_modal, ModalScrim};
|
||||||
use crate::ui_theme::{
|
use crate::ui_theme::{
|
||||||
ACCENT_PRIMARY, BG_ELEVATED, BG_ELEVATED_HI,
|
ACCENT_PRIMARY, BG_ELEVATED, BG_ELEVATED_HI,
|
||||||
BORDER_SUBTLE, HighContrastBorder, RADIUS_SM, STATE_DANGER, TEXT_DISABLED,
|
BORDER_SUBTLE, HighContrastBorder, RADIUS_SM, STATE_DANGER, TEXT_DISABLED,
|
||||||
@@ -208,6 +208,7 @@ impl Plugin for SyncSetupPlugin {
|
|||||||
fn open_sync_setup_modal(
|
fn open_sync_setup_modal(
|
||||||
mut events: MessageReader<SyncConfigureRequestEvent>,
|
mut events: MessageReader<SyncConfigureRequestEvent>,
|
||||||
existing: Query<(), With<SyncSetupScreen>>,
|
existing: Query<(), With<SyncSetupScreen>>,
|
||||||
|
other_modal_scrims: Query<(), (With<ModalScrim>, Without<SyncSetupScreen>)>,
|
||||||
mut commands: Commands,
|
mut commands: Commands,
|
||||||
mut focused: ResMut<SyncFocusedField>,
|
mut focused: ResMut<SyncFocusedField>,
|
||||||
font_res: Option<Res<FontResource>>,
|
font_res: Option<Res<FontResource>>,
|
||||||
@@ -219,6 +220,9 @@ fn open_sync_setup_modal(
|
|||||||
if !existing.is_empty() {
|
if !existing.is_empty() {
|
||||||
return; // Already open.
|
return; // Already open.
|
||||||
}
|
}
|
||||||
|
if !other_modal_scrims.is_empty() {
|
||||||
|
return; // Another modal is already visible.
|
||||||
|
}
|
||||||
*focused = SyncFocusedField::Url;
|
*focused = SyncFocusedField::Url;
|
||||||
spawn_sync_setup_modal(&mut commands, font_res.as_deref());
|
spawn_sync_setup_modal(&mut commands, font_res.as_deref());
|
||||||
}
|
}
|
||||||
@@ -301,7 +305,7 @@ fn update_field_borders(
|
|||||||
fn handle_auth_button(
|
fn handle_auth_button(
|
||||||
login_q: Query<&Interaction, (Changed<Interaction>, With<SyncLoginButton>)>,
|
login_q: Query<&Interaction, (Changed<Interaction>, With<SyncLoginButton>)>,
|
||||||
register_q: Query<&Interaction, (Changed<Interaction>, With<SyncRegisterButton>)>,
|
register_q: Query<&Interaction, (Changed<Interaction>, With<SyncRegisterButton>)>,
|
||||||
fields: Query<(&SyncFieldKind, &SyncFieldBuffer)>,
|
mut fields: Query<(&SyncFieldKind, &mut SyncFieldBuffer)>,
|
||||||
rt: Res<TokioRuntimeResource>,
|
rt: Res<TokioRuntimeResource>,
|
||||||
mut pending: ResMut<PendingAuthTask>,
|
mut pending: ResMut<PendingAuthTask>,
|
||||||
mut error_nodes: Query<(&mut Text, &mut TextColor), With<SyncAuthError>>,
|
mut error_nodes: Query<(&mut Text, &mut TextColor), With<SyncAuthError>>,
|
||||||
@@ -354,9 +358,10 @@ fn handle_auth_button(
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clear error and show busy indicator.
|
// Clear previous error and show busy indicator.
|
||||||
for (mut text, _) in &mut error_nodes {
|
for (mut text, mut color) in &mut error_nodes {
|
||||||
text.0 = "Connecting…".to_string();
|
text.0 = String::new();
|
||||||
|
color.0 = TEXT_SECONDARY;
|
||||||
}
|
}
|
||||||
for mut vis in &mut busy_nodes {
|
for mut vis in &mut busy_nodes {
|
||||||
*vis = Visibility::Visible;
|
*vis = Visibility::Visible;
|
||||||
@@ -387,6 +392,14 @@ fn handle_auth_button(
|
|||||||
pending.task = Some(task);
|
pending.task = Some(task);
|
||||||
pending.url = url;
|
pending.url = url;
|
||||||
pending.username = username;
|
pending.username = username;
|
||||||
|
|
||||||
|
// Zero the password buffer immediately — it must not linger in ECS
|
||||||
|
// components after the credential has been handed off to the async task.
|
||||||
|
for (kind, mut buf) in &mut fields {
|
||||||
|
if *kind == SyncFieldKind::Password {
|
||||||
|
buf.0.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Polls the in-flight auth task. On success updates settings + provider.
|
/// Polls the in-flight auth task. On success updates settings + provider.
|
||||||
@@ -540,6 +553,7 @@ fn handle_logout(
|
|||||||
fn open_delete_confirm_modal(
|
fn open_delete_confirm_modal(
|
||||||
mut events: MessageReader<DeleteAccountRequestEvent>,
|
mut events: MessageReader<DeleteAccountRequestEvent>,
|
||||||
existing: Query<(), With<DeleteConfirmScreen>>,
|
existing: Query<(), With<DeleteConfirmScreen>>,
|
||||||
|
other_modal_scrims: Query<(), (With<ModalScrim>, Without<DeleteConfirmScreen>)>,
|
||||||
mut commands: Commands,
|
mut commands: Commands,
|
||||||
font_res: Option<Res<FontResource>>,
|
font_res: Option<Res<FontResource>>,
|
||||||
) {
|
) {
|
||||||
@@ -550,6 +564,9 @@ fn open_delete_confirm_modal(
|
|||||||
if !existing.is_empty() {
|
if !existing.is_empty() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if !other_modal_scrims.is_empty() {
|
||||||
|
return; // Another modal is already visible.
|
||||||
|
}
|
||||||
spawn_delete_confirm_modal(&mut commands, font_res.as_deref());
|
spawn_delete_confirm_modal(&mut commands, font_res.as_deref());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -675,20 +692,31 @@ fn spawn_sync_setup_modal(commands: &mut Commands, font_res: Option<&FontResourc
|
|||||||
font_res,
|
font_res,
|
||||||
);
|
);
|
||||||
|
|
||||||
// Error / status line.
|
// Error / status line — two distinct children so visibility and
|
||||||
|
// text can be controlled independently.
|
||||||
body.spawn(Node {
|
body.spawn(Node {
|
||||||
min_height: Val::Px(18.0),
|
min_height: Val::Px(18.0),
|
||||||
|
flex_direction: FlexDirection::Row,
|
||||||
|
align_items: AlignItems::Center,
|
||||||
|
column_gap: VAL_SPACE_2,
|
||||||
..default()
|
..default()
|
||||||
})
|
})
|
||||||
.with_children(|row| {
|
.with_children(|row| {
|
||||||
|
// Busy indicator: shown while the auth task is in flight.
|
||||||
row.spawn((
|
row.spawn((
|
||||||
SyncAuthError,
|
|
||||||
SyncBusyOverlay,
|
SyncBusyOverlay,
|
||||||
Text::new(String::new()),
|
Text::new("…"),
|
||||||
make_font(font_res, TYPE_CAPTION),
|
make_font(font_res, TYPE_CAPTION),
|
||||||
TextColor(TEXT_SECONDARY),
|
TextColor(TEXT_SECONDARY),
|
||||||
Visibility::Hidden,
|
Visibility::Hidden,
|
||||||
));
|
));
|
||||||
|
// Error / status text: always laid out, empty when idle.
|
||||||
|
row.spawn((
|
||||||
|
SyncAuthError,
|
||||||
|
Text::new(String::new()),
|
||||||
|
make_font(font_res, TYPE_CAPTION),
|
||||||
|
TextColor(TEXT_SECONDARY),
|
||||||
|
));
|
||||||
});
|
});
|
||||||
|
|
||||||
// Tab hint — desktop only; no Tab key on Android.
|
// Tab hint — desktop only; no Tab key on Android.
|
||||||
|
|||||||
@@ -182,12 +182,16 @@ fn sync_card_image_set_with_active_theme(
|
|||||||
mut events: MessageReader<AssetEvent<CardTheme>>,
|
mut events: MessageReader<AssetEvent<CardTheme>>,
|
||||||
active: Option<Res<ActiveTheme>>,
|
active: Option<Res<ActiveTheme>>,
|
||||||
themes: Res<Assets<CardTheme>>,
|
themes: Res<Assets<CardTheme>>,
|
||||||
|
asset_server: Option<Res<AssetServer>>,
|
||||||
mut card_image_set: Option<ResMut<CardImageSet>>,
|
mut card_image_set: Option<ResMut<CardImageSet>>,
|
||||||
mut state_events: MessageWriter<StateChangedEvent>,
|
mut state_events: MessageWriter<StateChangedEvent>,
|
||||||
) {
|
) {
|
||||||
let Some(active) = active else { return };
|
let Some(active) = active else { return };
|
||||||
let active_id = active.0.id();
|
let active_id = active.0.id();
|
||||||
|
|
||||||
let mut should_sync = false;
|
let mut should_sync = false;
|
||||||
|
|
||||||
|
// Consume asset events — covers the normal first-load path.
|
||||||
for ev in events.read() {
|
for ev in events.read() {
|
||||||
let id = match ev {
|
let id = match ev {
|
||||||
AssetEvent::LoadedWithDependencies { id }
|
AssetEvent::LoadedWithDependencies { id }
|
||||||
@@ -198,6 +202,22 @@ fn sync_card_image_set_with_active_theme(
|
|||||||
should_sync = true;
|
should_sync = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// A→B→A switch: Bevy does not re-fire LoadedWithDependencies for a
|
||||||
|
// handle whose asset is already cached. Detect this by checking that
|
||||||
|
// `ActiveTheme` itself changed this frame (the resource was just
|
||||||
|
// replaced by `react_to_settings_theme_change`) and the underlying
|
||||||
|
// asset is already fully loaded. If so, sync immediately rather than
|
||||||
|
// waiting for an event that will never arrive.
|
||||||
|
if !should_sync
|
||||||
|
&& active.is_changed()
|
||||||
|
&& asset_server
|
||||||
|
.as_ref()
|
||||||
|
.is_some_and(|as_| as_.is_loaded_with_dependencies(active.0.id()))
|
||||||
|
{
|
||||||
|
should_sync = true;
|
||||||
|
}
|
||||||
|
|
||||||
if !should_sync {
|
if !should_sync {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -172,14 +172,15 @@ fn advance_time_attack(
|
|||||||
paused: Option<Res<crate::pause_plugin::PausedResource>>,
|
paused: Option<Res<crate::pause_plugin::PausedResource>>,
|
||||||
path: Option<Res<TimeAttackSessionPath>>,
|
path: Option<Res<TimeAttackSessionPath>>,
|
||||||
home_screens: Query<(), With<crate::home_plugin::HomeScreen>>,
|
home_screens: Query<(), With<crate::home_plugin::HomeScreen>>,
|
||||||
|
win_overlays: Query<(), With<crate::win_summary_plugin::WinSummaryOverlay>>,
|
||||||
) {
|
) {
|
||||||
if !session.active {
|
if !session.active {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// Mirrors `tick_elapsed_time`: pause while the launch / mode-picker
|
// Pause the countdown while Home, the Pause overlay, or the Win Summary
|
||||||
// Home modal is up so the countdown doesn't burn while the player
|
// overlay is visible — the player should not lose time while reading results
|
||||||
// is choosing what to play next.
|
// or navigating menus.
|
||||||
if paused.is_some_and(|p| p.0) || !home_screens.is_empty() {
|
if paused.is_some_and(|p| p.0) || !home_screens.is_empty() || !win_overlays.is_empty() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
session.remaining_secs -= time.delta_secs();
|
session.remaining_secs -= time.delta_secs();
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ use crate::progress_plugin::ProgressResource;
|
|||||||
use crate::resources::GameStateResource;
|
use crate::resources::GameStateResource;
|
||||||
use crate::settings_plugin::SettingsResource;
|
use crate::settings_plugin::SettingsResource;
|
||||||
use crate::stats_plugin::{StatsResource, StatsUpdate};
|
use crate::stats_plugin::{StatsResource, StatsUpdate};
|
||||||
|
use crate::ui_modal::ModalScrim;
|
||||||
use crate::ui_theme::{
|
use crate::ui_theme::{
|
||||||
scaled_duration, ACCENT_PRIMARY, BG_BASE, BG_ELEVATED, MOTION_SCORE_BREAKDOWN_FADE_SECS,
|
scaled_duration, ACCENT_PRIMARY, BG_BASE, BG_ELEVATED, MOTION_SCORE_BREAKDOWN_FADE_SECS,
|
||||||
MOTION_SCORE_BREAKDOWN_STAGGER_SECS, MOTION_WIN_SHAKE_AMPLITUDE, MOTION_WIN_SHAKE_SECS,
|
MOTION_SCORE_BREAKDOWN_STAGGER_SECS, MOTION_WIN_SHAKE_AMPLITUDE, MOTION_WIN_SHAKE_SECS,
|
||||||
@@ -507,7 +508,7 @@ fn collect_session_achievements(
|
|||||||
) {
|
) {
|
||||||
// Reset on any new-game request (including mode switches via Z/X/C/T) so
|
// Reset on any new-game request (including mode switches via Z/X/C/T) so
|
||||||
// achievements from the previous session are not carried into the next one.
|
// achievements from the previous session are not carried into the next one.
|
||||||
if new_games.read().last().is_some() {
|
if new_games.read().next().is_some() {
|
||||||
session.names.clear();
|
session.names.clear();
|
||||||
}
|
}
|
||||||
for ev in unlocks.read() {
|
for ev in unlocks.read() {
|
||||||
@@ -538,6 +539,7 @@ fn spawn_win_summary_after_delay(
|
|||||||
settings: Option<Res<SettingsResource>>,
|
settings: Option<Res<SettingsResource>>,
|
||||||
time: Res<Time>,
|
time: Res<Time>,
|
||||||
overlays: Query<Entity, With<WinSummaryOverlay>>,
|
overlays: Query<Entity, With<WinSummaryOverlay>>,
|
||||||
|
other_scrims: Query<(), (With<ModalScrim>, Without<WinSummaryOverlay>)>,
|
||||||
mut delay: Local<Option<f32>>,
|
mut delay: Local<Option<f32>>,
|
||||||
) {
|
) {
|
||||||
// Process new win events.
|
// Process new win events.
|
||||||
@@ -568,8 +570,8 @@ fn spawn_win_summary_after_delay(
|
|||||||
*remaining -= time.delta_secs();
|
*remaining -= time.delta_secs();
|
||||||
if *remaining <= 0.0 {
|
if *remaining <= 0.0 {
|
||||||
*delay = None;
|
*delay = None;
|
||||||
// Only spawn if there is no overlay already.
|
// Only spawn if no overlay of any kind is already visible.
|
||||||
if overlays.is_empty() {
|
if overlays.is_empty() && other_scrims.is_empty() {
|
||||||
// Drain any XpAwardedEvents that arrived this frame but were
|
// Drain any XpAwardedEvents that arrived this frame but were
|
||||||
// not yet consumed by `cache_win_data` (which may run later in
|
// not yet consumed by `cache_win_data` (which may run later in
|
||||||
// the same schedule). Accumulating here ensures the modal
|
// the same schedule). Accumulating here ensures the modal
|
||||||
@@ -757,6 +759,7 @@ fn spawn_overlay(
|
|||||||
commands
|
commands
|
||||||
.spawn((
|
.spawn((
|
||||||
WinSummaryOverlay,
|
WinSummaryOverlay,
|
||||||
|
ModalScrim,
|
||||||
Node {
|
Node {
|
||||||
position_type: PositionType::Absolute,
|
position_type: PositionType::Absolute,
|
||||||
left: Val::Percent(0.0),
|
left: Val::Percent(0.0),
|
||||||
@@ -769,6 +772,7 @@ fn spawn_overlay(
|
|||||||
..default()
|
..default()
|
||||||
},
|
},
|
||||||
BackgroundColor(SCRIM),
|
BackgroundColor(SCRIM),
|
||||||
|
GlobalZIndex(Z_WIN_CASCADE),
|
||||||
ZIndex(Z_WIN_CASCADE),
|
ZIndex(Z_WIN_CASCADE),
|
||||||
))
|
))
|
||||||
.with_children(|root| {
|
.with_children(|root| {
|
||||||
|
|||||||
@@ -341,8 +341,6 @@ pub async fn get_me(
|
|||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Allowed MIME types for uploaded avatars.
|
|
||||||
const ALLOWED_IMAGE_TYPES: &[&str] = &["image/jpeg", "image/png", "image/webp", "image/gif"];
|
|
||||||
/// Maximum avatar upload size in bytes (1 MB).
|
/// Maximum avatar upload size in bytes (1 MB).
|
||||||
const AVATAR_MAX_BYTES: usize = 1024 * 1024;
|
const AVATAR_MAX_BYTES: usize = 1024 * 1024;
|
||||||
|
|
||||||
@@ -361,23 +359,15 @@ pub async fn upload_avatar(
|
|||||||
.and_then(|v| v.to_str().ok())
|
.and_then(|v| v.to_str().ok())
|
||||||
.unwrap_or("")
|
.unwrap_or("")
|
||||||
.to_string();
|
.to_string();
|
||||||
let ext = if mime.contains("jpeg") || mime.contains("jpg") {
|
let ext = match mime.as_str() {
|
||||||
"jpg"
|
"image/jpeg" | "image/jpg" => "jpg",
|
||||||
} else if mime.contains("png") {
|
"image/png" => "png",
|
||||||
"png"
|
"image/webp" => "webp",
|
||||||
} else if mime.contains("webp") {
|
"image/gif" => "gif",
|
||||||
"webp"
|
_ => return Err(AppError::BadRequest(
|
||||||
} else if mime.contains("gif") {
|
|
||||||
"gif"
|
|
||||||
} else {
|
|
||||||
return Err(AppError::BadRequest(
|
|
||||||
"avatar must be image/jpeg, image/png, image/webp, or image/gif".into(),
|
"avatar must be image/jpeg, image/png, image/webp, or image/gif".into(),
|
||||||
));
|
)),
|
||||||
};
|
};
|
||||||
|
|
||||||
if !ALLOWED_IMAGE_TYPES.iter().any(|t| mime.starts_with(t)) {
|
|
||||||
return Err(AppError::BadRequest("unsupported image type".into()));
|
|
||||||
}
|
|
||||||
if body.len() > AVATAR_MAX_BYTES {
|
if body.len() > AVATAR_MAX_BYTES {
|
||||||
return Err(AppError::BadRequest("avatar must be ≤ 1 MB".into()));
|
return Err(AppError::BadRequest("avatar must be ≤ 1 MB".into()));
|
||||||
}
|
}
|
||||||
@@ -390,7 +380,10 @@ pub async fn upload_avatar(
|
|||||||
// Write to a temp file then atomically rename so concurrent readers never
|
// Write to a temp file then atomically rename so concurrent readers never
|
||||||
// see a partially-written avatar.
|
// see a partially-written avatar.
|
||||||
std::fs::write(&tmp_path, &body).map_err(|e| AppError::Internal(e.to_string()))?;
|
std::fs::write(&tmp_path, &body).map_err(|e| AppError::Internal(e.to_string()))?;
|
||||||
std::fs::rename(&tmp_path, &path).map_err(|e| AppError::Internal(e.to_string()))?;
|
if let Err(e) = std::fs::rename(&tmp_path, &path) {
|
||||||
|
let _ = std::fs::remove_file(&tmp_path);
|
||||||
|
return Err(AppError::Internal(e.to_string()));
|
||||||
|
}
|
||||||
// Remove stale files with other extensions after the atomic rename.
|
// Remove stale files with other extensions after the atomic rename.
|
||||||
for old_ext in &["jpg", "png", "webp", "gif"] {
|
for old_ext in &["jpg", "png", "webp", "gif"] {
|
||||||
if *old_ext != ext {
|
if *old_ext != ext {
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ use crate::{error::AppError, middleware::AuthenticatedUser, AppState};
|
|||||||
/// the desktop client's transitive dependencies.
|
/// the desktop client's transitive dependencies.
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
struct ReplayHeader {
|
struct ReplayHeader {
|
||||||
seed: i64,
|
seed: u64,
|
||||||
draw_mode: String,
|
draw_mode: String,
|
||||||
mode: String,
|
mode: String,
|
||||||
time_seconds: i64,
|
time_seconds: i64,
|
||||||
@@ -94,6 +94,9 @@ pub async fn upload(
|
|||||||
let id = Uuid::new_v4().to_string();
|
let id = Uuid::new_v4().to_string();
|
||||||
let received_at = Utc::now().to_rfc3339();
|
let received_at = Utc::now().to_rfc3339();
|
||||||
let replay_json = serde_json::to_string(&payload)?;
|
let replay_json = serde_json::to_string(&payload)?;
|
||||||
|
// SQLite INTEGER columns bind as i64. Reinterpret the u64 bits — the
|
||||||
|
// database stores the same 8 bytes; high-bit seeds round-trip correctly.
|
||||||
|
let seed_i64 = header.seed as i64;
|
||||||
|
|
||||||
sqlx::query!(
|
sqlx::query!(
|
||||||
r#"INSERT INTO replays (
|
r#"INSERT INTO replays (
|
||||||
@@ -102,7 +105,7 @@ pub async fn upload(
|
|||||||
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"#,
|
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"#,
|
||||||
id,
|
id,
|
||||||
user.user_id,
|
user.user_id,
|
||||||
header.seed,
|
seed_i64,
|
||||||
header.draw_mode,
|
header.draw_mode,
|
||||||
header.mode,
|
header.mode,
|
||||||
header.time_seconds,
|
header.time_seconds,
|
||||||
@@ -116,7 +119,7 @@ pub async fn upload(
|
|||||||
|
|
||||||
// Update leaderboard best score/time for opted-in users when this replay
|
// Update leaderboard best score/time for opted-in users when this replay
|
||||||
// beats their existing best. Only classic mode counts for the leaderboard.
|
// beats their existing best. Only classic mode counts for the leaderboard.
|
||||||
if header.mode == "classic" {
|
if header.mode == "Classic" {
|
||||||
sqlx::query!(
|
sqlx::query!(
|
||||||
r#"UPDATE leaderboard
|
r#"UPDATE leaderboard
|
||||||
SET best_score = ?,
|
SET best_score = ?,
|
||||||
|
|||||||
@@ -300,6 +300,7 @@ function render(s) {
|
|||||||
board.querySelectorAll(".card.drop-target").forEach(e => e.classList.remove("drop-target"));
|
board.querySelectorAll(".card.drop-target").forEach(e => e.classList.remove("drop-target"));
|
||||||
|
|
||||||
if (s.is_auto_completable && !s.is_won && !acTimer) {
|
if (s.is_auto_completable && !s.is_won && !acTimer) {
|
||||||
|
stopTimer(); // freeze elapsed time at the moment the player's last move completes
|
||||||
acTimer = setInterval(doAutoCompleteStep, 380);
|
acTimer = setInterval(doAutoCompleteStep, 380);
|
||||||
}
|
}
|
||||||
if (s.is_won) {
|
if (s.is_won) {
|
||||||
|
|||||||
@@ -40,20 +40,32 @@ export class ReplayPlayer {
|
|||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* Snapshot the current `GameState` as a JS object (see `StateSnapshot`).
|
* Snapshot the current `GameState` as a JS object (see `StateSnapshot`).
|
||||||
|
*
|
||||||
|
* Throws a JS string exception on serialisation failure (should never
|
||||||
|
* occur in practice — `StateSnapshot` contains only primitive types).
|
||||||
* @returns {any}
|
* @returns {any}
|
||||||
*/
|
*/
|
||||||
state() {
|
state() {
|
||||||
const ret = wasm.replayplayer_state(this.__wbg_ptr);
|
const ret = wasm.replayplayer_state(this.__wbg_ptr);
|
||||||
return ret;
|
if (ret[2]) {
|
||||||
|
throw takeFromExternrefTable0(ret[1]);
|
||||||
|
}
|
||||||
|
return takeFromExternrefTable0(ret[0]);
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* Apply the next move; returns the post-step snapshot, or `null`
|
* Apply the next move; returns the post-step snapshot, or `null`
|
||||||
* once the move list is exhausted.
|
* once the move list is exhausted.
|
||||||
|
*
|
||||||
|
* Returns `null` (not an exception) when the replay is finished.
|
||||||
|
* Throws a JS string exception on serialisation failure.
|
||||||
* @returns {any}
|
* @returns {any}
|
||||||
*/
|
*/
|
||||||
step() {
|
step() {
|
||||||
const ret = wasm.replayplayer_step(this.__wbg_ptr);
|
const ret = wasm.replayplayer_step(this.__wbg_ptr);
|
||||||
return ret;
|
if (ret[2]) {
|
||||||
|
throw takeFromExternrefTable0(ret[1]);
|
||||||
|
}
|
||||||
|
return takeFromExternrefTable0(ret[0]);
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* 0-indexed position of the next move to apply.
|
* 0-indexed position of the next move to apply.
|
||||||
@@ -157,11 +169,16 @@ export class SolitaireGame {
|
|||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* Full pile snapshot as a JS object.
|
* Full pile snapshot as a JS object.
|
||||||
|
*
|
||||||
|
* Throws a JS string exception on serialisation failure.
|
||||||
* @returns {any}
|
* @returns {any}
|
||||||
*/
|
*/
|
||||||
state() {
|
state() {
|
||||||
const ret = wasm.solitairegame_state(this.__wbg_ptr);
|
const ret = wasm.solitairegame_state(this.__wbg_ptr);
|
||||||
return ret;
|
if (ret[2]) {
|
||||||
|
throw takeFromExternrefTable0(ret[1]);
|
||||||
|
}
|
||||||
|
return takeFromExternrefTable0(ret[0]);
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* Undo the last move. Returns `{ok, error?, snapshot?}`.
|
* Undo the last move. Returns `{ok, error?, snapshot?}`.
|
||||||
@@ -180,6 +197,13 @@ function __wbg_get_imports() {
|
|||||||
const ret = Error(getStringFromWasm0(arg0, arg1));
|
const ret = Error(getStringFromWasm0(arg0, arg1));
|
||||||
return ret;
|
return ret;
|
||||||
},
|
},
|
||||||
|
__wbg_String_8564e559799eccda: function(arg0, arg1) {
|
||||||
|
const ret = String(arg1);
|
||||||
|
const ptr1 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
||||||
|
const len1 = WASM_VECTOR_LEN;
|
||||||
|
getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true);
|
||||||
|
getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true);
|
||||||
|
},
|
||||||
__wbg___wbindgen_throw_9c75d47bf9e7731e: function(arg0, arg1) {
|
__wbg___wbindgen_throw_9c75d47bf9e7731e: function(arg0, arg1) {
|
||||||
throw new Error(getStringFromWasm0(arg0, arg1));
|
throw new Error(getStringFromWasm0(arg0, arg1));
|
||||||
},
|
},
|
||||||
|
|||||||
Binary file not shown.
+34
-17
@@ -108,23 +108,27 @@ fn merge_stats(
|
|||||||
let merged_games_won = local.games_won.max(remote.games_won);
|
let merged_games_won = local.games_won.max(remote.games_won);
|
||||||
let merged_games_played = local.games_played.max(remote.games_played);
|
let merged_games_played = local.games_played.max(remote.games_played);
|
||||||
|
|
||||||
// Recompute average time from the merged totals. If no wins yet, keep 0.
|
// Carry the average time from whichever side contributed merged_games_won.
|
||||||
|
// Taking max(total_time)/max(wins) misattributes time when the side with
|
||||||
|
// more wins has a lower total — use the winning side's average directly.
|
||||||
let avg_time_seconds = if merged_games_won == 0 {
|
let avg_time_seconds = if merged_games_won == 0 {
|
||||||
0
|
0
|
||||||
|
} else if local.games_won >= remote.games_won {
|
||||||
|
local.avg_time_seconds
|
||||||
} else {
|
} else {
|
||||||
// Use whichever side has more wins to approximate total time, then blend.
|
remote.avg_time_seconds
|
||||||
// We don't have total_time stored, so we reconstruct it from avg * count.
|
|
||||||
let local_total = local.avg_time_seconds as u128 * local.games_won as u128;
|
|
||||||
let remote_total = remote.avg_time_seconds as u128 * remote.games_won as u128;
|
|
||||||
// Take max total time (conservative — avoids underestimating total play time).
|
|
||||||
let best_total = local_total.max(remote_total);
|
|
||||||
(best_total / merged_games_won as u128) as u64
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Derive games_lost from the merged played/won counts so the invariant
|
||||||
|
// games_won + games_lost <= games_played is always satisfied. Computing
|
||||||
|
// max(local.games_lost, remote.games_lost) independently can push
|
||||||
|
// games_won + games_lost above games_played after a divergent merge.
|
||||||
|
let merged_games_lost = merged_games_played.saturating_sub(merged_games_won);
|
||||||
|
|
||||||
StatsSnapshot {
|
StatsSnapshot {
|
||||||
games_played: merged_games_played,
|
games_played: merged_games_played,
|
||||||
games_won: merged_games_won,
|
games_won: merged_games_won,
|
||||||
games_lost: local.games_lost.max(remote.games_lost),
|
games_lost: merged_games_lost,
|
||||||
win_streak_current: local.win_streak_current.max(remote.win_streak_current),
|
win_streak_current: local.win_streak_current.max(remote.win_streak_current),
|
||||||
win_streak_best: local.win_streak_best.max(remote.win_streak_best),
|
win_streak_best: local.win_streak_best.max(remote.win_streak_best),
|
||||||
avg_time_seconds,
|
avg_time_seconds,
|
||||||
@@ -454,14 +458,28 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn stats_games_lost_takes_max() {
|
fn stats_games_lost_derived_from_played_minus_won() {
|
||||||
|
// games_lost must equal games_played - games_won so the invariant
|
||||||
|
// games_won + games_lost <= games_played is always satisfied.
|
||||||
let mut local = default_payload();
|
let mut local = default_payload();
|
||||||
|
local.stats.games_played = 20;
|
||||||
|
local.stats.games_won = 8;
|
||||||
local.stats.games_lost = 12;
|
local.stats.games_lost = 12;
|
||||||
let mut remote = default_payload();
|
let mut remote = default_payload();
|
||||||
remote.stats.games_lost = 8;
|
remote.stats.games_played = 15;
|
||||||
|
remote.stats.games_won = 10;
|
||||||
|
remote.stats.games_lost = 5;
|
||||||
|
|
||||||
|
// merged: games_played = max(20, 15) = 20; games_won = max(8, 10) = 10
|
||||||
|
// games_lost must be 20 - 10 = 10, NOT max(12, 5) = 12
|
||||||
let (merged, _) = merge(&local, &remote);
|
let (merged, _) = merge(&local, &remote);
|
||||||
assert_eq!(merged.stats.games_lost, 12);
|
assert_eq!(merged.stats.games_played, 20);
|
||||||
|
assert_eq!(merged.stats.games_won, 10);
|
||||||
|
assert_eq!(merged.stats.games_lost, 10);
|
||||||
|
assert!(
|
||||||
|
merged.stats.games_won + merged.stats.games_lost <= merged.stats.games_played,
|
||||||
|
"games_won + games_lost must never exceed games_played"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -502,11 +520,10 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn stats_avg_time_recomputed_from_merged_totals() {
|
fn stats_avg_time_recomputed_from_merged_totals() {
|
||||||
// local: 4 wins averaging 100s each (total = 400s)
|
// local: 4 wins averaging 100s each
|
||||||
// remote: 6 wins averaging 200s each (total = 1200s)
|
// remote: 6 wins averaging 200s each
|
||||||
// merged_games_won = max(4, 6) = 6
|
// merged_games_won = max(4, 6) = 6 → remote contributed the wins
|
||||||
// best_total = max(400, 1200) = 1200
|
// avg_time_seconds must be remote's 200s, not a blend of totals
|
||||||
// avg = 1200 / 6 = 200
|
|
||||||
let mut local = default_payload();
|
let mut local = default_payload();
|
||||||
local.stats.games_won = 4;
|
local.stats.games_won = 4;
|
||||||
local.stats.avg_time_seconds = 100;
|
local.stats.avg_time_seconds = 100;
|
||||||
|
|||||||
Reference in New Issue
Block a user