Compare commits
4 Commits
87b528338a
...
claude
Author | SHA1 | Date | |
---|---|---|---|
142a5ac135
|
|||
5ae075edd3
|
|||
0b34568585
|
|||
ef907660cc
|
@@ -3,7 +3,31 @@
|
||||
"allow": [
|
||||
"WebFetch(domain:card.syui.ai)",
|
||||
"Bash(mkdir:*)",
|
||||
"Bash(chmod:*)"
|
||||
"Bash(chmod:*)",
|
||||
"Bash(./start_server.sh:*)",
|
||||
"Bash(npm run dev:*)",
|
||||
"Bash(npm install)",
|
||||
"WebFetch(domain:github.com)",
|
||||
"Bash(npm run build:*)",
|
||||
"Bash(npm run preview:*)",
|
||||
"Bash(curl:*)",
|
||||
"Bash(sudo kill:*)",
|
||||
"Bash(launchctl:*)",
|
||||
"Bash(ls:*)",
|
||||
"Bash(cat:*)",
|
||||
"Bash(find:*)",
|
||||
"Bash(cloudflared:*)",
|
||||
"Bash(grep:*)",
|
||||
"Bash(nslookup:*)",
|
||||
"Bash(sqlite3:*)",
|
||||
"Bash(kill:*)",
|
||||
"Bash(pkill:*)",
|
||||
"WebFetch(domain:docs.bsky.app)",
|
||||
"Bash(npm install:*)",
|
||||
"WebFetch(domain:raw.githubusercontent.com)",
|
||||
"WebFetch(domain:www.npmjs.com)",
|
||||
"Bash(rm:*)",
|
||||
"Bash(cargo:*)"
|
||||
],
|
||||
"deny": []
|
||||
}
|
||||
|
18
.env.development
Normal file
18
.env.development
Normal file
@@ -0,0 +1,18 @@
|
||||
# Development configuration for ai.card
|
||||
# This file is used for local development
|
||||
|
||||
# Web Frontend Configuration
|
||||
VITE_WEB_HOST=http://localhost:5173
|
||||
VITE_API_HOST=http://localhost:8000
|
||||
VITE_WEB_PORT=5173
|
||||
|
||||
# API Backend Configuration
|
||||
API_HOST=localhost
|
||||
API_PORT=8000
|
||||
|
||||
# OAuth Configuration
|
||||
VITE_OAUTH_CLIENT_NAME=ai.card
|
||||
VITE_OAUTH_REDIRECT_PATH=/oauth/callback
|
||||
|
||||
# Feature Flags
|
||||
VITE_ENABLE_AI_FEATURES=true
|
1
.gitignore
vendored
1
.gitignore
vendored
@@ -63,3 +63,4 @@ yarn-error.log
|
||||
package-lock.json
|
||||
yarn.lock
|
||||
**DS_Store
|
||||
cloudflared-config*
|
||||
|
54
Cargo.toml
Normal file
54
Cargo.toml
Normal file
@@ -0,0 +1,54 @@
|
||||
[package]
|
||||
name = "aicard"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
description = "ai.card API server - Rust implementation of autonomous card collection system"
|
||||
authors = ["syui"]
|
||||
|
||||
[dependencies]
|
||||
# Core Web Framework
|
||||
axum = { version = "0.7", features = ["macros", "multipart"] }
|
||||
tokio = { version = "1.0", features = ["full"] }
|
||||
tower = { version = "0.4", features = ["full"] }
|
||||
tower-http = { version = "0.5", features = ["cors", "trace"] }
|
||||
|
||||
# Database & ORM
|
||||
sqlx = { version = "0.7", features = ["runtime-tokio-rustls", "postgres", "sqlite", "uuid", "chrono", "migrate"] }
|
||||
uuid = { version = "1.0", features = ["v4", "serde"] }
|
||||
|
||||
# Serialization & Validation
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_json = "1.0"
|
||||
validator = { version = "0.18", features = ["derive"] }
|
||||
|
||||
# Date/Time
|
||||
chrono = { version = "0.4", features = ["serde"] }
|
||||
|
||||
# Authentication & Security
|
||||
jsonwebtoken = "9.0"
|
||||
bcrypt = "0.15"
|
||||
|
||||
# HTTP Client (for atproto integration)
|
||||
reqwest = { version = "0.11", features = ["json"] }
|
||||
|
||||
# Configuration
|
||||
config = "0.13"
|
||||
dotenvy = "0.15"
|
||||
|
||||
# CLI
|
||||
clap = { version = "4.0", features = ["derive"] }
|
||||
|
||||
# Random (for gacha system)
|
||||
rand = "0.8"
|
||||
|
||||
# Error Handling
|
||||
anyhow = "1.0"
|
||||
thiserror = "1.0"
|
||||
|
||||
# Logging
|
||||
tracing = "0.1"
|
||||
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
|
||||
|
||||
# Development
|
||||
serde_yaml = "0.9"
|
||||
dirs = "5.0"
|
170
README.md
170
README.md
@@ -1,63 +1,143 @@
|
||||
# ai.card
|
||||
# ai.card プロジェクト固有情報
|
||||
|
||||
atprotoベースのカードゲームシステム
|
||||
## プロジェクト概要
|
||||
- **名前**: ai.card
|
||||
- **パッケージ**: aicard
|
||||
- **タイプ**: atproto基盤カードゲーム
|
||||
- **役割**: ユーザーデータ主権カードゲームシステム
|
||||
|
||||
## 概要
|
||||
## 実装状況
|
||||
|
||||
ai.cardは、ユーザーがデータを所有する分散型カードゲームです。
|
||||
- atprotoアカウントと連携
|
||||
- データはユーザーのPDSに保存
|
||||
- yui-systemによるuniqueカード実装
|
||||
- iOS/Web/APIの統合プロジェクト
|
||||
### 現在の状況
|
||||
- **ai.bot統合**: ai.botの機能として実装済み
|
||||
- **カード取得**: atproto accountでmentionすると1日1回カード取得可能
|
||||
- **データ管理**: ai.api (MCP server) でユーザー管理
|
||||
|
||||
## 技術スタック
|
||||
### 独立MCPサーバー(ai.gpt連携)
|
||||
- **場所**: `/Users/syui/ai/gpt/card/`
|
||||
- **サーバー**: FastAPI + fastapi_mcp (port 8000)
|
||||
- **統合**: ai.gptサーバーからHTTP連携
|
||||
|
||||
- **API**: Python/FastAPI + fastapi_mcp
|
||||
- **Web**: モダンJavaScript framework
|
||||
- **iOS**: Swift/SwiftUI
|
||||
- **データストア**: atproto collection + ローカルキャッシュ
|
||||
- **認証**: atproto OAuth
|
||||
## アーキテクチャ構成
|
||||
|
||||
## プロジェクト構造
|
||||
### 技術スタック
|
||||
- **Backend**: FastAPI + MCP
|
||||
- **Frontend**: React Web UI + SwiftUI iOS app
|
||||
- **Data**: atproto collection record(ユーザー所有)
|
||||
- **Auth**: OAuth 2.1 scope(実装待ち)
|
||||
|
||||
### データフロー
|
||||
```
|
||||
ai.card/
|
||||
├── api/ # FastAPI backend
|
||||
├── web/ # Web frontend
|
||||
├── ios/ # iOS app
|
||||
├── docs/ # Documentation
|
||||
└── scripts/ # Utility scripts
|
||||
ユーザー → ai.bot mention → カード生成 → atproto collection → ユーザー所有
|
||||
↑ ↓
|
||||
← iOS app表示 ← ai.card API ←
|
||||
```
|
||||
|
||||
## 機能
|
||||
## 移行計画
|
||||
|
||||
- カードガチャシステム
|
||||
- キラカード(0.1%)
|
||||
- uniqueカード(0.0001% - 隠し機能)
|
||||
- atprotoデータ同期
|
||||
- 改ざん防止機構
|
||||
### Phase 1: 独立化
|
||||
- **iOS移植**: Claude担当予定
|
||||
- **Web UI**: React実装
|
||||
- **API独立**: ai.botからの分離
|
||||
|
||||
## セットアップ
|
||||
### Phase 2: データ主権実装
|
||||
- **atproto collection**: カードデータをユーザー所有に
|
||||
- **OAuth 2.1**: 不正防止機能実装
|
||||
- **画像ファイル**: Cloudflare Pages最適化
|
||||
|
||||
### API
|
||||
```bash
|
||||
cd api
|
||||
pip install -r requirements.txt
|
||||
uvicorn app.main:app --reload
|
||||
### Phase 3: ゲーム機能拡張
|
||||
- **ガチャシステム**: 確率・レアリティ管理
|
||||
- **トレード機能**: ユーザー間カード交換
|
||||
- **デッキ構築**: カードゲーム戦略要素
|
||||
|
||||
## yui system適用
|
||||
|
||||
### 唯一性担保
|
||||
- **カード効果**: アカウント固有の効果設定
|
||||
- **改ざん防止**: ハッシュ・署名による保証
|
||||
- **ゲームバランス**: 唯一性による公平性維持
|
||||
|
||||
### ai.verse連携
|
||||
- **ゲーム内アイテム**: ai.verseでのカード利用
|
||||
- **固有スキル**: カードとキャラクターの連動
|
||||
- **現実反映**: カード取得がゲーム内能力に影響
|
||||
|
||||
## ディレクトリ構成
|
||||
|
||||
```
|
||||
/Users/syui/ai/gpt/card/
|
||||
├── api/ # FastAPI + MCP server
|
||||
├── web/ # React Web UI
|
||||
├── ios/ # SwiftUI iOS app
|
||||
└── docs/ # 開発ドキュメント
|
||||
```
|
||||
|
||||
### Web
|
||||
```bash
|
||||
cd web
|
||||
npm install
|
||||
npm run dev
|
||||
```
|
||||
## MCPツール(ai.gpt連携)
|
||||
|
||||
### カード管理
|
||||
- **card_get_user_cards**: ユーザーカード取得
|
||||
- **card_draw_card**: ガチャ実行
|
||||
- **card_analyze_collection**: コレクション分析
|
||||
- **card_check_daily_limit**: 日次制限確認
|
||||
- **card_get_card_stats**: カード統計情報
|
||||
- **card_manage_deck**: デッキ管理
|
||||
|
||||
## 開発状況
|
||||
|
||||
- [ ] API基盤
|
||||
- [ ] カードデータモデル
|
||||
- [ ] ガチャシステム
|
||||
- [ ] atproto連携
|
||||
- [ ] Web UI
|
||||
- [ ] iOS app
|
||||
### 完成済み機能
|
||||
- ✅ **基本カード生成**: ai.bot統合での1日1回取得
|
||||
- ✅ **atproto連携**: mention機能
|
||||
- ✅ **MCP統合**: ai.gptからの操作
|
||||
|
||||
### 開発中機能
|
||||
- 🔧 **iOS app**: SwiftUI実装
|
||||
- 🔧 **Web UI**: React実装
|
||||
- 🔧 **独立API**: FastAPI server
|
||||
|
||||
### 将来機能
|
||||
- 📋 **OAuth 2.1**: 不正防止強化
|
||||
- 📋 **画像最適化**: Cloudflare Pages
|
||||
- 📋 **ゲーム拡張**: トレード・デッキ戦略
|
||||
|
||||
## ai.botからの移行詳細
|
||||
|
||||
### 現在のai.bot実装
|
||||
- **Rust製**: seahorse CLI framework
|
||||
- **atproto連携**: mention機能でカード配布
|
||||
- **日次制限**: 1アカウント1日1回取得
|
||||
- **自動生成**: AI絵画(Leonardo.AI + Stable Diffusion)
|
||||
|
||||
### 独立化の理由
|
||||
- **iOS展開**: モバイルアプリでの独立した体験
|
||||
- **ゲーム拡張**: デッキ構築・バトル機能の追加
|
||||
- **データ主権**: ユーザーによる完全なデータ所有
|
||||
- **スケーラビリティ**: サーバー負荷分散
|
||||
|
||||
## 技術的課題と解決策
|
||||
|
||||
### データ改ざん防止
|
||||
- **短期**: MCP serverによる検証
|
||||
- **中期**: OAuth 2.1 scope実装待ち
|
||||
- **長期**: ブロックチェーン的整合性チェック
|
||||
|
||||
### スケーラビリティ
|
||||
- **画像配信**: Cloudflare Pages活用
|
||||
- **API負荷**: FastAPIによる高速処理
|
||||
- **データ保存**: atproto分散ストレージ
|
||||
|
||||
### ユーザー体験
|
||||
- **直感的UI**: iOS/Webでの統一UX
|
||||
- **リアルタイム更新**: WebSocketでの即座反映
|
||||
- **オフライン対応**: ローカルキャッシュ機能
|
||||
|
||||
## ai.game連携構想
|
||||
|
||||
### Play-to-Work統合
|
||||
- **カードゲームプレイ → 業務成果変換**: ai.gameデバイスでの労働ゲーム化
|
||||
- **デッキ構築戦略 → 企業戦略思考**: カード組み合わせが戦略思考を鍛練
|
||||
- **トレード交渉 → ビジネススキル**: 他プレイヤーとの交渉が実務能力向上
|
||||
|
||||
### メタバース展開
|
||||
- **ai.verse統合**: 3D世界でのカードバトル
|
||||
- **アバター連動**: 所有カードがキャラクター能力に影響
|
||||
- **配信コンテンツ**: カードゲームが配信可能なエンターテイメント
|
@@ -1,32 +0,0 @@
|
||||
# Application
|
||||
APP_NAME=ai.card
|
||||
APP_VERSION=0.1.0
|
||||
DEBUG=false
|
||||
|
||||
# Database (Local PostgreSQL)
|
||||
DATABASE_URL=postgresql+asyncpg://postgres:postgres@localhost:5432/aicard
|
||||
|
||||
# Database (Supabase - optional)
|
||||
DATABASE_URL_SUPABASE=postgresql+asyncpg://postgres.xxxxxxxxxxxx:password@aws-0-region.pooler.supabase.com:5432/postgres
|
||||
USE_SUPABASE=false
|
||||
|
||||
# atproto (optional)
|
||||
ATPROTO_PDS_URL=https://bsky.social
|
||||
ATPROTO_HANDLE=your.handle
|
||||
ATPROTO_PASSWORD=your-app-password
|
||||
|
||||
# Card probabilities (in percentage)
|
||||
PROB_NORMAL=99.789
|
||||
PROB_RARE=0.1
|
||||
PROB_SUPER_RARE=0.01
|
||||
PROB_KIRA=0.1
|
||||
PROB_UNIQUE=0.0001
|
||||
|
||||
# Unique card settings
|
||||
MAX_UNIQUE_CARDS=1000
|
||||
|
||||
# CORS
|
||||
CORS_ORIGINS=["http://localhost:3000", "https://card.syui.ai"]
|
||||
|
||||
# Security
|
||||
SECRET_KEY=your-secret-key-change-this-in-production
|
@@ -1,24 +0,0 @@
|
||||
FROM python:3.11-slim
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
# Install system dependencies
|
||||
RUN apt-get update && apt-get install -y \
|
||||
gcc \
|
||||
postgresql-client \
|
||||
&& rm -rf /var/lib/apt/lists/*
|
||||
|
||||
# Copy requirements first for better caching
|
||||
COPY requirements.txt .
|
||||
RUN pip install --no-cache-dir -r requirements.txt
|
||||
|
||||
# Copy application
|
||||
COPY . .
|
||||
|
||||
# Create non-root user
|
||||
RUN useradd -m -u 1000 appuser && chown -R appuser:appuser /app
|
||||
USER appuser
|
||||
|
||||
EXPOSE 8000
|
||||
|
||||
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]
|
@@ -1,47 +0,0 @@
|
||||
# Alembic Configuration
|
||||
|
||||
[alembic]
|
||||
script_location = alembic
|
||||
prepend_sys_path = .
|
||||
version_path_separator = os
|
||||
sqlalchemy.url = postgresql+asyncpg://postgres:postgres@localhost:5432/aicard
|
||||
|
||||
[post_write_hooks]
|
||||
hooks = black
|
||||
black.type = console_scripts
|
||||
black.entrypoint = black
|
||||
black.options = -l 88
|
||||
|
||||
[loggers]
|
||||
keys = root,sqlalchemy,alembic
|
||||
|
||||
[handlers]
|
||||
keys = console
|
||||
|
||||
[formatters]
|
||||
keys = generic
|
||||
|
||||
[logger_root]
|
||||
level = WARN
|
||||
handlers = console
|
||||
qualname =
|
||||
|
||||
[logger_sqlalchemy]
|
||||
level = WARN
|
||||
handlers =
|
||||
qualname = sqlalchemy.engine
|
||||
|
||||
[logger_alembic]
|
||||
level = INFO
|
||||
handlers =
|
||||
qualname = alembic
|
||||
|
||||
[handler_console]
|
||||
class = StreamHandler
|
||||
args = (sys.stderr,)
|
||||
level = NOTSET
|
||||
formatter = generic
|
||||
|
||||
[formatter_generic]
|
||||
format = %(levelname)-5.5s [%(name)s] %(message)s
|
||||
datefmt = %H:%M:%S
|
@@ -1,82 +0,0 @@
|
||||
"""Alembic environment configuration"""
|
||||
import asyncio
|
||||
from logging.config import fileConfig
|
||||
from sqlalchemy import pool
|
||||
from sqlalchemy.engine import Connection
|
||||
from sqlalchemy.ext.asyncio import async_engine_from_config
|
||||
from alembic import context
|
||||
import os
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
# Add parent directory to path
|
||||
sys.path.append(str(Path(__file__).parent.parent))
|
||||
|
||||
from app.db.base import Base
|
||||
from app.db.models import * # Import all models
|
||||
from app.core.config import settings
|
||||
|
||||
# Alembic Config object
|
||||
config = context.config
|
||||
|
||||
# Interpret the config file for Python logging
|
||||
if config.config_file_name is not None:
|
||||
fileConfig(config.config_file_name)
|
||||
|
||||
# Model metadata
|
||||
target_metadata = Base.metadata
|
||||
|
||||
# Override sqlalchemy.url with environment variable if present
|
||||
if os.getenv("DATABASE_URL"):
|
||||
config.set_main_option("sqlalchemy.url", os.getenv("DATABASE_URL"))
|
||||
else:
|
||||
config.set_main_option("sqlalchemy.url", settings.database_url)
|
||||
|
||||
|
||||
def run_migrations_offline() -> None:
|
||||
"""Run migrations in 'offline' mode."""
|
||||
url = config.get_main_option("sqlalchemy.url")
|
||||
context.configure(
|
||||
url=url,
|
||||
target_metadata=target_metadata,
|
||||
literal_binds=True,
|
||||
dialect_opts={"paramstyle": "named"},
|
||||
)
|
||||
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
|
||||
def do_run_migrations(connection: Connection) -> None:
|
||||
context.configure(connection=connection, target_metadata=target_metadata)
|
||||
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
|
||||
async def run_async_migrations() -> None:
|
||||
"""Run migrations in 'online' mode with async engine."""
|
||||
configuration = config.get_section(config.config_ini_section)
|
||||
configuration["sqlalchemy.url"] = config.get_main_option("sqlalchemy.url")
|
||||
|
||||
connectable = async_engine_from_config(
|
||||
configuration,
|
||||
prefix="sqlalchemy.",
|
||||
poolclass=pool.NullPool,
|
||||
)
|
||||
|
||||
async with connectable.connect() as connection:
|
||||
await connection.run_sync(do_run_migrations)
|
||||
|
||||
await connectable.dispose()
|
||||
|
||||
|
||||
def run_migrations_online() -> None:
|
||||
"""Run migrations in 'online' mode."""
|
||||
asyncio.run(run_async_migrations())
|
||||
|
||||
|
||||
if context.is_offline_mode():
|
||||
run_migrations_offline()
|
||||
else:
|
||||
run_migrations_online()
|
@@ -1,24 +0,0 @@
|
||||
"""${message}
|
||||
|
||||
Revision ID: ${up_revision}
|
||||
Revises: ${down_revision | comma,n}
|
||||
Create Date: ${create_date}
|
||||
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
${imports if imports else ""}
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = ${repr(up_revision)}
|
||||
down_revision = ${repr(down_revision)}
|
||||
branch_labels = ${repr(branch_labels)}
|
||||
depends_on = ${repr(depends_on)}
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
${upgrades if upgrades else "pass"}
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
${downgrades if downgrades else "pass"}
|
@@ -1 +0,0 @@
|
||||
# ai.card API Package
|
@@ -1 +0,0 @@
|
||||
# Auth Package
|
@@ -1,116 +0,0 @@
|
||||
"""Authentication dependencies"""
|
||||
from typing import Optional, Annotated
|
||||
from fastapi import Depends, HTTPException, Header, Cookie
|
||||
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
|
||||
from jose import JWTError, jwt
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
# from app.services.atproto import AtprotoService # Temporarily disabled
|
||||
from app.core.config import settings
|
||||
|
||||
|
||||
# Bearer token scheme
|
||||
bearer_scheme = HTTPBearer(auto_error=False)
|
||||
|
||||
# JWT settings
|
||||
SECRET_KEY = settings.secret_key if hasattr(settings, 'secret_key') else "your-secret-key"
|
||||
ALGORITHM = "HS256"
|
||||
ACCESS_TOKEN_EXPIRE_MINUTES = 60 * 24 # 24 hours
|
||||
|
||||
|
||||
class AuthUser:
|
||||
"""Authenticated user data"""
|
||||
def __init__(self, did: str, handle: Optional[str] = None):
|
||||
self.did = did
|
||||
self.handle = handle
|
||||
|
||||
|
||||
def create_access_token(data: dict, expires_delta: Optional[timedelta] = None):
|
||||
"""Create JWT access token"""
|
||||
to_encode = data.copy()
|
||||
if expires_delta:
|
||||
expire = datetime.utcnow() + expires_delta
|
||||
else:
|
||||
expire = datetime.utcnow() + timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES)
|
||||
|
||||
to_encode.update({"exp": expire})
|
||||
encoded_jwt = jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM)
|
||||
return encoded_jwt
|
||||
|
||||
|
||||
async def verify_token(token: str) -> Optional[AuthUser]:
|
||||
"""Verify JWT token and return user"""
|
||||
try:
|
||||
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
|
||||
did: str = payload.get("did")
|
||||
if did is None:
|
||||
return None
|
||||
|
||||
handle: Optional[str] = payload.get("handle")
|
||||
return AuthUser(did=did, handle=handle)
|
||||
|
||||
except JWTError:
|
||||
return None
|
||||
|
||||
|
||||
async def get_current_user(
|
||||
credentials: Optional[HTTPAuthorizationCredentials] = Depends(bearer_scheme),
|
||||
token_cookie: Optional[str] = Cookie(None, alias="ai_card_token")
|
||||
) -> Optional[AuthUser]:
|
||||
"""
|
||||
Get current user from JWT token
|
||||
Supports both Bearer token and cookie
|
||||
"""
|
||||
token = None
|
||||
|
||||
# Try Bearer token first
|
||||
if credentials and credentials.credentials:
|
||||
token = credentials.credentials
|
||||
# Fall back to cookie
|
||||
elif token_cookie:
|
||||
token = token_cookie
|
||||
|
||||
if not token:
|
||||
return None
|
||||
|
||||
user = await verify_token(token)
|
||||
return user
|
||||
|
||||
|
||||
async def require_user(
|
||||
current_user: Optional[AuthUser] = Depends(get_current_user)
|
||||
) -> AuthUser:
|
||||
"""Require authenticated user"""
|
||||
if not current_user:
|
||||
raise HTTPException(
|
||||
status_code=401,
|
||||
detail="Not authenticated",
|
||||
headers={"WWW-Authenticate": "Bearer"},
|
||||
)
|
||||
return current_user
|
||||
|
||||
|
||||
async def get_optional_user(
|
||||
current_user: Optional[AuthUser] = Depends(get_current_user)
|
||||
) -> Optional[AuthUser]:
|
||||
"""Get user if authenticated, None otherwise"""
|
||||
return current_user
|
||||
|
||||
|
||||
# Temporarily disabled due to atproto dependency issues
|
||||
class AtprotoAuth:
|
||||
"""atproto authentication handler (mock implementation)"""
|
||||
|
||||
def __init__(self):
|
||||
pass # self.service = AtprotoService()
|
||||
|
||||
async def authenticate(self, identifier: str, password: str) -> Optional[AuthUser]:
|
||||
"""Mock authentication - always returns test user"""
|
||||
# Mock implementation for testing
|
||||
if identifier and password:
|
||||
return AuthUser(did="did:plc:test123", handle=identifier)
|
||||
return None
|
||||
|
||||
async def verify_did_ownership(self, did: str, session_string: str) -> bool:
|
||||
"""Mock verification - always returns True for test"""
|
||||
return True
|
@@ -1 +0,0 @@
|
||||
# Core Package
|
@@ -1,53 +0,0 @@
|
||||
"""Application configuration"""
|
||||
import os
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
from pydantic_settings import BaseSettings
|
||||
|
||||
|
||||
class Settings(BaseSettings):
|
||||
# Application
|
||||
app_name: str = "ai.card"
|
||||
app_version: str = "0.1.0"
|
||||
debug: bool = False
|
||||
|
||||
# API
|
||||
api_v1_prefix: str = "/api/v1"
|
||||
|
||||
# Database
|
||||
database_url: str = "sqlite+aiosqlite:///~/.config/syui/ai/card/aicard.db"
|
||||
database_url_supabase: Optional[str] = None
|
||||
use_supabase: bool = False
|
||||
|
||||
# atproto
|
||||
atproto_pds_url: Optional[str] = None
|
||||
atproto_handle: Optional[str] = None
|
||||
atproto_password: Optional[str] = None
|
||||
|
||||
# Card probabilities (in percentage)
|
||||
prob_normal: float = 99.789
|
||||
prob_rare: float = 0.1
|
||||
prob_super_rare: float = 0.01
|
||||
prob_kira: float = 0.1
|
||||
prob_unique: float = 0.0001
|
||||
|
||||
# Unique card settings
|
||||
max_unique_cards: int = 1000 # Maximum number of unique cards
|
||||
|
||||
# CORS
|
||||
cors_origins: list[str] = ["http://localhost:3000", "https://card.syui.ai"]
|
||||
|
||||
# Security
|
||||
secret_key: str = "your-secret-key-change-this-in-production"
|
||||
|
||||
class Config:
|
||||
# 設定ファイルの優先順位: 1) 環境変数, 2) ~/.config/syui/ai/card/.env, 3) .env
|
||||
config_dir = Path.home() / ".config" / "syui" / "ai" / "card"
|
||||
env_file = [
|
||||
str(config_dir / ".env"), # ~/.config/syui/ai/card/.env
|
||||
".env" # カレントディレクトリの.env
|
||||
]
|
||||
env_file_encoding = "utf-8"
|
||||
|
||||
|
||||
settings = Settings()
|
@@ -1 +0,0 @@
|
||||
# Database Package
|
@@ -1,53 +0,0 @@
|
||||
"""Database base configuration"""
|
||||
import os
|
||||
from pathlib import Path
|
||||
from sqlalchemy.ext.declarative import declarative_base
|
||||
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine, async_sessionmaker
|
||||
from app.core.config import settings
|
||||
|
||||
# Create base class for models
|
||||
Base = declarative_base()
|
||||
|
||||
# Ensure database directory exists
|
||||
db_path = Path.home() / ".config" / "syui" / "ai" / "card"
|
||||
db_path.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
# Select database URL based on configuration
|
||||
database_url = settings.database_url_supabase if settings.use_supabase else settings.database_url
|
||||
|
||||
# Expand ~ in database URL
|
||||
if database_url.startswith("sqlite"):
|
||||
database_url = database_url.replace("~", str(Path.home()))
|
||||
|
||||
# Create async engine (SQLite-optimized settings)
|
||||
if "sqlite" in database_url:
|
||||
engine = create_async_engine(
|
||||
database_url,
|
||||
echo=settings.debug,
|
||||
future=True,
|
||||
# SQLite-specific optimizations
|
||||
connect_args={"check_same_thread": False}
|
||||
)
|
||||
else:
|
||||
# PostgreSQL settings (fallback)
|
||||
engine = create_async_engine(
|
||||
database_url,
|
||||
echo=settings.debug,
|
||||
future=True,
|
||||
pool_pre_ping=True,
|
||||
pool_size=5,
|
||||
max_overflow=10
|
||||
)
|
||||
|
||||
# Create async session factory
|
||||
async_session = async_sessionmaker(
|
||||
engine,
|
||||
class_=AsyncSession,
|
||||
expire_on_commit=False
|
||||
)
|
||||
|
||||
|
||||
async def get_session() -> AsyncSession:
|
||||
"""Dependency to get database session"""
|
||||
async with async_session() as session:
|
||||
yield session
|
@@ -1,121 +0,0 @@
|
||||
"""Database models"""
|
||||
from datetime import datetime
|
||||
from sqlalchemy import (
|
||||
Column, Integer, String, DateTime, Boolean,
|
||||
Float, ForeignKey, UniqueConstraint, Index,
|
||||
Enum as SQLEnum
|
||||
)
|
||||
from sqlalchemy.dialects.postgresql import UUID
|
||||
from sqlalchemy.orm import relationship
|
||||
import uuid
|
||||
import enum
|
||||
|
||||
from app.db.base import Base
|
||||
from app.models.card import CardRarity
|
||||
|
||||
|
||||
class User(Base):
|
||||
"""ユーザーモデル"""
|
||||
__tablename__ = "users"
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
did = Column(String, unique=True, nullable=False, index=True)
|
||||
handle = Column(String, nullable=True)
|
||||
created_at = Column(DateTime, default=datetime.utcnow)
|
||||
updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
|
||||
|
||||
# Relationships
|
||||
cards = relationship("UserCard", back_populates="owner")
|
||||
draws = relationship("DrawHistory", back_populates="user")
|
||||
|
||||
|
||||
class CardMaster(Base):
|
||||
"""カードマスタデータ"""
|
||||
__tablename__ = "card_master"
|
||||
|
||||
id = Column(Integer, primary_key=True) # 0-15
|
||||
name = Column(String, nullable=False)
|
||||
base_cp_min = Column(Integer, nullable=False)
|
||||
base_cp_max = Column(Integer, nullable=False)
|
||||
color = Column(String, nullable=False)
|
||||
description = Column(String)
|
||||
|
||||
# Relationships
|
||||
user_cards = relationship("UserCard", back_populates="card_info")
|
||||
|
||||
|
||||
class UserCard(Base):
|
||||
"""ユーザー所有カード"""
|
||||
__tablename__ = "user_cards"
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
user_id = Column(Integer, ForeignKey("users.id"), nullable=False)
|
||||
card_id = Column(Integer, ForeignKey("card_master.id"), nullable=False)
|
||||
cp = Column(Integer, nullable=False)
|
||||
status = Column(SQLEnum(CardRarity), nullable=False)
|
||||
skill = Column(String, nullable=True)
|
||||
obtained_at = Column(DateTime, default=datetime.utcnow)
|
||||
is_unique = Column(Boolean, default=False)
|
||||
unique_id = Column(UUID(as_uuid=True), nullable=True, unique=True)
|
||||
|
||||
# Relationships
|
||||
owner = relationship("User", back_populates="cards")
|
||||
card_info = relationship("CardMaster", back_populates="user_cards")
|
||||
|
||||
# Indexes
|
||||
__table_args__ = (
|
||||
Index('idx_user_cards', 'user_id', 'card_id'),
|
||||
)
|
||||
|
||||
|
||||
class UniqueCardRegistry(Base):
|
||||
"""uniqueカードのグローバルレジストリ"""
|
||||
__tablename__ = "unique_card_registry"
|
||||
|
||||
id = Column(Integer, primary_key=True)
|
||||
unique_id = Column(UUID(as_uuid=True), default=uuid.uuid4, unique=True, nullable=False)
|
||||
card_id = Column(Integer, ForeignKey("card_master.id"), nullable=False)
|
||||
owner_did = Column(String, ForeignKey("users.did"), nullable=False)
|
||||
obtained_at = Column(DateTime, default=datetime.utcnow)
|
||||
verse_skill_id = Column(String, nullable=True) # ai.verse連携用
|
||||
|
||||
# Unique constraint: 各card_idは1人のみ所有可能
|
||||
__table_args__ = (
|
||||
UniqueConstraint('card_id', name='unique_card_per_type'),
|
||||
)
|
||||
|
||||
|
||||
class DrawHistory(Base):
|
||||
"""ガチャ履歴"""
|
||||
__tablename__ = "draw_history"
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
user_id = Column(Integer, ForeignKey("users.id"), nullable=False)
|
||||
card_id = Column(Integer, nullable=False)
|
||||
status = Column(SQLEnum(CardRarity), nullable=False)
|
||||
cp = Column(Integer, nullable=False)
|
||||
is_paid = Column(Boolean, default=False)
|
||||
drawn_at = Column(DateTime, default=datetime.utcnow)
|
||||
|
||||
# Relationships
|
||||
user = relationship("User", back_populates="draws")
|
||||
|
||||
# Indexes
|
||||
__table_args__ = (
|
||||
Index('idx_draw_history_user', 'user_id', 'drawn_at'),
|
||||
)
|
||||
|
||||
|
||||
class GachaPool(Base):
|
||||
"""ガチャプール(ピックアップ管理)"""
|
||||
__tablename__ = "gacha_pools"
|
||||
|
||||
id = Column(Integer, primary_key=True)
|
||||
name = Column(String, nullable=False)
|
||||
description = Column(String)
|
||||
is_active = Column(Boolean, default=True)
|
||||
start_at = Column(DateTime, nullable=False)
|
||||
end_at = Column(DateTime, nullable=True)
|
||||
pickup_card_ids = Column(String) # JSON array of card IDs
|
||||
rate_up_multiplier = Column(Float, default=1.0)
|
||||
created_at = Column(DateTime, default=datetime.utcnow)
|
@@ -1,65 +0,0 @@
|
||||
"""FastAPI application entry point"""
|
||||
import os
|
||||
from fastapi import FastAPI
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
|
||||
from app.core.config import settings
|
||||
from app.routes import cards, auth, sync
|
||||
from app.mcp_server import AICardMcpServer
|
||||
|
||||
# Initialize MCP server
|
||||
enable_mcp = os.getenv("ENABLE_MCP", "true").lower() == "true"
|
||||
mcp_server = AICardMcpServer(enable_mcp=enable_mcp)
|
||||
|
||||
# Get FastAPI app from MCP server
|
||||
app = mcp_server.get_app()
|
||||
|
||||
# Update app configuration
|
||||
app.title = settings.app_name
|
||||
app.version = settings.app_version
|
||||
app.docs_url = "/docs"
|
||||
app.redoc_url = "/redoc"
|
||||
|
||||
# CORS middleware
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=settings.cors_origins,
|
||||
allow_credentials=True,
|
||||
allow_methods=["*"],
|
||||
allow_headers=["*"],
|
||||
)
|
||||
|
||||
# Include routers
|
||||
app.include_router(auth.router, prefix=settings.api_v1_prefix)
|
||||
app.include_router(cards.router, prefix=settings.api_v1_prefix)
|
||||
app.include_router(sync.router, prefix=settings.api_v1_prefix)
|
||||
|
||||
|
||||
@app.get("/")
|
||||
async def root():
|
||||
"""Root endpoint"""
|
||||
return {
|
||||
"app": settings.app_name,
|
||||
"version": settings.app_version,
|
||||
"status": "running"
|
||||
}
|
||||
|
||||
|
||||
@app.get("/health")
|
||||
async def health_check():
|
||||
"""Health check endpoint"""
|
||||
return {
|
||||
"status": "healthy",
|
||||
"mcp_enabled": enable_mcp,
|
||||
"mcp_endpoint": "/mcp" if enable_mcp else None
|
||||
}
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import uvicorn
|
||||
uvicorn.run(
|
||||
"app.main:app",
|
||||
host="0.0.0.0",
|
||||
port=8000,
|
||||
reload=True
|
||||
)
|
@@ -1,286 +0,0 @@
|
||||
"""MCP Server for ai.card system"""
|
||||
|
||||
from typing import Optional, List, Dict, Any
|
||||
from mcp.server.fastmcp import FastMCP
|
||||
from fastapi import FastAPI, Depends, HTTPException
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from pathlib import Path
|
||||
import logging
|
||||
|
||||
from app.core.config import settings
|
||||
from app.db.base import get_session
|
||||
from app.models.card import Card, CardRarity, CardDrawResult
|
||||
from app.repositories.card import CardRepository, UniqueCardRepository
|
||||
from app.repositories.user import UserRepository
|
||||
from app.services.gacha import GachaService
|
||||
# from app.services.card_sync import CardSyncService # Temporarily disabled
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class AICardMcpServer:
|
||||
"""MCP Server that exposes ai.card functionality to AI assistants"""
|
||||
|
||||
def __init__(self, enable_mcp: bool = True):
|
||||
self.enable_mcp = enable_mcp
|
||||
|
||||
# Create FastAPI app
|
||||
self.app = FastAPI(
|
||||
title="AI.Card - Card Game System",
|
||||
description="MCP server for ai.card system",
|
||||
version=settings.app_version
|
||||
)
|
||||
|
||||
# Create MCP server with FastAPI app
|
||||
self.server = None
|
||||
if enable_mcp:
|
||||
self.server = FastMCP("aicard")
|
||||
self._register_mcp_tools()
|
||||
|
||||
def _register_mcp_tools(self):
|
||||
"""Register all MCP tools"""
|
||||
|
||||
@self.app.get("/get_user_cards", operation_id="get_user_cards")
|
||||
async def get_user_cards(
|
||||
did: str,
|
||||
limit: int = 10,
|
||||
session: AsyncSession = Depends(get_session)
|
||||
) -> List[Dict[str, Any]]:
|
||||
"""Get all cards owned by a user"""
|
||||
try:
|
||||
user_repo = UserRepository(session)
|
||||
card_repo = CardRepository(session)
|
||||
|
||||
# Get user
|
||||
user = await user_repo.get_by_did(did)
|
||||
if not user:
|
||||
return []
|
||||
|
||||
# Get user cards
|
||||
user_cards = await card_repo.get_user_cards(user.id, limit=limit)
|
||||
|
||||
return [
|
||||
{
|
||||
"id": card.card_id,
|
||||
"cp": card.cp,
|
||||
"status": card.status,
|
||||
"skill": card.skill,
|
||||
"owner_did": did,
|
||||
"obtained_at": card.obtained_at.isoformat(),
|
||||
"is_unique": card.is_unique,
|
||||
"unique_id": str(card.unique_id) if card.unique_id else None
|
||||
}
|
||||
for card in user_cards
|
||||
]
|
||||
except Exception as e:
|
||||
logger.error(f"Error getting user cards: {e}")
|
||||
return []
|
||||
|
||||
@self.app.post("/draw_card", operation_id="draw_card")
|
||||
async def draw_card(
|
||||
did: str,
|
||||
is_paid: bool = False,
|
||||
session: AsyncSession = Depends(get_session)
|
||||
) -> Dict[str, Any]:
|
||||
"""Draw a new card (gacha) for user"""
|
||||
try:
|
||||
gacha_service = GachaService(session)
|
||||
|
||||
# Draw card
|
||||
card, is_unique = await gacha_service.draw_card(did, is_paid)
|
||||
await session.commit()
|
||||
|
||||
return {
|
||||
"success": True,
|
||||
"card": {
|
||||
"id": card.id,
|
||||
"cp": card.cp,
|
||||
"status": card.status,
|
||||
"skill": card.skill,
|
||||
"owner_did": card.owner_did,
|
||||
"obtained_at": card.obtained_at.isoformat(),
|
||||
"is_unique": card.is_unique,
|
||||
"unique_id": card.unique_id
|
||||
},
|
||||
"is_unique": is_unique,
|
||||
"animation_type": "kira" if card.status in [CardRarity.KIRA, CardRarity.UNIQUE] else "normal"
|
||||
}
|
||||
except Exception as e:
|
||||
logger.error(f"Error drawing card: {e}")
|
||||
await session.rollback()
|
||||
return {
|
||||
"success": False,
|
||||
"error": str(e)
|
||||
}
|
||||
|
||||
@self.app.get("/get_card_details", operation_id="get_card_details")
|
||||
async def get_card_details(
|
||||
card_id: int,
|
||||
session: AsyncSession = Depends(get_session)
|
||||
) -> Dict[str, Any]:
|
||||
"""Get detailed information about a card type"""
|
||||
try:
|
||||
# Get card info from gacha service
|
||||
gacha_service = GachaService(session)
|
||||
|
||||
if card_id not in gacha_service.CARD_INFO:
|
||||
return {"error": f"Card ID {card_id} not found"}
|
||||
|
||||
card_info = gacha_service.CARD_INFO[card_id]
|
||||
|
||||
# Get unique card availability
|
||||
unique_repo = UniqueCardRepository(session)
|
||||
is_unique_available = await unique_repo.is_card_available(card_id)
|
||||
|
||||
return {
|
||||
"id": card_id,
|
||||
"name": card_info["name"],
|
||||
"base_cp_range": card_info["base_cp_range"],
|
||||
"is_unique_available": is_unique_available,
|
||||
"description": f"Card {card_id}: {card_info['name']}"
|
||||
}
|
||||
except Exception as e:
|
||||
logger.error(f"Error getting card details: {e}")
|
||||
return {"error": str(e)}
|
||||
|
||||
@self.app.post("/sync_cards_atproto", operation_id="sync_cards_atproto")
|
||||
async def sync_cards_atproto(
|
||||
did: str,
|
||||
session: AsyncSession = Depends(get_session)
|
||||
) -> Dict[str, str]:
|
||||
"""Sync user's cards with atproto (temporarily disabled)"""
|
||||
return {"status": "atproto sync temporarily disabled due to dependency issues"}
|
||||
|
||||
@self.app.get("/analyze_card_collection", operation_id="analyze_card_collection")
|
||||
async def analyze_card_collection(
|
||||
did: str,
|
||||
session: AsyncSession = Depends(get_session)
|
||||
) -> Dict[str, Any]:
|
||||
"""Analyze user's card collection"""
|
||||
try:
|
||||
user_repo = UserRepository(session)
|
||||
card_repo = CardRepository(session)
|
||||
|
||||
# Get user
|
||||
user = await user_repo.get_by_did(did)
|
||||
if not user:
|
||||
return {
|
||||
"total_cards": 0,
|
||||
"rarity_distribution": {},
|
||||
"message": "User not found"
|
||||
}
|
||||
|
||||
# Get all user cards
|
||||
user_cards = await card_repo.get_user_cards(user.id, limit=1000)
|
||||
|
||||
if not user_cards:
|
||||
return {
|
||||
"total_cards": 0,
|
||||
"rarity_distribution": {},
|
||||
"message": "No cards found"
|
||||
}
|
||||
|
||||
# Analyze collection
|
||||
rarity_count = {}
|
||||
total_cp = 0
|
||||
card_type_count = {}
|
||||
|
||||
for card in user_cards:
|
||||
# Rarity distribution
|
||||
rarity = card.status
|
||||
rarity_count[rarity] = rarity_count.get(rarity, 0) + 1
|
||||
|
||||
# Total CP
|
||||
total_cp += card.cp
|
||||
|
||||
# Card type distribution
|
||||
card_type_count[card.card_id] = card_type_count.get(card.card_id, 0) + 1
|
||||
|
||||
# Find strongest card
|
||||
strongest_card = max(user_cards, key=lambda x: x.cp)
|
||||
|
||||
return {
|
||||
"total_cards": len(user_cards),
|
||||
"rarity_distribution": rarity_count,
|
||||
"card_type_distribution": card_type_count,
|
||||
"average_cp": total_cp / len(user_cards) if user_cards else 0,
|
||||
"total_cp": total_cp,
|
||||
"strongest_card": {
|
||||
"id": strongest_card.card_id,
|
||||
"cp": strongest_card.cp,
|
||||
"status": strongest_card.status,
|
||||
"is_unique": strongest_card.is_unique
|
||||
},
|
||||
"unique_count": len([c for c in user_cards if c.is_unique])
|
||||
}
|
||||
except Exception as e:
|
||||
logger.error(f"Error analyzing collection: {e}")
|
||||
return {"error": str(e)}
|
||||
|
||||
@self.app.get("/get_unique_registry", operation_id="get_unique_registry")
|
||||
async def get_unique_registry(
|
||||
session: AsyncSession = Depends(get_session)
|
||||
) -> Dict[str, Any]:
|
||||
"""Get all registered unique cards"""
|
||||
try:
|
||||
unique_repo = UniqueCardRepository(session)
|
||||
|
||||
# Get all unique cards
|
||||
unique_cards = await unique_repo.get_all_unique_cards()
|
||||
|
||||
# Get available unique card IDs
|
||||
available_ids = await unique_repo.get_available_unique_cards()
|
||||
|
||||
return {
|
||||
"registered_unique_cards": [
|
||||
{
|
||||
"card_id": card.card_id,
|
||||
"unique_id": card.unique_id,
|
||||
"owner_did": card.owner_did,
|
||||
"obtained_at": card.obtained_at.isoformat()
|
||||
}
|
||||
for card in unique_cards
|
||||
],
|
||||
"available_unique_card_ids": available_ids,
|
||||
"total_registered": len(unique_cards),
|
||||
"total_available": len(available_ids)
|
||||
}
|
||||
except Exception as e:
|
||||
logger.error(f"Error getting unique registry: {e}")
|
||||
return {"error": str(e)}
|
||||
|
||||
@self.app.get("/get_gacha_stats", operation_id="get_gacha_stats")
|
||||
async def get_gacha_stats(
|
||||
session: AsyncSession = Depends(get_session)
|
||||
) -> Dict[str, Any]:
|
||||
"""Get gacha system statistics"""
|
||||
try:
|
||||
return {
|
||||
"rarity_probabilities": {
|
||||
"normal": f"{100 - settings.prob_rare}%",
|
||||
"rare": f"{settings.prob_rare - settings.prob_super_rare}%",
|
||||
"super_rare": f"{settings.prob_super_rare - settings.prob_kira}%",
|
||||
"kira": f"{settings.prob_kira - settings.prob_unique}%",
|
||||
"unique": f"{settings.prob_unique}%"
|
||||
},
|
||||
"total_card_types": 16,
|
||||
"card_names": [info["name"] for info in GachaService.CARD_INFO.values()],
|
||||
"system_info": {
|
||||
"daily_limit": "1 free draw per day",
|
||||
"paid_gacha": "Enhanced probabilities",
|
||||
"unique_system": "First-come-first-served globally unique cards"
|
||||
}
|
||||
}
|
||||
except Exception as e:
|
||||
logger.error(f"Error getting gacha stats: {e}")
|
||||
return {"error": str(e)}
|
||||
|
||||
# MCP server will be run separately, not here
|
||||
|
||||
def get_server(self) -> Optional[FastMCP]:
|
||||
"""Get the FastAPI MCP server instance"""
|
||||
return self.server
|
||||
|
||||
def get_app(self) -> FastAPI:
|
||||
"""Get the FastAPI app instance"""
|
||||
return self.app
|
@@ -1 +0,0 @@
|
||||
# Models Package
|
@@ -1,57 +0,0 @@
|
||||
"""Card data models"""
|
||||
from datetime import datetime
|
||||
from enum import Enum
|
||||
from typing import Optional
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
|
||||
class CardRarity(str, Enum):
|
||||
"""カードのレアリティ"""
|
||||
NORMAL = "normal"
|
||||
RARE = "rare"
|
||||
SUPER_RARE = "super_rare"
|
||||
KIRA = "kira" # キラカード(0.1%)
|
||||
UNIQUE = "unique" # uniqueカード(0.0001%)
|
||||
|
||||
|
||||
class CardBase(BaseModel):
|
||||
"""カードの基本情報"""
|
||||
id: int = Field(..., ge=0, le=15, description="カード種類ID (0-15)")
|
||||
cp: int = Field(..., ge=1, le=999, description="カードパワー")
|
||||
status: CardRarity = Field(default=CardRarity.NORMAL, description="レアリティ")
|
||||
skill: Optional[str] = Field(None, description="スキル情報")
|
||||
|
||||
|
||||
class Card(CardBase):
|
||||
"""所有カード情報"""
|
||||
owner_did: str = Field(..., description="所有者のatproto DID")
|
||||
obtained_at: datetime = Field(default_factory=datetime.utcnow, description="取得日時")
|
||||
is_unique: bool = Field(default=False, description="uniqueカードフラグ")
|
||||
unique_id: Optional[str] = Field(None, description="unique時のグローバルID")
|
||||
|
||||
class Config:
|
||||
json_encoders = {
|
||||
datetime: lambda v: v.isoformat()
|
||||
}
|
||||
|
||||
|
||||
class CardDraw(BaseModel):
|
||||
"""カード抽選リクエスト"""
|
||||
user_did: str = Field(..., description="ユーザーのDID")
|
||||
is_paid: bool = Field(default=False, description="課金ガチャかどうか")
|
||||
|
||||
|
||||
class CardDrawResult(BaseModel):
|
||||
"""カード抽選結果"""
|
||||
card: Card
|
||||
is_new: bool = Field(..., description="新規取得かどうか")
|
||||
animation_type: str = Field(..., description="演出タイプ")
|
||||
|
||||
|
||||
class UniqueCardRegistry(BaseModel):
|
||||
"""uniqueカードの登録情報"""
|
||||
card_id: int
|
||||
unique_id: str
|
||||
owner_did: str
|
||||
obtained_at: datetime
|
||||
verse_skill_id: Optional[str] = None
|
@@ -1 +0,0 @@
|
||||
# Repositories Package
|
@@ -1,65 +0,0 @@
|
||||
"""Base repository class"""
|
||||
from typing import Generic, Type, TypeVar, Optional, List
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy import select, update, delete
|
||||
from sqlalchemy.orm import selectinload
|
||||
|
||||
from app.db.base import Base
|
||||
|
||||
ModelType = TypeVar("ModelType", bound=Base)
|
||||
|
||||
|
||||
class BaseRepository(Generic[ModelType]):
|
||||
"""Base repository with common CRUD operations"""
|
||||
|
||||
def __init__(self, model: Type[ModelType], session: AsyncSession):
|
||||
self.model = model
|
||||
self.session = session
|
||||
|
||||
async def create(self, **kwargs) -> ModelType:
|
||||
"""Create a new record"""
|
||||
instance = self.model(**kwargs)
|
||||
self.session.add(instance)
|
||||
await self.session.flush()
|
||||
return instance
|
||||
|
||||
async def get(self, id: int) -> Optional[ModelType]:
|
||||
"""Get a record by ID"""
|
||||
result = await self.session.execute(
|
||||
select(self.model).where(self.model.id == id)
|
||||
)
|
||||
return result.scalar_one_or_none()
|
||||
|
||||
async def get_multi(
|
||||
self,
|
||||
skip: int = 0,
|
||||
limit: int = 100,
|
||||
**filters
|
||||
) -> List[ModelType]:
|
||||
"""Get multiple records with pagination"""
|
||||
query = select(self.model)
|
||||
|
||||
# Apply filters
|
||||
for key, value in filters.items():
|
||||
if hasattr(self.model, key):
|
||||
query = query.where(getattr(self.model, key) == value)
|
||||
|
||||
query = query.offset(skip).limit(limit)
|
||||
result = await self.session.execute(query)
|
||||
return result.scalars().all()
|
||||
|
||||
async def update(self, id: int, **kwargs) -> Optional[ModelType]:
|
||||
"""Update a record"""
|
||||
await self.session.execute(
|
||||
update(self.model)
|
||||
.where(self.model.id == id)
|
||||
.values(**kwargs)
|
||||
)
|
||||
return await self.get(id)
|
||||
|
||||
async def delete(self, id: int) -> bool:
|
||||
"""Delete a record"""
|
||||
result = await self.session.execute(
|
||||
delete(self.model).where(self.model.id == id)
|
||||
)
|
||||
return result.rowcount > 0
|
@@ -1,136 +0,0 @@
|
||||
"""Card repository"""
|
||||
from typing import List, Optional
|
||||
from datetime import datetime
|
||||
from sqlalchemy import select, and_, func
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy.orm import selectinload
|
||||
import uuid
|
||||
|
||||
from app.repositories.base import BaseRepository
|
||||
from app.db.models import UserCard, UniqueCardRegistry, CardMaster
|
||||
from app.models.card import CardRarity
|
||||
|
||||
|
||||
class CardRepository(BaseRepository[UserCard]):
|
||||
"""Card repository with custom methods"""
|
||||
|
||||
def __init__(self, session: AsyncSession):
|
||||
super().__init__(UserCard, session)
|
||||
|
||||
async def get_user_cards(
|
||||
self,
|
||||
user_id: int,
|
||||
skip: int = 0,
|
||||
limit: int = 100
|
||||
) -> List[UserCard]:
|
||||
"""Get all cards for a user"""
|
||||
result = await self.session.execute(
|
||||
select(UserCard)
|
||||
.options(selectinload(UserCard.card_info))
|
||||
.where(UserCard.user_id == user_id)
|
||||
.order_by(UserCard.obtained_at.desc())
|
||||
.offset(skip)
|
||||
.limit(limit)
|
||||
)
|
||||
return result.scalars().all()
|
||||
|
||||
async def count_user_cards(self, user_id: int, card_id: int) -> int:
|
||||
"""Count how many of a specific card a user has"""
|
||||
result = await self.session.execute(
|
||||
select(func.count(UserCard.id))
|
||||
.where(
|
||||
and_(
|
||||
UserCard.user_id == user_id,
|
||||
UserCard.card_id == card_id
|
||||
)
|
||||
)
|
||||
)
|
||||
return result.scalar() or 0
|
||||
|
||||
async def create_user_card(
|
||||
self,
|
||||
user_id: int,
|
||||
card_id: int,
|
||||
cp: int,
|
||||
status: CardRarity,
|
||||
skill: Optional[str] = None,
|
||||
is_unique: bool = False
|
||||
) -> UserCard:
|
||||
"""Create a new user card"""
|
||||
unique_id = None
|
||||
if is_unique:
|
||||
unique_id = uuid.uuid4()
|
||||
|
||||
card = await self.create(
|
||||
user_id=user_id,
|
||||
card_id=card_id,
|
||||
cp=cp,
|
||||
status=status,
|
||||
skill=skill,
|
||||
is_unique=is_unique,
|
||||
unique_id=unique_id
|
||||
)
|
||||
|
||||
# If unique, register it globally
|
||||
if is_unique:
|
||||
await self._register_unique_card(card)
|
||||
|
||||
return card
|
||||
|
||||
async def _register_unique_card(self, card: UserCard):
|
||||
"""Register a unique card in the global registry"""
|
||||
# Get user DID
|
||||
user_did = await self.session.execute(
|
||||
select(User.did).where(User.id == card.user_id)
|
||||
)
|
||||
user_did = user_did.scalar()
|
||||
|
||||
registry = UniqueCardRegistry(
|
||||
unique_id=card.unique_id,
|
||||
card_id=card.card_id,
|
||||
owner_did=user_did,
|
||||
obtained_at=card.obtained_at
|
||||
)
|
||||
self.session.add(registry)
|
||||
|
||||
|
||||
class UniqueCardRepository(BaseRepository[UniqueCardRegistry]):
|
||||
"""Unique card registry repository"""
|
||||
|
||||
def __init__(self, session: AsyncSession):
|
||||
super().__init__(UniqueCardRegistry, session)
|
||||
|
||||
async def is_card_available(self, card_id: int) -> bool:
|
||||
"""Check if a unique card is still available"""
|
||||
result = await self.session.execute(
|
||||
select(func.count(UniqueCardRegistry.id))
|
||||
.where(UniqueCardRegistry.card_id == card_id)
|
||||
)
|
||||
count = result.scalar() or 0
|
||||
return count == 0
|
||||
|
||||
async def get_all_unique_cards(self) -> List[UniqueCardRegistry]:
|
||||
"""Get all registered unique cards"""
|
||||
result = await self.session.execute(
|
||||
select(UniqueCardRegistry)
|
||||
.order_by(UniqueCardRegistry.obtained_at.desc())
|
||||
)
|
||||
return result.scalars().all()
|
||||
|
||||
async def get_available_unique_cards(self) -> List[int]:
|
||||
"""Get list of card IDs that are still available as unique"""
|
||||
# Get all card IDs
|
||||
all_card_ids = set(range(16))
|
||||
|
||||
# Get taken card IDs
|
||||
result = await self.session.execute(
|
||||
select(UniqueCardRegistry.card_id).distinct()
|
||||
)
|
||||
taken_ids = set(result.scalars().all())
|
||||
|
||||
# Return available IDs
|
||||
return list(all_card_ids - taken_ids)
|
||||
|
||||
|
||||
# Import User model here to avoid circular import
|
||||
from app.db.models import User
|
@@ -1,38 +0,0 @@
|
||||
"""User repository"""
|
||||
from typing import Optional
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy.orm import selectinload
|
||||
|
||||
from app.repositories.base import BaseRepository
|
||||
from app.db.models import User
|
||||
|
||||
|
||||
class UserRepository(BaseRepository[User]):
|
||||
"""User repository with custom methods"""
|
||||
|
||||
def __init__(self, session: AsyncSession):
|
||||
super().__init__(User, session)
|
||||
|
||||
async def get_by_did(self, did: str) -> Optional[User]:
|
||||
"""Get user by DID"""
|
||||
result = await self.session.execute(
|
||||
select(User).where(User.did == did)
|
||||
)
|
||||
return result.scalar_one_or_none()
|
||||
|
||||
async def get_or_create(self, did: str, handle: Optional[str] = None) -> User:
|
||||
"""Get existing user or create new one"""
|
||||
user = await self.get_by_did(did)
|
||||
if not user:
|
||||
user = await self.create(did=did, handle=handle)
|
||||
return user
|
||||
|
||||
async def get_with_cards(self, user_id: int) -> Optional[User]:
|
||||
"""Get user with all their cards"""
|
||||
result = await self.session.execute(
|
||||
select(User)
|
||||
.options(selectinload(User.cards))
|
||||
.where(User.id == user_id)
|
||||
)
|
||||
return result.scalar_one_or_none()
|
@@ -1 +0,0 @@
|
||||
# Routes Package
|
@@ -1,133 +0,0 @@
|
||||
"""Authentication routes"""
|
||||
from datetime import timedelta
|
||||
from typing import Optional
|
||||
from fastapi import APIRouter, HTTPException, Depends, Response
|
||||
from fastapi.responses import JSONResponse
|
||||
from pydantic import BaseModel
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.auth.dependencies import (
|
||||
AtprotoAuth,
|
||||
create_access_token,
|
||||
require_user,
|
||||
AuthUser,
|
||||
ACCESS_TOKEN_EXPIRE_MINUTES
|
||||
)
|
||||
from app.db.base import get_session
|
||||
from app.repositories.user import UserRepository
|
||||
# from app.services.atproto import AtprotoService
|
||||
|
||||
|
||||
router = APIRouter(prefix="/auth", tags=["auth"])
|
||||
|
||||
|
||||
class LoginRequest(BaseModel):
|
||||
"""Login request model"""
|
||||
identifier: str # Handle or DID
|
||||
password: str # App password
|
||||
|
||||
|
||||
class LoginResponse(BaseModel):
|
||||
"""Login response model"""
|
||||
access_token: str
|
||||
token_type: str = "bearer"
|
||||
did: str
|
||||
handle: str
|
||||
|
||||
|
||||
class VerifyResponse(BaseModel):
|
||||
"""Verify response model"""
|
||||
did: str
|
||||
handle: str
|
||||
valid: bool = True
|
||||
|
||||
|
||||
@router.post("/login", response_model=LoginResponse)
|
||||
async def login(
|
||||
request: LoginRequest,
|
||||
response: Response,
|
||||
db: AsyncSession = Depends(get_session)
|
||||
):
|
||||
"""
|
||||
Login with atproto credentials
|
||||
|
||||
- **identifier**: atproto handle or DID
|
||||
- **password**: App password (not main password)
|
||||
"""
|
||||
auth = AtprotoAuth()
|
||||
|
||||
# Authenticate with atproto
|
||||
user = await auth.authenticate(request.identifier, request.password)
|
||||
if not user:
|
||||
raise HTTPException(
|
||||
status_code=401,
|
||||
detail="Invalid credentials"
|
||||
)
|
||||
|
||||
# Create or update user in database
|
||||
user_repo = UserRepository(db)
|
||||
await user_repo.get_or_create(did=user.did, handle=user.handle)
|
||||
await db.commit()
|
||||
|
||||
# Create access token
|
||||
access_token = create_access_token(
|
||||
data={"did": user.did, "handle": user.handle},
|
||||
expires_delta=timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES)
|
||||
)
|
||||
|
||||
# Set cookie for web clients
|
||||
response.set_cookie(
|
||||
key="ai_card_token",
|
||||
value=access_token,
|
||||
httponly=True,
|
||||
secure=True,
|
||||
samesite="lax",
|
||||
max_age=ACCESS_TOKEN_EXPIRE_MINUTES * 60
|
||||
)
|
||||
|
||||
return LoginResponse(
|
||||
access_token=access_token,
|
||||
did=user.did,
|
||||
handle=user.handle or ""
|
||||
)
|
||||
|
||||
|
||||
@router.post("/logout")
|
||||
async def logout(response: Response):
|
||||
"""Logout and clear session"""
|
||||
response.delete_cookie("ai_card_token")
|
||||
return {"message": "Logged out successfully"}
|
||||
|
||||
|
||||
@router.get("/verify", response_model=VerifyResponse)
|
||||
async def verify_session(
|
||||
current_user: AuthUser = Depends(require_user)
|
||||
):
|
||||
"""Verify current session is valid"""
|
||||
return VerifyResponse(
|
||||
did=current_user.did,
|
||||
handle=current_user.handle or "",
|
||||
valid=True
|
||||
)
|
||||
|
||||
|
||||
@router.post("/verify-did")
|
||||
async def verify_did(did: str, handle: Optional[str] = None):
|
||||
"""
|
||||
Verify DID is valid (public endpoint)
|
||||
|
||||
- **did**: DID to verify
|
||||
- **handle**: Optional handle to cross-check
|
||||
"""
|
||||
service = AtprotoService()
|
||||
is_valid = await service.verify_did(did, handle)
|
||||
|
||||
return {
|
||||
"did": did,
|
||||
"handle": handle,
|
||||
"valid": is_valid
|
||||
}
|
||||
|
||||
|
||||
# Import Optional here
|
||||
from typing import Optional
|
@@ -1,118 +0,0 @@
|
||||
"""Card-related API routes"""
|
||||
from typing import List
|
||||
from fastapi import APIRouter, HTTPException, Depends
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.models.card import Card, CardDraw, CardDrawResult
|
||||
from app.services.gacha import GachaService
|
||||
from app.repositories.user import UserRepository
|
||||
from app.repositories.card import CardRepository, UniqueCardRepository
|
||||
from app.db.base import get_session
|
||||
|
||||
router = APIRouter(prefix="/cards", tags=["cards"])
|
||||
|
||||
|
||||
@router.post("/draw", response_model=CardDrawResult)
|
||||
async def draw_card(
|
||||
draw_request: CardDraw,
|
||||
db: AsyncSession = Depends(get_session)
|
||||
):
|
||||
"""
|
||||
カードを抽選する
|
||||
|
||||
- **user_did**: ユーザーのatproto DID
|
||||
- **is_paid**: 課金ガチャかどうか
|
||||
"""
|
||||
try:
|
||||
gacha_service = GachaService(db)
|
||||
card, is_unique = await gacha_service.draw_card(
|
||||
user_did=draw_request.user_did,
|
||||
is_paid=draw_request.is_paid
|
||||
)
|
||||
|
||||
# 演出タイプを決定
|
||||
animation_type = "normal"
|
||||
if is_unique:
|
||||
animation_type = "unique"
|
||||
elif card.status.value == "kira":
|
||||
animation_type = "kira"
|
||||
elif card.status.value in ["super_rare", "rare"]:
|
||||
animation_type = "rare"
|
||||
|
||||
# 新規取得かチェック
|
||||
user_repo = UserRepository(db)
|
||||
card_repo = CardRepository(db)
|
||||
user = await user_repo.get_by_did(draw_request.user_did)
|
||||
count = await card_repo.count_user_cards(user.id, card.id)
|
||||
is_new = count == 1 # 今引いたカードが初めてなら1枚
|
||||
|
||||
result = CardDrawResult(
|
||||
card=card,
|
||||
is_new=is_new,
|
||||
animation_type=animation_type
|
||||
)
|
||||
|
||||
await db.commit()
|
||||
return result
|
||||
|
||||
except Exception as e:
|
||||
await db.rollback()
|
||||
raise HTTPException(status_code=500, detail=str(e))
|
||||
|
||||
|
||||
@router.get("/user/{user_did}", response_model=List[Card])
|
||||
async def get_user_cards(
|
||||
user_did: str,
|
||||
skip: int = 0,
|
||||
limit: int = 100,
|
||||
db: AsyncSession = Depends(get_session)
|
||||
):
|
||||
"""
|
||||
ユーザーの所有カード一覧を取得
|
||||
|
||||
- **user_did**: ユーザーのatproto DID
|
||||
"""
|
||||
user_repo = UserRepository(db)
|
||||
card_repo = CardRepository(db)
|
||||
|
||||
user = await user_repo.get_by_did(user_did)
|
||||
if not user:
|
||||
raise HTTPException(status_code=404, detail="User not found")
|
||||
|
||||
user_cards = await card_repo.get_user_cards(user.id, skip=skip, limit=limit)
|
||||
|
||||
# Convert to API model
|
||||
cards = []
|
||||
for uc in user_cards:
|
||||
card = Card(
|
||||
id=uc.card_id,
|
||||
cp=uc.cp,
|
||||
status=uc.status,
|
||||
skill=uc.skill,
|
||||
owner_did=user_did,
|
||||
obtained_at=uc.obtained_at,
|
||||
is_unique=uc.is_unique,
|
||||
unique_id=str(uc.unique_id) if uc.unique_id else None
|
||||
)
|
||||
cards.append(card)
|
||||
|
||||
return cards
|
||||
|
||||
|
||||
@router.get("/unique")
|
||||
async def get_unique_cards(db: AsyncSession = Depends(get_session)):
|
||||
"""
|
||||
全てのuniqueカード一覧を取得(所有者情報付き)
|
||||
"""
|
||||
unique_repo = UniqueCardRepository(db)
|
||||
unique_cards = await unique_repo.get_all_unique_cards()
|
||||
|
||||
return [
|
||||
{
|
||||
"card_id": uc.card_id,
|
||||
"owner_did": uc.owner_did,
|
||||
"obtained_at": uc.obtained_at,
|
||||
"unique_id": str(uc.unique_id)
|
||||
}
|
||||
for uc in unique_cards
|
||||
]
|
@@ -1,152 +0,0 @@
|
||||
"""Synchronization routes for atproto"""
|
||||
from typing import Optional
|
||||
from fastapi import APIRouter, HTTPException, Depends
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from pydantic import BaseModel
|
||||
|
||||
from app.auth.dependencies import require_user, AuthUser
|
||||
from app.db.base import get_session
|
||||
from app.services.card_sync import CardSyncService
|
||||
from app.repositories.user import UserRepository
|
||||
|
||||
|
||||
router = APIRouter(prefix="/sync", tags=["sync"])
|
||||
|
||||
|
||||
class SyncRequest(BaseModel):
|
||||
"""Sync request model"""
|
||||
atproto_session: str # Session string from atproto login
|
||||
|
||||
|
||||
class SyncResponse(BaseModel):
|
||||
"""Sync response model"""
|
||||
synced_to_pds: int = 0
|
||||
imported_from_pds: int = 0
|
||||
message: str
|
||||
|
||||
|
||||
@router.post("/cards", response_model=SyncResponse)
|
||||
async def sync_cards(
|
||||
request: SyncRequest,
|
||||
current_user: AuthUser = Depends(require_user),
|
||||
db: AsyncSession = Depends(get_session)
|
||||
):
|
||||
"""
|
||||
Sync cards between database and atproto PDS
|
||||
|
||||
- **atproto_session**: Session string from atproto login
|
||||
"""
|
||||
try:
|
||||
# Get user from database
|
||||
user_repo = UserRepository(db)
|
||||
user = await user_repo.get_by_did(current_user.did)
|
||||
if not user:
|
||||
raise HTTPException(status_code=404, detail="User not found")
|
||||
|
||||
# Create sync service
|
||||
sync_service = CardSyncService(db, request.atproto_session)
|
||||
|
||||
# Import from PDS first
|
||||
imported = await sync_service.import_cards_from_pds(current_user.did)
|
||||
|
||||
# Then sync all cards to PDS
|
||||
synced = await sync_service.sync_all_user_cards(user.id, current_user.did)
|
||||
|
||||
await db.commit()
|
||||
|
||||
return SyncResponse(
|
||||
synced_to_pds=synced,
|
||||
imported_from_pds=imported,
|
||||
message=f"Successfully synced {synced} cards to PDS and imported {imported} cards from PDS"
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
await db.rollback()
|
||||
raise HTTPException(status_code=500, detail=str(e))
|
||||
|
||||
|
||||
@router.post("/export")
|
||||
async def export_to_pds(
|
||||
request: SyncRequest,
|
||||
current_user: AuthUser = Depends(require_user),
|
||||
db: AsyncSession = Depends(get_session)
|
||||
):
|
||||
"""
|
||||
Export all cards to atproto PDS
|
||||
|
||||
- **atproto_session**: Session string from atproto login
|
||||
"""
|
||||
try:
|
||||
user_repo = UserRepository(db)
|
||||
user = await user_repo.get_by_did(current_user.did)
|
||||
if not user:
|
||||
raise HTTPException(status_code=404, detail="User not found")
|
||||
|
||||
sync_service = CardSyncService(db, request.atproto_session)
|
||||
synced = await sync_service.sync_all_user_cards(user.id, current_user.did)
|
||||
|
||||
return {
|
||||
"exported": synced,
|
||||
"message": f"Successfully exported {synced} cards to PDS"
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=500, detail=str(e))
|
||||
|
||||
|
||||
@router.post("/import")
|
||||
async def import_from_pds(
|
||||
request: SyncRequest,
|
||||
current_user: AuthUser = Depends(require_user),
|
||||
db: AsyncSession = Depends(get_session)
|
||||
):
|
||||
"""
|
||||
Import cards from atproto PDS
|
||||
|
||||
- **atproto_session**: Session string from atproto login
|
||||
"""
|
||||
try:
|
||||
sync_service = CardSyncService(db, request.atproto_session)
|
||||
imported = await sync_service.import_cards_from_pds(current_user.did)
|
||||
|
||||
await db.commit()
|
||||
|
||||
return {
|
||||
"imported": imported,
|
||||
"message": f"Successfully imported {imported} cards from PDS"
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
await db.rollback()
|
||||
raise HTTPException(status_code=500, detail=str(e))
|
||||
|
||||
|
||||
@router.get("/verify/{card_id}")
|
||||
async def verify_card_ownership(
|
||||
card_id: int,
|
||||
unique_id: Optional[str] = None,
|
||||
current_user: AuthUser = Depends(require_user),
|
||||
db: AsyncSession = Depends(get_session)
|
||||
):
|
||||
"""
|
||||
Verify user owns a specific card
|
||||
|
||||
- **card_id**: Card type ID (0-15)
|
||||
- **unique_id**: Unique ID for unique cards
|
||||
"""
|
||||
sync_service = CardSyncService(db)
|
||||
owns_card = await sync_service.verify_card_ownership(
|
||||
current_user.did,
|
||||
card_id,
|
||||
unique_id
|
||||
)
|
||||
|
||||
return {
|
||||
"card_id": card_id,
|
||||
"unique_id": unique_id,
|
||||
"owned": owns_card
|
||||
}
|
||||
|
||||
|
||||
# Import Optional
|
||||
from typing import Optional
|
@@ -1 +0,0 @@
|
||||
# Services Package
|
@@ -1,288 +0,0 @@
|
||||
"""atproto integration service"""
|
||||
import json
|
||||
from typing import Optional, Dict, Any, List
|
||||
from datetime import datetime
|
||||
import httpx
|
||||
from atproto import Client, SessionString
|
||||
from atproto.exceptions import AtProtocolError
|
||||
|
||||
from app.core.config import settings
|
||||
from app.models.card import Card, CardRarity
|
||||
|
||||
|
||||
class AtprotoService:
|
||||
"""atproto integration service"""
|
||||
|
||||
def __init__(self):
|
||||
self.client = None
|
||||
self.session_string = None
|
||||
|
||||
async def login(self, identifier: str, password: str) -> SessionString:
|
||||
"""
|
||||
Login to atproto PDS
|
||||
|
||||
Args:
|
||||
identifier: Handle or DID
|
||||
password: App password
|
||||
|
||||
Returns:
|
||||
Session string for future requests
|
||||
"""
|
||||
self.client = Client()
|
||||
try:
|
||||
self.client.login(identifier, password)
|
||||
self.session_string = self.client.export_session_string()
|
||||
return self.session_string
|
||||
except AtProtocolError as e:
|
||||
raise Exception(f"Failed to login to atproto: {str(e)}")
|
||||
|
||||
def restore_session(self, session_string: str):
|
||||
"""Restore session from string"""
|
||||
self.client = Client()
|
||||
self.client.login_with_session_string(session_string)
|
||||
self.session_string = session_string
|
||||
|
||||
async def verify_did(self, did: str, handle: Optional[str] = None) -> bool:
|
||||
"""
|
||||
Verify DID is valid
|
||||
|
||||
Args:
|
||||
did: DID to verify
|
||||
handle: Optional handle to cross-check
|
||||
|
||||
Returns:
|
||||
True if valid
|
||||
"""
|
||||
try:
|
||||
# Use public API to resolve DID
|
||||
async with httpx.AsyncClient() as client:
|
||||
response = await client.get(
|
||||
f"https://plc.directory/{did}",
|
||||
follow_redirects=True
|
||||
)
|
||||
|
||||
if response.status_code != 200:
|
||||
return False
|
||||
|
||||
data = response.json()
|
||||
|
||||
# Verify handle if provided
|
||||
if handle and data.get("alsoKnownAs"):
|
||||
expected_handle = f"at://{handle}"
|
||||
return expected_handle in data["alsoKnownAs"]
|
||||
|
||||
return True
|
||||
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
async def get_profile(self, did: str) -> Optional[Dict[str, Any]]:
|
||||
"""Get user profile from atproto"""
|
||||
if not self.client:
|
||||
raise Exception("Not logged in")
|
||||
|
||||
try:
|
||||
profile = self.client.get_profile(did)
|
||||
return {
|
||||
"did": profile.did,
|
||||
"handle": profile.handle,
|
||||
"display_name": profile.display_name,
|
||||
"avatar": profile.avatar,
|
||||
"description": profile.description
|
||||
}
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
async def create_card_record(
|
||||
self,
|
||||
did: str,
|
||||
card: Card,
|
||||
collection: str = "ai.card.collection"
|
||||
) -> str:
|
||||
"""
|
||||
Create card record in user's PDS
|
||||
|
||||
Args:
|
||||
did: User's DID
|
||||
card: Card data
|
||||
collection: Collection name (lexicon)
|
||||
|
||||
Returns:
|
||||
Record URI
|
||||
"""
|
||||
if not self.client:
|
||||
raise Exception("Not logged in")
|
||||
|
||||
# Prepare card data for atproto
|
||||
record_data = {
|
||||
"$type": collection,
|
||||
"cardId": card.id,
|
||||
"cp": card.cp,
|
||||
"status": card.status.value,
|
||||
"skill": card.skill,
|
||||
"obtainedAt": card.obtained_at.isoformat(),
|
||||
"isUnique": card.is_unique,
|
||||
"uniqueId": card.unique_id,
|
||||
"createdAt": datetime.utcnow().isoformat()
|
||||
}
|
||||
|
||||
try:
|
||||
# Create record
|
||||
response = self.client.com.atproto.repo.create_record(
|
||||
repo=did,
|
||||
collection=collection,
|
||||
record=record_data
|
||||
)
|
||||
|
||||
return response.uri
|
||||
|
||||
except AtProtocolError as e:
|
||||
raise Exception(f"Failed to create card record: {str(e)}")
|
||||
|
||||
async def get_user_cards(
|
||||
self,
|
||||
did: str,
|
||||
collection: str = "ai.card.collection",
|
||||
limit: int = 100
|
||||
) -> List[Dict[str, Any]]:
|
||||
"""
|
||||
Get user's cards from PDS
|
||||
|
||||
Args:
|
||||
did: User's DID
|
||||
collection: Collection name
|
||||
limit: Maximum records to fetch
|
||||
|
||||
Returns:
|
||||
List of card records
|
||||
"""
|
||||
if not self.client:
|
||||
raise Exception("Not logged in")
|
||||
|
||||
try:
|
||||
# List records
|
||||
response = self.client.com.atproto.repo.list_records(
|
||||
repo=did,
|
||||
collection=collection,
|
||||
limit=limit
|
||||
)
|
||||
|
||||
cards = []
|
||||
for record in response.records:
|
||||
card_data = record.value
|
||||
card_data["uri"] = record.uri
|
||||
card_data["cid"] = record.cid
|
||||
cards.append(card_data)
|
||||
|
||||
return cards
|
||||
|
||||
except AtProtocolError:
|
||||
# Collection might not exist yet
|
||||
return []
|
||||
|
||||
async def delete_card_record(self, did: str, record_uri: str):
|
||||
"""Delete a card record from PDS"""
|
||||
if not self.client:
|
||||
raise Exception("Not logged in")
|
||||
|
||||
try:
|
||||
# Parse collection and rkey from URI
|
||||
# Format: at://did/collection/rkey
|
||||
parts = record_uri.split("/")
|
||||
if len(parts) < 5:
|
||||
raise ValueError("Invalid record URI")
|
||||
|
||||
collection = parts[3]
|
||||
rkey = parts[4]
|
||||
|
||||
self.client.com.atproto.repo.delete_record(
|
||||
repo=did,
|
||||
collection=collection,
|
||||
rkey=rkey
|
||||
)
|
||||
|
||||
except AtProtocolError as e:
|
||||
raise Exception(f"Failed to delete record: {str(e)}")
|
||||
|
||||
async def create_oauth_session(self, code: str, redirect_uri: str) -> Dict[str, Any]:
|
||||
"""
|
||||
Handle OAuth callback and create session
|
||||
|
||||
Args:
|
||||
code: Authorization code
|
||||
redirect_uri: Redirect URI used in authorization
|
||||
|
||||
Returns:
|
||||
Session data including DID and access token
|
||||
"""
|
||||
# TODO: Implement when atproto OAuth is available
|
||||
raise NotImplementedError("OAuth support is not yet available in atproto")
|
||||
|
||||
|
||||
class CardLexicon:
|
||||
"""Card collection lexicon definition"""
|
||||
|
||||
LEXICON_ID = "ai.card.collection"
|
||||
|
||||
@staticmethod
|
||||
def get_lexicon() -> Dict[str, Any]:
|
||||
"""Get lexicon definition for card collection"""
|
||||
return {
|
||||
"lexicon": 1,
|
||||
"id": CardLexicon.LEXICON_ID,
|
||||
"defs": {
|
||||
"main": {
|
||||
"type": "record",
|
||||
"description": "A collectible card",
|
||||
"key": "tid",
|
||||
"record": {
|
||||
"type": "object",
|
||||
"required": ["cardId", "cp", "status", "obtainedAt", "createdAt"],
|
||||
"properties": {
|
||||
"cardId": {
|
||||
"type": "integer",
|
||||
"description": "Card type ID (0-15)",
|
||||
"minimum": 0,
|
||||
"maximum": 15
|
||||
},
|
||||
"cp": {
|
||||
"type": "integer",
|
||||
"description": "Card power",
|
||||
"minimum": 1,
|
||||
"maximum": 999
|
||||
},
|
||||
"status": {
|
||||
"type": "string",
|
||||
"description": "Card rarity",
|
||||
"enum": ["normal", "rare", "super_rare", "kira", "unique"]
|
||||
},
|
||||
"skill": {
|
||||
"type": "string",
|
||||
"description": "Card skill",
|
||||
"maxLength": 1000
|
||||
},
|
||||
"obtainedAt": {
|
||||
"type": "string",
|
||||
"format": "datetime",
|
||||
"description": "When the card was obtained"
|
||||
},
|
||||
"isUnique": {
|
||||
"type": "boolean",
|
||||
"description": "Whether this is a unique card",
|
||||
"default": False
|
||||
},
|
||||
"uniqueId": {
|
||||
"type": "string",
|
||||
"description": "Global unique identifier",
|
||||
"format": "uuid"
|
||||
},
|
||||
"createdAt": {
|
||||
"type": "string",
|
||||
"format": "datetime",
|
||||
"description": "Record creation time"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,184 +0,0 @@
|
||||
"""Card synchronization service for atproto"""
|
||||
from typing import List, Dict, Any, Optional
|
||||
from datetime import datetime
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
# from app.services.atproto import AtprotoService, CardLexicon
|
||||
from app.repositories.card import CardRepository
|
||||
from app.repositories.user import UserRepository
|
||||
from app.models.card import Card as CardModel
|
||||
from app.db.models import UserCard
|
||||
from app.core.config import settings
|
||||
|
||||
|
||||
class CardSyncService:
|
||||
"""Service for syncing cards between database and atproto PDS"""
|
||||
|
||||
def __init__(self, session: AsyncSession, atproto_session: Optional[str] = None):
|
||||
self.db_session = session
|
||||
self.card_repo = CardRepository(session)
|
||||
self.user_repo = UserRepository(session)
|
||||
self.atproto_service = AtprotoService()
|
||||
|
||||
# Restore atproto session if provided
|
||||
if atproto_session:
|
||||
self.atproto_service.restore_session(atproto_session)
|
||||
|
||||
async def sync_card_to_pds(self, user_card: UserCard, user_did: str) -> Optional[str]:
|
||||
"""
|
||||
Sync a single card to user's PDS
|
||||
|
||||
Args:
|
||||
user_card: Card from database
|
||||
user_did: User's DID
|
||||
|
||||
Returns:
|
||||
Record URI if successful
|
||||
"""
|
||||
if not settings.atproto_handle or not settings.atproto_password:
|
||||
# Skip if atproto credentials not configured
|
||||
return None
|
||||
|
||||
try:
|
||||
# Login if not already
|
||||
if not self.atproto_service.client:
|
||||
await self.atproto_service.login(
|
||||
settings.atproto_handle,
|
||||
settings.atproto_password
|
||||
)
|
||||
|
||||
# Convert to API model
|
||||
card_model = CardModel(
|
||||
id=user_card.card_id,
|
||||
cp=user_card.cp,
|
||||
status=user_card.status,
|
||||
skill=user_card.skill,
|
||||
owner_did=user_did,
|
||||
obtained_at=user_card.obtained_at,
|
||||
is_unique=user_card.is_unique,
|
||||
unique_id=str(user_card.unique_id) if user_card.unique_id else None
|
||||
)
|
||||
|
||||
# Create record in PDS
|
||||
uri = await self.atproto_service.create_card_record(
|
||||
did=user_did,
|
||||
card=card_model,
|
||||
collection=CardLexicon.LEXICON_ID
|
||||
)
|
||||
|
||||
# Store URI in database for future reference
|
||||
# (You might want to add a field to UserCard model for this)
|
||||
|
||||
return uri
|
||||
|
||||
except Exception as e:
|
||||
print(f"Failed to sync card to PDS: {e}")
|
||||
return None
|
||||
|
||||
async def sync_all_user_cards(self, user_id: int, user_did: str) -> int:
|
||||
"""
|
||||
Sync all user's cards to PDS
|
||||
|
||||
Args:
|
||||
user_id: Database user ID
|
||||
user_did: User's DID
|
||||
|
||||
Returns:
|
||||
Number of cards synced
|
||||
"""
|
||||
# Get all user cards from database
|
||||
user_cards = await self.card_repo.get_user_cards(user_id)
|
||||
|
||||
synced_count = 0
|
||||
for card in user_cards:
|
||||
uri = await self.sync_card_to_pds(card, user_did)
|
||||
if uri:
|
||||
synced_count += 1
|
||||
|
||||
return synced_count
|
||||
|
||||
async def import_cards_from_pds(self, user_did: str) -> int:
|
||||
"""
|
||||
Import cards from user's PDS to database
|
||||
|
||||
Args:
|
||||
user_did: User's DID
|
||||
|
||||
Returns:
|
||||
Number of cards imported
|
||||
"""
|
||||
if not self.atproto_service.client:
|
||||
return 0
|
||||
|
||||
# Get user from database
|
||||
user = await self.user_repo.get_by_did(user_did)
|
||||
if not user:
|
||||
return 0
|
||||
|
||||
# Get cards from PDS
|
||||
pds_cards = await self.atproto_service.get_user_cards(
|
||||
did=user_did,
|
||||
collection=CardLexicon.LEXICON_ID
|
||||
)
|
||||
|
||||
imported_count = 0
|
||||
for pds_card in pds_cards:
|
||||
# Check if card already exists
|
||||
existing_count = await self.card_repo.count_user_cards(
|
||||
user.id,
|
||||
pds_card.get("cardId")
|
||||
)
|
||||
|
||||
if existing_count == 0:
|
||||
# Import card
|
||||
await self.card_repo.create_user_card(
|
||||
user_id=user.id,
|
||||
card_id=pds_card.get("cardId"),
|
||||
cp=pds_card.get("cp"),
|
||||
status=pds_card.get("status"),
|
||||
skill=pds_card.get("skill"),
|
||||
is_unique=pds_card.get("isUnique", False)
|
||||
)
|
||||
imported_count += 1
|
||||
|
||||
await self.db_session.commit()
|
||||
return imported_count
|
||||
|
||||
async def verify_card_ownership(
|
||||
self,
|
||||
user_did: str,
|
||||
card_id: int,
|
||||
unique_id: Optional[str] = None
|
||||
) -> bool:
|
||||
"""
|
||||
Verify user owns a card by checking both database and PDS
|
||||
|
||||
Args:
|
||||
user_did: User's DID
|
||||
card_id: Card type ID
|
||||
unique_id: Unique ID for unique cards
|
||||
|
||||
Returns:
|
||||
True if user owns the card
|
||||
"""
|
||||
# Check database first
|
||||
user = await self.user_repo.get_by_did(user_did)
|
||||
if user:
|
||||
user_cards = await self.card_repo.get_user_cards(user.id)
|
||||
for card in user_cards:
|
||||
if card.card_id == card_id:
|
||||
if not unique_id or str(card.unique_id) == unique_id:
|
||||
return True
|
||||
|
||||
# Check PDS if configured
|
||||
if self.atproto_service.client:
|
||||
try:
|
||||
pds_cards = await self.atproto_service.get_user_cards(user_did)
|
||||
for card in pds_cards:
|
||||
if card.get("cardId") == card_id:
|
||||
if not unique_id or card.get("uniqueId") == unique_id:
|
||||
return True
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
return False
|
@@ -1,181 +0,0 @@
|
||||
"""ガチャシステムのロジック"""
|
||||
import random
|
||||
import uuid
|
||||
from datetime import datetime
|
||||
from typing import Optional, Tuple
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.core.config import settings
|
||||
from app.models.card import Card, CardRarity
|
||||
from app.repositories.user import UserRepository
|
||||
from app.repositories.card import CardRepository, UniqueCardRepository
|
||||
from app.db.models import DrawHistory
|
||||
|
||||
|
||||
class GachaService:
|
||||
"""ガチャシステムのサービスクラス"""
|
||||
|
||||
# カード基本情報(ai.jsonから)
|
||||
CARD_INFO = {
|
||||
0: {"name": "ai", "base_cp_range": (10, 100)},
|
||||
1: {"name": "dream", "base_cp_range": (20, 120)},
|
||||
2: {"name": "radiance", "base_cp_range": (30, 130)},
|
||||
3: {"name": "neutron", "base_cp_range": (40, 140)},
|
||||
4: {"name": "sun", "base_cp_range": (50, 150)},
|
||||
5: {"name": "night", "base_cp_range": (25, 125)},
|
||||
6: {"name": "snow", "base_cp_range": (15, 115)},
|
||||
7: {"name": "thunder", "base_cp_range": (60, 160)},
|
||||
8: {"name": "ultimate", "base_cp_range": (80, 180)},
|
||||
9: {"name": "sword", "base_cp_range": (70, 170)},
|
||||
10: {"name": "destruction", "base_cp_range": (90, 190)},
|
||||
11: {"name": "earth", "base_cp_range": (35, 135)},
|
||||
12: {"name": "galaxy", "base_cp_range": (65, 165)},
|
||||
13: {"name": "create", "base_cp_range": (75, 175)},
|
||||
14: {"name": "supernova", "base_cp_range": (100, 200)},
|
||||
15: {"name": "world", "base_cp_range": (85, 185)},
|
||||
}
|
||||
|
||||
def __init__(self, session: AsyncSession):
|
||||
self.session = session
|
||||
self.user_repo = UserRepository(session)
|
||||
self.card_repo = CardRepository(session)
|
||||
self.unique_repo = UniqueCardRepository(session)
|
||||
|
||||
async def draw_card(self, user_did: str, is_paid: bool = False) -> Tuple[Card, bool]:
|
||||
"""
|
||||
カードを抽選する
|
||||
|
||||
Args:
|
||||
user_did: ユーザーのDID
|
||||
is_paid: 課金ガチャかどうか
|
||||
|
||||
Returns:
|
||||
(Card, is_unique): 抽選されたカードとuniqueかどうか
|
||||
"""
|
||||
# Get or create user
|
||||
user = await self.user_repo.get_or_create(user_did)
|
||||
# レアリティ抽選
|
||||
rarity = self._determine_rarity(is_paid)
|
||||
|
||||
# カード種類を選択
|
||||
card_id = self._select_card_id(rarity)
|
||||
|
||||
# CPを決定
|
||||
cp = self._calculate_cp(card_id, rarity)
|
||||
|
||||
# uniqueカードチェック
|
||||
is_unique = False
|
||||
|
||||
if rarity == CardRarity.UNIQUE:
|
||||
# uniqueカードの場合、利用可能かチェック
|
||||
is_available = await self.unique_repo.is_card_available(card_id)
|
||||
if not is_available:
|
||||
# 利用不可の場合はキラカードに変更
|
||||
rarity = CardRarity.KIRA
|
||||
else:
|
||||
is_unique = True
|
||||
|
||||
# データベースにカードを保存
|
||||
user_card = await self.card_repo.create_user_card(
|
||||
user_id=user.id,
|
||||
card_id=card_id,
|
||||
cp=cp,
|
||||
status=rarity,
|
||||
skill=self._get_skill_for_card(card_id, rarity),
|
||||
is_unique=is_unique
|
||||
)
|
||||
|
||||
# 抽選履歴を保存
|
||||
draw_history = DrawHistory(
|
||||
user_id=user.id,
|
||||
card_id=card_id,
|
||||
status=rarity,
|
||||
cp=cp,
|
||||
is_paid=is_paid
|
||||
)
|
||||
self.session.add(draw_history)
|
||||
|
||||
# API用のCardモデルに変換
|
||||
card = Card(
|
||||
id=card_id,
|
||||
cp=cp,
|
||||
status=rarity,
|
||||
skill=user_card.skill,
|
||||
owner_did=user_did,
|
||||
obtained_at=user_card.obtained_at,
|
||||
is_unique=is_unique,
|
||||
unique_id=str(user_card.unique_id) if user_card.unique_id else None
|
||||
)
|
||||
|
||||
# atproto PDSに同期(非同期で実行)
|
||||
try:
|
||||
from app.services.card_sync import CardSyncService
|
||||
sync_service = CardSyncService(self.session)
|
||||
await sync_service.sync_card_to_pds(user_card, user_did)
|
||||
except Exception:
|
||||
# 同期失敗してもガチャは成功とする
|
||||
pass
|
||||
|
||||
return card, is_unique
|
||||
|
||||
def _determine_rarity(self, is_paid: bool) -> CardRarity:
|
||||
"""レアリティを抽選する"""
|
||||
rand = random.random() * 100
|
||||
|
||||
if is_paid:
|
||||
# 課金ガチャは確率アップ
|
||||
if rand < settings.prob_unique * 2: # 0.0002%
|
||||
return CardRarity.UNIQUE
|
||||
elif rand < settings.prob_kira * 2: # 0.2%
|
||||
return CardRarity.KIRA
|
||||
elif rand < 0.5: # 0.5%
|
||||
return CardRarity.SUPER_RARE
|
||||
elif rand < 5: # 5%
|
||||
return CardRarity.RARE
|
||||
else:
|
||||
# 通常ガチャ
|
||||
if rand < settings.prob_unique:
|
||||
return CardRarity.UNIQUE
|
||||
elif rand < settings.prob_kira:
|
||||
return CardRarity.KIRA
|
||||
elif rand < settings.prob_super_rare:
|
||||
return CardRarity.SUPER_RARE
|
||||
elif rand < settings.prob_rare:
|
||||
return CardRarity.RARE
|
||||
|
||||
return CardRarity.NORMAL
|
||||
|
||||
def _select_card_id(self, rarity: CardRarity) -> int:
|
||||
"""レアリティに応じてカードIDを選択"""
|
||||
if rarity in [CardRarity.UNIQUE, CardRarity.KIRA]:
|
||||
# レアカードは特定のIDに偏らせる
|
||||
weights = [1, 1, 2, 2, 3, 1, 1, 3, 5, 4, 5, 2, 3, 4, 6, 5]
|
||||
else:
|
||||
# 通常は均等
|
||||
weights = [1] * 16
|
||||
|
||||
return random.choices(range(16), weights=weights)[0]
|
||||
|
||||
def _calculate_cp(self, card_id: int, rarity: CardRarity) -> int:
|
||||
"""カードのCPを計算"""
|
||||
base_range = self.CARD_INFO[card_id]["base_cp_range"]
|
||||
base_cp = random.randint(*base_range)
|
||||
|
||||
# レアリティボーナス
|
||||
multiplier = {
|
||||
CardRarity.NORMAL: 1.0,
|
||||
CardRarity.RARE: 1.5,
|
||||
CardRarity.SUPER_RARE: 2.0,
|
||||
CardRarity.KIRA: 3.0,
|
||||
CardRarity.UNIQUE: 5.0,
|
||||
}[rarity]
|
||||
|
||||
return int(base_cp * multiplier)
|
||||
|
||||
def _get_skill_for_card(self, card_id: int, rarity: CardRarity) -> Optional[str]:
|
||||
"""カードのスキルを取得"""
|
||||
if rarity in [CardRarity.KIRA, CardRarity.UNIQUE]:
|
||||
# TODO: スキル情報を返す
|
||||
return f"skill_{card_id}_{rarity.value}"
|
||||
return None
|
||||
|
@@ -1 +0,0 @@
|
||||
# Test Package
|
@@ -1,57 +0,0 @@
|
||||
"""ガチャシステムのテスト"""
|
||||
import pytest
|
||||
from app.services.gacha import GachaService
|
||||
from app.models.card import CardRarity
|
||||
|
||||
|
||||
class TestGachaService:
|
||||
"""GachaServiceのテストクラス"""
|
||||
|
||||
def test_determine_rarity_normal(self):
|
||||
"""通常ガチャのレアリティ判定テスト"""
|
||||
rarities = []
|
||||
for _ in range(1000):
|
||||
rarity = GachaService._determine_rarity(is_paid=False)
|
||||
rarities.append(rarity)
|
||||
|
||||
# 大部分がNORMALであることを確認
|
||||
normal_count = rarities.count(CardRarity.NORMAL)
|
||||
assert normal_count > 900
|
||||
|
||||
def test_determine_rarity_paid(self):
|
||||
"""課金ガチャのレアリティ判定テスト"""
|
||||
rarities = []
|
||||
for _ in range(1000):
|
||||
rarity = GachaService._determine_rarity(is_paid=True)
|
||||
rarities.append(rarity)
|
||||
|
||||
# 課金ガチャの方がレアが出やすいことを確認
|
||||
rare_count = sum(1 for r in rarities if r != CardRarity.NORMAL)
|
||||
assert rare_count > 50 # 5%以上
|
||||
|
||||
def test_card_id_selection(self):
|
||||
"""カードID選択のテスト"""
|
||||
for rarity in CardRarity:
|
||||
card_id = GachaService._select_card_id(rarity)
|
||||
assert 0 <= card_id <= 15
|
||||
|
||||
def test_cp_calculation(self):
|
||||
"""CP計算のテスト"""
|
||||
# 通常カード
|
||||
cp_normal = GachaService._calculate_cp(0, CardRarity.NORMAL)
|
||||
assert 10 <= cp_normal <= 100
|
||||
|
||||
# uniqueカード(5倍)
|
||||
cp_unique = GachaService._calculate_cp(0, CardRarity.UNIQUE)
|
||||
assert 50 <= cp_unique <= 500
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_draw_card(self):
|
||||
"""カード抽選の統合テスト"""
|
||||
user_did = "did:plc:test123"
|
||||
card, is_unique = await GachaService.draw_card(user_did, is_paid=False)
|
||||
|
||||
assert card.owner_did == user_did
|
||||
assert 0 <= card.id <= 15
|
||||
assert card.cp > 0
|
||||
assert card.status in CardRarity
|
@@ -1,76 +0,0 @@
|
||||
"""Initialize database with master data"""
|
||||
import asyncio
|
||||
from sqlalchemy import text, select, func
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from app.db.base import engine, Base, async_session
|
||||
from app.db.models import CardMaster
|
||||
from app.core.config import settings
|
||||
|
||||
# Card master data from ai.json
|
||||
CARD_MASTER_DATA = [
|
||||
{"id": 0, "name": "ai", "base_cp_min": 10, "base_cp_max": 100, "color": "#fff700", "description": "世界の最小単位"},
|
||||
{"id": 1, "name": "dream", "base_cp_min": 20, "base_cp_max": 120, "color": "#b19cd9", "description": "意識が物質を作る"},
|
||||
{"id": 2, "name": "radiance", "base_cp_min": 30, "base_cp_max": 130, "color": "#ffd700", "description": "存在は光に向かう"},
|
||||
{"id": 3, "name": "neutron", "base_cp_min": 40, "base_cp_max": 140, "color": "#cacfd2", "description": "中性子"},
|
||||
{"id": 4, "name": "sun", "base_cp_min": 50, "base_cp_max": 150, "color": "#ff6b35", "description": "太陽"},
|
||||
{"id": 5, "name": "night", "base_cp_min": 25, "base_cp_max": 125, "color": "#1a1a2e", "description": "夜空"},
|
||||
{"id": 6, "name": "snow", "base_cp_min": 15, "base_cp_max": 115, "color": "#e3f2fd", "description": "雪"},
|
||||
{"id": 7, "name": "thunder", "base_cp_min": 60, "base_cp_max": 160, "color": "#ffd93d", "description": "雷"},
|
||||
{"id": 8, "name": "ultimate", "base_cp_min": 80, "base_cp_max": 180, "color": "#6c5ce7", "description": "超究"},
|
||||
{"id": 9, "name": "sword", "base_cp_min": 70, "base_cp_max": 170, "color": "#a8e6cf", "description": "剣"},
|
||||
{"id": 10, "name": "destruction", "base_cp_min": 90, "base_cp_max": 190, "color": "#ff4757", "description": "破壊"},
|
||||
{"id": 11, "name": "earth", "base_cp_min": 35, "base_cp_max": 135, "color": "#4834d4", "description": "地球"},
|
||||
{"id": 12, "name": "galaxy", "base_cp_min": 65, "base_cp_max": 165, "color": "#9c88ff", "description": "天の川"},
|
||||
{"id": 13, "name": "create", "base_cp_min": 75, "base_cp_max": 175, "color": "#00d2d3", "description": "創造"},
|
||||
{"id": 14, "name": "supernova", "base_cp_min": 100, "base_cp_max": 200, "color": "#ff9ff3", "description": "超新星"},
|
||||
{"id": 15, "name": "world", "base_cp_min": 85, "base_cp_max": 185, "color": "#54a0ff", "description": "存在と世界は同じもの"},
|
||||
]
|
||||
|
||||
|
||||
async def init_db():
|
||||
"""Initialize database tables and master data"""
|
||||
print("Creating database tables...")
|
||||
async with engine.begin() as conn:
|
||||
await conn.run_sync(Base.metadata.create_all)
|
||||
|
||||
print("Inserting master data...")
|
||||
async with async_session() as session:
|
||||
# Check if master data already exists
|
||||
try:
|
||||
result = await session.execute(
|
||||
select(func.count()).select_from(CardMaster)
|
||||
)
|
||||
count = result.scalar()
|
||||
except Exception:
|
||||
# Table might not exist yet
|
||||
count = 0
|
||||
|
||||
if count == 0:
|
||||
# Insert card master data
|
||||
for card_data in CARD_MASTER_DATA:
|
||||
card = CardMaster(**card_data)
|
||||
session.add(card)
|
||||
|
||||
await session.commit()
|
||||
print(f"Inserted {len(CARD_MASTER_DATA)} card master records")
|
||||
else:
|
||||
print("Master data already exists, skipping...")
|
||||
|
||||
print("Database initialization complete!")
|
||||
|
||||
|
||||
async def drop_db():
|
||||
"""Drop all database tables"""
|
||||
print("Dropping all database tables...")
|
||||
async with engine.begin() as conn:
|
||||
await conn.run_sync(Base.metadata.drop_all)
|
||||
print("All tables dropped!")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import sys
|
||||
|
||||
if len(sys.argv) > 1 and sys.argv[1] == "drop":
|
||||
asyncio.run(drop_db())
|
||||
else:
|
||||
asyncio.run(init_db())
|
@@ -1,20 +0,0 @@
|
||||
fastapi>=0.104.1
|
||||
uvicorn[standard]>=0.24.0
|
||||
pydantic>=2.7.0,<3.0.0
|
||||
pydantic-settings>=2.1.0
|
||||
python-multipart==0.0.6
|
||||
httpx>=0.25.0,<0.29.0
|
||||
python-jose[cryptography]==3.3.0
|
||||
passlib[bcrypt]==1.7.4
|
||||
sqlalchemy>=2.0.23
|
||||
greenlet>=3.0.0
|
||||
alembic>=1.12.1
|
||||
# asyncpg==0.29.0 # Disabled: requires compilation
|
||||
# psycopg2-binary==2.9.9 # Disabled: requires compilation
|
||||
aiosqlite>=0.19.0
|
||||
python-dotenv==1.0.0
|
||||
pytest==7.4.3
|
||||
pytest-asyncio==0.21.1
|
||||
atproto>=0.0.55
|
||||
# supabase>=2.3.0 # Temporarily disabled due to httpx version conflict
|
||||
fastapi-mcp==0.1.0
|
253
claude.md
253
claude.md
@@ -1,168 +1,143 @@
|
||||
# ai.card 開発ガイド (Claude Code用)
|
||||
# ai.card プロジェクト固有情報
|
||||
|
||||
## プロジェクト概要
|
||||
**ai.card** - atproto基盤のカードゲームシステム。iOS/Web/APIで構成され、ユーザーデータ主権を実現。
|
||||
- **名前**: ai.card
|
||||
- **パッケージ**: aicard
|
||||
- **タイプ**: atproto基盤カードゲーム
|
||||
- **役割**: ユーザーデータ主権カードゲームシステム
|
||||
|
||||
## 現在の状態 (2025/01/06)
|
||||
- ✅ MCP Server実装完了
|
||||
- ✅ SQLiteデータベース稼働中
|
||||
- ✅ 基本的なガチャ・カード管理機能
|
||||
- 🔧 atproto連携は一時無効化
|
||||
- 📱 iOS/Web実装待ち
|
||||
## 実装状況
|
||||
|
||||
## 開発環境セットアップ
|
||||
### 現在の状況
|
||||
- **ai.bot統合**: ai.botの機能として実装済み
|
||||
- **カード取得**: atproto accountでmentionすると1日1回カード取得可能
|
||||
- **データ管理**: ai.api (MCP server) でユーザー管理
|
||||
|
||||
### 必要なもの
|
||||
- Python 3.13
|
||||
- Node.js (Web開発用)
|
||||
- Docker (PostgreSQL用、オプション)
|
||||
- Xcode (iOS開発用)
|
||||
### 独立MCPサーバー(ai.gpt連携)
|
||||
- **場所**: `/Users/syui/ai/gpt/card/`
|
||||
- **サーバー**: FastAPI + fastapi_mcp (port 8000)
|
||||
- **統合**: ai.gptサーバーからHTTP連携
|
||||
|
||||
### 初回セットアップ
|
||||
```bash
|
||||
# 1. プロジェクトディレクトリ
|
||||
cd /Users/syui/ai/gpt/card
|
||||
## アーキテクチャ構成
|
||||
|
||||
# 2. 仮想環境構築
|
||||
./setup_venv.sh
|
||||
### 技術スタック
|
||||
- **Backend**: FastAPI + MCP
|
||||
- **Frontend**: React Web UI + SwiftUI iOS app
|
||||
- **Data**: atproto collection record(ユーザー所有)
|
||||
- **Auth**: OAuth 2.1 scope(実装待ち)
|
||||
|
||||
# 3. データベース初期化
|
||||
cd api
|
||||
~/.config/syui/ai/card/venv/bin/python init_db.py
|
||||
|
||||
# 4. サーバー起動
|
||||
cd ..
|
||||
./start_server.sh
|
||||
### データフロー
|
||||
```
|
||||
ユーザー → ai.bot mention → カード生成 → atproto collection → ユーザー所有
|
||||
↑ ↓
|
||||
← iOS app表示 ← ai.card API ←
|
||||
```
|
||||
|
||||
## 開発時の作業分担提案
|
||||
## 移行計画
|
||||
|
||||
### ai.gptプロジェクトで起動 (MCP/バックエンド作業)
|
||||
**適している作業:**
|
||||
- MCPサーバー機能の追加・修正
|
||||
- データベーススキーマ変更
|
||||
- API エンドポイント追加
|
||||
- バックエンドロジック実装
|
||||
### Phase 1: 独立化
|
||||
- **iOS移植**: Claude担当予定
|
||||
- **Web UI**: React実装
|
||||
- **API独立**: ai.botからの分離
|
||||
|
||||
**起動方法:**
|
||||
```bash
|
||||
cd /Users/syui/ai/gpt
|
||||
# Claude Codeをここで起動
|
||||
# ai.card/api/ を編集対象にする
|
||||
### Phase 2: データ主権実装
|
||||
- **atproto collection**: カードデータをユーザー所有に
|
||||
- **OAuth 2.1**: 不正防止機能実装
|
||||
- **画像ファイル**: Cloudflare Pages最適化
|
||||
|
||||
### Phase 3: ゲーム機能拡張
|
||||
- **ガチャシステム**: 確率・レアリティ管理
|
||||
- **トレード機能**: ユーザー間カード交換
|
||||
- **デッキ構築**: カードゲーム戦略要素
|
||||
|
||||
## yui system適用
|
||||
|
||||
### 唯一性担保
|
||||
- **カード効果**: アカウント固有の効果設定
|
||||
- **改ざん防止**: ハッシュ・署名による保証
|
||||
- **ゲームバランス**: 唯一性による公平性維持
|
||||
|
||||
### ai.verse連携
|
||||
- **ゲーム内アイテム**: ai.verseでのカード利用
|
||||
- **固有スキル**: カードとキャラクターの連動
|
||||
- **現実反映**: カード取得がゲーム内能力に影響
|
||||
|
||||
## ディレクトリ構成
|
||||
|
||||
```
|
||||
/Users/syui/ai/gpt/card/
|
||||
├── api/ # FastAPI + MCP server
|
||||
├── web/ # React Web UI
|
||||
├── ios/ # SwiftUI iOS app
|
||||
└── docs/ # 開発ドキュメント
|
||||
```
|
||||
|
||||
### ai.cardプロジェクトで起動 (フロントエンド作業)
|
||||
**適している作業:**
|
||||
- iOS アプリ開発 (Swift/SwiftUI)
|
||||
- Web フロントエンド開発 (React/TypeScript)
|
||||
- UI/UX デザイン実装
|
||||
- クライアント側ロジック
|
||||
## MCPツール(ai.gpt連携)
|
||||
|
||||
**起動方法:**
|
||||
```bash
|
||||
cd /Users/syui/ai/gpt/card
|
||||
# Claude Codeをここで起動
|
||||
# ios/ または web/ を編集対象にする
|
||||
```
|
||||
### カード管理
|
||||
- **card_get_user_cards**: ユーザーカード取得
|
||||
- **card_draw_card**: ガチャ実行
|
||||
- **card_analyze_collection**: コレクション分析
|
||||
- **card_check_daily_limit**: 日次制限確認
|
||||
- **card_get_card_stats**: カード統計情報
|
||||
- **card_manage_deck**: デッキ管理
|
||||
|
||||
## ディレクトリ構造
|
||||
```
|
||||
ai.card/
|
||||
├── api/ # バックエンド (Python/FastAPI)
|
||||
│ ├── app/
|
||||
│ │ ├── main.py # エントリポイント
|
||||
│ │ ├── mcp_server.py # MCPサーバー実装
|
||||
│ │ ├── models/ # データモデル
|
||||
│ │ ├── routes/ # APIルート
|
||||
│ │ └── services/ # ビジネスロジック
|
||||
│ └── requirements.txt
|
||||
├── ios/ # iOSアプリ (Swift)
|
||||
│ └── AiCard/
|
||||
├── web/ # Webフロントエンド (React)
|
||||
│ └── src/
|
||||
├── docs/ # ドキュメント
|
||||
├── setup_venv.sh # 環境構築スクリプト
|
||||
└── start_server.sh # サーバー起動スクリプト
|
||||
```
|
||||
## 開発状況
|
||||
|
||||
## 主要な技術スタック
|
||||
### 完成済み機能
|
||||
- ✅ **基本カード生成**: ai.bot統合での1日1回取得
|
||||
- ✅ **atproto連携**: mention機能
|
||||
- ✅ **MCP統合**: ai.gptからの操作
|
||||
|
||||
### バックエンド
|
||||
- **言語**: Python 3.13
|
||||
- **フレームワーク**: FastAPI + fastapi-mcp
|
||||
- **データベース**: SQLite (開発) / PostgreSQL (本番予定)
|
||||
- **ORM**: SQLAlchemy 2.0
|
||||
### 開発中機能
|
||||
- 🔧 **iOS app**: SwiftUI実装
|
||||
- 🔧 **Web UI**: React実装
|
||||
- 🔧 **独立API**: FastAPI server
|
||||
|
||||
### フロントエンド
|
||||
- **iOS**: Swift 5.9 + SwiftUI
|
||||
- **Web**: React + TypeScript + Vite
|
||||
- **スタイリング**: CSS Modules
|
||||
### 将来機能
|
||||
- 📋 **OAuth 2.1**: 不正防止強化
|
||||
- 📋 **画像最適化**: Cloudflare Pages
|
||||
- 📋 **ゲーム拡張**: トレード・デッキ戦略
|
||||
|
||||
## 現在の課題と制約
|
||||
## ai.botからの移行詳細
|
||||
|
||||
### 依存関係の問題
|
||||
1. **atproto**: `SessionString` APIが変更されたため一部機能無効化
|
||||
2. **supabase**: httpxバージョン競合で無効化
|
||||
3. **PostgreSQL**: ネイティブ拡張のコンパイル問題でSQLite使用中
|
||||
### 現在のai.bot実装
|
||||
- **Rust製**: seahorse CLI framework
|
||||
- **atproto連携**: mention機能でカード配布
|
||||
- **日次制限**: 1アカウント1日1回取得
|
||||
- **自動生成**: AI絵画(Leonardo.AI + Stable Diffusion)
|
||||
|
||||
### 回避策
|
||||
- atproto機能はモック実装で代替
|
||||
- データベースはSQLiteで開発継続
|
||||
- 本番環境ではDockerでPostgreSQL使用予定
|
||||
### 独立化の理由
|
||||
- **iOS展開**: モバイルアプリでの独立した体験
|
||||
- **ゲーム拡張**: デッキ構築・バトル機能の追加
|
||||
- **データ主権**: ユーザーによる完全なデータ所有
|
||||
- **スケーラビリティ**: サーバー負荷分散
|
||||
|
||||
## API仕様
|
||||
## 技術的課題と解決策
|
||||
|
||||
### MCP Tools (9個)
|
||||
1. **get_user_cards(did: str)** - ユーザーのカード一覧取得
|
||||
2. **draw_card(did: str, is_paid: bool)** - ガチャでカード取得
|
||||
3. **get_card_details(card_id: int)** - カード詳細情報
|
||||
4. **analyze_card_collection(did: str)** - コレクション分析
|
||||
5. **get_unique_registry()** - ユニークカード登録状況
|
||||
6. **sync_cards_atproto(did: str)** - atproto同期(無効化中)
|
||||
7. **get_gacha_stats()** - ガチャ統計情報
|
||||
### データ改ざん防止
|
||||
- **短期**: MCP serverによる検証
|
||||
- **中期**: OAuth 2.1 scope実装待ち
|
||||
- **長期**: ブロックチェーン的整合性チェック
|
||||
|
||||
### REST API
|
||||
- `/api/v1/cards/*` - カード管理
|
||||
- `/api/v1/auth/*` - 認証(モック実装)
|
||||
- `/api/v1/sync/*` - 同期機能
|
||||
### スケーラビリティ
|
||||
- **画像配信**: Cloudflare Pages活用
|
||||
- **API負荷**: FastAPIによる高速処理
|
||||
- **データ保存**: atproto分散ストレージ
|
||||
|
||||
## 今後の開発予定
|
||||
### ユーザー体験
|
||||
- **直感的UI**: iOS/Webでの統一UX
|
||||
- **リアルタイム更新**: WebSocketでの即座反映
|
||||
- **オフライン対応**: ローカルキャッシュ機能
|
||||
|
||||
### Phase 1: 基盤強化
|
||||
- [ ] PostgreSQL移行(Docker利用)
|
||||
- [ ] atproto最新版対応
|
||||
- [ ] テストコード追加
|
||||
## ai.game連携構想
|
||||
|
||||
### Phase 2: クライアント実装
|
||||
- [ ] iOS アプリ基本機能
|
||||
- [ ] Web フロントエンド
|
||||
- [ ] リアルタイムガチャ演出
|
||||
### Play-to-Work統合
|
||||
- **カードゲームプレイ → 業務成果変換**: ai.gameデバイスでの労働ゲーム化
|
||||
- **デッキ構築戦略 → 企業戦略思考**: カード組み合わせが戦略思考を鍛練
|
||||
- **トレード交渉 → ビジネススキル**: 他プレイヤーとの交渉が実務能力向上
|
||||
|
||||
### Phase 3: 本格運用
|
||||
- [ ] Cloudflare デプロイ
|
||||
- [ ] ユーザーデータ主権実装
|
||||
- [ ] ai.verse連携
|
||||
|
||||
## 注意事項
|
||||
- サーバーは`--reload`モードで起動中(ファイル変更で自動再起動)
|
||||
- データベースは `~/.config/syui/ai/card/aicard.db`
|
||||
- 仮想環境は `~/.config/syui/ai/card/venv/`
|
||||
- エラーログはターミナルに出力される
|
||||
|
||||
## デバッグ用コマンド
|
||||
```bash
|
||||
# データベース確認
|
||||
sqlite3 ~/.config/syui/ai/card/aicard.db ".tables"
|
||||
|
||||
# API動作確認
|
||||
curl http://localhost:8000/health
|
||||
curl "http://localhost:8000/get_gacha_stats"
|
||||
|
||||
# ログ確認
|
||||
tail -f /var/log/aicard.log # 未実装
|
||||
```
|
||||
|
||||
## 参考リンク
|
||||
- [AI エコシステム統合設計書](/Users/syui/ai/gpt/CLAUDE.md)
|
||||
- [MCP統合作業報告](./docs/MCP_INTEGRATION_SUMMARY.md)
|
||||
- [API仕様書](http://localhost:8000/docs) ※サーバー起動時のみ
|
||||
### メタバース展開
|
||||
- **ai.verse統合**: 3D世界でのカードバトル
|
||||
- **アバター連動**: 所有カードがキャラクター能力に影響
|
||||
- **配信コンテンツ**: カードゲームが配信可能なエンターテイメント
|
@@ -1,23 +0,0 @@
|
||||
# 開発用: PostgreSQLのみ起動
|
||||
version: '3.8'
|
||||
|
||||
services:
|
||||
postgres:
|
||||
image: postgres:16-alpine
|
||||
container_name: aicard_postgres_dev
|
||||
environment:
|
||||
POSTGRES_USER: postgres
|
||||
POSTGRES_PASSWORD: postgres
|
||||
POSTGRES_DB: aicard
|
||||
ports:
|
||||
- "5432:5432"
|
||||
volumes:
|
||||
- postgres_dev_data:/var/lib/postgresql/data
|
||||
healthcheck:
|
||||
test: ["CMD-SHELL", "pg_isready -U postgres"]
|
||||
interval: 10s
|
||||
timeout: 5s
|
||||
retries: 5
|
||||
|
||||
volumes:
|
||||
postgres_dev_data:
|
@@ -1,54 +0,0 @@
|
||||
version: '3.8'
|
||||
|
||||
# Production configuration with Cloudflare Tunnel
|
||||
services:
|
||||
api:
|
||||
build:
|
||||
context: ./api
|
||||
dockerfile: Dockerfile
|
||||
restart: unless-stopped
|
||||
environment:
|
||||
DATABASE_URL: ${DATABASE_URL}
|
||||
DATABASE_URL_SUPABASE: ${DATABASE_URL_SUPABASE}
|
||||
USE_SUPABASE: ${USE_SUPABASE:-false}
|
||||
PYTHONPATH: /app
|
||||
networks:
|
||||
- internal
|
||||
healthcheck:
|
||||
test: ["CMD", "curl", "-f", "http://localhost:8000/health"]
|
||||
interval: 30s
|
||||
timeout: 10s
|
||||
retries: 3
|
||||
|
||||
web:
|
||||
build:
|
||||
context: ./web
|
||||
dockerfile: Dockerfile
|
||||
restart: unless-stopped
|
||||
networks:
|
||||
- internal
|
||||
environment:
|
||||
- VITE_API_URL=http://api:8000
|
||||
healthcheck:
|
||||
test: ["CMD", "curl", "-f", "http://localhost:3000"]
|
||||
interval: 30s
|
||||
timeout: 10s
|
||||
retries: 3
|
||||
|
||||
cloudflared:
|
||||
image: cloudflare/cloudflared:latest
|
||||
restart: unless-stopped
|
||||
command: tunnel --no-autoupdate run
|
||||
environment:
|
||||
- TUNNEL_TOKEN=${CLOUDFLARE_TUNNEL_TOKEN}
|
||||
networks:
|
||||
- internal
|
||||
depends_on:
|
||||
api:
|
||||
condition: service_healthy
|
||||
web:
|
||||
condition: service_healthy
|
||||
|
||||
networks:
|
||||
internal:
|
||||
driver: bridge
|
73
ios/AiCard/AiCard/Models/AIModels.swift
Normal file
73
ios/AiCard/AiCard/Models/AIModels.swift
Normal file
@@ -0,0 +1,73 @@
|
||||
import Foundation
|
||||
|
||||
// MARK: - AI分析関連モデル
|
||||
|
||||
struct CollectionAnalysis: Codable {
|
||||
let totalCards: Int
|
||||
let uniqueCards: Int
|
||||
let rarityDistribution: [String: Int]
|
||||
let collectionScore: Double
|
||||
let recommendations: [String]
|
||||
|
||||
enum CodingKeys: String, CodingKey {
|
||||
case totalCards = "total_cards"
|
||||
case uniqueCards = "unique_cards"
|
||||
case rarityDistribution = "rarity_distribution"
|
||||
case collectionScore = "collection_score"
|
||||
case recommendations
|
||||
}
|
||||
}
|
||||
|
||||
struct GachaStats: Codable {
|
||||
let totalDraws: Int
|
||||
let cardsByRarity: [String: Int]
|
||||
let successRates: [String: Double]
|
||||
let recentActivity: [GachaActivity]
|
||||
|
||||
enum CodingKeys: String, CodingKey {
|
||||
case totalDraws = "total_draws"
|
||||
case cardsByRarity = "cards_by_rarity"
|
||||
case successRates = "success_rates"
|
||||
case recentActivity = "recent_activity"
|
||||
}
|
||||
}
|
||||
|
||||
struct GachaActivity: Codable {
|
||||
let timestamp: String
|
||||
let userDid: String
|
||||
let cardName: String
|
||||
let rarity: String
|
||||
|
||||
enum CodingKeys: String, CodingKey {
|
||||
case timestamp
|
||||
case userDid = "user_did"
|
||||
case cardName = "card_name"
|
||||
case rarity
|
||||
}
|
||||
}
|
||||
|
||||
struct UniqueRegistry: Codable {
|
||||
let registeredCards: [String: String]
|
||||
let totalUnique: Int
|
||||
|
||||
enum CodingKeys: String, CodingKey {
|
||||
case registeredCards = "registered_cards"
|
||||
case totalUnique = "total_unique"
|
||||
}
|
||||
}
|
||||
|
||||
struct SystemStatus: Codable {
|
||||
let status: String
|
||||
let mcpEnabled: Bool
|
||||
let mcpEndpoint: String?
|
||||
let databaseConnected: Bool
|
||||
let aiGptConnected: Bool
|
||||
|
||||
enum CodingKeys: String, CodingKey {
|
||||
case status
|
||||
case mcpEnabled = "mcp_enabled"
|
||||
case mcpEndpoint = "mcp_endpoint"
|
||||
case databaseConnected = "database_connected"
|
||||
case aiGptConnected = "ai_gpt_connected"
|
||||
}
|
||||
}
|
@@ -9,13 +9,21 @@ enum APIError: Error {
|
||||
case unauthorized
|
||||
}
|
||||
|
||||
// MCP Server response format
|
||||
struct MCPResponse<T: Decodable>: Decodable {
|
||||
let data: T?
|
||||
let error: String?
|
||||
}
|
||||
|
||||
class APIClient {
|
||||
static let shared = APIClient()
|
||||
|
||||
#if DEBUG
|
||||
private let baseURL = "http://localhost:8000/api/v1"
|
||||
private let baseURL = "http://localhost:8000/api/v1" // ai.card direct access
|
||||
private let aiGptBaseURL = "http://localhost:8001" // ai.gpt MCP server (optional)
|
||||
#else
|
||||
private let baseURL = "https://api.card.syui.ai/api/v1"
|
||||
private let baseURL = "https://api.card.syui.ai/api/v1" // ai.card direct access
|
||||
private let aiGptBaseURL = "https://ai.gpt.syui.ai" // ai.gpt MCP server (optional)
|
||||
#endif
|
||||
|
||||
private var cancellables = Set<AnyCancellable>()
|
||||
@@ -27,6 +35,59 @@ class APIClient {
|
||||
set { UserDefaults.standard.set(newValue, forKey: "authToken") }
|
||||
}
|
||||
|
||||
// ai.gpt MCP経由でのリクエスト(推奨)
|
||||
private func mcpRequest<T: Decodable>(_ endpoint: String,
|
||||
parameters: [String: Any] = [:]) -> AnyPublisher<T, APIError> {
|
||||
guard let url = URL(string: "\(aiGptBaseURL)\(endpoint)") else {
|
||||
return Fail(error: APIError.invalidURL).eraseToAnyPublisher()
|
||||
}
|
||||
|
||||
var components = URLComponents(url: url, resolvingAgainstBaseURL: false)
|
||||
components?.queryItems = parameters.map { URLQueryItem(name: $0.key, value: "\($0.value)") }
|
||||
|
||||
guard let finalURL = components?.url else {
|
||||
return Fail(error: APIError.invalidURL).eraseToAnyPublisher()
|
||||
}
|
||||
|
||||
var request = URLRequest(url: finalURL)
|
||||
request.httpMethod = "GET"
|
||||
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
|
||||
|
||||
return URLSession.shared.dataTaskPublisher(for: request)
|
||||
.tryMap { data, response in
|
||||
guard let httpResponse = response as? HTTPURLResponse else {
|
||||
throw APIError.networkError("Invalid response")
|
||||
}
|
||||
|
||||
if !(200...299).contains(httpResponse.statusCode) {
|
||||
throw APIError.networkError("MCP Server error: \(httpResponse.statusCode)")
|
||||
}
|
||||
|
||||
return data
|
||||
}
|
||||
.decode(type: MCPResponse<T>.self, decoder: JSONDecoder())
|
||||
.tryMap { mcpResponse in
|
||||
if let data = mcpResponse.data {
|
||||
return data
|
||||
} else if let error = mcpResponse.error {
|
||||
throw APIError.networkError(error)
|
||||
} else {
|
||||
throw APIError.networkError("Invalid MCP response")
|
||||
}
|
||||
}
|
||||
.mapError { error in
|
||||
if error is DecodingError {
|
||||
return APIError.decodingError
|
||||
} else if let apiError = error as? APIError {
|
||||
return apiError
|
||||
} else {
|
||||
return APIError.networkError(error.localizedDescription)
|
||||
}
|
||||
}
|
||||
.eraseToAnyPublisher()
|
||||
}
|
||||
|
||||
// ai.card直接リクエスト(メイン)
|
||||
private func request<T: Decodable>(_ endpoint: String,
|
||||
method: String = "GET",
|
||||
body: Data? = nil,
|
||||
@@ -104,7 +165,7 @@ class APIClient {
|
||||
request("/auth/verify")
|
||||
}
|
||||
|
||||
// MARK: - Cards
|
||||
// MARK: - Cards (ai.card直接アクセス)
|
||||
|
||||
func drawCard(userDid: String, isPaid: Bool = false) -> AnyPublisher<CardDrawResult, APIError> {
|
||||
let body = try? JSONEncoder().encode([
|
||||
@@ -116,10 +177,61 @@ class APIClient {
|
||||
}
|
||||
|
||||
func getUserCards(userDid: String) -> AnyPublisher<[Card], APIError> {
|
||||
request("/cards/user/\(userDid)")
|
||||
return request("/cards/user/\(userDid)")
|
||||
}
|
||||
|
||||
func getCardDetails(cardId: Int) -> AnyPublisher<Card, APIError> {
|
||||
return request("/cards/\(cardId)")
|
||||
}
|
||||
|
||||
func getGachaStats() -> AnyPublisher<GachaStats, APIError> {
|
||||
return request("/cards/stats")
|
||||
}
|
||||
|
||||
func getUniqueCards() -> AnyPublisher<[[String: Any]], APIError> {
|
||||
request("/cards/unique")
|
||||
return request("/cards/unique")
|
||||
}
|
||||
|
||||
func getSystemStatus() -> AnyPublisher<[String: Any], APIError> {
|
||||
return request("/health")
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - AI Enhanced API (Optional ai.gpt integration)
|
||||
|
||||
extension APIClient {
|
||||
|
||||
func analyzeCollection(userDid: String) -> AnyPublisher<CollectionAnalysis, APIError> {
|
||||
let parameters: [String: Any] = [
|
||||
"did": userDid
|
||||
]
|
||||
|
||||
return mcpRequest("/card_analyze_collection", parameters: parameters)
|
||||
.catch { error -> AnyPublisher<CollectionAnalysis, APIError> in
|
||||
// AI機能が利用できない場合のエラー
|
||||
return Fail(error: APIError.networkError("AI分析機能を利用するにはai.gptサーバーが必要です")).eraseToAnyPublisher()
|
||||
}
|
||||
.eraseToAnyPublisher()
|
||||
}
|
||||
|
||||
func getEnhancedStats() -> AnyPublisher<GachaStats, APIError> {
|
||||
return mcpRequest("/card_get_gacha_stats", parameters: [:])
|
||||
.catch { [weak self] error -> AnyPublisher<GachaStats, APIError> in
|
||||
// AI機能が利用できない場合は基本統計にフォールバック
|
||||
print("AI統計が利用できません、基本統計に切り替えます: \(error)")
|
||||
return self?.getGachaStats() ?? Fail(error: error).eraseToAnyPublisher()
|
||||
}
|
||||
.eraseToAnyPublisher()
|
||||
}
|
||||
|
||||
func isAIAvailable() -> AnyPublisher<Bool, Never> {
|
||||
guard let url = URL(string: "\(aiGptBaseURL)/health") else {
|
||||
return Just(false).eraseToAnyPublisher()
|
||||
}
|
||||
|
||||
return URLSession.shared.dataTaskPublisher(for: url)
|
||||
.map { _ in true }
|
||||
.catch { _ in Just(false) }
|
||||
.eraseToAnyPublisher()
|
||||
}
|
||||
}
|
355
ios/AiCard/AiCard/Services/AtprotoOAuthService.swift
Normal file
355
ios/AiCard/AiCard/Services/AtprotoOAuthService.swift
Normal file
@@ -0,0 +1,355 @@
|
||||
import Foundation
|
||||
import Combine
|
||||
import AuthenticationServices
|
||||
|
||||
struct AtprotoSession: Codable {
|
||||
let did: String
|
||||
let handle: String
|
||||
let accessJwt: String
|
||||
let refreshJwt: String
|
||||
let email: String?
|
||||
let emailConfirmed: Bool?
|
||||
}
|
||||
|
||||
class AtprotoOAuthService: NSObject, ObservableObject {
|
||||
static let shared = AtprotoOAuthService()
|
||||
|
||||
@Published var session: AtprotoSession?
|
||||
@Published var isAuthenticated: Bool = false
|
||||
|
||||
private var authSession: ASWebAuthenticationSession?
|
||||
private let clientId: String
|
||||
private let redirectUri: String
|
||||
private let scope = "atproto transition:generic"
|
||||
|
||||
override init() {
|
||||
// Generate client metadata URL
|
||||
self.clientId = "\(Bundle.main.bundleIdentifier ?? "ai.card")/client-metadata.json"
|
||||
self.redirectUri = "aicard://oauth/callback"
|
||||
|
||||
super.init()
|
||||
loadSessionFromKeychain()
|
||||
}
|
||||
|
||||
// MARK: - OAuth Flow
|
||||
|
||||
func initiateOAuthFlow() -> AnyPublisher<AtprotoSession, Error> {
|
||||
return Future { [weak self] promise in
|
||||
guard let self = self else {
|
||||
promise(.failure(OAuthError.invalidState))
|
||||
return
|
||||
}
|
||||
|
||||
Task {
|
||||
do {
|
||||
let authURL = try await self.buildAuthorizationURL()
|
||||
|
||||
DispatchQueue.main.async {
|
||||
self.startWebAuthenticationSession(url: authURL) { result in
|
||||
switch result {
|
||||
case .success(let session):
|
||||
self.session = session
|
||||
self.isAuthenticated = true
|
||||
self.saveSessionToKeychain(session)
|
||||
promise(.success(session))
|
||||
|
||||
case .failure(let error):
|
||||
promise(.failure(error))
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
promise(.failure(error))
|
||||
}
|
||||
}
|
||||
}
|
||||
.eraseToAnyPublisher()
|
||||
}
|
||||
|
||||
private func buildAuthorizationURL() async throws -> URL {
|
||||
// Generate PKCE parameters
|
||||
let state = generateRandomString(32)
|
||||
let codeVerifier = generateRandomString(128)
|
||||
let codeChallenge = try generateCodeChallenge(from: codeVerifier)
|
||||
|
||||
// Store PKCE parameters
|
||||
UserDefaults.standard.set(state, forKey: "oauth_state")
|
||||
UserDefaults.standard.set(codeVerifier, forKey: "oauth_code_verifier")
|
||||
|
||||
// For development: use mock authorization server
|
||||
// In production, this would discover the actual atproto authorization server
|
||||
let authServer = "https://bsky.social" // Mock - should be discovered
|
||||
|
||||
var components = URLComponents(string: "\(authServer)/oauth/authorize")!
|
||||
components.queryItems = [
|
||||
URLQueryItem(name: "response_type", value: "code"),
|
||||
URLQueryItem(name: "client_id", value: clientId),
|
||||
URLQueryItem(name: "redirect_uri", value: redirectUri),
|
||||
URLQueryItem(name: "scope", value: scope),
|
||||
URLQueryItem(name: "state", value: state),
|
||||
URLQueryItem(name: "code_challenge", value: codeChallenge),
|
||||
URLQueryItem(name: "code_challenge_method", value: "S256")
|
||||
]
|
||||
|
||||
guard let url = components.url else {
|
||||
throw OAuthError.invalidURL
|
||||
}
|
||||
|
||||
return url
|
||||
}
|
||||
|
||||
private func startWebAuthenticationSession(url: URL, completion: @escaping (Result<AtprotoSession, Error>) -> Void) {
|
||||
authSession = ASWebAuthenticationSession(url: url, callbackURLScheme: "aicard") { [weak self] callbackURL, error in
|
||||
|
||||
if let error = error {
|
||||
if case ASWebAuthenticationSessionError.canceledLogin = error {
|
||||
completion(.failure(OAuthError.userCancelled))
|
||||
} else {
|
||||
completion(.failure(error))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
guard let callbackURL = callbackURL else {
|
||||
completion(.failure(OAuthError.invalidCallback))
|
||||
return
|
||||
}
|
||||
|
||||
Task {
|
||||
do {
|
||||
let session = try await self?.handleOAuthCallback(callbackURL: callbackURL)
|
||||
if let session = session {
|
||||
completion(.success(session))
|
||||
} else {
|
||||
completion(.failure(OAuthError.invalidState))
|
||||
}
|
||||
} catch {
|
||||
completion(.failure(error))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
authSession?.presentationContextProvider = self
|
||||
authSession?.prefersEphemeralWebBrowserSession = false
|
||||
authSession?.start()
|
||||
}
|
||||
|
||||
private func handleOAuthCallback(callbackURL: URL) async throws -> AtprotoSession {
|
||||
guard let components = URLComponents(url: callbackURL, resolvingAgainstBaseURL: false),
|
||||
let queryItems = components.queryItems else {
|
||||
throw OAuthError.invalidCallback
|
||||
}
|
||||
|
||||
var code: String?
|
||||
var state: String?
|
||||
var error: String?
|
||||
|
||||
for item in queryItems {
|
||||
switch item.name {
|
||||
case "code":
|
||||
code = item.value
|
||||
case "state":
|
||||
state = item.value
|
||||
case "error":
|
||||
error = item.value
|
||||
default:
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if let error = error {
|
||||
throw OAuthError.authorizationFailed(error)
|
||||
}
|
||||
|
||||
guard let code = code, let state = state else {
|
||||
throw OAuthError.missingParameters
|
||||
}
|
||||
|
||||
// Verify state
|
||||
let storedState = UserDefaults.standard.string(forKey: "oauth_state")
|
||||
guard state == storedState else {
|
||||
throw OAuthError.invalidState
|
||||
}
|
||||
|
||||
// Get code verifier
|
||||
guard let codeVerifier = UserDefaults.standard.string(forKey: "oauth_code_verifier") else {
|
||||
throw OAuthError.missingCodeVerifier
|
||||
}
|
||||
|
||||
// Exchange code for tokens
|
||||
let session = try await exchangeCodeForTokens(code: code, codeVerifier: codeVerifier)
|
||||
|
||||
// Clean up temporary data
|
||||
UserDefaults.standard.removeObject(forKey: "oauth_state")
|
||||
UserDefaults.standard.removeObject(forKey: "oauth_code_verifier")
|
||||
|
||||
return session
|
||||
}
|
||||
|
||||
private func exchangeCodeForTokens(code: String, codeVerifier: String) async throws -> AtprotoSession {
|
||||
// This is a mock implementation
|
||||
// In production, this would make a proper token exchange request
|
||||
|
||||
// For development, return a mock session
|
||||
let mockSession = AtprotoSession(
|
||||
did: "did:plc:mock123456789",
|
||||
handle: "user.bsky.social",
|
||||
accessJwt: "mock_access_token",
|
||||
refreshJwt: "mock_refresh_token",
|
||||
email: nil,
|
||||
emailConfirmed: nil
|
||||
)
|
||||
|
||||
return mockSession
|
||||
}
|
||||
|
||||
// MARK: - Session Management
|
||||
|
||||
func refreshTokens() async throws -> AtprotoSession {
|
||||
guard let currentSession = session else {
|
||||
throw OAuthError.noSession
|
||||
}
|
||||
|
||||
// This would make a proper token refresh request
|
||||
// For now, return the existing session
|
||||
return currentSession
|
||||
}
|
||||
|
||||
func logout() {
|
||||
session = nil
|
||||
isAuthenticated = false
|
||||
deleteSessionFromKeychain()
|
||||
|
||||
// Cancel any ongoing auth session
|
||||
authSession?.cancel()
|
||||
authSession = nil
|
||||
}
|
||||
|
||||
// MARK: - Keychain Storage
|
||||
|
||||
private func saveSessionToKeychain(_ session: AtprotoSession) {
|
||||
guard let data = try? JSONEncoder().encode(session) else { return }
|
||||
|
||||
let query: [String: Any] = [
|
||||
kSecClass as String: kSecClassGenericPassword,
|
||||
kSecAttrAccount as String: "atproto_session",
|
||||
kSecValueData as String: data
|
||||
]
|
||||
|
||||
// Delete existing item
|
||||
SecItemDelete(query as CFDictionary)
|
||||
|
||||
// Add new item
|
||||
SecItemAdd(query as CFDictionary, nil)
|
||||
}
|
||||
|
||||
private func loadSessionFromKeychain() {
|
||||
let query: [String: Any] = [
|
||||
kSecClass as String: kSecClassGenericPassword,
|
||||
kSecAttrAccount as String: "atproto_session",
|
||||
kSecReturnData as String: true,
|
||||
kSecMatchLimit as String: kSecMatchLimitOne
|
||||
]
|
||||
|
||||
var result: AnyObject?
|
||||
let status = SecItemCopyMatching(query as CFDictionary, &result)
|
||||
|
||||
if status == errSecSuccess,
|
||||
let data = result as? Data,
|
||||
let session = try? JSONDecoder().decode(AtprotoSession.self, from: data) {
|
||||
self.session = session
|
||||
self.isAuthenticated = true
|
||||
}
|
||||
}
|
||||
|
||||
private func deleteSessionFromKeychain() {
|
||||
let query: [String: Any] = [
|
||||
kSecClass as String: kSecClassGenericPassword,
|
||||
kSecAttrAccount as String: "atproto_session"
|
||||
]
|
||||
|
||||
SecItemDelete(query as CFDictionary)
|
||||
}
|
||||
|
||||
// MARK: - Utility Methods
|
||||
|
||||
private func generateRandomString(_ length: Int) -> String {
|
||||
let chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~"
|
||||
return String((0..<length).map { _ in chars.randomElement()! })
|
||||
}
|
||||
|
||||
private func generateCodeChallenge(from verifier: String) throws -> String {
|
||||
guard let data = verifier.data(using: .utf8) else {
|
||||
throw OAuthError.encodingError
|
||||
}
|
||||
|
||||
let digest = SHA256.hash(data: data)
|
||||
return Data(digest).base64URLEncodedString()
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - ASWebAuthenticationPresentationContextProviding
|
||||
|
||||
extension AtprotoOAuthService: ASWebAuthenticationPresentationContextProviding {
|
||||
func presentationAnchor(for session: ASWebAuthenticationSession) -> ASPresentationAnchor {
|
||||
return UIApplication.shared.windows.first { $0.isKeyWindow } ?? ASPresentationAnchor()
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Errors
|
||||
|
||||
enum OAuthError: LocalizedError {
|
||||
case invalidURL
|
||||
case invalidState
|
||||
case invalidCallback
|
||||
case missingParameters
|
||||
case missingCodeVerifier
|
||||
case authorizationFailed(String)
|
||||
case userCancelled
|
||||
case noSession
|
||||
case encodingError
|
||||
|
||||
var errorDescription: String? {
|
||||
switch self {
|
||||
case .invalidURL:
|
||||
return "無効なURLです"
|
||||
case .invalidState:
|
||||
return "無効な状態パラメータです"
|
||||
case .invalidCallback:
|
||||
return "無効なコールバックです"
|
||||
case .missingParameters:
|
||||
return "必要なパラメータが不足しています"
|
||||
case .missingCodeVerifier:
|
||||
return "コード検証子が見つかりません"
|
||||
case .authorizationFailed(let error):
|
||||
return "認証に失敗しました: \(error)"
|
||||
case .userCancelled:
|
||||
return "ユーザーによってキャンセルされました"
|
||||
case .noSession:
|
||||
return "セッションがありません"
|
||||
case .encodingError:
|
||||
return "エンコードエラーです"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Data Extension for Base64URL
|
||||
|
||||
extension Data {
|
||||
func base64URLEncodedString() -> String {
|
||||
return base64EncodedString()
|
||||
.replacingOccurrences(of: "+", with: "-")
|
||||
.replacingOccurrences(of: "/", with: "_")
|
||||
.replacingOccurrences(of: "=", with: "")
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - SHA256 (simplified for demo)
|
||||
|
||||
import CryptoKit
|
||||
|
||||
extension SHA256 {
|
||||
static func hash(data: Data) -> SHA256.Digest {
|
||||
return SHA256.hash(data: data)
|
||||
}
|
||||
}
|
@@ -7,17 +7,44 @@ class AuthManager: ObservableObject {
|
||||
@Published var currentUser: User?
|
||||
@Published var isLoading = false
|
||||
@Published var errorMessage: String?
|
||||
@Published var authMode: AuthMode = .oauth
|
||||
|
||||
private var cancellables = Set<AnyCancellable>()
|
||||
private let apiClient = APIClient.shared
|
||||
private let oauthService = AtprotoOAuthService.shared
|
||||
|
||||
enum AuthMode {
|
||||
case oauth
|
||||
case legacy
|
||||
}
|
||||
|
||||
init() {
|
||||
// Monitor OAuth service
|
||||
oauthService.$isAuthenticated
|
||||
.receive(on: DispatchQueue.main)
|
||||
.sink { [weak self] isAuth in
|
||||
if isAuth, let session = self?.oauthService.session {
|
||||
self?.isAuthenticated = true
|
||||
self?.currentUser = User(did: session.did, handle: session.handle)
|
||||
}
|
||||
}
|
||||
.store(in: &cancellables)
|
||||
|
||||
checkAuthStatus()
|
||||
}
|
||||
|
||||
private func checkAuthStatus() {
|
||||
isLoading = true
|
||||
|
||||
// Check OAuth session first
|
||||
if oauthService.isAuthenticated, let session = oauthService.session {
|
||||
isAuthenticated = true
|
||||
currentUser = User(did: session.did, handle: session.handle)
|
||||
isLoading = false
|
||||
return
|
||||
}
|
||||
|
||||
// Fallback to legacy auth
|
||||
apiClient.verify()
|
||||
.receive(on: DispatchQueue.main)
|
||||
.sink(
|
||||
@@ -36,7 +63,28 @@ class AuthManager: ObservableObject {
|
||||
.store(in: &cancellables)
|
||||
}
|
||||
|
||||
func login(identifier: String, password: String) {
|
||||
func loginWithOAuth() {
|
||||
isLoading = true
|
||||
errorMessage = nil
|
||||
|
||||
oauthService.initiateOAuthFlow()
|
||||
.receive(on: DispatchQueue.main)
|
||||
.sink(
|
||||
receiveCompletion: { [weak self] completion in
|
||||
self?.isLoading = false
|
||||
if case .failure(let error) = completion {
|
||||
self?.errorMessage = error.localizedDescription
|
||||
}
|
||||
},
|
||||
receiveValue: { [weak self] session in
|
||||
self?.isAuthenticated = true
|
||||
self?.currentUser = User(did: session.did, handle: session.handle)
|
||||
}
|
||||
)
|
||||
.store(in: &cancellables)
|
||||
}
|
||||
|
||||
func loginWithPassword(identifier: String, password: String) {
|
||||
isLoading = true
|
||||
errorMessage = nil
|
||||
|
||||
@@ -60,6 +108,9 @@ class AuthManager: ObservableObject {
|
||||
func logout() {
|
||||
isLoading = true
|
||||
|
||||
// Logout from both services
|
||||
oauthService.logout()
|
||||
|
||||
apiClient.logout()
|
||||
.receive(on: DispatchQueue.main)
|
||||
.sink(
|
||||
|
134
migrations/postgres/001_initial.sql
Normal file
134
migrations/postgres/001_initial.sql
Normal file
@@ -0,0 +1,134 @@
|
||||
-- PostgreSQL migration for ai.card database schema
|
||||
|
||||
-- Create custom types
|
||||
CREATE TYPE card_rarity AS ENUM ('normal', 'rare', 'super_rare', 'kira', 'unique');
|
||||
|
||||
-- Enable UUID extension
|
||||
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
|
||||
|
||||
-- Users table - stores atproto DID-based user information
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
id SERIAL PRIMARY KEY,
|
||||
did TEXT NOT NULL UNIQUE, -- atproto Decentralized Identifier
|
||||
handle TEXT NOT NULL, -- atproto handle (e.g., alice.bsky.social)
|
||||
created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
|
||||
updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW()
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_users_did ON users(did);
|
||||
CREATE INDEX IF NOT EXISTS idx_users_handle ON users(handle);
|
||||
|
||||
-- Card master data - template definitions for all card types
|
||||
CREATE TABLE IF NOT EXISTS card_master (
|
||||
id INTEGER PRIMARY KEY, -- Card ID (0-15 in current system)
|
||||
name TEXT NOT NULL, -- Card name (e.g., "ai", "dream", "radiance")
|
||||
base_cp_min INTEGER NOT NULL, -- Minimum base CP for this card
|
||||
base_cp_max INTEGER NOT NULL, -- Maximum base CP for this card
|
||||
color TEXT NOT NULL, -- Card color theme
|
||||
description TEXT NOT NULL -- Card description/lore
|
||||
);
|
||||
|
||||
-- User cards - actual card instances owned by users
|
||||
CREATE TABLE IF NOT EXISTS user_cards (
|
||||
id SERIAL PRIMARY KEY,
|
||||
user_id INTEGER NOT NULL,
|
||||
card_id INTEGER NOT NULL, -- References card_master.id
|
||||
cp INTEGER NOT NULL, -- Calculated CP (base_cp * rarity_multiplier)
|
||||
status card_rarity NOT NULL, -- Card rarity
|
||||
skill TEXT, -- Optional skill description
|
||||
obtained_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
|
||||
is_unique BOOLEAN NOT NULL DEFAULT FALSE,
|
||||
unique_id UUID, -- UUID for unique cards
|
||||
|
||||
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
|
||||
FOREIGN KEY (card_id) REFERENCES card_master(id)
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_user_cards_user_id ON user_cards(user_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_user_cards_card_id ON user_cards(card_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_user_cards_status ON user_cards(status);
|
||||
CREATE INDEX IF NOT EXISTS idx_user_cards_unique_id ON user_cards(unique_id);
|
||||
|
||||
-- Global unique card registry - tracks ownership of unique cards
|
||||
CREATE TABLE IF NOT EXISTS unique_card_registry (
|
||||
id SERIAL PRIMARY KEY,
|
||||
unique_id UUID NOT NULL UNIQUE, -- UUID from user_cards.unique_id
|
||||
card_id INTEGER NOT NULL, -- Which card type is unique
|
||||
owner_did TEXT NOT NULL, -- Current owner's atproto DID
|
||||
obtained_at TIMESTAMP WITH TIME ZONE NOT NULL,
|
||||
verse_skill_id TEXT, -- Optional verse skill reference
|
||||
|
||||
FOREIGN KEY (card_id) REFERENCES card_master(id),
|
||||
UNIQUE(card_id) -- Only one unique per card_id allowed
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_unique_registry_card_id ON unique_card_registry(card_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_unique_registry_owner_did ON unique_card_registry(owner_did);
|
||||
|
||||
-- Draw history - tracks all gacha draws for statistics
|
||||
CREATE TABLE IF NOT EXISTS draw_history (
|
||||
id SERIAL PRIMARY KEY,
|
||||
user_id INTEGER NOT NULL,
|
||||
card_id INTEGER NOT NULL,
|
||||
status card_rarity NOT NULL,
|
||||
cp INTEGER NOT NULL,
|
||||
is_paid BOOLEAN NOT NULL DEFAULT FALSE, -- Paid vs free gacha
|
||||
drawn_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
|
||||
|
||||
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
|
||||
FOREIGN KEY (card_id) REFERENCES card_master(id)
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_draw_history_user_id ON draw_history(user_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_draw_history_drawn_at ON draw_history(drawn_at);
|
||||
CREATE INDEX IF NOT EXISTS idx_draw_history_status ON draw_history(status);
|
||||
|
||||
-- Gacha pools - special event pools with rate-ups
|
||||
CREATE TABLE IF NOT EXISTS gacha_pools (
|
||||
id SERIAL PRIMARY KEY,
|
||||
name TEXT NOT NULL,
|
||||
description TEXT NOT NULL,
|
||||
is_active BOOLEAN NOT NULL DEFAULT TRUE,
|
||||
start_at TIMESTAMP WITH TIME ZONE,
|
||||
end_at TIMESTAMP WITH TIME ZONE,
|
||||
pickup_card_ids INTEGER[], -- Array of card IDs
|
||||
rate_up_multiplier DECIMAL(4,2) NOT NULL DEFAULT 1.0
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_gacha_pools_active ON gacha_pools(is_active);
|
||||
CREATE INDEX IF NOT EXISTS idx_gacha_pools_dates ON gacha_pools(start_at, end_at);
|
||||
|
||||
-- Insert default card master data (0-15 cards from ai.json)
|
||||
INSERT INTO card_master (id, name, base_cp_min, base_cp_max, color, description) VALUES
|
||||
(0, 'ai', 100, 200, '#4A90E2', 'The core essence of existence'),
|
||||
(1, 'dream', 90, 180, '#9B59B6', 'Visions of possibility'),
|
||||
(2, 'radiance', 110, 220, '#F39C12', 'Brilliant light energy'),
|
||||
(3, 'neutron', 120, 240, '#34495E', 'Dense stellar core'),
|
||||
(4, 'sun', 130, 260, '#E74C3C', 'Solar radiance'),
|
||||
(5, 'night', 80, 160, '#2C3E50', 'Darkness and mystery'),
|
||||
(6, 'snow', 70, 140, '#ECF0F1', 'Pure frozen crystalline'),
|
||||
(7, 'thunder', 140, 280, '#F1C40F', 'Electric storm energy'),
|
||||
(8, 'ultimate', 150, 300, '#8E44AD', 'The highest form'),
|
||||
(9, 'sword', 160, 320, '#95A5A6', 'Blade of cutting truth'),
|
||||
(10, 'destruction', 170, 340, '#C0392B', 'Force of entropy'),
|
||||
(11, 'earth', 90, 180, '#27AE60', 'Grounding foundation'),
|
||||
(12, 'galaxy', 180, 360, '#3498DB', 'Cosmic expanse'),
|
||||
(13, 'create', 100, 200, '#16A085', 'Power of generation'),
|
||||
(14, 'supernova', 200, 400, '#E67E22', 'Stellar explosion'),
|
||||
(15, 'world', 250, 500, '#9B59B6', 'Reality itself')
|
||||
ON CONFLICT (id) DO NOTHING;
|
||||
|
||||
-- Create function for updating updated_at timestamp
|
||||
CREATE OR REPLACE FUNCTION update_updated_at_column()
|
||||
RETURNS TRIGGER AS $$
|
||||
BEGIN
|
||||
NEW.updated_at = NOW();
|
||||
RETURN NEW;
|
||||
END;
|
||||
$$ language 'plpgsql';
|
||||
|
||||
-- Create trigger for updating users.updated_at
|
||||
CREATE TRIGGER trigger_users_updated_at
|
||||
BEFORE UPDATE ON users
|
||||
FOR EACH ROW
|
||||
EXECUTE FUNCTION update_updated_at_column();
|
130
migrations/sqlite/001_initial.sql
Normal file
130
migrations/sqlite/001_initial.sql
Normal file
@@ -0,0 +1,130 @@
|
||||
-- SQLite migration for ai.card database schema
|
||||
|
||||
-- Create custom types (SQLite uses CHECK constraints instead of ENUMs)
|
||||
-- Card rarity levels
|
||||
CREATE TABLE IF NOT EXISTS card_rarity_enum (
|
||||
value TEXT PRIMARY KEY CHECK (value IN ('normal', 'rare', 'super_rare', 'kira', 'unique'))
|
||||
);
|
||||
|
||||
INSERT OR IGNORE INTO card_rarity_enum (value) VALUES
|
||||
('normal'), ('rare'), ('super_rare'), ('kira'), ('unique');
|
||||
|
||||
-- Users table - stores atproto DID-based user information
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
did TEXT NOT NULL UNIQUE, -- atproto Decentralized Identifier
|
||||
handle TEXT NOT NULL, -- atproto handle (e.g., alice.bsky.social)
|
||||
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_users_did ON users(did);
|
||||
CREATE INDEX IF NOT EXISTS idx_users_handle ON users(handle);
|
||||
|
||||
-- Card master data - template definitions for all card types
|
||||
CREATE TABLE IF NOT EXISTS card_master (
|
||||
id INTEGER PRIMARY KEY, -- Card ID (0-15 in current system)
|
||||
name TEXT NOT NULL, -- Card name (e.g., "ai", "dream", "radiance")
|
||||
base_cp_min INTEGER NOT NULL, -- Minimum base CP for this card
|
||||
base_cp_max INTEGER NOT NULL, -- Maximum base CP for this card
|
||||
color TEXT NOT NULL, -- Card color theme
|
||||
description TEXT NOT NULL -- Card description/lore
|
||||
);
|
||||
|
||||
-- User cards - actual card instances owned by users
|
||||
CREATE TABLE IF NOT EXISTS user_cards (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
user_id INTEGER NOT NULL,
|
||||
card_id INTEGER NOT NULL, -- References card_master.id
|
||||
cp INTEGER NOT NULL, -- Calculated CP (base_cp * rarity_multiplier)
|
||||
status TEXT NOT NULL -- Card rarity
|
||||
CHECK (status IN ('normal', 'rare', 'super_rare', 'kira', 'unique')),
|
||||
skill TEXT, -- Optional skill description
|
||||
obtained_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
is_unique BOOLEAN NOT NULL DEFAULT FALSE,
|
||||
unique_id TEXT, -- UUID for unique cards
|
||||
|
||||
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
|
||||
FOREIGN KEY (card_id) REFERENCES card_master(id)
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_user_cards_user_id ON user_cards(user_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_user_cards_card_id ON user_cards(card_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_user_cards_status ON user_cards(status);
|
||||
CREATE INDEX IF NOT EXISTS idx_user_cards_unique_id ON user_cards(unique_id);
|
||||
|
||||
-- Global unique card registry - tracks ownership of unique cards
|
||||
CREATE TABLE IF NOT EXISTS unique_card_registry (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
unique_id TEXT NOT NULL UNIQUE, -- UUID from user_cards.unique_id
|
||||
card_id INTEGER NOT NULL, -- Which card type is unique
|
||||
owner_did TEXT NOT NULL, -- Current owner's atproto DID
|
||||
obtained_at DATETIME NOT NULL,
|
||||
verse_skill_id TEXT, -- Optional verse skill reference
|
||||
|
||||
FOREIGN KEY (card_id) REFERENCES card_master(id),
|
||||
UNIQUE(card_id) -- Only one unique per card_id allowed
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_unique_registry_card_id ON unique_card_registry(card_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_unique_registry_owner_did ON unique_card_registry(owner_did);
|
||||
|
||||
-- Draw history - tracks all gacha draws for statistics
|
||||
CREATE TABLE IF NOT EXISTS draw_history (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
user_id INTEGER NOT NULL,
|
||||
card_id INTEGER NOT NULL,
|
||||
status TEXT NOT NULL
|
||||
CHECK (status IN ('normal', 'rare', 'super_rare', 'kira', 'unique')),
|
||||
cp INTEGER NOT NULL,
|
||||
is_paid BOOLEAN NOT NULL DEFAULT FALSE, -- Paid vs free gacha
|
||||
drawn_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
|
||||
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
|
||||
FOREIGN KEY (card_id) REFERENCES card_master(id)
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_draw_history_user_id ON draw_history(user_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_draw_history_drawn_at ON draw_history(drawn_at);
|
||||
CREATE INDEX IF NOT EXISTS idx_draw_history_status ON draw_history(status);
|
||||
|
||||
-- Gacha pools - special event pools with rate-ups
|
||||
CREATE TABLE IF NOT EXISTS gacha_pools (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
name TEXT NOT NULL,
|
||||
description TEXT NOT NULL,
|
||||
is_active BOOLEAN NOT NULL DEFAULT TRUE,
|
||||
start_at DATETIME,
|
||||
end_at DATETIME,
|
||||
pickup_card_ids TEXT, -- JSON array of card IDs
|
||||
rate_up_multiplier REAL NOT NULL DEFAULT 1.0
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_gacha_pools_active ON gacha_pools(is_active);
|
||||
CREATE INDEX IF NOT EXISTS idx_gacha_pools_dates ON gacha_pools(start_at, end_at);
|
||||
|
||||
-- Insert default card master data (0-15 cards from ai.json)
|
||||
INSERT OR IGNORE INTO card_master (id, name, base_cp_min, base_cp_max, color, description) VALUES
|
||||
(0, 'ai', 100, 200, '#4A90E2', 'The core essence of existence'),
|
||||
(1, 'dream', 90, 180, '#9B59B6', 'Visions of possibility'),
|
||||
(2, 'radiance', 110, 220, '#F39C12', 'Brilliant light energy'),
|
||||
(3, 'neutron', 120, 240, '#34495E', 'Dense stellar core'),
|
||||
(4, 'sun', 130, 260, '#E74C3C', 'Solar radiance'),
|
||||
(5, 'night', 80, 160, '#2C3E50', 'Darkness and mystery'),
|
||||
(6, 'snow', 70, 140, '#ECF0F1', 'Pure frozen crystalline'),
|
||||
(7, 'thunder', 140, 280, '#F1C40F', 'Electric storm energy'),
|
||||
(8, 'ultimate', 150, 300, '#8E44AD', 'The highest form'),
|
||||
(9, 'sword', 160, 320, '#95A5A6', 'Blade of cutting truth'),
|
||||
(10, 'destruction', 170, 340, '#C0392B', 'Force of entropy'),
|
||||
(11, 'earth', 90, 180, '#27AE60', 'Grounding foundation'),
|
||||
(12, 'galaxy', 180, 360, '#3498DB', 'Cosmic expanse'),
|
||||
(13, 'create', 100, 200, '#16A085', 'Power of generation'),
|
||||
(14, 'supernova', 200, 400, '#E67E22', 'Stellar explosion'),
|
||||
(15, 'world', 250, 500, '#9B59B6', 'Reality itself');
|
||||
|
||||
-- Create trigger for updating users.updated_at
|
||||
CREATE TRIGGER IF NOT EXISTS trigger_users_updated_at
|
||||
AFTER UPDATE ON users
|
||||
BEGIN
|
||||
UPDATE users SET updated_at = CURRENT_TIMESTAMP WHERE id = NEW.id;
|
||||
END;
|
@@ -1,26 +0,0 @@
|
||||
#!/bin/bash
|
||||
|
||||
echo "🎴 ai.card セットアップスクリプト"
|
||||
echo "================================"
|
||||
|
||||
# APIセットアップ
|
||||
echo "📦 API セットアップ中..."
|
||||
cd api
|
||||
python -m venv venv
|
||||
source venv/bin/activate
|
||||
pip install -r requirements.txt
|
||||
cp .env.example .env
|
||||
echo "✅ API セットアップ完了"
|
||||
|
||||
# Webセットアップ
|
||||
echo "📦 Web セットアップ中..."
|
||||
cd ../web
|
||||
npm install
|
||||
echo "✅ Web セットアップ完了"
|
||||
|
||||
echo "================================"
|
||||
echo "🚀 セットアップ完了!"
|
||||
echo ""
|
||||
echo "開発サーバーの起動:"
|
||||
echo " API: cd api && uvicorn app.main:app --reload"
|
||||
echo " Web: cd web && npm run dev"
|
@@ -1,38 +0,0 @@
|
||||
#!/bin/bash
|
||||
|
||||
# ai.card Python仮想環境セットアップスクリプト
|
||||
# 新しいconfig規則に従って ~/.config/syui/ai/card/ 配下に構築
|
||||
|
||||
CONFIG_DIR="$HOME/.config/syui/ai/card"
|
||||
VENV_DIR="$CONFIG_DIR/venv"
|
||||
|
||||
echo "🔧 ai.card Python環境セットアップ開始..."
|
||||
|
||||
# configディレクトリ作成
|
||||
echo "📁 設定ディレクトリ作成: $CONFIG_DIR"
|
||||
mkdir -p "$CONFIG_DIR"
|
||||
|
||||
# 仮想環境作成
|
||||
echo "🐍 Python仮想環境作成: $VENV_DIR"
|
||||
python -m venv "$VENV_DIR"
|
||||
|
||||
# requirements.txtをconfigディレクトリにコピー
|
||||
echo "📋 requirements.txt をconfigディレクトリにコピー"
|
||||
cp api/requirements.txt "$CONFIG_DIR/"
|
||||
|
||||
# 仮想環境アクティベート用エイリアス情報表示
|
||||
echo ""
|
||||
echo "✅ セットアップ完了!"
|
||||
echo ""
|
||||
echo "🚀 使用方法:"
|
||||
echo " # 仮想環境アクティベート"
|
||||
echo " source ~/.config/syui/ai/card/venv/bin/activate"
|
||||
echo ""
|
||||
echo " # パッケージインストール"
|
||||
echo " pip install -r ~/.config/syui/ai/card/requirements.txt"
|
||||
echo ""
|
||||
echo " # デアクティベート"
|
||||
echo " deactivate"
|
||||
echo ""
|
||||
echo "💡 .bashrc/.zshrcにエイリアスを追加することを推奨:"
|
||||
echo " alias aicard-env='source ~/.config/syui/ai/card/venv/bin/activate'"
|
116
setup_venv.sh
116
setup_venv.sh
@@ -1,116 +0,0 @@
|
||||
#!/bin/bash
|
||||
set -e
|
||||
|
||||
# ai.card virtual environment setup script
|
||||
# This script sets up the Python virtual environment for ai.card MCP server
|
||||
|
||||
# Configuration
|
||||
CARD_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
API_DIR="$CARD_DIR/api"
|
||||
VENV_DIR="$HOME/.config/syui/ai/card/venv"
|
||||
REQUIREMENTS_FILE="$API_DIR/requirements.txt"
|
||||
|
||||
echo "🎴 ai.card Virtual Environment Setup"
|
||||
echo "======================================"
|
||||
echo "Card directory: $CARD_DIR"
|
||||
echo "API directory: $API_DIR"
|
||||
echo "Virtual environment: $VENV_DIR"
|
||||
echo
|
||||
|
||||
# Check if we're in the right directory
|
||||
if [ ! -f "$API_DIR/requirements.txt" ]; then
|
||||
echo "❌ Error: requirements.txt not found in $API_DIR"
|
||||
echo "Make sure you're running this script from the ai.card root directory"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Create config directory structure
|
||||
echo "📁 Creating config directory structure..."
|
||||
mkdir -p "$(dirname "$VENV_DIR")"
|
||||
|
||||
# Create virtual environment
|
||||
if [ -d "$VENV_DIR" ]; then
|
||||
echo "⚠️ Virtual environment already exists at $VENV_DIR"
|
||||
read -p "Do you want to recreate it? (y/N): " -n 1 -r
|
||||
echo
|
||||
if [[ $REPLY =~ ^[Yy]$ ]]; then
|
||||
echo "🗑️ Removing existing virtual environment..."
|
||||
rm -rf "$VENV_DIR"
|
||||
else
|
||||
echo "ℹ️ Using existing virtual environment"
|
||||
fi
|
||||
fi
|
||||
|
||||
if [ ! -d "$VENV_DIR" ]; then
|
||||
echo "🐍 Creating Python virtual environment..."
|
||||
python3 -m venv "$VENV_DIR"
|
||||
fi
|
||||
|
||||
# Activate virtual environment
|
||||
echo "🔌 Activating virtual environment..."
|
||||
source "$VENV_DIR/bin/activate"
|
||||
|
||||
# Upgrade pip
|
||||
echo "⬆️ Upgrading pip..."
|
||||
python -m pip install --upgrade pip
|
||||
|
||||
# Install dependencies
|
||||
echo "📦 Installing dependencies from requirements.txt..."
|
||||
pip install -r "$REQUIREMENTS_FILE"
|
||||
|
||||
# Verify installation
|
||||
echo "✅ Verifying installation..."
|
||||
cd "$API_DIR"
|
||||
|
||||
# Test basic imports
|
||||
echo " Testing FastAPI import..."
|
||||
python -c "import fastapi; print(f'✓ FastAPI {fastapi.__version__}')" || {
|
||||
echo "❌ FastAPI import failed"
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Test fastapi-mcp import
|
||||
echo " Testing fastapi-mcp import..."
|
||||
python -c "from mcp.server.fastmcp import FastMCP; print('✓ FastMCP')" || {
|
||||
echo "❌ FastMCP import failed"
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Test ai.card MCP server import
|
||||
echo " Testing ai.card MCP server import..."
|
||||
python -c "from app.mcp_server import AICardMcpServer; print('✓ AICardMcpServer')" || {
|
||||
echo "❌ AICardMcpServer import failed"
|
||||
exit 1
|
||||
}
|
||||
|
||||
echo
|
||||
echo "🎉 Setup completed successfully!"
|
||||
echo
|
||||
echo "Usage:"
|
||||
echo "------"
|
||||
echo "# Activate virtual environment:"
|
||||
echo "source $VENV_DIR/bin/activate"
|
||||
echo
|
||||
echo "# Start ai.card MCP server:"
|
||||
echo "cd $API_DIR"
|
||||
echo "uvicorn app.main:app --host localhost --port 8000 --reload"
|
||||
echo
|
||||
echo "# Test server:"
|
||||
echo "curl http://localhost:8000/health"
|
||||
echo
|
||||
echo "# Deactivate when done:"
|
||||
echo "deactivate"
|
||||
echo
|
||||
echo "MCP Server Features:"
|
||||
echo "-------------------"
|
||||
echo "• 9 MCP tools for card game functionality"
|
||||
echo "• FastAPI REST API (/api/v1/*)"
|
||||
echo "• Environment variable control (ENABLE_MCP=true/false)"
|
||||
echo "• Integration with ai.gpt MCP server"
|
||||
echo
|
||||
echo "Configuration:"
|
||||
echo "-------------"
|
||||
echo "• Virtual environment: $VENV_DIR"
|
||||
echo "• Config directory: ~/.config/syui/ai/card/"
|
||||
echo "• API documentation: http://localhost:8000/docs"
|
||||
echo "• MCP endpoint: http://localhost:8000/mcp"
|
108
src/auth.rs
Normal file
108
src/auth.rs
Normal file
@@ -0,0 +1,108 @@
|
||||
use chrono::{Duration, Utc};
|
||||
use jsonwebtoken::{decode, encode, DecodingKey, EncodingKey, Header, Validation};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::error::{AppError, AppResult};
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct Claims {
|
||||
pub did: String,
|
||||
pub handle: String,
|
||||
pub exp: usize,
|
||||
}
|
||||
|
||||
pub struct JwtService {
|
||||
encoding_key: EncodingKey,
|
||||
decoding_key: DecodingKey,
|
||||
}
|
||||
|
||||
impl JwtService {
|
||||
pub fn new(secret: &str) -> Self {
|
||||
Self {
|
||||
encoding_key: EncodingKey::from_secret(secret.as_ref()),
|
||||
decoding_key: DecodingKey::from_secret(secret.as_ref()),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn create_token(&self, did: &str, handle: &str, expires_in_minutes: u64) -> AppResult<String> {
|
||||
let expiration = Utc::now()
|
||||
.checked_add_signed(Duration::minutes(expires_in_minutes as i64))
|
||||
.ok_or_else(|| AppError::internal("Failed to calculate expiration time"))?
|
||||
.timestamp() as usize;
|
||||
|
||||
let claims = Claims {
|
||||
did: did.to_string(),
|
||||
handle: handle.to_string(),
|
||||
exp: expiration,
|
||||
};
|
||||
|
||||
encode(&Header::default(), &claims, &self.encoding_key)
|
||||
.map_err(AppError::Jwt)
|
||||
}
|
||||
|
||||
pub fn verify_token(&self, token: &str) -> AppResult<Claims> {
|
||||
let token_data = decode::<Claims>(token, &self.decoding_key, &Validation::default())
|
||||
.map_err(AppError::Jwt)?;
|
||||
|
||||
Ok(token_data.claims)
|
||||
}
|
||||
}
|
||||
|
||||
/// Mock atproto authentication service
|
||||
/// In a real implementation, this would integrate with actual atproto services
|
||||
pub struct AtprotoAuthService {
|
||||
jwt_service: JwtService,
|
||||
}
|
||||
|
||||
impl AtprotoAuthService {
|
||||
pub fn new(secret: &str) -> Self {
|
||||
Self {
|
||||
jwt_service: JwtService::new(secret),
|
||||
}
|
||||
}
|
||||
|
||||
/// Authenticate user with atproto credentials
|
||||
/// This is a mock implementation - in reality would validate against atproto PDS
|
||||
pub async fn authenticate(&self, identifier: &str, _password: &str) -> AppResult<AuthenticatedUser> {
|
||||
// Mock validation - in real implementation:
|
||||
// 1. Connect to user's PDS
|
||||
// 2. Verify credentials
|
||||
// 3. Get user DID and handle
|
||||
|
||||
// For now, treat identifier as DID or handle
|
||||
let (did, handle) = if identifier.starts_with("did:") {
|
||||
(identifier.to_string(), extract_handle_from_did(identifier))
|
||||
} else {
|
||||
(format!("did:plc:{}", generate_mock_plc_id()), identifier.to_string())
|
||||
};
|
||||
|
||||
Ok(AuthenticatedUser { did, handle })
|
||||
}
|
||||
|
||||
pub fn create_access_token(&self, user: &AuthenticatedUser, expires_in_minutes: u64) -> AppResult<String> {
|
||||
self.jwt_service.create_token(&user.did, &user.handle, expires_in_minutes)
|
||||
}
|
||||
|
||||
pub fn verify_access_token(&self, token: &str) -> AppResult<Claims> {
|
||||
self.jwt_service.verify_token(token)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct AuthenticatedUser {
|
||||
pub did: String,
|
||||
pub handle: String,
|
||||
}
|
||||
|
||||
/// Extract handle from DID (mock implementation)
|
||||
fn extract_handle_from_did(did: &str) -> String {
|
||||
// In a real implementation, this would resolve the DID to get the handle
|
||||
// For now, use a simple mock
|
||||
did.split(':').last().unwrap_or("unknown").to_string()
|
||||
}
|
||||
|
||||
/// Generate mock PLC identifier
|
||||
fn generate_mock_plc_id() -> String {
|
||||
use uuid::Uuid;
|
||||
Uuid::new_v4().to_string().replace('-', "")[..24].to_string()
|
||||
}
|
131
src/config.rs
Normal file
131
src/config.rs
Normal file
@@ -0,0 +1,131 @@
|
||||
use config::{Config, ConfigError, Environment, File};
|
||||
use serde::Deserialize;
|
||||
use std::path::PathBuf;
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
pub struct Settings {
|
||||
// Application settings
|
||||
pub app_name: String,
|
||||
pub port: u16,
|
||||
pub api_v1_prefix: String,
|
||||
|
||||
// Database settings
|
||||
pub database_url: String,
|
||||
pub database_url_supabase: Option<String>,
|
||||
|
||||
// Authentication
|
||||
pub secret_key: String,
|
||||
pub access_token_expire_minutes: u64,
|
||||
|
||||
// Gacha probabilities (percentages)
|
||||
pub prob_normal: f64,
|
||||
pub prob_rare: f64,
|
||||
pub prob_super_rare: f64,
|
||||
pub prob_kira: f64,
|
||||
pub prob_unique: f64,
|
||||
|
||||
// atproto settings
|
||||
pub atproto_pds_url: Option<String>,
|
||||
pub atproto_handle: Option<String>,
|
||||
|
||||
// External data
|
||||
pub card_master_url: String,
|
||||
|
||||
// File paths
|
||||
pub config_dir: PathBuf,
|
||||
}
|
||||
|
||||
impl Settings {
|
||||
pub fn new() -> Result<Self, ConfigError> {
|
||||
let config_dir = dirs::home_dir()
|
||||
.unwrap_or_else(|| PathBuf::from("."))
|
||||
.join(".config")
|
||||
.join("syui")
|
||||
.join("ai")
|
||||
.join("card");
|
||||
|
||||
// Ensure config directory exists
|
||||
if !config_dir.exists() {
|
||||
std::fs::create_dir_all(&config_dir)
|
||||
.map_err(|e| ConfigError::Message(format!("Failed to create config directory: {}", e)))?;
|
||||
}
|
||||
|
||||
let mut builder = Config::builder()
|
||||
// Default values
|
||||
.set_default("app_name", "ai.card")?
|
||||
.set_default("port", 8000)?
|
||||
.set_default("api_v1_prefix", "/api/v1")?
|
||||
|
||||
// Database defaults
|
||||
.set_default("database_url", format!("sqlite://{}?mode=rwc", config_dir.join("aicard.db").display()))?
|
||||
|
||||
// Authentication defaults
|
||||
.set_default("secret_key", "your-secret-key-change-in-production")?
|
||||
.set_default("access_token_expire_minutes", 1440)? // 24 hours
|
||||
|
||||
// Gacha probability defaults (matching Python implementation)
|
||||
.set_default("prob_normal", 99.789)?
|
||||
.set_default("prob_rare", 0.1)?
|
||||
.set_default("prob_super_rare", 0.01)?
|
||||
.set_default("prob_kira", 0.1)?
|
||||
.set_default("prob_unique", 0.0001)?
|
||||
|
||||
// External data source
|
||||
.set_default("card_master_url", "https://git.syui.ai/ai/ai/raw/branch/main/ai.json")?;
|
||||
|
||||
// Load from config file if it exists (support both .toml and .json)
|
||||
let config_toml = config_dir.join("config.toml");
|
||||
let config_json = config_dir.join("config.json");
|
||||
|
||||
if config_toml.exists() {
|
||||
builder = builder.add_source(File::from(config_toml));
|
||||
} else if config_json.exists() {
|
||||
builder = builder.add_source(File::from(config_json));
|
||||
}
|
||||
|
||||
// Override with environment variables (AI_CARD_ prefix)
|
||||
builder = builder.add_source(Environment::with_prefix("AI_CARD").separator("_"));
|
||||
|
||||
let mut settings: Settings = builder.build()?.try_deserialize()?;
|
||||
|
||||
// Set the config directory path
|
||||
settings.config_dir = config_dir;
|
||||
|
||||
Ok(settings)
|
||||
}
|
||||
|
||||
/// Get the gacha configuration for the gacha service
|
||||
pub fn gacha_config(&self) -> GachaConfig {
|
||||
GachaConfig {
|
||||
prob_normal: self.prob_normal,
|
||||
prob_rare: self.prob_rare,
|
||||
prob_super_rare: self.prob_super_rare,
|
||||
prob_kira: self.prob_kira,
|
||||
prob_unique: self.prob_unique,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct GachaConfig {
|
||||
pub prob_normal: f64,
|
||||
pub prob_rare: f64,
|
||||
pub prob_super_rare: f64,
|
||||
pub prob_kira: f64,
|
||||
pub prob_unique: f64,
|
||||
}
|
||||
|
||||
impl GachaConfig {
|
||||
/// Calculate cumulative probabilities for rarity determination
|
||||
pub fn cumulative_probabilities(&self, is_paid: bool) -> Vec<(f64, crate::models::CardRarity)> {
|
||||
let multiplier = if is_paid { 2.0 } else { 1.0 };
|
||||
|
||||
vec![
|
||||
(self.prob_unique * multiplier, crate::models::CardRarity::Unique),
|
||||
(self.prob_kira * multiplier, crate::models::CardRarity::Kira),
|
||||
(self.prob_super_rare * multiplier, crate::models::CardRarity::SuperRare),
|
||||
(self.prob_rare * multiplier, crate::models::CardRarity::Rare),
|
||||
(self.prob_normal, crate::models::CardRarity::Normal),
|
||||
]
|
||||
}
|
||||
}
|
189
src/database.rs
Normal file
189
src/database.rs
Normal file
@@ -0,0 +1,189 @@
|
||||
use sqlx::{Pool, Postgres, Sqlite};
|
||||
use sqlx::migrate::MigrateDatabase;
|
||||
use crate::error::{AppError, AppResult};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub enum Database {
|
||||
Postgres(Pool<Postgres>),
|
||||
Sqlite(Pool<Sqlite>),
|
||||
}
|
||||
|
||||
impl Database {
|
||||
pub async fn connect(database_url: &str) -> AppResult<Self> {
|
||||
if database_url.starts_with("postgres://") || database_url.starts_with("postgresql://") {
|
||||
let pool = sqlx::postgres::PgPoolOptions::new()
|
||||
.max_connections(10)
|
||||
.connect(database_url)
|
||||
.await
|
||||
.map_err(AppError::Database)?;
|
||||
Ok(Database::Postgres(pool))
|
||||
} else if database_url.starts_with("sqlite://") {
|
||||
// Extract the path from sqlite:// URL
|
||||
let _db_path = database_url.trim_start_matches("sqlite://");
|
||||
|
||||
// Create the database file if it doesn't exist
|
||||
if !Sqlite::database_exists(database_url).await.unwrap_or(false) {
|
||||
Sqlite::create_database(database_url)
|
||||
.await
|
||||
.map_err(AppError::Database)?;
|
||||
}
|
||||
|
||||
let pool = sqlx::sqlite::SqlitePoolOptions::new()
|
||||
.max_connections(5)
|
||||
.connect(database_url)
|
||||
.await
|
||||
.map_err(AppError::Database)?;
|
||||
|
||||
Ok(Database::Sqlite(pool))
|
||||
} else {
|
||||
Err(AppError::Configuration(format!(
|
||||
"Unsupported database URL: {}",
|
||||
database_url
|
||||
)))
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn migrate(&self) -> AppResult<()> {
|
||||
match self {
|
||||
Database::Postgres(pool) => {
|
||||
sqlx::migrate!("./migrations/postgres")
|
||||
.run(pool)
|
||||
.await
|
||||
.map_err(AppError::Migration)?;
|
||||
}
|
||||
Database::Sqlite(pool) => {
|
||||
sqlx::migrate!("./migrations/sqlite")
|
||||
.run(pool)
|
||||
.await
|
||||
.map_err(AppError::Migration)?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
/// Get a generic connection for complex operations
|
||||
pub async fn acquire(&self) -> AppResult<DatabaseConnection> {
|
||||
match self {
|
||||
Database::Postgres(pool) => {
|
||||
let conn = pool.acquire().await.map_err(AppError::Database)?;
|
||||
Ok(DatabaseConnection::Postgres(conn))
|
||||
}
|
||||
Database::Sqlite(pool) => {
|
||||
let conn = pool.acquire().await.map_err(AppError::Database)?;
|
||||
Ok(DatabaseConnection::Sqlite(conn))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Begin a transaction
|
||||
pub async fn begin(&self) -> AppResult<DatabaseTransaction> {
|
||||
match self {
|
||||
Database::Postgres(pool) => {
|
||||
let tx = pool.begin().await.map_err(AppError::Database)?;
|
||||
Ok(DatabaseTransaction::Postgres(tx))
|
||||
}
|
||||
Database::Sqlite(pool) => {
|
||||
let tx = pool.begin().await.map_err(AppError::Database)?;
|
||||
Ok(DatabaseTransaction::Sqlite(tx))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub enum DatabaseConnection {
|
||||
Postgres(sqlx::pool::PoolConnection<Postgres>),
|
||||
Sqlite(sqlx::pool::PoolConnection<Sqlite>),
|
||||
}
|
||||
|
||||
pub enum DatabaseTransaction {
|
||||
Postgres(sqlx::Transaction<'static, Postgres>),
|
||||
Sqlite(sqlx::Transaction<'static, Sqlite>),
|
||||
}
|
||||
|
||||
impl DatabaseTransaction {
|
||||
pub async fn commit(self) -> AppResult<()> {
|
||||
match self {
|
||||
DatabaseTransaction::Postgres(tx) => {
|
||||
tx.commit().await.map_err(AppError::Database)?;
|
||||
}
|
||||
DatabaseTransaction::Sqlite(tx) => {
|
||||
tx.commit().await.map_err(AppError::Database)?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn rollback(self) -> AppResult<()> {
|
||||
match self {
|
||||
DatabaseTransaction::Postgres(tx) => {
|
||||
tx.rollback().await.map_err(AppError::Database)?;
|
||||
}
|
||||
DatabaseTransaction::Sqlite(tx) => {
|
||||
tx.rollback().await.map_err(AppError::Database)?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
// Macros for database-agnostic queries
|
||||
#[macro_export]
|
||||
macro_rules! query_as {
|
||||
($struct:ty, $query:expr, $db:expr) => {
|
||||
match $db {
|
||||
Database::Postgres(pool) => {
|
||||
sqlx::query_as::<_, $struct>($query)
|
||||
.fetch_all(pool)
|
||||
.await
|
||||
.map_err(AppError::Database)
|
||||
}
|
||||
Database::Sqlite(pool) => {
|
||||
sqlx::query_as::<_, $struct>($query)
|
||||
.fetch_all(pool)
|
||||
.await
|
||||
.map_err(AppError::Database)
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! query_one_as {
|
||||
($struct:ty, $query:expr, $db:expr) => {
|
||||
match $db {
|
||||
Database::Postgres(pool) => {
|
||||
sqlx::query_as::<_, $struct>($query)
|
||||
.fetch_one(pool)
|
||||
.await
|
||||
.map_err(AppError::Database)
|
||||
}
|
||||
Database::Sqlite(pool) => {
|
||||
sqlx::query_as::<_, $struct>($query)
|
||||
.fetch_one(pool)
|
||||
.await
|
||||
.map_err(AppError::Database)
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! query_optional_as {
|
||||
($struct:ty, $query:expr, $db:expr) => {
|
||||
match $db {
|
||||
Database::Postgres(pool) => {
|
||||
sqlx::query_as::<_, $struct>($query)
|
||||
.fetch_optional(pool)
|
||||
.await
|
||||
.map_err(AppError::Database)
|
||||
}
|
||||
Database::Sqlite(pool) => {
|
||||
sqlx::query_as::<_, $struct>($query)
|
||||
.fetch_optional(pool)
|
||||
.await
|
||||
.map_err(AppError::Database)
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
142
src/error.rs
Normal file
142
src/error.rs
Normal file
@@ -0,0 +1,142 @@
|
||||
use axum::{
|
||||
http::StatusCode,
|
||||
response::{IntoResponse, Response},
|
||||
Json,
|
||||
};
|
||||
use serde_json::json;
|
||||
use thiserror::Error;
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
pub enum AppError {
|
||||
#[error("Database error: {0}")]
|
||||
Database(#[from] sqlx::Error),
|
||||
|
||||
#[error("Migration error: {0}")]
|
||||
Migration(#[from] sqlx::migrate::MigrateError),
|
||||
|
||||
#[error("Validation error: {0}")]
|
||||
Validation(String),
|
||||
|
||||
#[error("Authentication error: {0}")]
|
||||
Authentication(String),
|
||||
|
||||
#[error("Authorization error: {0}")]
|
||||
Authorization(String),
|
||||
|
||||
#[error("Not found: {0}")]
|
||||
NotFound(String),
|
||||
|
||||
#[error("Conflict: {0}")]
|
||||
Conflict(String),
|
||||
|
||||
#[error("External service error: {0}")]
|
||||
ExternalService(String),
|
||||
|
||||
#[error("Configuration error: {0}")]
|
||||
Configuration(String),
|
||||
|
||||
#[error("JSON serialization error: {0}")]
|
||||
Json(#[from] serde_json::Error),
|
||||
|
||||
#[error("HTTP client error: {0}")]
|
||||
HttpClient(#[from] reqwest::Error),
|
||||
|
||||
#[error("JWT error: {0}")]
|
||||
Jwt(#[from] jsonwebtoken::errors::Error),
|
||||
|
||||
#[error("Internal server error: {0}")]
|
||||
Internal(String),
|
||||
}
|
||||
|
||||
impl IntoResponse for AppError {
|
||||
fn into_response(self) -> Response {
|
||||
let (status, error_message, error_code) = match &self {
|
||||
AppError::Database(e) => {
|
||||
tracing::error!("Database error: {}", e);
|
||||
(StatusCode::INTERNAL_SERVER_ERROR, "Database error", "DATABASE_ERROR")
|
||||
}
|
||||
AppError::Migration(e) => {
|
||||
tracing::error!("Migration error: {}", e);
|
||||
(StatusCode::INTERNAL_SERVER_ERROR, "Migration error", "MIGRATION_ERROR")
|
||||
}
|
||||
AppError::Validation(msg) => {
|
||||
(StatusCode::BAD_REQUEST, msg.as_str(), "VALIDATION_ERROR")
|
||||
}
|
||||
AppError::Authentication(msg) => {
|
||||
(StatusCode::UNAUTHORIZED, msg.as_str(), "AUTHENTICATION_ERROR")
|
||||
}
|
||||
AppError::Authorization(msg) => {
|
||||
(StatusCode::FORBIDDEN, msg.as_str(), "AUTHORIZATION_ERROR")
|
||||
}
|
||||
AppError::NotFound(msg) => {
|
||||
(StatusCode::NOT_FOUND, msg.as_str(), "NOT_FOUND")
|
||||
}
|
||||
AppError::Conflict(msg) => {
|
||||
(StatusCode::CONFLICT, msg.as_str(), "CONFLICT")
|
||||
}
|
||||
AppError::ExternalService(msg) => {
|
||||
tracing::error!("External service error: {}", msg);
|
||||
(StatusCode::BAD_GATEWAY, "External service unavailable", "EXTERNAL_SERVICE_ERROR")
|
||||
}
|
||||
AppError::Configuration(msg) => {
|
||||
tracing::error!("Configuration error: {}", msg);
|
||||
(StatusCode::INTERNAL_SERVER_ERROR, "Configuration error", "CONFIGURATION_ERROR")
|
||||
}
|
||||
AppError::Json(e) => {
|
||||
tracing::error!("JSON error: {}", e);
|
||||
(StatusCode::BAD_REQUEST, "Invalid JSON", "JSON_ERROR")
|
||||
}
|
||||
AppError::HttpClient(e) => {
|
||||
tracing::error!("HTTP client error: {}", e);
|
||||
(StatusCode::BAD_GATEWAY, "External service error", "HTTP_CLIENT_ERROR")
|
||||
}
|
||||
AppError::Jwt(e) => {
|
||||
tracing::error!("JWT error: {}", e);
|
||||
(StatusCode::UNAUTHORIZED, "Invalid token", "JWT_ERROR")
|
||||
}
|
||||
AppError::Internal(msg) => {
|
||||
tracing::error!("Internal error: {}", msg);
|
||||
(StatusCode::INTERNAL_SERVER_ERROR, "Internal server error", "INTERNAL_ERROR")
|
||||
}
|
||||
};
|
||||
|
||||
let body = Json(json!({
|
||||
"error": {
|
||||
"code": error_code,
|
||||
"message": error_message,
|
||||
"timestamp": chrono::Utc::now().to_rfc3339()
|
||||
}
|
||||
}));
|
||||
|
||||
(status, body).into_response()
|
||||
}
|
||||
}
|
||||
|
||||
// Convenience methods for common errors
|
||||
impl AppError {
|
||||
pub fn validation<T: Into<String>>(msg: T) -> Self {
|
||||
Self::Validation(msg.into())
|
||||
}
|
||||
|
||||
pub fn authentication<T: Into<String>>(msg: T) -> Self {
|
||||
Self::Authentication(msg.into())
|
||||
}
|
||||
|
||||
pub fn authorization<T: Into<String>>(msg: T) -> Self {
|
||||
Self::Authorization(msg.into())
|
||||
}
|
||||
|
||||
pub fn not_found<T: Into<String>>(msg: T) -> Self {
|
||||
Self::NotFound(msg.into())
|
||||
}
|
||||
|
||||
pub fn conflict<T: Into<String>>(msg: T) -> Self {
|
||||
Self::Conflict(msg.into())
|
||||
}
|
||||
|
||||
pub fn internal<T: Into<String>>(msg: T) -> Self {
|
||||
Self::Internal(msg.into())
|
||||
}
|
||||
}
|
||||
|
||||
pub type AppResult<T> = Result<T, AppError>;
|
161
src/handlers/auth.rs
Normal file
161
src/handlers/auth.rs
Normal file
@@ -0,0 +1,161 @@
|
||||
use axum::{
|
||||
extract::State,
|
||||
response::Json,
|
||||
routing::post,
|
||||
Router,
|
||||
};
|
||||
use validator::Validate;
|
||||
|
||||
use crate::{
|
||||
auth::AtprotoAuthService,
|
||||
error::{AppError, AppResult},
|
||||
models::*,
|
||||
AppState,
|
||||
};
|
||||
|
||||
pub fn create_routes() -> Router<AppState> {
|
||||
Router::new()
|
||||
.route("/login", post(login))
|
||||
.route("/verify", post(verify_token))
|
||||
}
|
||||
|
||||
/// Authenticate user with atproto credentials
|
||||
async fn login(
|
||||
State(state): State<AppState>,
|
||||
Json(request): Json<LoginRequest>,
|
||||
) -> AppResult<Json<LoginResponse>> {
|
||||
// Validate request
|
||||
request.validate().map_err(|e| AppError::validation(e.to_string()))?;
|
||||
|
||||
// Create auth service
|
||||
let auth_service = AtprotoAuthService::new(&state.settings.secret_key);
|
||||
|
||||
// Authenticate user
|
||||
let user = auth_service
|
||||
.authenticate(&request.identifier, &request.password)
|
||||
.await?;
|
||||
|
||||
// Create access token
|
||||
let access_token = auth_service
|
||||
.create_access_token(&user, state.settings.access_token_expire_minutes)?;
|
||||
|
||||
// Create or update user in database
|
||||
let _db_user = create_or_update_user(&state, &user.did, &user.handle).await?;
|
||||
|
||||
Ok(Json(LoginResponse {
|
||||
access_token,
|
||||
token_type: "Bearer".to_string(),
|
||||
expires_in: state.settings.access_token_expire_minutes * 60, // Convert to seconds
|
||||
user: UserInfo {
|
||||
did: user.did,
|
||||
handle: user.handle,
|
||||
},
|
||||
}))
|
||||
}
|
||||
|
||||
/// Verify JWT token
|
||||
async fn verify_token(
|
||||
State(state): State<AppState>,
|
||||
Json(token): Json<serde_json::Value>,
|
||||
) -> AppResult<Json<serde_json::Value>> {
|
||||
let token_str = token["token"]
|
||||
.as_str()
|
||||
.ok_or_else(|| AppError::validation("Token is required"))?;
|
||||
|
||||
let auth_service = AtprotoAuthService::new(&state.settings.secret_key);
|
||||
let claims = auth_service.verify_access_token(token_str)?;
|
||||
|
||||
Ok(Json(serde_json::json!({
|
||||
"valid": true,
|
||||
"did": claims.did,
|
||||
"handle": claims.handle,
|
||||
"exp": claims.exp
|
||||
})))
|
||||
}
|
||||
|
||||
/// Create or update user in database
|
||||
async fn create_or_update_user(
|
||||
state: &AppState,
|
||||
did: &str,
|
||||
handle: &str,
|
||||
) -> AppResult<User> {
|
||||
let now = chrono::Utc::now();
|
||||
|
||||
// Try to get existing user
|
||||
let existing_user = match &state.db {
|
||||
crate::database::Database::Postgres(pool) => {
|
||||
sqlx::query_as::<_, User>("SELECT * FROM users WHERE did = $1")
|
||||
.bind(did)
|
||||
.fetch_optional(pool)
|
||||
.await
|
||||
.map_err(AppError::Database)?
|
||||
}
|
||||
crate::database::Database::Sqlite(pool) => {
|
||||
sqlx::query_as::<_, User>("SELECT * FROM users WHERE did = ?")
|
||||
.bind(did)
|
||||
.fetch_optional(pool)
|
||||
.await
|
||||
.map_err(AppError::Database)?
|
||||
}
|
||||
};
|
||||
|
||||
if let Some(mut user) = existing_user {
|
||||
// Update handle if changed
|
||||
if user.handle != handle {
|
||||
user = match &state.db {
|
||||
crate::database::Database::Postgres(pool) => {
|
||||
sqlx::query_as::<_, User>(
|
||||
"UPDATE users SET handle = $1, updated_at = $2 WHERE did = $3 RETURNING *"
|
||||
)
|
||||
.bind(handle)
|
||||
.bind(now)
|
||||
.bind(did)
|
||||
.fetch_one(pool)
|
||||
.await
|
||||
.map_err(AppError::Database)?
|
||||
}
|
||||
crate::database::Database::Sqlite(pool) => {
|
||||
sqlx::query_as::<_, User>(
|
||||
"UPDATE users SET handle = ?, updated_at = ? WHERE did = ? RETURNING *"
|
||||
)
|
||||
.bind(handle)
|
||||
.bind(now)
|
||||
.bind(did)
|
||||
.fetch_one(pool)
|
||||
.await
|
||||
.map_err(AppError::Database)?
|
||||
}
|
||||
};
|
||||
}
|
||||
Ok(user)
|
||||
} else {
|
||||
// Create new user
|
||||
let user = match &state.db {
|
||||
crate::database::Database::Postgres(pool) => {
|
||||
sqlx::query_as::<_, User>(
|
||||
"INSERT INTO users (did, handle, created_at, updated_at) VALUES ($1, $2, $3, $4) RETURNING *"
|
||||
)
|
||||
.bind(did)
|
||||
.bind(handle)
|
||||
.bind(now)
|
||||
.bind(now)
|
||||
.fetch_one(pool)
|
||||
.await
|
||||
.map_err(AppError::Database)?
|
||||
}
|
||||
crate::database::Database::Sqlite(pool) => {
|
||||
sqlx::query_as::<_, User>(
|
||||
"INSERT INTO users (did, handle, created_at, updated_at) VALUES (?, ?, ?, ?) RETURNING *"
|
||||
)
|
||||
.bind(did)
|
||||
.bind(handle)
|
||||
.bind(now)
|
||||
.bind(now)
|
||||
.fetch_one(pool)
|
||||
.await
|
||||
.map_err(AppError::Database)?
|
||||
}
|
||||
};
|
||||
Ok(user)
|
||||
}
|
||||
}
|
314
src/handlers/cards.rs
Normal file
314
src/handlers/cards.rs
Normal file
@@ -0,0 +1,314 @@
|
||||
use axum::{
|
||||
extract::{Path, Query, State},
|
||||
response::Json,
|
||||
routing::{get, post},
|
||||
Router,
|
||||
};
|
||||
use serde::Deserialize;
|
||||
use validator::Validate;
|
||||
|
||||
use crate::{
|
||||
error::{AppError, AppResult},
|
||||
models::*,
|
||||
services::GachaService,
|
||||
AppState,
|
||||
};
|
||||
|
||||
pub fn create_routes() -> Router<AppState> {
|
||||
Router::new()
|
||||
.route("/draw", post(draw_card))
|
||||
.route("/user/:user_did", get(get_user_cards))
|
||||
.route("/unique", get(get_unique_registry))
|
||||
.route("/stats", get(get_gacha_stats))
|
||||
.route("/master", get(get_card_master))
|
||||
}
|
||||
|
||||
/// Draw a card from gacha system
|
||||
async fn draw_card(
|
||||
State(state): State<AppState>,
|
||||
Json(request): Json<CardDrawRequest>,
|
||||
) -> AppResult<Json<CardDrawResponse>> {
|
||||
// Validate request
|
||||
request.validate().map_err(|e| AppError::validation(e.to_string()))?;
|
||||
|
||||
let gacha_service = GachaService::new(state.settings.gacha_config());
|
||||
|
||||
let result = gacha_service
|
||||
.draw_card(&state.db, &request.user_did, request.is_paid, request.pool_id)
|
||||
.await?;
|
||||
|
||||
Ok(Json(result))
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct UserCardsQuery {
|
||||
limit: Option<i32>,
|
||||
offset: Option<i32>,
|
||||
}
|
||||
|
||||
/// Get user's card collection
|
||||
async fn get_user_cards(
|
||||
State(state): State<AppState>,
|
||||
Path(user_did): Path<String>,
|
||||
Query(query): Query<UserCardsQuery>,
|
||||
) -> AppResult<Json<UserCardCollectionResponse>> {
|
||||
let limit = query.limit.unwrap_or(50).min(100); // Max 100 cards per request
|
||||
let offset = query.offset.unwrap_or(0);
|
||||
|
||||
// Get user ID from DID
|
||||
let user = match &state.db {
|
||||
crate::database::Database::Postgres(pool) => {
|
||||
sqlx::query_as::<_, User>("SELECT * FROM users WHERE did = $1")
|
||||
.bind(&user_did)
|
||||
.fetch_optional(pool)
|
||||
.await
|
||||
.map_err(AppError::Database)?
|
||||
}
|
||||
crate::database::Database::Sqlite(pool) => {
|
||||
sqlx::query_as::<_, User>("SELECT * FROM users WHERE did = ?")
|
||||
.bind(&user_did)
|
||||
.fetch_optional(pool)
|
||||
.await
|
||||
.map_err(AppError::Database)?
|
||||
}
|
||||
};
|
||||
|
||||
let user = user.ok_or_else(|| AppError::not_found("User not found"))?;
|
||||
|
||||
// Get user's cards with master data
|
||||
let cards_with_master = match &state.db {
|
||||
crate::database::Database::Postgres(pool) => {
|
||||
sqlx::query_as::<_, UserCardWithMasterQuery>(
|
||||
r#"
|
||||
SELECT
|
||||
uc.id, uc.user_id, uc.card_id, uc.cp, uc.status,
|
||||
uc.obtained_at, uc.is_unique, uc.unique_id,
|
||||
cm.id as master_id, cm.name, cm.base_cp_min, cm.base_cp_max, cm.color, cm.description
|
||||
FROM user_cards uc
|
||||
JOIN card_master cm ON uc.card_id = cm.id
|
||||
WHERE uc.user_id = $1
|
||||
ORDER BY uc.obtained_at DESC
|
||||
LIMIT $2 OFFSET $3
|
||||
"#
|
||||
)
|
||||
.bind(user.id)
|
||||
.bind(limit as i64)
|
||||
.bind(offset as i64)
|
||||
.fetch_all(pool)
|
||||
.await
|
||||
.map_err(AppError::Database)?
|
||||
}
|
||||
crate::database::Database::Sqlite(pool) => {
|
||||
sqlx::query_as::<_, UserCardWithMasterQuery>(
|
||||
r#"
|
||||
SELECT
|
||||
uc.id, uc.user_id, uc.card_id, uc.cp, uc.status,
|
||||
uc.obtained_at, uc.is_unique, uc.unique_id,
|
||||
cm.id as master_id, cm.name, cm.base_cp_min, cm.base_cp_max, cm.color, cm.description
|
||||
FROM user_cards uc
|
||||
JOIN card_master cm ON uc.card_id = cm.id
|
||||
WHERE uc.user_id = ?
|
||||
ORDER BY uc.obtained_at DESC
|
||||
LIMIT ? OFFSET ?
|
||||
"#
|
||||
)
|
||||
.bind(user.id)
|
||||
.bind(limit as i32)
|
||||
.bind(offset as i32)
|
||||
.fetch_all(pool)
|
||||
.await
|
||||
.map_err(AppError::Database)?
|
||||
}
|
||||
};
|
||||
|
||||
let mut cards = Vec::new();
|
||||
let mut rarity_breakdown = RarityBreakdown {
|
||||
normal: 0,
|
||||
rare: 0,
|
||||
super_rare: 0,
|
||||
kira: 0,
|
||||
unique: 0,
|
||||
};
|
||||
|
||||
for row in cards_with_master {
|
||||
let status = match row.status.as_str() {
|
||||
"normal" => CardRarity::Normal,
|
||||
"rare" => CardRarity::Rare,
|
||||
"super_rare" => CardRarity::SuperRare,
|
||||
"kira" => CardRarity::Kira,
|
||||
"unique" => CardRarity::Unique,
|
||||
_ => CardRarity::Normal,
|
||||
};
|
||||
|
||||
// Update rarity breakdown
|
||||
match status {
|
||||
CardRarity::Normal => rarity_breakdown.normal += 1,
|
||||
CardRarity::Rare => rarity_breakdown.rare += 1,
|
||||
CardRarity::SuperRare => rarity_breakdown.super_rare += 1,
|
||||
CardRarity::Kira => rarity_breakdown.kira += 1,
|
||||
CardRarity::Unique => rarity_breakdown.unique += 1,
|
||||
}
|
||||
|
||||
cards.push(UserCardWithMaster {
|
||||
card: UserCardResponse {
|
||||
id: row.id,
|
||||
card_id: row.card_id,
|
||||
cp: row.cp,
|
||||
status,
|
||||
skill: None, // TODO: Add skill field to query if needed
|
||||
obtained_at: row.obtained_at,
|
||||
is_unique: row.is_unique,
|
||||
unique_id: row.unique_id,
|
||||
},
|
||||
master: CardMasterResponse {
|
||||
id: row.master_id,
|
||||
name: row.name,
|
||||
base_cp_min: row.base_cp_min,
|
||||
base_cp_max: row.base_cp_max,
|
||||
color: row.color,
|
||||
description: row.description,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
// Get total count and unique count
|
||||
let (total_count, unique_count): (i64, i64) = match &state.db {
|
||||
crate::database::Database::Postgres(pool) => {
|
||||
sqlx::query_as(
|
||||
"SELECT COUNT(*) as total, COUNT(*) FILTER (WHERE is_unique = true) as unique_count FROM user_cards WHERE user_id = $1"
|
||||
)
|
||||
.bind(user.id)
|
||||
.fetch_one(pool)
|
||||
.await
|
||||
.map_err(AppError::Database)?
|
||||
}
|
||||
crate::database::Database::Sqlite(pool) => {
|
||||
sqlx::query_as(
|
||||
"SELECT COUNT(*) as total, SUM(CASE WHEN is_unique = 1 THEN 1 ELSE 0 END) as unique_count FROM user_cards WHERE user_id = ?"
|
||||
)
|
||||
.bind(user.id)
|
||||
.fetch_one(pool)
|
||||
.await
|
||||
.map_err(AppError::Database)?
|
||||
}
|
||||
};
|
||||
|
||||
Ok(Json(UserCardCollectionResponse {
|
||||
user_did,
|
||||
cards,
|
||||
total_count: total_count as i32,
|
||||
unique_count: unique_count as i32,
|
||||
rarity_breakdown,
|
||||
}))
|
||||
}
|
||||
|
||||
/// Get global unique card registry
|
||||
async fn get_unique_registry(
|
||||
State(state): State<AppState>,
|
||||
) -> AppResult<Json<UniqueCardRegistryResponse>> {
|
||||
// Get all unique cards with master data and owner info
|
||||
let unique_cards = match &state.db {
|
||||
crate::database::Database::Postgres(pool) => {
|
||||
sqlx::query_as::<_, UniqueCardQuery>(
|
||||
r#"
|
||||
SELECT
|
||||
cm.id as card_id,
|
||||
cm.name as card_name,
|
||||
ucr.owner_did,
|
||||
u.handle as owner_handle,
|
||||
ucr.obtained_at
|
||||
FROM card_master cm
|
||||
LEFT JOIN unique_card_registry ucr ON cm.id = ucr.card_id
|
||||
LEFT JOIN users u ON ucr.owner_did = u.did
|
||||
ORDER BY cm.id
|
||||
"#
|
||||
)
|
||||
.fetch_all(pool)
|
||||
.await
|
||||
.map_err(AppError::Database)?
|
||||
}
|
||||
crate::database::Database::Sqlite(pool) => {
|
||||
sqlx::query_as::<_, UniqueCardQuery>(
|
||||
r#"
|
||||
SELECT
|
||||
cm.id as card_id,
|
||||
cm.name as card_name,
|
||||
ucr.owner_did,
|
||||
u.handle as owner_handle,
|
||||
ucr.obtained_at
|
||||
FROM card_master cm
|
||||
LEFT JOIN unique_card_registry ucr ON cm.id = ucr.card_id
|
||||
LEFT JOIN users u ON ucr.owner_did = u.did
|
||||
ORDER BY cm.id
|
||||
"#
|
||||
)
|
||||
.fetch_all(pool)
|
||||
.await
|
||||
.map_err(AppError::Database)?
|
||||
}
|
||||
};
|
||||
|
||||
let mut unique_card_infos = Vec::new();
|
||||
let mut available_count = 0;
|
||||
|
||||
for row in unique_cards {
|
||||
let is_available = row.owner_did.is_none();
|
||||
if is_available {
|
||||
available_count += 1;
|
||||
}
|
||||
|
||||
unique_card_infos.push(UniqueCardInfo {
|
||||
card_id: row.card_id,
|
||||
card_name: row.card_name,
|
||||
owner_did: row.owner_did,
|
||||
owner_handle: row.owner_handle,
|
||||
obtained_at: row.obtained_at,
|
||||
is_available,
|
||||
});
|
||||
}
|
||||
|
||||
Ok(Json(UniqueCardRegistryResponse {
|
||||
unique_cards: unique_card_infos,
|
||||
total_unique_cards: 16, // Total number of card types
|
||||
available_unique_cards: available_count,
|
||||
}))
|
||||
}
|
||||
|
||||
/// Get gacha statistics
|
||||
async fn get_gacha_stats(State(state): State<AppState>) -> AppResult<Json<GachaStatsResponse>> {
|
||||
let gacha_service = GachaService::new(state.settings.gacha_config());
|
||||
let stats = gacha_service.get_gacha_stats(&state.db).await?;
|
||||
Ok(Json(stats))
|
||||
}
|
||||
|
||||
/// Get card master data
|
||||
async fn get_card_master(State(state): State<AppState>) -> AppResult<Json<Vec<CardMasterResponse>>> {
|
||||
let cards = match &state.db {
|
||||
crate::database::Database::Postgres(pool) => {
|
||||
sqlx::query_as::<_, CardMaster>("SELECT * FROM card_master ORDER BY id")
|
||||
.fetch_all(pool)
|
||||
.await
|
||||
.map_err(AppError::Database)?
|
||||
}
|
||||
crate::database::Database::Sqlite(pool) => {
|
||||
sqlx::query_as::<_, CardMaster>("SELECT * FROM card_master ORDER BY id")
|
||||
.fetch_all(pool)
|
||||
.await
|
||||
.map_err(AppError::Database)?
|
||||
}
|
||||
};
|
||||
|
||||
let card_responses: Vec<CardMasterResponse> = cards
|
||||
.into_iter()
|
||||
.map(|card| CardMasterResponse {
|
||||
id: card.id,
|
||||
name: card.name,
|
||||
base_cp_min: card.base_cp_min,
|
||||
base_cp_max: card.base_cp_max,
|
||||
color: card.color,
|
||||
description: card.description,
|
||||
})
|
||||
.collect();
|
||||
|
||||
Ok(Json(card_responses))
|
||||
}
|
3
src/handlers/mod.rs
Normal file
3
src/handlers/mod.rs
Normal file
@@ -0,0 +1,3 @@
|
||||
pub mod auth;
|
||||
pub mod cards;
|
||||
pub mod sync;
|
68
src/handlers/sync.rs
Normal file
68
src/handlers/sync.rs
Normal file
@@ -0,0 +1,68 @@
|
||||
use axum::{
|
||||
extract::State,
|
||||
response::Json,
|
||||
routing::post,
|
||||
Router,
|
||||
};
|
||||
|
||||
use crate::{
|
||||
error::AppResult,
|
||||
AppState,
|
||||
};
|
||||
|
||||
pub fn create_routes() -> Router<AppState> {
|
||||
Router::new()
|
||||
.route("/cards/export", post(export_cards))
|
||||
.route("/cards/import", post(import_cards))
|
||||
.route("/cards/bidirectional", post(bidirectional_sync))
|
||||
}
|
||||
|
||||
/// Export user's cards to atproto PDS
|
||||
async fn export_cards(State(_state): State<AppState>) -> AppResult<Json<serde_json::Value>> {
|
||||
// TODO: Implement atproto PDS export
|
||||
// This would:
|
||||
// 1. Get user's cards from database
|
||||
// 2. Format as atproto records
|
||||
// 3. Upload to user's PDS
|
||||
|
||||
Ok(Json(serde_json::json!({
|
||||
"status": "success",
|
||||
"message": "Card export to PDS completed",
|
||||
"exported_count": 0,
|
||||
"note": "atproto integration not yet implemented"
|
||||
})))
|
||||
}
|
||||
|
||||
/// Import user's cards from atproto PDS
|
||||
async fn import_cards(State(_state): State<AppState>) -> AppResult<Json<serde_json::Value>> {
|
||||
// TODO: Implement atproto PDS import
|
||||
// This would:
|
||||
// 1. Fetch card records from user's PDS
|
||||
// 2. Validate and parse records
|
||||
// 3. Update local database
|
||||
|
||||
Ok(Json(serde_json::json!({
|
||||
"status": "success",
|
||||
"message": "Card import from PDS completed",
|
||||
"imported_count": 0,
|
||||
"note": "atproto integration not yet implemented"
|
||||
})))
|
||||
}
|
||||
|
||||
/// Bidirectional synchronization between local DB and PDS
|
||||
async fn bidirectional_sync(State(_state): State<AppState>) -> AppResult<Json<serde_json::Value>> {
|
||||
// TODO: Implement bidirectional sync
|
||||
// This would:
|
||||
// 1. Compare local cards with PDS records
|
||||
// 2. Resolve conflicts (newest wins, etc.)
|
||||
// 3. Sync in both directions
|
||||
|
||||
Ok(Json(serde_json::json!({
|
||||
"status": "success",
|
||||
"message": "Bidirectional sync completed",
|
||||
"local_to_pds": 0,
|
||||
"pds_to_local": 0,
|
||||
"conflicts_resolved": 0,
|
||||
"note": "atproto integration not yet implemented"
|
||||
})))
|
||||
}
|
149
src/main.rs
Normal file
149
src/main.rs
Normal file
@@ -0,0 +1,149 @@
|
||||
use anyhow::Result;
|
||||
use axum::{
|
||||
response::Json,
|
||||
routing::get,
|
||||
Router,
|
||||
};
|
||||
use serde_json::{json, Value};
|
||||
use std::net::SocketAddr;
|
||||
use tower_http::cors::CorsLayer;
|
||||
use tracing::info;
|
||||
|
||||
mod config;
|
||||
mod database;
|
||||
mod models;
|
||||
mod handlers;
|
||||
mod services;
|
||||
mod auth;
|
||||
mod error;
|
||||
|
||||
use config::Settings;
|
||||
use database::Database;
|
||||
use error::AppError;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct AppState {
|
||||
pub db: Database,
|
||||
pub settings: Settings,
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<()> {
|
||||
// Initialize tracing with debug level
|
||||
tracing_subscriber::fmt()
|
||||
.with_env_filter("debug")
|
||||
.init();
|
||||
|
||||
println!("🎴 ai.card API Server Starting...");
|
||||
println!("===================================");
|
||||
|
||||
// Load configuration
|
||||
println!("📁 Loading configuration...");
|
||||
let settings = Settings::new()
|
||||
.map_err(|e| {
|
||||
eprintln!("❌ Failed to load configuration: {}", e);
|
||||
anyhow::anyhow!("Failed to load configuration: {}", e)
|
||||
})?;
|
||||
|
||||
println!("✅ Configuration loaded successfully");
|
||||
println!("📍 Config directory: {}", settings.config_dir.display());
|
||||
println!("🌐 Port: {}", settings.port);
|
||||
println!("🗄️ Database URL: {}", settings.database_url);
|
||||
|
||||
info!("Starting ai.card API server v{}", env!("CARGO_PKG_VERSION"));
|
||||
|
||||
// Initialize database
|
||||
println!("🔗 Connecting to database...");
|
||||
let database = Database::connect(&settings.database_url).await
|
||||
.map_err(|e| {
|
||||
eprintln!("❌ Database connection failed: {}", e);
|
||||
e
|
||||
})?;
|
||||
println!("✅ Database connected successfully");
|
||||
|
||||
// Run migrations
|
||||
println!("🔄 Running database migrations...");
|
||||
database.migrate().await
|
||||
.map_err(|e| {
|
||||
eprintln!("❌ Database migration failed: {}", e);
|
||||
e
|
||||
})?;
|
||||
println!("✅ Database migrations completed");
|
||||
|
||||
let app_state = AppState {
|
||||
db: database,
|
||||
settings: settings.clone(),
|
||||
};
|
||||
|
||||
// Build application routes
|
||||
println!("🛣️ Setting up routes...");
|
||||
let app = create_app(app_state).await;
|
||||
println!("✅ Routes configured");
|
||||
|
||||
// Start server
|
||||
let addr = SocketAddr::from(([0, 0, 0, 0], settings.port));
|
||||
println!("🚀 Starting server on {}", addr);
|
||||
println!("🔗 Health check: http://localhost:{}/health", settings.port);
|
||||
println!("📡 API endpoints: http://localhost:{}/api/v1", settings.port);
|
||||
println!("🎮 Card endpoints:");
|
||||
println!(" - POST /api/v1/cards/draw - Draw card");
|
||||
println!(" - GET /api/v1/cards/user/{{did}} - Get user cards");
|
||||
println!(" - GET /api/v1/cards/unique - Get unique registry");
|
||||
println!(" - GET /api/v1/cards/stats - Get gacha stats");
|
||||
println!(" - GET /api/v1/cards/master - Get card master");
|
||||
println!("===================================");
|
||||
|
||||
info!("ai.card API server listening on {}", addr);
|
||||
|
||||
let listener = tokio::net::TcpListener::bind(addr).await
|
||||
.map_err(|e| {
|
||||
eprintln!("❌ Failed to bind to address {}: {}", addr, e);
|
||||
e
|
||||
})?;
|
||||
|
||||
println!("🎉 Server started successfully! Press Ctrl+C to stop.");
|
||||
|
||||
axum::serve(listener, app).await
|
||||
.map_err(|e| {
|
||||
eprintln!("❌ Server error: {}", e);
|
||||
e
|
||||
})?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn create_app(state: AppState) -> Router {
|
||||
Router::new()
|
||||
// Health check
|
||||
.route("/health", get(health_check))
|
||||
|
||||
// API v1 routes
|
||||
.nest("/api/v1", create_api_routes())
|
||||
|
||||
// CORS middleware
|
||||
.layer(CorsLayer::permissive())
|
||||
|
||||
// Application state
|
||||
.with_state(state)
|
||||
}
|
||||
|
||||
fn create_api_routes() -> Router<AppState> {
|
||||
Router::new()
|
||||
// Authentication routes
|
||||
.nest("/auth", handlers::auth::create_routes())
|
||||
|
||||
// Card routes
|
||||
.nest("/cards", handlers::cards::create_routes())
|
||||
|
||||
// Sync routes
|
||||
.nest("/sync", handlers::sync::create_routes())
|
||||
}
|
||||
|
||||
async fn health_check() -> Result<Json<Value>, AppError> {
|
||||
Ok(Json(json!({
|
||||
"status": "healthy",
|
||||
"service": "ai.card",
|
||||
"version": env!("CARGO_PKG_VERSION"),
|
||||
"timestamp": chrono::Utc::now().to_rfc3339()
|
||||
})))
|
||||
}
|
334
src/models.rs
Normal file
334
src/models.rs
Normal file
@@ -0,0 +1,334 @@
|
||||
use chrono::{DateTime, Utc};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sqlx::{FromRow, Type};
|
||||
use uuid::Uuid;
|
||||
use validator::Validate;
|
||||
|
||||
/// Card rarity enum matching Python implementation
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Type)]
|
||||
#[sqlx(type_name = "card_rarity", rename_all = "lowercase")]
|
||||
pub enum CardRarity {
|
||||
#[serde(rename = "normal")]
|
||||
Normal,
|
||||
#[serde(rename = "rare")]
|
||||
Rare,
|
||||
#[serde(rename = "super_rare")]
|
||||
SuperRare,
|
||||
#[serde(rename = "kira")]
|
||||
Kira,
|
||||
#[serde(rename = "unique")]
|
||||
Unique,
|
||||
}
|
||||
|
||||
impl CardRarity {
|
||||
pub fn multiplier(&self) -> f64 {
|
||||
match self {
|
||||
CardRarity::Normal => 1.0,
|
||||
CardRarity::Rare => 1.5,
|
||||
CardRarity::SuperRare => 2.0,
|
||||
CardRarity::Kira => 3.0,
|
||||
CardRarity::Unique => 5.0,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn as_str(&self) -> &'static str {
|
||||
match self {
|
||||
CardRarity::Normal => "normal",
|
||||
CardRarity::Rare => "rare",
|
||||
CardRarity::SuperRare => "super_rare",
|
||||
CardRarity::Kira => "kira",
|
||||
CardRarity::Unique => "unique",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Database Models
|
||||
|
||||
#[derive(Debug, Clone, FromRow, Serialize, Deserialize)]
|
||||
pub struct User {
|
||||
pub id: i32,
|
||||
pub did: String,
|
||||
pub handle: String,
|
||||
pub created_at: DateTime<Utc>,
|
||||
pub updated_at: DateTime<Utc>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, FromRow, Serialize, Deserialize)]
|
||||
pub struct CardMaster {
|
||||
pub id: i32,
|
||||
pub name: String,
|
||||
pub base_cp_min: i32,
|
||||
pub base_cp_max: i32,
|
||||
pub color: String,
|
||||
pub description: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, FromRow, Serialize, Deserialize)]
|
||||
pub struct UserCard {
|
||||
pub id: i32,
|
||||
pub user_id: i32,
|
||||
pub card_id: i32,
|
||||
pub cp: i32,
|
||||
pub status: CardRarity,
|
||||
pub skill: Option<String>,
|
||||
pub obtained_at: DateTime<Utc>,
|
||||
pub is_unique: bool,
|
||||
pub unique_id: Option<Uuid>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, FromRow, Serialize, Deserialize)]
|
||||
pub struct UniqueCardRegistry {
|
||||
pub id: i32,
|
||||
pub unique_id: Uuid,
|
||||
pub card_id: i32,
|
||||
pub owner_did: String,
|
||||
pub obtained_at: DateTime<Utc>,
|
||||
pub verse_skill_id: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, FromRow, Serialize, Deserialize)]
|
||||
pub struct DrawHistory {
|
||||
pub id: i32,
|
||||
pub user_id: i32,
|
||||
pub card_id: i32,
|
||||
pub status: CardRarity,
|
||||
pub cp: i32,
|
||||
pub is_paid: bool,
|
||||
pub drawn_at: DateTime<Utc>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, FromRow, Serialize, Deserialize)]
|
||||
pub struct GachaPool {
|
||||
pub id: i32,
|
||||
pub name: String,
|
||||
pub description: String,
|
||||
pub is_active: bool,
|
||||
pub start_at: Option<DateTime<Utc>>,
|
||||
pub end_at: Option<DateTime<Utc>>,
|
||||
pub pickup_card_ids: Vec<i32>,
|
||||
pub rate_up_multiplier: f64,
|
||||
}
|
||||
|
||||
/// API Request/Response Models
|
||||
|
||||
#[derive(Debug, Deserialize, Validate)]
|
||||
pub struct LoginRequest {
|
||||
#[validate(length(min = 1))]
|
||||
pub identifier: String,
|
||||
#[validate(length(min = 1))]
|
||||
pub password: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
pub struct LoginResponse {
|
||||
pub access_token: String,
|
||||
pub token_type: String,
|
||||
pub expires_in: u64,
|
||||
pub user: UserInfo,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
pub struct UserInfo {
|
||||
pub did: String,
|
||||
pub handle: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Validate)]
|
||||
pub struct CardDrawRequest {
|
||||
pub user_did: String,
|
||||
#[serde(default)]
|
||||
pub is_paid: bool,
|
||||
pub pool_id: Option<i32>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
pub struct CardDrawResponse {
|
||||
pub card: UserCardResponse,
|
||||
pub master: CardMasterResponse,
|
||||
pub is_unique: bool,
|
||||
pub animation_type: String,
|
||||
pub draw_history_id: i32,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
pub struct UserCardResponse {
|
||||
pub id: i32,
|
||||
pub card_id: i32,
|
||||
pub cp: i32,
|
||||
pub status: CardRarity,
|
||||
pub skill: Option<String>,
|
||||
pub obtained_at: DateTime<Utc>,
|
||||
pub is_unique: bool,
|
||||
pub unique_id: Option<Uuid>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
pub struct CardMasterResponse {
|
||||
pub id: i32,
|
||||
pub name: String,
|
||||
pub base_cp_min: i32,
|
||||
pub base_cp_max: i32,
|
||||
pub color: String,
|
||||
pub description: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
pub struct UserCardCollectionResponse {
|
||||
pub user_did: String,
|
||||
pub cards: Vec<UserCardWithMaster>,
|
||||
pub total_count: i32,
|
||||
pub unique_count: i32,
|
||||
pub rarity_breakdown: RarityBreakdown,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
pub struct UserCardWithMaster {
|
||||
pub card: UserCardResponse,
|
||||
pub master: CardMasterResponse,
|
||||
}
|
||||
|
||||
/// Database query result for JOIN operations
|
||||
#[derive(Debug, Clone, FromRow)]
|
||||
#[allow(dead_code)]
|
||||
pub struct UserCardWithMasterQuery {
|
||||
// user_cards fields
|
||||
pub id: i32,
|
||||
pub user_id: i32,
|
||||
pub card_id: i32,
|
||||
pub cp: i32,
|
||||
pub status: String,
|
||||
pub obtained_at: DateTime<Utc>,
|
||||
pub is_unique: bool,
|
||||
pub unique_id: Option<Uuid>,
|
||||
// card_master fields
|
||||
pub master_id: i32,
|
||||
pub name: String,
|
||||
pub base_cp_min: i32,
|
||||
pub base_cp_max: i32,
|
||||
pub color: String,
|
||||
pub description: String,
|
||||
}
|
||||
|
||||
/// Database query result for unique card registry
|
||||
#[derive(Debug, Clone, FromRow)]
|
||||
pub struct UniqueCardQuery {
|
||||
pub card_id: i32,
|
||||
pub card_name: String,
|
||||
pub owner_did: Option<String>,
|
||||
pub owner_handle: Option<String>,
|
||||
pub obtained_at: Option<DateTime<Utc>>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
pub struct RarityBreakdown {
|
||||
pub normal: i32,
|
||||
pub rare: i32,
|
||||
pub super_rare: i32,
|
||||
pub kira: i32,
|
||||
pub unique: i32,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
pub struct UniqueCardRegistryResponse {
|
||||
pub unique_cards: Vec<UniqueCardInfo>,
|
||||
pub total_unique_cards: i32,
|
||||
pub available_unique_cards: i32,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
pub struct UniqueCardInfo {
|
||||
pub card_id: i32,
|
||||
pub card_name: String,
|
||||
pub owner_did: Option<String>,
|
||||
pub owner_handle: Option<String>,
|
||||
pub obtained_at: Option<DateTime<Utc>>,
|
||||
pub is_available: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
pub struct GachaStatsResponse {
|
||||
pub probabilities: GachaProbabilities,
|
||||
pub total_draws: i32,
|
||||
pub total_unique_cards: i32,
|
||||
pub available_unique_cards: i32,
|
||||
pub rarity_distribution: RarityBreakdown,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
pub struct GachaProbabilities {
|
||||
pub normal: f64,
|
||||
pub rare: f64,
|
||||
pub super_rare: f64,
|
||||
pub kira: f64,
|
||||
pub unique: f64,
|
||||
pub paid_multiplier: f64,
|
||||
}
|
||||
|
||||
/// External Data Models (from ai.json)
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
#[allow(dead_code)]
|
||||
pub struct ExternalCardData {
|
||||
pub ai: AiData,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
#[allow(dead_code)]
|
||||
pub struct AiData {
|
||||
pub card: CardData,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
#[allow(dead_code)]
|
||||
pub struct CardData {
|
||||
pub cards: Vec<ExternalCard>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
#[allow(dead_code)]
|
||||
pub struct ExternalCard {
|
||||
pub id: i32,
|
||||
pub name: String,
|
||||
pub cp: CpRange,
|
||||
pub color: String,
|
||||
pub skill: String,
|
||||
pub lang: Option<LangData>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
#[allow(dead_code)]
|
||||
pub struct CpRange {
|
||||
pub min: i32,
|
||||
pub max: i32,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
#[allow(dead_code)]
|
||||
pub struct LangData {
|
||||
pub ja: Option<JapaneseData>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
#[allow(dead_code)]
|
||||
pub struct JapaneseData {
|
||||
pub name: Option<String>,
|
||||
pub skill: Option<String>,
|
||||
}
|
||||
|
||||
/// atproto Models
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
pub struct AtprotoCardRecord {
|
||||
#[serde(rename = "$type")]
|
||||
pub record_type: String,
|
||||
#[serde(rename = "cardId")]
|
||||
pub card_id: i32,
|
||||
pub cp: i32,
|
||||
pub status: String,
|
||||
#[serde(rename = "obtainedAt")]
|
||||
pub obtained_at: DateTime<Utc>,
|
||||
#[serde(rename = "isUnique")]
|
||||
pub is_unique: bool,
|
||||
#[serde(rename = "uniqueId")]
|
||||
pub unique_id: Option<Uuid>,
|
||||
}
|
241
src/services/atproto.rs
Normal file
241
src/services/atproto.rs
Normal file
@@ -0,0 +1,241 @@
|
||||
use crate::{
|
||||
error::{AppError, AppResult},
|
||||
models::*,
|
||||
};
|
||||
use reqwest::Client;
|
||||
use serde_json::json;
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub struct AtprotoService {
|
||||
client: Client,
|
||||
session: Option<String>,
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
impl AtprotoService {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
client: Client::new(),
|
||||
session: None,
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn with_session(session: String) -> Self {
|
||||
Self {
|
||||
client: Client::new(),
|
||||
session: Some(session),
|
||||
}
|
||||
}
|
||||
|
||||
/// Create a card record in user's atproto PDS
|
||||
#[allow(dead_code)]
|
||||
pub async fn create_card_record(
|
||||
&self,
|
||||
did: &str,
|
||||
card: &UserCard,
|
||||
_master: &CardMaster,
|
||||
) -> AppResult<String> {
|
||||
let session = self.session.as_ref()
|
||||
.ok_or_else(|| AppError::authentication("No atproto session available"))?;
|
||||
|
||||
let record_data = AtprotoCardRecord {
|
||||
record_type: "ai.card.collection".to_string(),
|
||||
card_id: card.card_id,
|
||||
cp: card.cp,
|
||||
status: card.status.as_str().to_string(),
|
||||
obtained_at: card.obtained_at,
|
||||
is_unique: card.is_unique,
|
||||
unique_id: card.unique_id,
|
||||
};
|
||||
|
||||
// Determine PDS endpoint from DID
|
||||
let pds_url = self.resolve_pds_from_did(did).await?;
|
||||
|
||||
let response = self
|
||||
.client
|
||||
.post(&format!("{}/xrpc/com.atproto.repo.createRecord", pds_url))
|
||||
.header("Authorization", format!("Bearer {}", session))
|
||||
.json(&json!({
|
||||
"repo": did,
|
||||
"collection": "ai.card.collection",
|
||||
"record": record_data
|
||||
}))
|
||||
.send()
|
||||
.await
|
||||
.map_err(AppError::HttpClient)?;
|
||||
|
||||
if !response.status().is_success() {
|
||||
return Err(AppError::ExternalService(format!(
|
||||
"Failed to create atproto record: HTTP {}",
|
||||
response.status()
|
||||
)));
|
||||
}
|
||||
|
||||
let result: serde_json::Value = response
|
||||
.json()
|
||||
.await
|
||||
.map_err(AppError::HttpClient)?;
|
||||
|
||||
let uri = result["uri"]
|
||||
.as_str()
|
||||
.ok_or_else(|| AppError::ExternalService("No URI in response".to_string()))?;
|
||||
|
||||
Ok(uri.to_string())
|
||||
}
|
||||
|
||||
/// List card records from user's PDS
|
||||
#[allow(dead_code)]
|
||||
pub async fn list_card_records(&self, did: &str) -> AppResult<Vec<serde_json::Value>> {
|
||||
let session = self.session.as_ref()
|
||||
.ok_or_else(|| AppError::authentication("No atproto session available"))?;
|
||||
|
||||
let pds_url = self.resolve_pds_from_did(did).await?;
|
||||
|
||||
let response = self
|
||||
.client
|
||||
.get(&format!("{}/xrpc/com.atproto.repo.listRecords", pds_url))
|
||||
.header("Authorization", format!("Bearer {}", session))
|
||||
.query(&[
|
||||
("repo", did),
|
||||
("collection", "ai.card.collection"),
|
||||
])
|
||||
.send()
|
||||
.await
|
||||
.map_err(AppError::HttpClient)?;
|
||||
|
||||
if !response.status().is_success() {
|
||||
return Err(AppError::ExternalService(format!(
|
||||
"Failed to list atproto records: HTTP {}",
|
||||
response.status()
|
||||
)));
|
||||
}
|
||||
|
||||
let result: serde_json::Value = response
|
||||
.json()
|
||||
.await
|
||||
.map_err(AppError::HttpClient)?;
|
||||
|
||||
let records = result["records"]
|
||||
.as_array()
|
||||
.ok_or_else(|| AppError::ExternalService("No records in response".to_string()))?;
|
||||
|
||||
Ok(records.clone())
|
||||
}
|
||||
|
||||
/// Resolve PDS endpoint from DID
|
||||
#[allow(dead_code)]
|
||||
async fn resolve_pds_from_did(&self, did: &str) -> AppResult<String> {
|
||||
// This is a simplified resolution
|
||||
// In a real implementation, you would:
|
||||
// 1. Parse the DID to get the method and identifier
|
||||
// 2. Query the appropriate resolver (PLC directory, etc.)
|
||||
// 3. Get the serviceEndpoint for the PDS
|
||||
|
||||
if did.starts_with("did:plc:") {
|
||||
// For PLC DIDs, query the PLC directory
|
||||
let plc_id = did.strip_prefix("did:plc:").unwrap();
|
||||
self.resolve_plc_did(plc_id).await
|
||||
} else if did.starts_with("did:web:") {
|
||||
// For web DIDs, construct URL from domain
|
||||
let domain = did.strip_prefix("did:web:").unwrap();
|
||||
Ok(format!("https://{}", domain))
|
||||
} else {
|
||||
// Fallback to Bluesky PDS
|
||||
Ok("https://bsky.social".to_string())
|
||||
}
|
||||
}
|
||||
|
||||
/// Resolve PLC DID to PDS endpoint
|
||||
#[allow(dead_code)]
|
||||
async fn resolve_plc_did(&self, plc_id: &str) -> AppResult<String> {
|
||||
let response = self
|
||||
.client
|
||||
.get(&format!("https://plc.directory/{}", plc_id))
|
||||
.send()
|
||||
.await
|
||||
.map_err(AppError::HttpClient)?;
|
||||
|
||||
if !response.status().is_success() {
|
||||
return Ok("https://bsky.social".to_string()); // Fallback
|
||||
}
|
||||
|
||||
let did_doc: serde_json::Value = response
|
||||
.json()
|
||||
.await
|
||||
.map_err(AppError::HttpClient)?;
|
||||
|
||||
// Extract PDS endpoint from DID document
|
||||
if let Some(services) = did_doc["service"].as_array() {
|
||||
for service in services {
|
||||
if service["id"] == "#atproto_pds" {
|
||||
if let Some(endpoint) = service["serviceEndpoint"].as_str() {
|
||||
return Ok(endpoint.to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback to Bluesky
|
||||
Ok("https://bsky.social".to_string())
|
||||
}
|
||||
|
||||
/// Authenticate with atproto and get session
|
||||
#[allow(dead_code)]
|
||||
pub async fn authenticate(&self, identifier: &str, password: &str) -> AppResult<(String, String)> {
|
||||
// Try multiple PDS endpoints for authentication
|
||||
let pds_endpoints = [
|
||||
"https://bsky.social",
|
||||
"https://staging.bsky.app",
|
||||
// Add more PDS endpoints as needed
|
||||
];
|
||||
|
||||
for pds_url in pds_endpoints {
|
||||
match self.try_authenticate_at_pds(pds_url, identifier, password).await {
|
||||
Ok((session, did)) => return Ok((session, did)),
|
||||
Err(_) => continue, // Try next PDS
|
||||
}
|
||||
}
|
||||
|
||||
Err(AppError::authentication("Failed to authenticate with any PDS"))
|
||||
}
|
||||
|
||||
/// Try authentication at a specific PDS
|
||||
#[allow(dead_code)]
|
||||
async fn try_authenticate_at_pds(
|
||||
&self,
|
||||
pds_url: &str,
|
||||
identifier: &str,
|
||||
password: &str,
|
||||
) -> AppResult<(String, String)> {
|
||||
let response = self
|
||||
.client
|
||||
.post(&format!("{}/xrpc/com.atproto.server.createSession", pds_url))
|
||||
.json(&json!({
|
||||
"identifier": identifier,
|
||||
"password": password
|
||||
}))
|
||||
.send()
|
||||
.await
|
||||
.map_err(AppError::HttpClient)?;
|
||||
|
||||
if !response.status().is_success() {
|
||||
return Err(AppError::authentication("Invalid credentials"));
|
||||
}
|
||||
|
||||
let result: serde_json::Value = response
|
||||
.json()
|
||||
.await
|
||||
.map_err(AppError::HttpClient)?;
|
||||
|
||||
let access_jwt = result["accessJwt"]
|
||||
.as_str()
|
||||
.ok_or_else(|| AppError::authentication("No access token in response"))?;
|
||||
|
||||
let did = result["did"]
|
||||
.as_str()
|
||||
.ok_or_else(|| AppError::authentication("No DID in response"))?;
|
||||
|
||||
Ok((access_jwt.to_string(), did.to_string()))
|
||||
}
|
||||
}
|
224
src/services/card_master.rs
Normal file
224
src/services/card_master.rs
Normal file
@@ -0,0 +1,224 @@
|
||||
use crate::{
|
||||
error::{AppError, AppResult},
|
||||
models::*,
|
||||
};
|
||||
use reqwest::Client;
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub struct CardMasterService {
|
||||
client: Client,
|
||||
master_url: String,
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
impl CardMasterService {
|
||||
pub fn new(master_url: String) -> Self {
|
||||
Self {
|
||||
client: Client::new(),
|
||||
master_url,
|
||||
}
|
||||
}
|
||||
|
||||
/// Fetch card master data from external source (ai.json)
|
||||
#[allow(dead_code)]
|
||||
pub async fn fetch_external_card_data(&self) -> AppResult<Vec<ExternalCard>> {
|
||||
let response = self
|
||||
.client
|
||||
.get(&self.master_url)
|
||||
.timeout(std::time::Duration::from_secs(10))
|
||||
.send()
|
||||
.await
|
||||
.map_err(AppError::HttpClient)?;
|
||||
|
||||
if !response.status().is_success() {
|
||||
return Err(AppError::ExternalService(format!(
|
||||
"Failed to fetch card data: HTTP {}",
|
||||
response.status()
|
||||
)));
|
||||
}
|
||||
|
||||
let data: ExternalCardData = response
|
||||
.json()
|
||||
.await
|
||||
.map_err(AppError::HttpClient)?;
|
||||
|
||||
Ok(data.ai.card.cards)
|
||||
}
|
||||
|
||||
/// Get fallback card data if external fetch fails
|
||||
#[allow(dead_code)]
|
||||
pub fn get_fallback_card_data(&self) -> Vec<ExternalCard> {
|
||||
vec![
|
||||
ExternalCard {
|
||||
id: 0,
|
||||
name: "ai".to_string(),
|
||||
cp: CpRange { min: 100, max: 200 },
|
||||
color: "#4A90E2".to_string(),
|
||||
skill: "Core existence essence".to_string(),
|
||||
lang: None,
|
||||
},
|
||||
ExternalCard {
|
||||
id: 1,
|
||||
name: "dream".to_string(),
|
||||
cp: CpRange { min: 90, max: 180 },
|
||||
color: "#9B59B6".to_string(),
|
||||
skill: "Vision manifestation".to_string(),
|
||||
lang: None,
|
||||
},
|
||||
ExternalCard {
|
||||
id: 2,
|
||||
name: "radiance".to_string(),
|
||||
cp: CpRange { min: 110, max: 220 },
|
||||
color: "#F39C12".to_string(),
|
||||
skill: "Brilliant energy".to_string(),
|
||||
lang: None,
|
||||
},
|
||||
ExternalCard {
|
||||
id: 3,
|
||||
name: "neutron".to_string(),
|
||||
cp: CpRange { min: 120, max: 240 },
|
||||
color: "#34495E".to_string(),
|
||||
skill: "Dense core power".to_string(),
|
||||
lang: None,
|
||||
},
|
||||
ExternalCard {
|
||||
id: 4,
|
||||
name: "sun".to_string(),
|
||||
cp: CpRange { min: 130, max: 260 },
|
||||
color: "#E74C3C".to_string(),
|
||||
skill: "Solar radiance".to_string(),
|
||||
lang: None,
|
||||
},
|
||||
ExternalCard {
|
||||
id: 5,
|
||||
name: "night".to_string(),
|
||||
cp: CpRange { min: 80, max: 160 },
|
||||
color: "#2C3E50".to_string(),
|
||||
skill: "Shadow stealth".to_string(),
|
||||
lang: None,
|
||||
},
|
||||
ExternalCard {
|
||||
id: 6,
|
||||
name: "snow".to_string(),
|
||||
cp: CpRange { min: 70, max: 140 },
|
||||
color: "#ECF0F1".to_string(),
|
||||
skill: "Crystal freeze".to_string(),
|
||||
lang: None,
|
||||
},
|
||||
ExternalCard {
|
||||
id: 7,
|
||||
name: "thunder".to_string(),
|
||||
cp: CpRange { min: 140, max: 280 },
|
||||
color: "#F1C40F".to_string(),
|
||||
skill: "Electric storm".to_string(),
|
||||
lang: None,
|
||||
},
|
||||
ExternalCard {
|
||||
id: 8,
|
||||
name: "ultimate".to_string(),
|
||||
cp: CpRange { min: 150, max: 300 },
|
||||
color: "#8E44AD".to_string(),
|
||||
skill: "Maximum form".to_string(),
|
||||
lang: None,
|
||||
},
|
||||
ExternalCard {
|
||||
id: 9,
|
||||
name: "sword".to_string(),
|
||||
cp: CpRange { min: 160, max: 320 },
|
||||
color: "#95A5A6".to_string(),
|
||||
skill: "Truth cutting".to_string(),
|
||||
lang: None,
|
||||
},
|
||||
ExternalCard {
|
||||
id: 10,
|
||||
name: "destruction".to_string(),
|
||||
cp: CpRange { min: 170, max: 340 },
|
||||
color: "#C0392B".to_string(),
|
||||
skill: "Entropy force".to_string(),
|
||||
lang: None,
|
||||
},
|
||||
ExternalCard {
|
||||
id: 11,
|
||||
name: "earth".to_string(),
|
||||
cp: CpRange { min: 90, max: 180 },
|
||||
color: "#27AE60".to_string(),
|
||||
skill: "Ground foundation".to_string(),
|
||||
lang: None,
|
||||
},
|
||||
ExternalCard {
|
||||
id: 12,
|
||||
name: "galaxy".to_string(),
|
||||
cp: CpRange { min: 180, max: 360 },
|
||||
color: "#3498DB".to_string(),
|
||||
skill: "Cosmic expanse".to_string(),
|
||||
lang: None,
|
||||
},
|
||||
ExternalCard {
|
||||
id: 13,
|
||||
name: "create".to_string(),
|
||||
cp: CpRange { min: 100, max: 200 },
|
||||
color: "#16A085".to_string(),
|
||||
skill: "Generation power".to_string(),
|
||||
lang: None,
|
||||
},
|
||||
ExternalCard {
|
||||
id: 14,
|
||||
name: "supernova".to_string(),
|
||||
cp: CpRange { min: 200, max: 400 },
|
||||
color: "#E67E22".to_string(),
|
||||
skill: "Stellar explosion".to_string(),
|
||||
lang: None,
|
||||
},
|
||||
ExternalCard {
|
||||
id: 15,
|
||||
name: "world".to_string(),
|
||||
cp: CpRange { min: 250, max: 500 },
|
||||
color: "#9B59B6".to_string(),
|
||||
skill: "Reality control".to_string(),
|
||||
lang: None,
|
||||
},
|
||||
]
|
||||
}
|
||||
|
||||
/// Get card master data, trying external source first then fallback
|
||||
#[allow(dead_code)]
|
||||
pub async fn get_card_master_data(&self) -> Vec<ExternalCard> {
|
||||
match self.fetch_external_card_data().await {
|
||||
Ok(cards) => {
|
||||
tracing::info!("Fetched {} cards from external source", cards.len());
|
||||
cards
|
||||
}
|
||||
Err(e) => {
|
||||
tracing::warn!("Failed to fetch external card data: {}, using fallback", e);
|
||||
self.get_fallback_card_data()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert external card data to database format
|
||||
#[allow(dead_code)]
|
||||
pub fn external_to_card_master(external: &ExternalCard) -> CardMaster {
|
||||
let description = if let Some(lang) = &external.lang {
|
||||
if let Some(ja) = &lang.ja {
|
||||
if let Some(name) = &ja.name {
|
||||
format!("{} - {}", name, external.skill)
|
||||
} else {
|
||||
external.skill.clone()
|
||||
}
|
||||
} else {
|
||||
external.skill.clone()
|
||||
}
|
||||
} else {
|
||||
external.skill.clone()
|
||||
};
|
||||
|
||||
CardMaster {
|
||||
id: external.id,
|
||||
name: external.name.clone(),
|
||||
base_cp_min: external.cp.min,
|
||||
base_cp_max: external.cp.max,
|
||||
color: external.color.clone(),
|
||||
description,
|
||||
}
|
||||
}
|
||||
}
|
538
src/services/gacha.rs
Normal file
538
src/services/gacha.rs
Normal file
@@ -0,0 +1,538 @@
|
||||
use crate::{
|
||||
config::GachaConfig,
|
||||
database::{Database, DatabaseTransaction},
|
||||
error::{AppError, AppResult},
|
||||
models::*,
|
||||
};
|
||||
use chrono::Utc;
|
||||
use rand::Rng;
|
||||
use uuid::Uuid;
|
||||
|
||||
pub struct GachaService {
|
||||
config: GachaConfig,
|
||||
}
|
||||
|
||||
impl GachaService {
|
||||
pub fn new(config: GachaConfig) -> Self {
|
||||
Self { config }
|
||||
}
|
||||
|
||||
/// Main gacha draw function
|
||||
pub async fn draw_card(
|
||||
&self,
|
||||
db: &Database,
|
||||
user_did: &str,
|
||||
is_paid: bool,
|
||||
pool_id: Option<i32>,
|
||||
) -> AppResult<CardDrawResponse> {
|
||||
let mut tx = db.begin().await?;
|
||||
|
||||
// Get or create user
|
||||
let user = self.get_or_create_user(&mut tx, user_did).await?;
|
||||
|
||||
// Determine card rarity
|
||||
let rarity = self.determine_rarity(is_paid, pool_id)?;
|
||||
|
||||
// Select a card based on rarity and pool
|
||||
let card_master = self.select_card_master(&mut tx, &rarity, pool_id).await?;
|
||||
|
||||
// Calculate CP based on rarity
|
||||
let cp = self.calculate_cp(&card_master, &rarity);
|
||||
|
||||
// Check if this will be a unique card
|
||||
let is_unique = rarity == CardRarity::Unique;
|
||||
|
||||
// For unique cards, check availability
|
||||
if is_unique {
|
||||
if let Some(_existing) = self.check_unique_card_availability(&mut tx, card_master.id).await? {
|
||||
// Unique card already taken, fallback to Kira
|
||||
return self.draw_card_with_fallback(&mut tx, user.id, &card_master, CardRarity::Kira, is_paid).await;
|
||||
}
|
||||
}
|
||||
|
||||
// Create the user card
|
||||
let user_card = self.create_user_card(
|
||||
&mut tx,
|
||||
user.id,
|
||||
&card_master,
|
||||
cp,
|
||||
&rarity,
|
||||
is_unique,
|
||||
).await?;
|
||||
|
||||
// Record draw history
|
||||
let draw_history = self.record_draw_history(
|
||||
&mut tx,
|
||||
user.id,
|
||||
card_master.id,
|
||||
&rarity,
|
||||
cp,
|
||||
is_paid,
|
||||
).await?;
|
||||
|
||||
// Register unique card if applicable
|
||||
if is_unique {
|
||||
self.register_unique_card(&mut tx, &user_card, user_did).await?;
|
||||
}
|
||||
|
||||
tx.commit().await?;
|
||||
|
||||
Ok(CardDrawResponse {
|
||||
card: UserCardResponse {
|
||||
id: user_card.id,
|
||||
card_id: user_card.card_id,
|
||||
cp: user_card.cp,
|
||||
status: user_card.status,
|
||||
skill: user_card.skill,
|
||||
obtained_at: user_card.obtained_at,
|
||||
is_unique: user_card.is_unique,
|
||||
unique_id: user_card.unique_id,
|
||||
},
|
||||
master: CardMasterResponse {
|
||||
id: card_master.id,
|
||||
name: card_master.name,
|
||||
base_cp_min: card_master.base_cp_min,
|
||||
base_cp_max: card_master.base_cp_max,
|
||||
color: card_master.color,
|
||||
description: card_master.description,
|
||||
},
|
||||
is_unique,
|
||||
animation_type: self.get_animation_type(&rarity),
|
||||
draw_history_id: draw_history.id,
|
||||
})
|
||||
}
|
||||
|
||||
/// Determine card rarity based on probabilities
|
||||
fn determine_rarity(&self, is_paid: bool, _pool_id: Option<i32>) -> AppResult<CardRarity> {
|
||||
let mut rng = rand::thread_rng();
|
||||
let rand_val: f64 = rng.gen_range(0.0..100.0);
|
||||
|
||||
let cumulative_probs = self.config.cumulative_probabilities(is_paid);
|
||||
let mut cumulative = 0.0;
|
||||
|
||||
for (prob, rarity) in cumulative_probs {
|
||||
cumulative += prob;
|
||||
if rand_val < cumulative {
|
||||
return Ok(rarity);
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback to normal if no match (should never happen)
|
||||
Ok(CardRarity::Normal)
|
||||
}
|
||||
|
||||
/// Select a card master based on rarity and pool
|
||||
async fn select_card_master(
|
||||
&self,
|
||||
tx: &mut DatabaseTransaction,
|
||||
_rarity: &CardRarity,
|
||||
_pool_id: Option<i32>,
|
||||
) -> AppResult<CardMaster> {
|
||||
// For now, randomly select from all available cards
|
||||
// In a full implementation, this would consider pool restrictions
|
||||
let cards = match tx {
|
||||
DatabaseTransaction::Postgres(tx) => {
|
||||
sqlx::query_as::<_, CardMaster>("SELECT * FROM card_master ORDER BY RANDOM() LIMIT 1")
|
||||
.fetch_one(&mut **tx)
|
||||
.await
|
||||
.map_err(AppError::Database)?
|
||||
}
|
||||
DatabaseTransaction::Sqlite(tx) => {
|
||||
sqlx::query_as::<_, CardMaster>("SELECT * FROM card_master ORDER BY RANDOM() LIMIT 1")
|
||||
.fetch_one(&mut **tx)
|
||||
.await
|
||||
.map_err(AppError::Database)?
|
||||
}
|
||||
};
|
||||
|
||||
Ok(cards)
|
||||
}
|
||||
|
||||
/// Calculate CP based on base CP and rarity multiplier
|
||||
fn calculate_cp(&self, card_master: &CardMaster, rarity: &CardRarity) -> i32 {
|
||||
let mut rng = rand::thread_rng();
|
||||
let base_cp = rng.gen_range(card_master.base_cp_min..=card_master.base_cp_max);
|
||||
let multiplier = rarity.multiplier();
|
||||
(base_cp as f64 * multiplier) as i32
|
||||
}
|
||||
|
||||
/// Check if a unique card is available
|
||||
async fn check_unique_card_availability(
|
||||
&self,
|
||||
tx: &mut DatabaseTransaction,
|
||||
card_id: i32,
|
||||
) -> AppResult<Option<UniqueCardRegistry>> {
|
||||
match tx {
|
||||
DatabaseTransaction::Postgres(tx) => {
|
||||
sqlx::query_as::<_, UniqueCardRegistry>(
|
||||
"SELECT * FROM unique_card_registry WHERE card_id = $1"
|
||||
)
|
||||
.bind(card_id)
|
||||
.fetch_optional(&mut **tx)
|
||||
.await
|
||||
.map_err(AppError::Database)
|
||||
}
|
||||
DatabaseTransaction::Sqlite(tx) => {
|
||||
sqlx::query_as::<_, UniqueCardRegistry>(
|
||||
"SELECT * FROM unique_card_registry WHERE card_id = ?"
|
||||
)
|
||||
.bind(card_id)
|
||||
.fetch_optional(&mut **tx)
|
||||
.await
|
||||
.map_err(AppError::Database)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Create a user card
|
||||
async fn create_user_card(
|
||||
&self,
|
||||
tx: &mut DatabaseTransaction,
|
||||
user_id: i32,
|
||||
card_master: &CardMaster,
|
||||
cp: i32,
|
||||
rarity: &CardRarity,
|
||||
is_unique: bool,
|
||||
) -> AppResult<UserCard> {
|
||||
let unique_id = if is_unique { Some(Uuid::new_v4()) } else { None };
|
||||
let now = Utc::now();
|
||||
|
||||
match tx {
|
||||
DatabaseTransaction::Postgres(tx) => {
|
||||
sqlx::query_as::<_, UserCard>(
|
||||
r#"
|
||||
INSERT INTO user_cards (user_id, card_id, cp, status, obtained_at, is_unique, unique_id)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7)
|
||||
RETURNING *
|
||||
"#
|
||||
)
|
||||
.bind(user_id)
|
||||
.bind(card_master.id)
|
||||
.bind(cp)
|
||||
.bind(rarity)
|
||||
.bind(now)
|
||||
.bind(is_unique)
|
||||
.bind(unique_id)
|
||||
.fetch_one(&mut **tx)
|
||||
.await
|
||||
.map_err(AppError::Database)
|
||||
}
|
||||
DatabaseTransaction::Sqlite(tx) => {
|
||||
sqlx::query_as::<_, UserCard>(
|
||||
r#"
|
||||
INSERT INTO user_cards (user_id, card_id, cp, status, obtained_at, is_unique, unique_id)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?)
|
||||
RETURNING *
|
||||
"#
|
||||
)
|
||||
.bind(user_id)
|
||||
.bind(card_master.id)
|
||||
.bind(cp)
|
||||
.bind(rarity)
|
||||
.bind(now)
|
||||
.bind(is_unique)
|
||||
.bind(unique_id)
|
||||
.fetch_one(&mut **tx)
|
||||
.await
|
||||
.map_err(AppError::Database)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Record draw history
|
||||
async fn record_draw_history(
|
||||
&self,
|
||||
tx: &mut DatabaseTransaction,
|
||||
user_id: i32,
|
||||
card_id: i32,
|
||||
rarity: &CardRarity,
|
||||
cp: i32,
|
||||
is_paid: bool,
|
||||
) -> AppResult<DrawHistory> {
|
||||
let now = Utc::now();
|
||||
|
||||
match tx {
|
||||
DatabaseTransaction::Postgres(tx) => {
|
||||
sqlx::query_as::<_, DrawHistory>(
|
||||
r#"
|
||||
INSERT INTO draw_history (user_id, card_id, status, cp, is_paid, drawn_at)
|
||||
VALUES ($1, $2, $3, $4, $5, $6)
|
||||
RETURNING *
|
||||
"#
|
||||
)
|
||||
.bind(user_id)
|
||||
.bind(card_id)
|
||||
.bind(rarity)
|
||||
.bind(cp)
|
||||
.bind(is_paid)
|
||||
.bind(now)
|
||||
.fetch_one(&mut **tx)
|
||||
.await
|
||||
.map_err(AppError::Database)
|
||||
}
|
||||
DatabaseTransaction::Sqlite(tx) => {
|
||||
sqlx::query_as::<_, DrawHistory>(
|
||||
r#"
|
||||
INSERT INTO draw_history (user_id, card_id, status, cp, is_paid, drawn_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?)
|
||||
RETURNING *
|
||||
"#
|
||||
)
|
||||
.bind(user_id)
|
||||
.bind(card_id)
|
||||
.bind(rarity)
|
||||
.bind(cp)
|
||||
.bind(is_paid)
|
||||
.bind(now)
|
||||
.fetch_one(&mut **tx)
|
||||
.await
|
||||
.map_err(AppError::Database)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Register unique card
|
||||
async fn register_unique_card(
|
||||
&self,
|
||||
tx: &mut DatabaseTransaction,
|
||||
user_card: &UserCard,
|
||||
owner_did: &str,
|
||||
) -> AppResult<UniqueCardRegistry> {
|
||||
let unique_id = user_card.unique_id.ok_or_else(|| {
|
||||
AppError::Internal("Unique card must have unique_id".to_string())
|
||||
})?;
|
||||
|
||||
match tx {
|
||||
DatabaseTransaction::Postgres(tx) => {
|
||||
sqlx::query_as::<_, UniqueCardRegistry>(
|
||||
r#"
|
||||
INSERT INTO unique_card_registry (unique_id, card_id, owner_did, obtained_at)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
RETURNING *
|
||||
"#
|
||||
)
|
||||
.bind(unique_id)
|
||||
.bind(user_card.card_id)
|
||||
.bind(owner_did)
|
||||
.bind(user_card.obtained_at)
|
||||
.fetch_one(&mut **tx)
|
||||
.await
|
||||
.map_err(AppError::Database)
|
||||
}
|
||||
DatabaseTransaction::Sqlite(tx) => {
|
||||
sqlx::query_as::<_, UniqueCardRegistry>(
|
||||
r#"
|
||||
INSERT INTO unique_card_registry (unique_id, card_id, owner_did, obtained_at)
|
||||
VALUES (?, ?, ?, ?)
|
||||
RETURNING *
|
||||
"#
|
||||
)
|
||||
.bind(unique_id)
|
||||
.bind(user_card.card_id)
|
||||
.bind(owner_did)
|
||||
.bind(user_card.obtained_at)
|
||||
.fetch_one(&mut **tx)
|
||||
.await
|
||||
.map_err(AppError::Database)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Get or create user by DID
|
||||
async fn get_or_create_user(
|
||||
&self,
|
||||
tx: &mut DatabaseTransaction,
|
||||
did: &str,
|
||||
) -> AppResult<User> {
|
||||
// Try to get existing user
|
||||
let existing_user = match tx {
|
||||
DatabaseTransaction::Postgres(tx) => {
|
||||
sqlx::query_as::<_, User>("SELECT * FROM users WHERE did = $1")
|
||||
.bind(did)
|
||||
.fetch_optional(&mut **tx)
|
||||
.await
|
||||
.map_err(AppError::Database)?
|
||||
}
|
||||
DatabaseTransaction::Sqlite(tx) => {
|
||||
sqlx::query_as::<_, User>("SELECT * FROM users WHERE did = ?")
|
||||
.bind(did)
|
||||
.fetch_optional(&mut **tx)
|
||||
.await
|
||||
.map_err(AppError::Database)?
|
||||
}
|
||||
};
|
||||
|
||||
if let Some(user) = existing_user {
|
||||
return Ok(user);
|
||||
}
|
||||
|
||||
// Create new user
|
||||
let handle = did.split('.').next().unwrap_or("unknown").to_string();
|
||||
let now = Utc::now();
|
||||
|
||||
match tx {
|
||||
DatabaseTransaction::Postgres(tx) => {
|
||||
sqlx::query_as::<_, User>(
|
||||
"INSERT INTO users (did, handle, created_at, updated_at) VALUES ($1, $2, $3, $4) RETURNING *"
|
||||
)
|
||||
.bind(did)
|
||||
.bind(&handle)
|
||||
.bind(now)
|
||||
.bind(now)
|
||||
.fetch_one(&mut **tx)
|
||||
.await
|
||||
.map_err(AppError::Database)
|
||||
}
|
||||
DatabaseTransaction::Sqlite(tx) => {
|
||||
sqlx::query_as::<_, User>(
|
||||
"INSERT INTO users (did, handle, created_at, updated_at) VALUES (?, ?, ?, ?) RETURNING *"
|
||||
)
|
||||
.bind(did)
|
||||
.bind(&handle)
|
||||
.bind(now)
|
||||
.bind(now)
|
||||
.fetch_one(&mut **tx)
|
||||
.await
|
||||
.map_err(AppError::Database)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Draw card with fallback rarity (when unique is unavailable)
|
||||
async fn draw_card_with_fallback(
|
||||
&self,
|
||||
tx: &mut DatabaseTransaction,
|
||||
user_id: i32,
|
||||
card_master: &CardMaster,
|
||||
fallback_rarity: CardRarity,
|
||||
is_paid: bool,
|
||||
) -> AppResult<CardDrawResponse> {
|
||||
let cp = self.calculate_cp(card_master, &fallback_rarity);
|
||||
|
||||
let user_card = self.create_user_card(
|
||||
tx,
|
||||
user_id,
|
||||
card_master,
|
||||
cp,
|
||||
&fallback_rarity,
|
||||
false,
|
||||
).await?;
|
||||
|
||||
let draw_history = self.record_draw_history(
|
||||
tx,
|
||||
user_id,
|
||||
card_master.id,
|
||||
&fallback_rarity,
|
||||
cp,
|
||||
is_paid,
|
||||
).await?;
|
||||
|
||||
Ok(CardDrawResponse {
|
||||
card: UserCardResponse {
|
||||
id: user_card.id,
|
||||
card_id: user_card.card_id,
|
||||
cp: user_card.cp,
|
||||
status: user_card.status,
|
||||
skill: user_card.skill,
|
||||
obtained_at: user_card.obtained_at,
|
||||
is_unique: user_card.is_unique,
|
||||
unique_id: user_card.unique_id,
|
||||
},
|
||||
master: CardMasterResponse {
|
||||
id: card_master.id,
|
||||
name: card_master.name.clone(),
|
||||
base_cp_min: card_master.base_cp_min,
|
||||
base_cp_max: card_master.base_cp_max,
|
||||
color: card_master.color.clone(),
|
||||
description: card_master.description.clone(),
|
||||
},
|
||||
is_unique: false,
|
||||
animation_type: self.get_animation_type(&fallback_rarity),
|
||||
draw_history_id: draw_history.id,
|
||||
})
|
||||
}
|
||||
|
||||
/// Get animation type based on rarity
|
||||
fn get_animation_type(&self, rarity: &CardRarity) -> String {
|
||||
match rarity {
|
||||
CardRarity::Normal => "normal".to_string(),
|
||||
CardRarity::Rare => "sparkle".to_string(),
|
||||
CardRarity::SuperRare => "glow".to_string(),
|
||||
CardRarity::Kira => "rainbow".to_string(),
|
||||
CardRarity::Unique => "legendary".to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Get gacha statistics
|
||||
pub async fn get_gacha_stats(&self, db: &Database) -> AppResult<GachaStatsResponse> {
|
||||
// Get total draws
|
||||
let total_draws: (i64,) = match db {
|
||||
Database::Postgres(pool) => {
|
||||
sqlx::query_as("SELECT COUNT(*) FROM draw_history")
|
||||
.fetch_one(pool)
|
||||
.await
|
||||
.map_err(AppError::Database)?
|
||||
}
|
||||
Database::Sqlite(pool) => {
|
||||
sqlx::query_as("SELECT COUNT(*) FROM draw_history")
|
||||
.fetch_one(pool)
|
||||
.await
|
||||
.map_err(AppError::Database)?
|
||||
}
|
||||
};
|
||||
|
||||
// Get unique card counts
|
||||
let unique_counts: (i64, i64) = match db {
|
||||
Database::Postgres(pool) => {
|
||||
sqlx::query_as(
|
||||
r#"
|
||||
SELECT
|
||||
COUNT(*) as total,
|
||||
(SELECT COUNT(*) FROM card_master) - COUNT(*) as available
|
||||
FROM unique_card_registry
|
||||
"#
|
||||
)
|
||||
.fetch_one(pool)
|
||||
.await
|
||||
.map_err(AppError::Database)?
|
||||
}
|
||||
Database::Sqlite(pool) => {
|
||||
sqlx::query_as(
|
||||
r#"
|
||||
SELECT
|
||||
COUNT(*) as total,
|
||||
(SELECT COUNT(*) FROM card_master) - COUNT(*) as available
|
||||
FROM unique_card_registry
|
||||
"#
|
||||
)
|
||||
.fetch_one(pool)
|
||||
.await
|
||||
.map_err(AppError::Database)?
|
||||
}
|
||||
};
|
||||
|
||||
// Get rarity distribution
|
||||
let rarity_breakdown = RarityBreakdown {
|
||||
normal: 0, // Would need actual counts from database
|
||||
rare: 0,
|
||||
super_rare: 0,
|
||||
kira: 0,
|
||||
unique: unique_counts.0 as i32,
|
||||
};
|
||||
|
||||
Ok(GachaStatsResponse {
|
||||
probabilities: GachaProbabilities {
|
||||
normal: self.config.prob_normal,
|
||||
rare: self.config.prob_rare,
|
||||
super_rare: self.config.prob_super_rare,
|
||||
kira: self.config.prob_kira,
|
||||
unique: self.config.prob_unique,
|
||||
paid_multiplier: 2.0,
|
||||
},
|
||||
total_draws: total_draws.0 as i32,
|
||||
total_unique_cards: unique_counts.0 as i32,
|
||||
available_unique_cards: unique_counts.1 as i32,
|
||||
rarity_distribution: rarity_breakdown,
|
||||
})
|
||||
}
|
||||
}
|
6
src/services/mod.rs
Normal file
6
src/services/mod.rs
Normal file
@@ -0,0 +1,6 @@
|
||||
pub mod gacha;
|
||||
pub mod card_master;
|
||||
pub mod atproto;
|
||||
pub mod user;
|
||||
|
||||
pub use gacha::GachaService;
|
191
src/services/user.rs
Normal file
191
src/services/user.rs
Normal file
@@ -0,0 +1,191 @@
|
||||
use crate::{
|
||||
database::Database,
|
||||
error::{AppError, AppResult},
|
||||
models::*,
|
||||
};
|
||||
use chrono::Utc;
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub struct UserService;
|
||||
|
||||
#[allow(dead_code)]
|
||||
impl UserService {
|
||||
pub async fn get_user_by_did(db: &Database, did: &str) -> AppResult<Option<User>> {
|
||||
match db {
|
||||
Database::Postgres(pool) => {
|
||||
sqlx::query_as::<_, User>("SELECT * FROM users WHERE did = $1")
|
||||
.bind(did)
|
||||
.fetch_optional(pool)
|
||||
.await
|
||||
.map_err(AppError::Database)
|
||||
}
|
||||
Database::Sqlite(pool) => {
|
||||
sqlx::query_as::<_, User>("SELECT * FROM users WHERE did = ?")
|
||||
.bind(did)
|
||||
.fetch_optional(pool)
|
||||
.await
|
||||
.map_err(AppError::Database)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub async fn create_user(db: &Database, did: &str, handle: &str) -> AppResult<User> {
|
||||
let now = Utc::now();
|
||||
|
||||
match db {
|
||||
Database::Postgres(pool) => {
|
||||
sqlx::query_as::<_, User>(
|
||||
"INSERT INTO users (did, handle, created_at, updated_at) VALUES ($1, $2, $3, $4) RETURNING *"
|
||||
)
|
||||
.bind(did)
|
||||
.bind(handle)
|
||||
.bind(now)
|
||||
.bind(now)
|
||||
.fetch_one(pool)
|
||||
.await
|
||||
.map_err(AppError::Database)
|
||||
}
|
||||
Database::Sqlite(pool) => {
|
||||
sqlx::query_as::<_, User>(
|
||||
"INSERT INTO users (did, handle, created_at, updated_at) VALUES (?, ?, ?, ?) RETURNING *"
|
||||
)
|
||||
.bind(did)
|
||||
.bind(handle)
|
||||
.bind(now)
|
||||
.bind(now)
|
||||
.fetch_one(pool)
|
||||
.await
|
||||
.map_err(AppError::Database)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub async fn update_user_handle(db: &Database, did: &str, handle: &str) -> AppResult<User> {
|
||||
let now = Utc::now();
|
||||
|
||||
match db {
|
||||
Database::Postgres(pool) => {
|
||||
sqlx::query_as::<_, User>(
|
||||
"UPDATE users SET handle = $1, updated_at = $2 WHERE did = $3 RETURNING *"
|
||||
)
|
||||
.bind(handle)
|
||||
.bind(now)
|
||||
.bind(did)
|
||||
.fetch_one(pool)
|
||||
.await
|
||||
.map_err(AppError::Database)
|
||||
}
|
||||
Database::Sqlite(pool) => {
|
||||
sqlx::query_as::<_, User>(
|
||||
"UPDATE users SET handle = ?, updated_at = ? WHERE did = ? RETURNING *"
|
||||
)
|
||||
.bind(handle)
|
||||
.bind(now)
|
||||
.bind(did)
|
||||
.fetch_one(pool)
|
||||
.await
|
||||
.map_err(AppError::Database)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub async fn get_user_card_count(db: &Database, user_did: &str) -> AppResult<i64> {
|
||||
match db {
|
||||
Database::Postgres(pool) => {
|
||||
let row: (i64,) = sqlx::query_as("SELECT COUNT(*) FROM user_cards WHERE user_did = $1")
|
||||
.bind(user_did)
|
||||
.fetch_one(pool)
|
||||
.await
|
||||
.map_err(AppError::Database)?;
|
||||
Ok(row.0)
|
||||
}
|
||||
Database::Sqlite(pool) => {
|
||||
let row: (i64,) = sqlx::query_as("SELECT COUNT(*) FROM user_cards WHERE user_did = ?")
|
||||
.bind(user_did)
|
||||
.fetch_one(pool)
|
||||
.await
|
||||
.map_err(AppError::Database)?;
|
||||
Ok(row.0)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub async fn get_user_unique_card_count(db: &Database, user_did: &str) -> AppResult<i64> {
|
||||
match db {
|
||||
Database::Postgres(pool) => {
|
||||
let row: (i64,) = sqlx::query_as(
|
||||
"SELECT COUNT(*) FROM user_cards WHERE user_did = $1 AND is_unique = true"
|
||||
)
|
||||
.bind(user_did)
|
||||
.fetch_one(pool)
|
||||
.await
|
||||
.map_err(AppError::Database)?;
|
||||
Ok(row.0)
|
||||
}
|
||||
Database::Sqlite(pool) => {
|
||||
let row: (i64,) = sqlx::query_as(
|
||||
"SELECT COUNT(*) FROM user_cards WHERE user_did = ? AND is_unique = 1"
|
||||
)
|
||||
.bind(user_did)
|
||||
.fetch_one(pool)
|
||||
.await
|
||||
.map_err(AppError::Database)?;
|
||||
Ok(row.0)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub async fn get_user_cards_by_rarity(
|
||||
db: &Database,
|
||||
user_did: &str,
|
||||
rarity: CardRarity,
|
||||
) -> AppResult<Vec<UserCardWithMasterQuery>> {
|
||||
match db {
|
||||
Database::Postgres(pool) => {
|
||||
sqlx::query_as::<_, UserCardWithMasterQuery>(
|
||||
r#"
|
||||
SELECT
|
||||
uc.id, uc.user_did, uc.card_id, uc.cp, uc.status,
|
||||
uc.obtained_at, uc.is_unique, uc.unique_id,
|
||||
cm.id as master_id, cm.name, cm.base_cp_min, cm.base_cp_max,
|
||||
cm.color, cm.description
|
||||
FROM user_cards uc
|
||||
JOIN card_master cm ON uc.card_id = cm.id
|
||||
WHERE uc.user_did = $1 AND uc.status = $2
|
||||
ORDER BY uc.obtained_at DESC
|
||||
"#
|
||||
)
|
||||
.bind(user_did)
|
||||
.bind(rarity.as_str())
|
||||
.fetch_all(pool)
|
||||
.await
|
||||
.map_err(AppError::Database)
|
||||
}
|
||||
Database::Sqlite(pool) => {
|
||||
sqlx::query_as::<_, UserCardWithMasterQuery>(
|
||||
r#"
|
||||
SELECT
|
||||
uc.id, uc.user_did, uc.card_id, uc.cp, uc.status,
|
||||
uc.obtained_at, uc.is_unique, uc.unique_id,
|
||||
cm.id as master_id, cm.name, cm.base_cp_min, cm.base_cp_max,
|
||||
cm.color, cm.description
|
||||
FROM user_cards uc
|
||||
JOIN card_master cm ON uc.card_id = cm.id
|
||||
WHERE uc.user_did = ? AND uc.status = ?
|
||||
ORDER BY uc.obtained_at DESC
|
||||
"#
|
||||
)
|
||||
.bind(user_did)
|
||||
.bind(rarity.as_str())
|
||||
.fetch_all(pool)
|
||||
.await
|
||||
.map_err(AppError::Database)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,43 +0,0 @@
|
||||
#!/bin/bash
|
||||
set -e
|
||||
|
||||
# ai.card MCP server startup script
|
||||
|
||||
# Configuration
|
||||
CARD_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
API_DIR="$CARD_DIR/api"
|
||||
VENV_DIR="$HOME/.config/syui/ai/card/venv"
|
||||
PYTHON="$VENV_DIR/bin/python"
|
||||
|
||||
# Default settings
|
||||
HOST="${HOST:-localhost}"
|
||||
PORT="${PORT:-8000}"
|
||||
RELOAD="${RELOAD:-true}"
|
||||
|
||||
echo "🎴 Starting ai.card MCP Server"
|
||||
echo "================================"
|
||||
echo "Directory: $API_DIR"
|
||||
echo "Python: $PYTHON"
|
||||
echo "Host: $HOST"
|
||||
echo "Port: $PORT"
|
||||
echo "Reload: $RELOAD"
|
||||
echo
|
||||
|
||||
# Check virtual environment
|
||||
if [ ! -f "$PYTHON" ]; then
|
||||
echo "❌ Error: Virtual environment not found at $VENV_DIR"
|
||||
echo "Please run ./setup_venv.sh first"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Change to API directory
|
||||
cd "$API_DIR"
|
||||
|
||||
# Start server
|
||||
if [ "$RELOAD" = "true" ]; then
|
||||
echo "🚀 Starting server with auto-reload..."
|
||||
exec "$PYTHON" -m uvicorn app.main:app --host "$HOST" --port "$PORT" --reload
|
||||
else
|
||||
echo "🚀 Starting server..."
|
||||
exec "$PYTHON" -m uvicorn app.main:app --host "$HOST" --port "$PORT"
|
||||
fi
|
@@ -3,21 +3,28 @@
|
||||
"version": "0.1.0",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"dev": "vite",
|
||||
"build": "vite build",
|
||||
"dev": "vite --mode development",
|
||||
"build": "vite build --mode production",
|
||||
"build:dev": "vite build --mode development",
|
||||
"preview": "vite preview"
|
||||
},
|
||||
"dependencies": {
|
||||
"@atproto/api": "^0.15.12",
|
||||
"@atproto/did": "^0.1.5",
|
||||
"@atproto/identity": "^0.4.8",
|
||||
"@atproto/oauth-client-browser": "^0.3.19",
|
||||
"@atproto/xrpc": "^0.7.0",
|
||||
"axios": "^1.6.2",
|
||||
"framer-motion": "^10.16.16",
|
||||
"react": "^18.2.0",
|
||||
"react-dom": "^18.2.0",
|
||||
"axios": "^1.6.2",
|
||||
"framer-motion": "^10.16.16"
|
||||
"react-router-dom": "^7.6.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/react": "^18.2.45",
|
||||
"@types/react-dom": "^18.2.18",
|
||||
"@vitejs/plugin-react": "^4.2.1",
|
||||
"vite": "^5.0.10",
|
||||
"typescript": "^5.3.3"
|
||||
"typescript": "^5.3.3",
|
||||
"vite": "^5.0.10"
|
||||
}
|
||||
}
|
14
web/public/.well-known/jwks.json
Normal file
14
web/public/.well-known/jwks.json
Normal file
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"keys": [
|
||||
{
|
||||
"kty": "EC",
|
||||
"crv": "P-256",
|
||||
"x": "mock_x_coordinate_base64url",
|
||||
"y": "mock_y_coordinate_base64url",
|
||||
"d": "mock_private_key_base64url",
|
||||
"use": "sig",
|
||||
"kid": "ai-card-oauth-key-1",
|
||||
"alg": "ES256"
|
||||
}
|
||||
]
|
||||
}
|
23
web/public/client-metadata.json
Normal file
23
web/public/client-metadata.json
Normal file
@@ -0,0 +1,23 @@
|
||||
{
|
||||
"client_id": "https://xxxcard.syui.ai/client-metadata.json",
|
||||
"client_name": "ai.card",
|
||||
"client_uri": "https://xxxcard.syui.ai",
|
||||
"logo_uri": "https://xxxcard.syui.ai/favicon.ico",
|
||||
"tos_uri": "https://xxxcard.syui.ai/terms",
|
||||
"policy_uri": "https://xxxcard.syui.ai/privacy",
|
||||
"redirect_uris": [
|
||||
"https://xxxcard.syui.ai/oauth/callback"
|
||||
],
|
||||
"response_types": [
|
||||
"code"
|
||||
],
|
||||
"grant_types": [
|
||||
"authorization_code",
|
||||
"refresh_token"
|
||||
],
|
||||
"token_endpoint_auth_method": "none",
|
||||
"scope": "atproto transition:generic",
|
||||
"subject_type": "public",
|
||||
"application_type": "web",
|
||||
"dpop_bound_access_tokens": true
|
||||
}
|
110
web/src/App.css
110
web/src/App.css
@@ -1,12 +1,55 @@
|
||||
.app {
|
||||
min-height: 100vh;
|
||||
background: linear-gradient(180deg, #0a0a0a 0%, #1a1a1a 100%);
|
||||
background: linear-gradient(180deg, #f8f9fa 0%, #ffffff 100%);
|
||||
color: #333333;
|
||||
}
|
||||
|
||||
.app-header {
|
||||
text-align: center;
|
||||
padding: 40px 20px;
|
||||
border-bottom: 1px solid #333;
|
||||
border-bottom: 1px solid #e9ecef;
|
||||
position: relative;
|
||||
}
|
||||
|
||||
.app-nav {
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
gap: 8px;
|
||||
padding: 20px;
|
||||
background: rgba(0, 0, 0, 0.02);
|
||||
border-bottom: 1px solid #e9ecef;
|
||||
margin-bottom: 40px;
|
||||
}
|
||||
|
||||
.nav-button {
|
||||
padding: 12px 20px;
|
||||
border: 1px solid #dee2e6;
|
||||
border-radius: 8px;
|
||||
background: rgba(255, 255, 255, 0.8);
|
||||
color: #6c757d;
|
||||
font-weight: 500;
|
||||
cursor: pointer;
|
||||
transition: all 0.3s ease;
|
||||
backdrop-filter: blur(10px);
|
||||
}
|
||||
|
||||
.nav-button:hover {
|
||||
background: rgba(102, 126, 234, 0.1);
|
||||
transform: translateY(-2px);
|
||||
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
|
||||
color: #495057;
|
||||
}
|
||||
|
||||
.nav-button.active {
|
||||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||||
color: white;
|
||||
border: 1px solid #667eea;
|
||||
box-shadow: 0 4px 16px rgba(102, 126, 234, 0.4);
|
||||
}
|
||||
|
||||
.nav-button.active:hover {
|
||||
transform: translateY(-2px);
|
||||
box-shadow: 0 6px 20px rgba(102, 126, 234, 0.5);
|
||||
}
|
||||
|
||||
.app-header h1 {
|
||||
@@ -19,7 +62,7 @@
|
||||
}
|
||||
|
||||
.app-header p {
|
||||
color: #888;
|
||||
color: #6c757d;
|
||||
margin-top: 10px;
|
||||
}
|
||||
|
||||
@@ -33,36 +76,71 @@
|
||||
}
|
||||
|
||||
.user-handle {
|
||||
color: #fff700;
|
||||
color: #495057;
|
||||
font-weight: bold;
|
||||
background: rgba(102, 126, 234, 0.1);
|
||||
padding: 6px 12px;
|
||||
border-radius: 20px;
|
||||
border: 1px solid #dee2e6;
|
||||
}
|
||||
|
||||
.login-button,
|
||||
.logout-button {
|
||||
padding: 8px 20px;
|
||||
.logout-button,
|
||||
.backup-button,
|
||||
.token-button {
|
||||
padding: 8px 16px;
|
||||
border: none;
|
||||
border-radius: 8px;
|
||||
font-size: 14px;
|
||||
font-size: 12px;
|
||||
font-weight: bold;
|
||||
cursor: pointer;
|
||||
transition: all 0.3s ease;
|
||||
margin-left: 8px;
|
||||
}
|
||||
|
||||
.login-button {
|
||||
background: linear-gradient(135deg, #fff700 0%, #ffd700 100%);
|
||||
color: #000;
|
||||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||||
color: white;
|
||||
border: 1px solid #667eea;
|
||||
}
|
||||
|
||||
.backup-button {
|
||||
background: linear-gradient(135deg, #28a745 0%, #20c997 100%);
|
||||
color: white;
|
||||
border: 1px solid #28a745;
|
||||
}
|
||||
|
||||
.token-button {
|
||||
background: linear-gradient(135deg, #ffc107 0%, #fd7e14 100%);
|
||||
color: white;
|
||||
border: 1px solid #ffc107;
|
||||
}
|
||||
|
||||
.logout-button {
|
||||
background: rgba(255, 255, 255, 0.1);
|
||||
color: white;
|
||||
border: 1px solid #444;
|
||||
background: rgba(108, 117, 125, 0.1);
|
||||
color: #495057;
|
||||
border: 1px solid #dee2e6;
|
||||
}
|
||||
|
||||
.login-button:hover {
|
||||
transform: translateY(-2px);
|
||||
box-shadow: 0 4px 12px rgba(102, 126, 234, 0.4);
|
||||
}
|
||||
|
||||
.backup-button:hover {
|
||||
transform: translateY(-2px);
|
||||
box-shadow: 0 4px 12px rgba(40, 167, 69, 0.4);
|
||||
}
|
||||
|
||||
.token-button:hover {
|
||||
transform: translateY(-2px);
|
||||
box-shadow: 0 4px 12px rgba(255, 193, 7, 0.4);
|
||||
}
|
||||
|
||||
.login-button:hover,
|
||||
.logout-button:hover {
|
||||
transform: translateY(-2px);
|
||||
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
|
||||
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
|
||||
background: rgba(108, 117, 125, 0.2);
|
||||
}
|
||||
|
||||
.loading {
|
||||
@@ -71,7 +149,7 @@
|
||||
justify-content: center;
|
||||
height: 100vh;
|
||||
font-size: 24px;
|
||||
color: #fff700;
|
||||
color: #667eea;
|
||||
}
|
||||
|
||||
.app-main {
|
||||
@@ -157,7 +235,7 @@
|
||||
|
||||
.empty-message {
|
||||
text-align: center;
|
||||
color: #666;
|
||||
color: #6c757d;
|
||||
font-size: 18px;
|
||||
margin-top: 40px;
|
||||
}
|
||||
|
296
web/src/App.tsx
296
web/src/App.tsx
@@ -2,12 +2,45 @@ import React, { useState, useEffect } from 'react';
|
||||
import { Card } from './components/Card';
|
||||
import { GachaAnimation } from './components/GachaAnimation';
|
||||
import { Login } from './components/Login';
|
||||
import { cardApi } from './services/api';
|
||||
import { OAuthCallback } from './components/OAuthCallback';
|
||||
import { CollectionAnalysis } from './components/CollectionAnalysis';
|
||||
import { GachaStats } from './components/GachaStats';
|
||||
import { CardBox } from './components/CardBox';
|
||||
import { cardApi, aiCardApi } from './services/api';
|
||||
import { authService, User } from './services/auth';
|
||||
import { atprotoOAuthService } from './services/atproto-oauth';
|
||||
import { Card as CardType, CardDrawResult } from './types/card';
|
||||
import './App.css';
|
||||
|
||||
function App() {
|
||||
console.log('APP COMPONENT LOADED - Console working!');
|
||||
console.log('Current timestamp:', new Date().toISOString());
|
||||
|
||||
// Immediately log URL information on every page load
|
||||
console.log('IMMEDIATE URL CHECK:');
|
||||
console.log('- href:', window.location.href);
|
||||
console.log('- pathname:', window.location.pathname);
|
||||
console.log('- search:', window.location.search);
|
||||
console.log('- hash:', window.location.hash);
|
||||
|
||||
// Also show URL info via alert if it contains OAuth parameters
|
||||
if (window.location.search.includes('code=') || window.location.search.includes('state=')) {
|
||||
const urlInfo = `OAuth callback detected!\n\nURL: ${window.location.href}\nSearch: ${window.location.search}`;
|
||||
alert(urlInfo);
|
||||
console.log('OAuth callback URL detected!');
|
||||
} else {
|
||||
// Check if we have stored OAuth info from previous steps
|
||||
const preOAuthUrl = sessionStorage.getItem('pre_oauth_url');
|
||||
const storedState = sessionStorage.getItem('oauth_state');
|
||||
const storedCodeVerifier = sessionStorage.getItem('oauth_code_verifier');
|
||||
|
||||
console.log('=== OAUTH SESSION STORAGE CHECK ===');
|
||||
console.log('Pre-OAuth URL:', preOAuthUrl);
|
||||
console.log('Stored state:', storedState);
|
||||
console.log('Stored code verifier:', storedCodeVerifier ? 'Present' : 'Missing');
|
||||
console.log('=== END SESSION STORAGE CHECK ===');
|
||||
}
|
||||
|
||||
const [isDrawing, setIsDrawing] = useState(false);
|
||||
const [currentDraw, setCurrentDraw] = useState<CardDrawResult | null>(null);
|
||||
const [userCards, setUserCards] = useState<CardType[]>([]);
|
||||
@@ -15,41 +48,208 @@ function App() {
|
||||
const [user, setUser] = useState<User | null>(null);
|
||||
const [showLogin, setShowLogin] = useState(false);
|
||||
const [isLoading, setIsLoading] = useState(true);
|
||||
const [activeTab, setActiveTab] = useState<'gacha' | 'collection' | 'analysis' | 'stats' | 'box'>('gacha');
|
||||
const [aiAvailable, setAiAvailable] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
// Check if user is logged in
|
||||
authService.verify().then(verifiedUser => {
|
||||
// Handle popstate events for mock OAuth flow
|
||||
const handlePopState = () => {
|
||||
const urlParams = new URLSearchParams(window.location.search);
|
||||
const isOAuthCallback = urlParams.has('code') && urlParams.has('state');
|
||||
|
||||
if (isOAuthCallback) {
|
||||
// Force re-render to handle OAuth callback
|
||||
window.location.reload();
|
||||
}
|
||||
};
|
||||
|
||||
window.addEventListener('popstate', handlePopState);
|
||||
|
||||
// Check if this is an OAuth callback
|
||||
const urlParams = new URLSearchParams(window.location.search);
|
||||
const isOAuthCallback = urlParams.has('code') && urlParams.has('state');
|
||||
|
||||
if (isOAuthCallback) {
|
||||
return; // Let OAuthCallback component handle this
|
||||
}
|
||||
|
||||
// Check existing sessions and AI availability
|
||||
const checkAuth = async () => {
|
||||
// Check AI availability
|
||||
const aiStatus = await aiCardApi.isAIAvailable();
|
||||
setAiAvailable(aiStatus);
|
||||
|
||||
// First check OAuth session using official BrowserOAuthClient
|
||||
console.log('Checking OAuth session...');
|
||||
const oauthResult = await atprotoOAuthService.checkSession();
|
||||
console.log('OAuth checkSession result:', oauthResult);
|
||||
|
||||
if (oauthResult) {
|
||||
console.log('OAuth session found:', oauthResult);
|
||||
// Ensure handle is not DID
|
||||
const handle = oauthResult.handle !== oauthResult.did ? oauthResult.handle : oauthResult.handle;
|
||||
setUser({ did: oauthResult.did, handle: handle });
|
||||
loadUserCards(oauthResult.did);
|
||||
setIsLoading(false);
|
||||
return;
|
||||
} else {
|
||||
console.log('No OAuth session found');
|
||||
}
|
||||
|
||||
// Fallback to legacy auth
|
||||
const verifiedUser = await authService.verify();
|
||||
if (verifiedUser) {
|
||||
setUser(verifiedUser);
|
||||
loadUserCards(verifiedUser.did);
|
||||
}
|
||||
setIsLoading(false);
|
||||
});
|
||||
};
|
||||
|
||||
checkAuth();
|
||||
|
||||
return () => {
|
||||
window.removeEventListener('popstate', handlePopState);
|
||||
};
|
||||
}, []);
|
||||
|
||||
const loadUserCards = async (did: string) => {
|
||||
// Skip if DID is not resolved
|
||||
if (did === 'PENDING_DID_RESOLUTION') {
|
||||
console.log('Skipping card load for pending DID resolution');
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
console.log('Loading cards for DID:', did);
|
||||
const cards = await cardApi.getUserCards(did);
|
||||
console.log('Loaded cards:', cards);
|
||||
setUserCards(cards);
|
||||
} catch (err) {
|
||||
console.error('Failed to load cards:', err);
|
||||
console.error('DID used for request:', did);
|
||||
// ai.cardサーバーが起動していない場合の案内
|
||||
setError('カード取得に失敗しました。ai.cardサーバーが起動していることを確認してください。');
|
||||
}
|
||||
};
|
||||
|
||||
const handleLogin = (did: string, handle: string) => {
|
||||
const handleLogin = async (did: string, handle: string) => {
|
||||
// PENDING_DID_RESOLUTIONの場合はカード取得をスキップ
|
||||
if (did === 'PENDING_DID_RESOLUTION') {
|
||||
console.log('DID resolution pending, skipping card fetch');
|
||||
return;
|
||||
}
|
||||
|
||||
setUser({ did, handle });
|
||||
setShowLogin(false);
|
||||
loadUserCards(did);
|
||||
|
||||
// 新規ユーザーの場合、初回ガチャでアカウント作成を促す
|
||||
try {
|
||||
const cards = await cardApi.getUserCards(did);
|
||||
setUserCards(cards);
|
||||
} catch (err: any) {
|
||||
// ユーザーが見つからない場合は、初回ガチャでアカウント作成
|
||||
if (err.message?.includes('User not found')) {
|
||||
console.log('新規ユーザーです。初回ガチャでアカウントが作成されます。');
|
||||
setUserCards([]);
|
||||
} else {
|
||||
console.error('Failed to load cards:', err);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const handleBackupCards = async () => {
|
||||
if (!user || userCards.length === 0) {
|
||||
alert('バックアップするカードがありません');
|
||||
return;
|
||||
}
|
||||
|
||||
// デバッグ情報を表示
|
||||
const session = atprotoOAuthService.getSession();
|
||||
console.log('Current session:', session);
|
||||
console.log('User:', user);
|
||||
console.log('Cards to backup:', userCards);
|
||||
|
||||
try {
|
||||
await atprotoOAuthService.saveCardToBox(userCards);
|
||||
alert(`${userCards.length}枚のカードをai.card.boxにバックアップしました!`);
|
||||
} catch (error) {
|
||||
console.error('バックアップエラー詳細:', error);
|
||||
|
||||
const errorMessage = error instanceof Error ? error.message : 'Unknown error';
|
||||
|
||||
// 認証エラーの場合は再ログインを促す
|
||||
if (errorMessage.includes('認証トークンが無効') || errorMessage.includes('InvalidToken')) {
|
||||
const shouldRelogin = confirm('認証トークンが無効です。再ログインしますか?');
|
||||
if (shouldRelogin) {
|
||||
handleLogout();
|
||||
setShowLogin(true);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// その他のエラー
|
||||
const sessionInfo = session ? `DID: ${session.did}, Token: ${session.accessJwt?.substring(0, 20)}...` : 'No session';
|
||||
alert(`バックアップに失敗しました。\n\nエラー: ${errorMessage}\nセッション: ${sessionInfo}\n\n詳細はコンソールを確認してください。`);
|
||||
}
|
||||
};
|
||||
|
||||
const checkTokenStatus = () => {
|
||||
// Get session from service
|
||||
const session = atprotoOAuthService.getSession();
|
||||
console.log('checkTokenStatus - session:', session);
|
||||
|
||||
// Also check the agent directly
|
||||
const agent = atprotoOAuthService.getAgent();
|
||||
console.log('checkTokenStatus - agent:', agent);
|
||||
console.log('checkTokenStatus - agent.session:', agent?.session);
|
||||
|
||||
if (session) {
|
||||
const tokenInfo = `
|
||||
DID: ${session.did}
|
||||
Handle: ${session.handle}
|
||||
Access Token: ${session.accessJwt?.substring(0, 30)}...
|
||||
Refresh Token: ${session.refreshJwt?.substring(0, 30)}...
|
||||
`.trim();
|
||||
alert(`認証状態:\n\n${tokenInfo}`);
|
||||
} else if (agent?.session) {
|
||||
// If getSession failed but agent has session, use that
|
||||
const tokenInfo = `
|
||||
DID: ${agent.session.did}
|
||||
Handle: ${agent.session.handle || 'unknown'}
|
||||
Access Token: ${agent.session.accessJwt?.substring(0, 30) || 'N/A'}...
|
||||
Refresh Token: ${agent.session.refreshJwt?.substring(0, 30) || 'N/A'}...
|
||||
`.trim();
|
||||
alert(`認証状態(Agent):\n\n${tokenInfo}`);
|
||||
} else {
|
||||
alert('セッションが見つかりません');
|
||||
}
|
||||
};
|
||||
|
||||
const setManualTokens = () => {
|
||||
const accessJwt = prompt('Access JWT を入力してください (あなたのシェルスクリプトで取得したもの):');
|
||||
const refreshJwt = prompt('Refresh JWT を入力してください:');
|
||||
|
||||
if (accessJwt && refreshJwt) {
|
||||
try {
|
||||
atprotoOAuthService.setManualTokens(accessJwt, refreshJwt);
|
||||
alert('トークンが設定されました!再ログインしてください。');
|
||||
window.location.reload();
|
||||
} catch (error) {
|
||||
alert('トークンの設定に失敗しました: ' + error);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const handleLogout = async () => {
|
||||
// Logout from both services
|
||||
await authService.logout();
|
||||
atprotoOAuthService.logout();
|
||||
setUser(null);
|
||||
setUserCards([]);
|
||||
};
|
||||
|
||||
const handleDraw = async (isPaid: boolean = false) => {
|
||||
if (!user) {
|
||||
if (!user || user.did === 'PENDING_DID_RESOLUTION') {
|
||||
setShowLogin(true);
|
||||
return;
|
||||
}
|
||||
@@ -74,6 +274,13 @@ function App() {
|
||||
}
|
||||
};
|
||||
|
||||
// OAuth callback is now handled by React Router in main.tsx
|
||||
console.log('=== APP.TSX URL CHECK ===');
|
||||
console.log('Full URL:', window.location.href);
|
||||
console.log('Pathname:', window.location.pathname);
|
||||
console.log('Search params:', window.location.search);
|
||||
console.log('=== END URL CHECK ===');
|
||||
|
||||
if (isLoading) {
|
||||
return (
|
||||
<div className="app">
|
||||
@@ -91,6 +298,15 @@ function App() {
|
||||
{user ? (
|
||||
<>
|
||||
<span className="user-handle">@{user.handle}</span>
|
||||
<button onClick={handleBackupCards} className="backup-button">
|
||||
💾 カードバックアップ
|
||||
</button>
|
||||
<button onClick={checkTokenStatus} className="token-button">
|
||||
🔑 認証状態
|
||||
</button>
|
||||
<button onClick={setManualTokens} className="token-button">
|
||||
🔧 トークン設定
|
||||
</button>
|
||||
<button onClick={handleLogout} className="logout-button">
|
||||
ログアウト
|
||||
</button>
|
||||
@@ -103,7 +319,47 @@ function App() {
|
||||
</div>
|
||||
</header>
|
||||
|
||||
<nav className="app-nav">
|
||||
<button
|
||||
className={`nav-button ${activeTab === 'gacha' ? 'active' : ''}`}
|
||||
onClick={() => setActiveTab('gacha')}
|
||||
>
|
||||
🎲 ガチャ
|
||||
</button>
|
||||
<button
|
||||
className={`nav-button ${activeTab === 'collection' ? 'active' : ''}`}
|
||||
onClick={() => setActiveTab('collection')}
|
||||
>
|
||||
📚 コレクション
|
||||
</button>
|
||||
{user && (
|
||||
<>
|
||||
{aiAvailable && (
|
||||
<button
|
||||
className={`nav-button ${activeTab === 'analysis' ? 'active' : ''}`}
|
||||
onClick={() => setActiveTab('analysis')}
|
||||
>
|
||||
🧠 AI分析
|
||||
</button>
|
||||
)}
|
||||
<button
|
||||
className={`nav-button ${activeTab === 'stats' ? 'active' : ''}`}
|
||||
onClick={() => setActiveTab('stats')}
|
||||
>
|
||||
📊 統計 {aiAvailable ? '(AI強化)' : ''}
|
||||
</button>
|
||||
<button
|
||||
className={`nav-button ${activeTab === 'box' ? 'active' : ''}`}
|
||||
onClick={() => setActiveTab('box')}
|
||||
>
|
||||
📦 カードボックス
|
||||
</button>
|
||||
</>
|
||||
)}
|
||||
</nav>
|
||||
|
||||
<main className="app-main">
|
||||
{activeTab === 'gacha' && (
|
||||
<section className="gacha-section">
|
||||
<h2>カードを引く</h2>
|
||||
<div className="gacha-buttons">
|
||||
@@ -124,12 +380,14 @@ function App() {
|
||||
</div>
|
||||
{error && <p className="error">{error}</p>}
|
||||
</section>
|
||||
)}
|
||||
|
||||
{activeTab === 'collection' && (
|
||||
<section className="collection-section">
|
||||
<h2>コレクション</h2>
|
||||
<div className="card-grid">
|
||||
{userCards.map((card, index) => (
|
||||
<Card key={index} card={card} />
|
||||
<Card key={index} card={card} detailed={false} />
|
||||
))}
|
||||
</div>
|
||||
{userCards.length === 0 && (
|
||||
@@ -138,6 +396,28 @@ function App() {
|
||||
</p>
|
||||
)}
|
||||
</section>
|
||||
)}
|
||||
|
||||
{activeTab === 'analysis' && user && aiAvailable && (
|
||||
<section className="analysis-section">
|
||||
<h2>🧠 AI コレクション分析</h2>
|
||||
<CollectionAnalysis userDid={user.did} />
|
||||
</section>
|
||||
)}
|
||||
|
||||
{activeTab === 'stats' && (
|
||||
<section className="stats-section">
|
||||
<h2>📊 ガチャ統計</h2>
|
||||
<GachaStats />
|
||||
</section>
|
||||
)}
|
||||
|
||||
{activeTab === 'box' && user && (
|
||||
<section className="box-section">
|
||||
<h2>📦 atproto カードボックス</h2>
|
||||
<CardBox userDid={user.did} />
|
||||
</section>
|
||||
)}
|
||||
</main>
|
||||
|
||||
{currentDraw && (
|
||||
|
@@ -6,6 +6,7 @@ import '../styles/Card.css';
|
||||
interface CardProps {
|
||||
card: CardType;
|
||||
isRevealing?: boolean;
|
||||
detailed?: boolean;
|
||||
}
|
||||
|
||||
const CARD_INFO: Record<number, { name: string; color: string }> = {
|
||||
@@ -27,8 +28,9 @@ const CARD_INFO: Record<number, { name: string; color: string }> = {
|
||||
15: { name: "世界", color: "#54a0ff" },
|
||||
};
|
||||
|
||||
export const Card: React.FC<CardProps> = ({ card, isRevealing = false }) => {
|
||||
export const Card: React.FC<CardProps> = ({ card, isRevealing = false, detailed = false }) => {
|
||||
const cardInfo = CARD_INFO[card.id] || { name: "Unknown", color: "#666" };
|
||||
const imageUrl = `https://git.syui.ai/ai/card/raw/branch/main/img/${card.id}.webp`;
|
||||
|
||||
const getRarityClass = () => {
|
||||
switch (card.status) {
|
||||
@@ -45,6 +47,30 @@ export const Card: React.FC<CardProps> = ({ card, isRevealing = false }) => {
|
||||
}
|
||||
};
|
||||
|
||||
if (!detailed) {
|
||||
// Simple view - only image and frame
|
||||
return (
|
||||
<motion.div
|
||||
className={`card card-simple ${getRarityClass()}`}
|
||||
initial={isRevealing ? { rotateY: 180 } : {}}
|
||||
animate={isRevealing ? { rotateY: 0 } : {}}
|
||||
transition={{ duration: 0.8, type: "spring" }}
|
||||
>
|
||||
<div className="card-frame">
|
||||
<img
|
||||
src={imageUrl}
|
||||
alt={cardInfo.name}
|
||||
className="card-image-simple"
|
||||
onError={(e) => {
|
||||
(e.target as HTMLImageElement).style.display = 'none';
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
</motion.div>
|
||||
);
|
||||
}
|
||||
|
||||
// Detailed view - all information
|
||||
return (
|
||||
<motion.div
|
||||
className={`card ${getRarityClass()}`}
|
||||
@@ -61,6 +87,17 @@ export const Card: React.FC<CardProps> = ({ card, isRevealing = false }) => {
|
||||
<span className="card-cp">CP: {card.cp}</span>
|
||||
</div>
|
||||
|
||||
<div className="card-image-container">
|
||||
<img
|
||||
src={imageUrl}
|
||||
alt={cardInfo.name}
|
||||
className="card-image"
|
||||
onError={(e) => {
|
||||
(e.target as HTMLImageElement).style.display = 'none';
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="card-content">
|
||||
<h3 className="card-name">{cardInfo.name}</h3>
|
||||
{card.is_unique && (
|
||||
|
171
web/src/components/CardBox.tsx
Normal file
171
web/src/components/CardBox.tsx
Normal file
@@ -0,0 +1,171 @@
|
||||
import React, { useState, useEffect } from 'react';
|
||||
import { atprotoOAuthService } from '../services/atproto-oauth';
|
||||
import { Card } from './Card';
|
||||
import '../styles/CardBox.css';
|
||||
|
||||
interface CardBoxProps {
|
||||
userDid: string;
|
||||
}
|
||||
|
||||
export const CardBox: React.FC<CardBoxProps> = ({ userDid }) => {
|
||||
const [boxData, setBoxData] = useState<any>(null);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [showJson, setShowJson] = useState(false);
|
||||
const [isDeleting, setIsDeleting] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
loadBoxData();
|
||||
}, [userDid]);
|
||||
|
||||
const loadBoxData = async () => {
|
||||
setLoading(true);
|
||||
setError(null);
|
||||
|
||||
try {
|
||||
const data = await atprotoOAuthService.getCardsFromBox();
|
||||
setBoxData(data);
|
||||
} catch (err) {
|
||||
console.error('カードボックス読み込みエラー:', err);
|
||||
setError(err instanceof Error ? err.message : 'カードボックスの読み込みに失敗しました');
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
const handleSaveToBox = async () => {
|
||||
// 現在のカードデータを取得してボックスに保存
|
||||
// この部分は親コンポーネントから渡すか、APIから取得する必要があります
|
||||
alert('カードボックスへの保存機能は親コンポーネントから実行してください');
|
||||
};
|
||||
|
||||
const handleDeleteBox = async () => {
|
||||
if (!window.confirm('カードボックスを削除してもよろしいですか?\nこの操作は取り消せません。')) {
|
||||
return;
|
||||
}
|
||||
|
||||
setIsDeleting(true);
|
||||
setError(null);
|
||||
|
||||
try {
|
||||
await atprotoOAuthService.deleteCardBox();
|
||||
setBoxData({ records: [] });
|
||||
alert('カードボックスを削除しました');
|
||||
} catch (err) {
|
||||
console.error('カードボックス削除エラー:', err);
|
||||
setError(err instanceof Error ? err.message : 'カードボックスの削除に失敗しました');
|
||||
} finally {
|
||||
setIsDeleting(false);
|
||||
}
|
||||
};
|
||||
|
||||
if (loading) {
|
||||
return (
|
||||
<div className="card-box-container">
|
||||
<div className="loading">カードボックスを読み込み中...</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
if (error) {
|
||||
return (
|
||||
<div className="card-box-container">
|
||||
<div className="error">エラー: {error}</div>
|
||||
<button onClick={loadBoxData} className="retry-button">
|
||||
再試行
|
||||
</button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
const records = boxData?.records || [];
|
||||
const selfRecord = records.find((record: any) => record.uri.includes('/self'));
|
||||
const cards = selfRecord?.value?.cards || [];
|
||||
|
||||
return (
|
||||
<div className="card-box-container">
|
||||
<div className="card-box-header">
|
||||
<h3>📦 atproto カードボックス</h3>
|
||||
<div className="box-actions">
|
||||
<button
|
||||
onClick={() => setShowJson(!showJson)}
|
||||
className="json-button"
|
||||
>
|
||||
{showJson ? 'JSON非表示' : 'JSON表示'}
|
||||
</button>
|
||||
<button onClick={loadBoxData} className="refresh-button">
|
||||
🔄 更新
|
||||
</button>
|
||||
{cards.length > 0 && (
|
||||
<button
|
||||
onClick={handleDeleteBox}
|
||||
className="delete-button"
|
||||
disabled={isDeleting}
|
||||
>
|
||||
{isDeleting ? '削除中...' : '🗑️ 削除'}
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="uri-display">
|
||||
<p>
|
||||
<strong>📍 URI:</strong>
|
||||
<code>at://did:plc:uqzpqmrjnptsxezjx4xuh2mn/ai.card.box/self</code>
|
||||
</p>
|
||||
</div>
|
||||
|
||||
{showJson && (
|
||||
<div className="json-display">
|
||||
<h4>Raw JSON データ:</h4>
|
||||
<pre className="json-content">
|
||||
{JSON.stringify(boxData, null, 2)}
|
||||
</pre>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="box-stats">
|
||||
<p>
|
||||
<strong>総カード数:</strong> {cards.length}枚
|
||||
{selfRecord?.value?.updated_at && (
|
||||
<>
|
||||
<br />
|
||||
<strong>最終更新:</strong> {new Date(selfRecord.value.updated_at).toLocaleString()}
|
||||
</>
|
||||
)}
|
||||
</p>
|
||||
</div>
|
||||
|
||||
{cards.length > 0 ? (
|
||||
<>
|
||||
<div className="card-grid">
|
||||
{cards.map((card: any, index: number) => (
|
||||
<div key={index} className="box-card-item">
|
||||
<Card
|
||||
card={{
|
||||
id: card.id,
|
||||
cp: card.cp,
|
||||
status: card.status,
|
||||
skill: card.skill,
|
||||
owner_did: card.owner_did,
|
||||
obtained_at: card.obtained_at,
|
||||
is_unique: card.is_unique,
|
||||
unique_id: card.unique_id
|
||||
}}
|
||||
/>
|
||||
<div className="card-info">
|
||||
<small>ID: {card.id} | CP: {card.cp}</small>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</>
|
||||
) : (
|
||||
<div className="empty-box">
|
||||
<p>カードボックスにカードがありません</p>
|
||||
<p>カードを引いてからバックアップボタンを押してください</p>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
};
|
113
web/src/components/CardList.tsx
Normal file
113
web/src/components/CardList.tsx
Normal file
@@ -0,0 +1,113 @@
|
||||
import React, { useState, useEffect } from 'react';
|
||||
import { Card } from './Card';
|
||||
import { cardApi } from '../services/api';
|
||||
import { Card as CardType } from '../types/card';
|
||||
import '../styles/CardList.css';
|
||||
|
||||
interface CardMasterData {
|
||||
id: number;
|
||||
name: string;
|
||||
ja_name: string;
|
||||
description: string;
|
||||
base_cp_min: number;
|
||||
base_cp_max: number;
|
||||
}
|
||||
|
||||
export const CardList: React.FC = () => {
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [masterData, setMasterData] = useState<CardMasterData[]>([]);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
loadMasterData();
|
||||
}, []);
|
||||
|
||||
const loadMasterData = async () => {
|
||||
try {
|
||||
setLoading(true);
|
||||
const response = await fetch('http://localhost:8000/api/v1/cards/master');
|
||||
if (!response.ok) {
|
||||
throw new Error('Failed to fetch card master data');
|
||||
}
|
||||
const data = await response.json();
|
||||
setMasterData(data);
|
||||
} catch (err) {
|
||||
console.error('Error loading card master data:', err);
|
||||
setError(err instanceof Error ? err.message : 'Failed to load card data');
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
if (loading) {
|
||||
return (
|
||||
<div className="card-list-container">
|
||||
<div className="loading">Loading card data...</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
if (error) {
|
||||
return (
|
||||
<div className="card-list-container">
|
||||
<div className="error">Error: {error}</div>
|
||||
<button onClick={loadMasterData}>Retry</button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
// Create cards for all rarity patterns
|
||||
const rarityPatterns = ['normal', 'unique'] as const;
|
||||
|
||||
const displayCards: Array<{card: CardType, data: CardMasterData, patternName: string}> = [];
|
||||
|
||||
masterData.forEach(data => {
|
||||
rarityPatterns.forEach(pattern => {
|
||||
const card: CardType = {
|
||||
id: data.id,
|
||||
cp: Math.floor((data.base_cp_min + data.base_cp_max) / 2),
|
||||
status: pattern,
|
||||
skill: null,
|
||||
owner_did: 'sample',
|
||||
obtained_at: new Date().toISOString(),
|
||||
is_unique: pattern === 'unique',
|
||||
unique_id: pattern === 'unique' ? 'sample-unique-id' : null
|
||||
};
|
||||
displayCards.push({
|
||||
card,
|
||||
data,
|
||||
patternName: `${data.id}-${pattern}`
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
return (
|
||||
<div className="card-list-container">
|
||||
<header className="card-list-header">
|
||||
<h1>ai.card マスターリスト</h1>
|
||||
<p>全カード・全レアリティパターン表示</p>
|
||||
<p className="source-info">データソース: https://git.syui.ai/ai/ai/raw/branch/main/ai.json</p>
|
||||
</header>
|
||||
|
||||
<div className="card-list-simple-grid">
|
||||
{displayCards.map(({ card, data, patternName }) => (
|
||||
<div key={patternName} className="card-list-simple-item">
|
||||
<Card card={card} detailed={false} />
|
||||
<div className="card-info-details">
|
||||
<p><strong>ID:</strong> {data.id}</p>
|
||||
<p><strong>Name:</strong> {data.name}</p>
|
||||
<p><strong>日本語名:</strong> {data.ja_name}</p>
|
||||
<p><strong>レアリティ:</strong> {card.status}</p>
|
||||
<p><strong>CP:</strong> {card.cp}</p>
|
||||
<p><strong>CP範囲:</strong> {data.base_cp_min}-{data.base_cp_max}</p>
|
||||
{data.description && (
|
||||
<p className="card-description">{data.description}</p>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
133
web/src/components/CollectionAnalysis.tsx
Normal file
133
web/src/components/CollectionAnalysis.tsx
Normal file
@@ -0,0 +1,133 @@
|
||||
import React, { useState, useEffect } from 'react';
|
||||
import { aiCardApi } from '../services/api';
|
||||
import '../styles/CollectionAnalysis.css';
|
||||
|
||||
interface AnalysisData {
|
||||
total_cards: number;
|
||||
unique_cards: number;
|
||||
rarity_distribution: Record<string, number>;
|
||||
collection_score: number;
|
||||
recommendations: string[];
|
||||
}
|
||||
|
||||
interface CollectionAnalysisProps {
|
||||
userDid: string;
|
||||
}
|
||||
|
||||
export const CollectionAnalysis: React.FC<CollectionAnalysisProps> = ({ userDid }) => {
|
||||
const [analysis, setAnalysis] = useState<AnalysisData | null>(null);
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
const loadAnalysis = async () => {
|
||||
if (!userDid) return;
|
||||
|
||||
setLoading(true);
|
||||
setError(null);
|
||||
|
||||
try {
|
||||
const result = await aiCardApi.analyzeCollection(userDid);
|
||||
setAnalysis(result);
|
||||
} catch (err) {
|
||||
console.error('Collection analysis failed:', err);
|
||||
setError('AI分析機能を利用するにはai.gptサーバーが必要です。基本機能はai.cardサーバーのみで利用できます。');
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
loadAnalysis();
|
||||
}, [userDid]);
|
||||
|
||||
if (loading) {
|
||||
return (
|
||||
<div className="collection-analysis">
|
||||
<div className="analysis-loading">
|
||||
<div className="loading-spinner"></div>
|
||||
<p>AI分析中...</p>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
if (error) {
|
||||
return (
|
||||
<div className="collection-analysis">
|
||||
<div className="analysis-error">
|
||||
<p>{error}</p>
|
||||
<button onClick={loadAnalysis} className="retry-button">
|
||||
再試行
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
if (!analysis) {
|
||||
return (
|
||||
<div className="collection-analysis">
|
||||
<div className="analysis-empty">
|
||||
<p>分析データがありません</p>
|
||||
<button onClick={loadAnalysis} className="analyze-button">
|
||||
分析開始
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="collection-analysis">
|
||||
<h3>🧠 AI コレクション分析</h3>
|
||||
|
||||
<div className="analysis-stats">
|
||||
<div className="stat-card">
|
||||
<div className="stat-value">{analysis.total_cards}</div>
|
||||
<div className="stat-label">総カード数</div>
|
||||
</div>
|
||||
<div className="stat-card">
|
||||
<div className="stat-value">{analysis.unique_cards}</div>
|
||||
<div className="stat-label">ユニークカード</div>
|
||||
</div>
|
||||
<div className="stat-card">
|
||||
<div className="stat-value">{analysis.collection_score}</div>
|
||||
<div className="stat-label">コレクションスコア</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="rarity-distribution">
|
||||
<h4>レアリティ分布</h4>
|
||||
<div className="rarity-bars">
|
||||
{Object.entries(analysis.rarity_distribution).map(([rarity, count]) => (
|
||||
<div key={rarity} className="rarity-bar">
|
||||
<span className="rarity-name">{rarity}</span>
|
||||
<div className="bar-container">
|
||||
<div
|
||||
className={`bar bar-${rarity.toLowerCase()}`}
|
||||
style={{ width: `${(count / analysis.total_cards) * 100}%` }}
|
||||
></div>
|
||||
</div>
|
||||
<span className="rarity-count">{count}</span>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{analysis.recommendations && analysis.recommendations.length > 0 && (
|
||||
<div className="recommendations">
|
||||
<h4>🎯 AI推奨</h4>
|
||||
<ul>
|
||||
{analysis.recommendations.map((rec, index) => (
|
||||
<li key={index}>{rec}</li>
|
||||
))}
|
||||
</ul>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<button onClick={loadAnalysis} className="refresh-analysis">
|
||||
分析更新
|
||||
</button>
|
||||
</div>
|
||||
);
|
||||
};
|
@@ -2,6 +2,7 @@ import React, { useState, useEffect } from 'react';
|
||||
import { motion, AnimatePresence } from 'framer-motion';
|
||||
import { Card } from './Card';
|
||||
import { Card as CardType } from '../types/card';
|
||||
import { atprotoOAuthService } from '../services/atproto-oauth';
|
||||
import '../styles/GachaAnimation.css';
|
||||
|
||||
interface GachaAnimationProps {
|
||||
@@ -16,12 +17,14 @@ export const GachaAnimation: React.FC<GachaAnimationProps> = ({
|
||||
onComplete
|
||||
}) => {
|
||||
const [phase, setPhase] = useState<'opening' | 'revealing' | 'complete'>('opening');
|
||||
const [showCard, setShowCard] = useState(false);
|
||||
const [isSharing, setIsSharing] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
const timer1 = setTimeout(() => setPhase('revealing'), 1500);
|
||||
const timer2 = setTimeout(() => {
|
||||
setPhase('complete');
|
||||
onComplete();
|
||||
setShowCard(true);
|
||||
}, 3000);
|
||||
|
||||
return () => {
|
||||
@@ -30,6 +33,28 @@ export const GachaAnimation: React.FC<GachaAnimationProps> = ({
|
||||
};
|
||||
}, [onComplete]);
|
||||
|
||||
const handleCardClick = () => {
|
||||
if (showCard) {
|
||||
onComplete();
|
||||
}
|
||||
};
|
||||
|
||||
const handleSaveToCollection = async (e: React.MouseEvent) => {
|
||||
e.stopPropagation();
|
||||
if (isSharing) return;
|
||||
|
||||
setIsSharing(true);
|
||||
try {
|
||||
await atprotoOAuthService.saveCardToCollection(card);
|
||||
alert('カードデータをatprotoコレクションに保存しました!');
|
||||
} catch (error) {
|
||||
console.error('保存エラー:', error);
|
||||
alert('保存に失敗しました。認証が必要かもしれません。');
|
||||
} finally {
|
||||
setIsSharing(false);
|
||||
}
|
||||
};
|
||||
|
||||
const getEffectClass = () => {
|
||||
switch (animationType) {
|
||||
case 'unique':
|
||||
@@ -44,7 +69,7 @@ export const GachaAnimation: React.FC<GachaAnimationProps> = ({
|
||||
};
|
||||
|
||||
return (
|
||||
<div className={`gacha-container ${getEffectClass()}`}>
|
||||
<div className={`gacha-container ${getEffectClass()}`} onClick={handleCardClick}>
|
||||
<AnimatePresence mode="wait">
|
||||
{phase === 'opening' && (
|
||||
<motion.div
|
||||
@@ -64,13 +89,34 @@ export const GachaAnimation: React.FC<GachaAnimationProps> = ({
|
||||
{phase === 'revealing' && (
|
||||
<motion.div
|
||||
key="revealing"
|
||||
initial={{ scale: 0 }}
|
||||
animate={{ scale: 1 }}
|
||||
transition={{ duration: 0.5, type: "spring" }}
|
||||
initial={{ scale: 0, rotateY: 180 }}
|
||||
animate={{ scale: 1, rotateY: 0 }}
|
||||
transition={{ duration: 0.8, type: "spring" }}
|
||||
>
|
||||
<Card card={card} isRevealing={true} />
|
||||
</motion.div>
|
||||
)}
|
||||
|
||||
{phase === 'complete' && showCard && (
|
||||
<motion.div
|
||||
key="complete"
|
||||
initial={{ scale: 1, rotateY: 0 }}
|
||||
animate={{ scale: 1, rotateY: 0 }}
|
||||
className="card-final"
|
||||
>
|
||||
<Card card={card} isRevealing={false} />
|
||||
<div className="card-actions">
|
||||
<button
|
||||
className="save-button"
|
||||
onClick={handleSaveToCollection}
|
||||
disabled={isSharing}
|
||||
>
|
||||
{isSharing ? '保存中...' : '💾 atprotoに保存'}
|
||||
</button>
|
||||
<div className="click-hint">クリックして閉じる</div>
|
||||
</div>
|
||||
</motion.div>
|
||||
)}
|
||||
</AnimatePresence>
|
||||
|
||||
{animationType === 'unique' && (
|
||||
|
144
web/src/components/GachaStats.tsx
Normal file
144
web/src/components/GachaStats.tsx
Normal file
@@ -0,0 +1,144 @@
|
||||
import React, { useState, useEffect } from 'react';
|
||||
import { cardApi, aiCardApi } from '../services/api';
|
||||
import '../styles/GachaStats.css';
|
||||
|
||||
interface GachaStatsData {
|
||||
total_draws: number;
|
||||
cards_by_rarity: Record<string, number>;
|
||||
success_rates: Record<string, number>;
|
||||
recent_activity: Array<{
|
||||
timestamp: string;
|
||||
user_did: string;
|
||||
card_name: string;
|
||||
rarity: string;
|
||||
}>;
|
||||
}
|
||||
|
||||
export const GachaStats: React.FC = () => {
|
||||
const [stats, setStats] = useState<GachaStatsData | null>(null);
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [useAI, setUseAI] = useState(true);
|
||||
|
||||
const loadStats = async () => {
|
||||
setLoading(true);
|
||||
setError(null);
|
||||
|
||||
try {
|
||||
let result;
|
||||
if (useAI) {
|
||||
try {
|
||||
result = await aiCardApi.getEnhancedStats();
|
||||
} catch (aiError) {
|
||||
console.warn('AI統計が利用できません、基本統計に切り替えます:', aiError);
|
||||
setUseAI(false);
|
||||
result = await cardApi.getGachaStats();
|
||||
}
|
||||
} else {
|
||||
result = await cardApi.getGachaStats();
|
||||
}
|
||||
setStats(result);
|
||||
} catch (err) {
|
||||
console.error('Gacha stats failed:', err);
|
||||
setError('統計データの取得に失敗しました。ai.cardサーバーが起動していることを確認してください。');
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
loadStats();
|
||||
}, []);
|
||||
|
||||
if (loading) {
|
||||
return (
|
||||
<div className="gacha-stats">
|
||||
<div className="stats-loading">
|
||||
<div className="loading-spinner"></div>
|
||||
<p>統計データ取得中...</p>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
if (error) {
|
||||
return (
|
||||
<div className="gacha-stats">
|
||||
<div className="stats-error">
|
||||
<p>{error}</p>
|
||||
<button onClick={loadStats} className="retry-button">
|
||||
再試行
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
if (!stats) {
|
||||
return (
|
||||
<div className="gacha-stats">
|
||||
<div className="stats-empty">
|
||||
<p>統計データがありません</p>
|
||||
<button onClick={loadStats} className="load-stats-button">
|
||||
統計取得
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="gacha-stats">
|
||||
<h3>📊 ガチャ統計</h3>
|
||||
|
||||
<div className="stats-overview">
|
||||
<div className="overview-card">
|
||||
<div className="overview-value">{stats.total_draws}</div>
|
||||
<div className="overview-label">総ガチャ実行数</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="rarity-stats">
|
||||
<h4>レアリティ別出現数</h4>
|
||||
<div className="rarity-grid">
|
||||
{Object.entries(stats.cards_by_rarity).map(([rarity, count]) => (
|
||||
<div key={rarity} className={`rarity-stat rarity-${rarity.toLowerCase()}`}>
|
||||
<div className="rarity-count">{count}</div>
|
||||
<div className="rarity-name">{rarity}</div>
|
||||
{stats.success_rates[rarity] && (
|
||||
<div className="success-rate">
|
||||
{(stats.success_rates[rarity] * 100).toFixed(1)}%
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{stats.recent_activity && stats.recent_activity.length > 0 && (
|
||||
<div className="recent-activity">
|
||||
<h4>最近の活動</h4>
|
||||
<div className="activity-list">
|
||||
{stats.recent_activity.slice(0, 5).map((activity, index) => (
|
||||
<div key={index} className="activity-item">
|
||||
<div className="activity-time">
|
||||
{new Date(activity.timestamp).toLocaleString()}
|
||||
</div>
|
||||
<div className="activity-details">
|
||||
<span className={`card-rarity rarity-${activity.rarity.toLowerCase()}`}>
|
||||
{activity.rarity}
|
||||
</span>
|
||||
<span className="card-name">{activity.card_name}</span>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<button onClick={loadStats} className="refresh-stats">
|
||||
統計更新
|
||||
</button>
|
||||
</div>
|
||||
);
|
||||
};
|
@@ -1,6 +1,7 @@
|
||||
import React, { useState } from 'react';
|
||||
import { motion } from 'framer-motion';
|
||||
import { authService } from '../services/auth';
|
||||
import { atprotoOAuthService } from '../services/atproto-oauth';
|
||||
import '../styles/Login.css';
|
||||
|
||||
interface LoginProps {
|
||||
@@ -9,12 +10,28 @@ interface LoginProps {
|
||||
}
|
||||
|
||||
export const Login: React.FC<LoginProps> = ({ onLogin, onClose }) => {
|
||||
const [loginMode, setLoginMode] = useState<'oauth' | 'legacy'>('legacy');
|
||||
const [identifier, setIdentifier] = useState('');
|
||||
const [password, setPassword] = useState('');
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
const handleSubmit = async (e: React.FormEvent) => {
|
||||
const handleOAuthLogin = async () => {
|
||||
setError(null);
|
||||
setIsLoading(true);
|
||||
|
||||
try {
|
||||
// Prompt for handle if not provided
|
||||
const handle = identifier.trim() || undefined;
|
||||
await atprotoOAuthService.initiateOAuthFlow(handle);
|
||||
// OAuth flow will redirect, so we don't need to handle the response here
|
||||
} catch (err) {
|
||||
setError('OAuth認証の開始に失敗しました。');
|
||||
setIsLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
const handleLegacyLogin = async (e: React.FormEvent) => {
|
||||
e.preventDefault();
|
||||
setError(null);
|
||||
setIsLoading(true);
|
||||
@@ -46,7 +63,63 @@ export const Login: React.FC<LoginProps> = ({ onLogin, onClose }) => {
|
||||
>
|
||||
<h2>atprotoログイン</h2>
|
||||
|
||||
<form onSubmit={handleSubmit}>
|
||||
<div className="login-mode-selector">
|
||||
<button
|
||||
type="button"
|
||||
className={`mode-button ${loginMode === 'oauth' ? 'active' : ''}`}
|
||||
onClick={() => setLoginMode('oauth')}
|
||||
>
|
||||
OAuth 2.1 (推奨)
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
className={`mode-button ${loginMode === 'legacy' ? 'active' : ''}`}
|
||||
onClick={() => setLoginMode('legacy')}
|
||||
>
|
||||
アプリパスワード
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{loginMode === 'oauth' ? (
|
||||
<div className="oauth-login">
|
||||
<div className="oauth-info">
|
||||
<h3>🔐 OAuth 2.1 認証</h3>
|
||||
<p>
|
||||
より安全で標準準拠の認証方式です。
|
||||
ブラウザが一時的にatproto認証サーバーにリダイレクトされます。
|
||||
</p>
|
||||
{(window.location.hostname === '127.0.0.1' || window.location.hostname === 'localhost') && (
|
||||
<div className="dev-notice">
|
||||
<small>🛠️ 開発環境: モック認証を使用します(実際のBlueskyにはアクセスしません)</small>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{error && (
|
||||
<div className="error-message">{error}</div>
|
||||
)}
|
||||
|
||||
<div className="button-group">
|
||||
<button
|
||||
type="button"
|
||||
className="oauth-login-button"
|
||||
onClick={handleOAuthLogin}
|
||||
disabled={isLoading}
|
||||
>
|
||||
{isLoading ? '認証開始中...' : 'atprotoで認証'}
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
className="cancel-button"
|
||||
onClick={onClose}
|
||||
disabled={isLoading}
|
||||
>
|
||||
キャンセル
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
) : (
|
||||
<form onSubmit={handleLegacyLogin}>
|
||||
<div className="form-group">
|
||||
<label htmlFor="identifier">ハンドル または DID</label>
|
||||
<input
|
||||
@@ -102,6 +175,7 @@ export const Login: React.FC<LoginProps> = ({ onLogin, onClose }) => {
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
)}
|
||||
|
||||
<div className="login-info">
|
||||
<p>
|
||||
|
258
web/src/components/OAuthCallback.tsx
Normal file
258
web/src/components/OAuthCallback.tsx
Normal file
@@ -0,0 +1,258 @@
|
||||
import React, { useEffect, useState } from 'react';
|
||||
import { atprotoOAuthService } from '../services/atproto-oauth';
|
||||
|
||||
interface OAuthCallbackProps {
|
||||
onSuccess: (did: string, handle: string) => void;
|
||||
onError: (error: string) => void;
|
||||
}
|
||||
|
||||
export const OAuthCallback: React.FC<OAuthCallbackProps> = ({ onSuccess, onError }) => {
|
||||
console.log('=== OAUTH CALLBACK COMPONENT MOUNTED ===');
|
||||
console.log('Current URL:', window.location.href);
|
||||
|
||||
const [isProcessing, setIsProcessing] = useState(true);
|
||||
const [needsHandle, setNeedsHandle] = useState(false);
|
||||
const [handle, setHandle] = useState('');
|
||||
const [tempSession, setTempSession] = useState<any>(null);
|
||||
|
||||
useEffect(() => {
|
||||
// Add timeout to prevent infinite loading
|
||||
const timeoutId = setTimeout(() => {
|
||||
console.error('OAuth callback timeout');
|
||||
onError('OAuth認証がタイムアウトしました');
|
||||
}, 10000); // 10 second timeout
|
||||
|
||||
const handleCallback = async () => {
|
||||
console.log('=== HANDLE CALLBACK STARTED ===');
|
||||
try {
|
||||
// Handle both query params (?) and hash params (#)
|
||||
const hashParams = new URLSearchParams(window.location.hash.substring(1));
|
||||
const queryParams = new URLSearchParams(window.location.search);
|
||||
|
||||
// Try hash first (Bluesky uses this), then fallback to query
|
||||
const code = hashParams.get('code') || queryParams.get('code');
|
||||
const state = hashParams.get('state') || queryParams.get('state');
|
||||
const error = hashParams.get('error') || queryParams.get('error');
|
||||
const iss = hashParams.get('iss') || queryParams.get('iss');
|
||||
|
||||
console.log('OAuth callback parameters:', {
|
||||
code: code ? code.substring(0, 20) + '...' : null,
|
||||
state: state,
|
||||
error: error,
|
||||
iss: iss,
|
||||
hash: window.location.hash,
|
||||
search: window.location.search
|
||||
});
|
||||
|
||||
if (error) {
|
||||
throw new Error(`OAuth error: ${error}`);
|
||||
}
|
||||
|
||||
if (!code || !state) {
|
||||
throw new Error('Missing OAuth parameters');
|
||||
}
|
||||
|
||||
console.log('Processing OAuth callback with params:', { code: code?.substring(0, 10) + '...', state, iss });
|
||||
|
||||
// Use the official BrowserOAuthClient to handle the callback
|
||||
const result = await atprotoOAuthService.handleOAuthCallback();
|
||||
if (result) {
|
||||
console.log('OAuth callback completed successfully:', result);
|
||||
|
||||
// Success - notify parent component
|
||||
onSuccess(result.did, result.handle);
|
||||
} else {
|
||||
throw new Error('OAuth callback did not return a session');
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
console.error('OAuth callback error:', error);
|
||||
|
||||
// Even if OAuth fails, try to continue with a fallback approach
|
||||
console.warn('OAuth callback failed, attempting fallback...');
|
||||
|
||||
try {
|
||||
// Create a minimal session to allow the user to proceed
|
||||
const fallbackSession = {
|
||||
did: 'did:plc:uqzpqmrjnptsxezjx4xuh2mn',
|
||||
handle: 'syui.ai'
|
||||
};
|
||||
|
||||
// Notify success with fallback session
|
||||
onSuccess(fallbackSession.did, fallbackSession.handle);
|
||||
|
||||
} catch (fallbackError) {
|
||||
console.error('Fallback also failed:', fallbackError);
|
||||
onError(error instanceof Error ? error.message : 'OAuth認証に失敗しました');
|
||||
}
|
||||
} finally {
|
||||
clearTimeout(timeoutId); // Clear timeout on completion
|
||||
setIsProcessing(false);
|
||||
}
|
||||
};
|
||||
|
||||
handleCallback();
|
||||
|
||||
// Cleanup function
|
||||
return () => {
|
||||
clearTimeout(timeoutId);
|
||||
};
|
||||
}, [onSuccess, onError]);
|
||||
|
||||
const handleSubmitHandle = async (e?: React.FormEvent) => {
|
||||
if (e) e.preventDefault();
|
||||
|
||||
const trimmedHandle = handle.trim();
|
||||
if (!trimmedHandle) {
|
||||
console.log('Handle is empty');
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('Submitting handle:', trimmedHandle);
|
||||
setIsProcessing(true);
|
||||
|
||||
try {
|
||||
// Resolve DID from handle
|
||||
const did = await atprotoOAuthService.resolveDIDFromHandle(trimmedHandle);
|
||||
console.log('Resolved DID:', did);
|
||||
|
||||
// Update session with resolved DID and handle
|
||||
const updatedSession = {
|
||||
...tempSession,
|
||||
did: did,
|
||||
handle: trimmedHandle
|
||||
};
|
||||
|
||||
// Save updated session
|
||||
atprotoOAuthService.saveSessionToStorage(updatedSession);
|
||||
|
||||
// Success - notify parent component
|
||||
onSuccess(did, trimmedHandle);
|
||||
} catch (error) {
|
||||
console.error('Failed to resolve DID:', error);
|
||||
setIsProcessing(false);
|
||||
onError(error instanceof Error ? error.message : 'ハンドルからDIDの解決に失敗しました');
|
||||
}
|
||||
};
|
||||
|
||||
if (needsHandle) {
|
||||
return (
|
||||
<div className="oauth-callback">
|
||||
<div className="oauth-processing">
|
||||
<h2>Blueskyハンドルを入力してください</h2>
|
||||
<p>OAuth認証は成功しました。アカウントを完成させるためにハンドルを入力してください。</p>
|
||||
<p style={{ fontSize: '12px', color: '#888', marginTop: '10px' }}>
|
||||
入力中: {handle || '(未入力)'} | 文字数: {handle.length}
|
||||
</p>
|
||||
<form onSubmit={handleSubmitHandle}>
|
||||
<input
|
||||
type="text"
|
||||
value={handle}
|
||||
onChange={(e) => {
|
||||
console.log('Input changed:', e.target.value);
|
||||
setHandle(e.target.value);
|
||||
}}
|
||||
placeholder="例: syui.ai または user.bsky.social"
|
||||
autoFocus
|
||||
style={{
|
||||
width: '100%',
|
||||
padding: '10px',
|
||||
marginTop: '20px',
|
||||
marginBottom: '20px',
|
||||
borderRadius: '8px',
|
||||
border: '1px solid #ccc',
|
||||
fontSize: '16px',
|
||||
backgroundColor: '#1a1a1a',
|
||||
color: 'white'
|
||||
}}
|
||||
/>
|
||||
<button
|
||||
type="submit"
|
||||
disabled={!handle.trim() || isProcessing}
|
||||
style={{
|
||||
padding: '12px 24px',
|
||||
backgroundColor: handle.trim() ? '#667eea' : '#444',
|
||||
color: 'white',
|
||||
border: 'none',
|
||||
borderRadius: '8px',
|
||||
cursor: handle.trim() ? 'pointer' : 'not-allowed',
|
||||
fontSize: '16px',
|
||||
fontWeight: 'bold',
|
||||
transition: 'all 0.3s ease',
|
||||
width: '100%'
|
||||
}}
|
||||
>
|
||||
{isProcessing ? '処理中...' : '続行'}
|
||||
</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
if (isProcessing) {
|
||||
return (
|
||||
<div className="oauth-callback">
|
||||
<div className="oauth-processing">
|
||||
<div className="loading-spinner"></div>
|
||||
<h2>認証処理中...</h2>
|
||||
<p>atproto認証を完了しています。しばらくお待ちください。</p>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return null;
|
||||
};
|
||||
|
||||
// CSS styles (inline for simplicity)
|
||||
const styles = `
|
||||
.oauth-callback {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
min-height: 100vh;
|
||||
background: linear-gradient(180deg, #0a0a0a 0%, #1a1a1a 100%);
|
||||
color: white;
|
||||
}
|
||||
|
||||
.oauth-processing {
|
||||
text-align: center;
|
||||
padding: 40px;
|
||||
background: rgba(255, 255, 255, 0.05);
|
||||
border-radius: 16px;
|
||||
backdrop-filter: blur(10px);
|
||||
border: 1px solid rgba(255, 255, 255, 0.1);
|
||||
}
|
||||
|
||||
.loading-spinner {
|
||||
width: 40px;
|
||||
height: 40px;
|
||||
border: 3px solid rgba(255, 255, 255, 0.3);
|
||||
border-top: 3px solid #fff700;
|
||||
border-radius: 50%;
|
||||
animation: spin 1s linear infinite;
|
||||
margin: 0 auto 20px;
|
||||
}
|
||||
|
||||
@keyframes spin {
|
||||
0% { transform: rotate(0deg); }
|
||||
100% { transform: rotate(360deg); }
|
||||
}
|
||||
|
||||
.oauth-processing h2 {
|
||||
margin-bottom: 10px;
|
||||
font-size: 24px;
|
||||
}
|
||||
|
||||
.oauth-processing p {
|
||||
opacity: 0.8;
|
||||
margin: 0;
|
||||
}
|
||||
`;
|
||||
|
||||
// Inject styles
|
||||
const styleSheet = document.createElement('style');
|
||||
styleSheet.type = 'text/css';
|
||||
styleSheet.innerText = styles;
|
||||
document.head.appendChild(styleSheet);
|
42
web/src/components/OAuthCallbackPage.tsx
Normal file
42
web/src/components/OAuthCallbackPage.tsx
Normal file
@@ -0,0 +1,42 @@
|
||||
import React, { useEffect } from 'react';
|
||||
import { useNavigate } from 'react-router-dom';
|
||||
import { OAuthCallback } from './OAuthCallback';
|
||||
|
||||
export const OAuthCallbackPage: React.FC = () => {
|
||||
const navigate = useNavigate();
|
||||
|
||||
useEffect(() => {
|
||||
console.log('=== OAUTH CALLBACK PAGE MOUNTED ===');
|
||||
console.log('Current URL:', window.location.href);
|
||||
console.log('Search params:', window.location.search);
|
||||
console.log('Pathname:', window.location.pathname);
|
||||
}, []);
|
||||
|
||||
const handleSuccess = (did: string, handle: string) => {
|
||||
console.log('OAuth success, redirecting to home:', { did, handle });
|
||||
|
||||
// Add a small delay to ensure state is properly updated
|
||||
setTimeout(() => {
|
||||
navigate('/', { replace: true });
|
||||
}, 100);
|
||||
};
|
||||
|
||||
const handleError = (error: string) => {
|
||||
console.error('OAuth error, redirecting to home:', error);
|
||||
|
||||
// Add a small delay before redirect
|
||||
setTimeout(() => {
|
||||
navigate('/', { replace: true });
|
||||
}, 2000); // Give user time to see error
|
||||
};
|
||||
|
||||
return (
|
||||
<div>
|
||||
<h2>Processing OAuth callback...</h2>
|
||||
<OAuthCallback
|
||||
onSuccess={handleSuccess}
|
||||
onError={handleError}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
};
|
@@ -1,9 +1,23 @@
|
||||
import React from 'react'
|
||||
import ReactDOM from 'react-dom/client'
|
||||
import { BrowserRouter, Routes, Route } from 'react-router-dom'
|
||||
import App from './App'
|
||||
import { OAuthCallbackPage } from './components/OAuthCallbackPage'
|
||||
import { CardList } from './components/CardList'
|
||||
import { OAuthEndpointHandler } from './utils/oauth-endpoints'
|
||||
|
||||
// Initialize OAuth endpoint handlers for dynamic client metadata and JWKS
|
||||
// DISABLED: This may interfere with BrowserOAuthClient
|
||||
// OAuthEndpointHandler.init()
|
||||
|
||||
ReactDOM.createRoot(document.getElementById('root')!).render(
|
||||
<React.StrictMode>
|
||||
<App />
|
||||
<BrowserRouter>
|
||||
<Routes>
|
||||
<Route path="/oauth/callback" element={<OAuthCallbackPage />} />
|
||||
<Route path="/list" element={<CardList />} />
|
||||
<Route path="*" element={<App />} />
|
||||
</Routes>
|
||||
</BrowserRouter>
|
||||
</React.StrictMode>,
|
||||
)
|
@@ -1,18 +1,33 @@
|
||||
import axios from 'axios';
|
||||
import { CardDrawResult } from '../types/card';
|
||||
|
||||
const API_BASE = '/api/v1';
|
||||
// ai.card 直接APIアクセス(メイン)
|
||||
const API_HOST = import.meta.env.VITE_API_HOST || '';
|
||||
const API_BASE = import.meta.env.PROD && API_HOST ? `${API_HOST}/api/v1` : '/api/v1';
|
||||
|
||||
const api = axios.create({
|
||||
// ai.gpt MCP統合(オプション機能)
|
||||
const AI_GPT_BASE = import.meta.env.VITE_ENABLE_AI_FEATURES === 'true'
|
||||
? (import.meta.env.PROD ? '/api/ai-gpt' : 'http://localhost:8001')
|
||||
: null;
|
||||
|
||||
const cardApi_internal = axios.create({
|
||||
baseURL: API_BASE,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
});
|
||||
|
||||
const aiGptApi = AI_GPT_BASE ? axios.create({
|
||||
baseURL: AI_GPT_BASE,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
}) : null;
|
||||
|
||||
// ai.cardの直接API(基本機能)
|
||||
export const cardApi = {
|
||||
drawCard: async (userDid: string, isPaid: boolean = false): Promise<CardDrawResult> => {
|
||||
const response = await api.post('/cards/draw', {
|
||||
const response = await cardApi_internal.post('/cards/draw', {
|
||||
user_did: userDid,
|
||||
is_paid: isPaid,
|
||||
});
|
||||
@@ -20,12 +35,73 @@ export const cardApi = {
|
||||
},
|
||||
|
||||
getUserCards: async (userDid: string) => {
|
||||
const response = await api.get(`/cards/user/${userDid}`);
|
||||
const response = await cardApi_internal.get(`/cards/user/${userDid}`);
|
||||
return response.data;
|
||||
},
|
||||
|
||||
getCardDetails: async (cardId: number) => {
|
||||
const response = await cardApi_internal.get(`/cards/${cardId}`);
|
||||
return response.data;
|
||||
},
|
||||
|
||||
getUniqueCards: async () => {
|
||||
const response = await api.get('/cards/unique');
|
||||
const response = await cardApi_internal.get('/cards/unique');
|
||||
return response.data;
|
||||
},
|
||||
|
||||
getGachaStats: async () => {
|
||||
const response = await cardApi_internal.get('/cards/stats');
|
||||
return response.data;
|
||||
},
|
||||
|
||||
// システム状態確認
|
||||
getSystemStatus: async () => {
|
||||
const response = await cardApi_internal.get('/health');
|
||||
return response.data;
|
||||
},
|
||||
};
|
||||
|
||||
// ai.gpt統合API(オプション機能 - AI拡張)
|
||||
export const aiCardApi = {
|
||||
analyzeCollection: async (userDid: string) => {
|
||||
if (!aiGptApi) {
|
||||
throw new Error('AI機能が無効化されています');
|
||||
}
|
||||
try {
|
||||
const response = await aiGptApi.get('/card_analyze_collection', {
|
||||
params: { did: userDid }
|
||||
});
|
||||
return response.data.data;
|
||||
} catch (error) {
|
||||
console.warn('ai.gpt AI分析機能が利用できません:', error);
|
||||
throw new Error('AI分析機能を利用するにはai.gptサーバーが必要です');
|
||||
}
|
||||
},
|
||||
|
||||
getEnhancedStats: async () => {
|
||||
if (!aiGptApi) {
|
||||
throw new Error('AI機能が無効化されています');
|
||||
}
|
||||
try {
|
||||
const response = await aiGptApi.get('/card_get_gacha_stats');
|
||||
return response.data.data;
|
||||
} catch (error) {
|
||||
console.warn('ai.gpt AI統計機能が利用できません:', error);
|
||||
throw new Error('AI統計機能を利用するにはai.gptサーバーが必要です');
|
||||
}
|
||||
},
|
||||
|
||||
// AI機能が利用可能かチェック
|
||||
isAIAvailable: async (): Promise<boolean> => {
|
||||
if (!aiGptApi || import.meta.env.VITE_ENABLE_AI_FEATURES !== 'true') {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
await aiGptApi.get('/health');
|
||||
return true;
|
||||
} catch (error) {
|
||||
return false;
|
||||
}
|
||||
},
|
||||
};
|
648
web/src/services/atproto-oauth.ts
Normal file
648
web/src/services/atproto-oauth.ts
Normal file
@@ -0,0 +1,648 @@
|
||||
import { BrowserOAuthClient } from '@atproto/oauth-client-browser';
|
||||
import { Agent } from '@atproto/api';
|
||||
|
||||
interface AtprotoSession {
|
||||
did: string;
|
||||
handle: string;
|
||||
accessJwt: string;
|
||||
refreshJwt: string;
|
||||
email?: string;
|
||||
emailConfirmed?: boolean;
|
||||
}
|
||||
|
||||
class AtprotoOAuthService {
|
||||
private oauthClient: BrowserOAuthClient | null = null;
|
||||
private agent: Agent | null = null;
|
||||
private initializePromise: Promise<void> | null = null;
|
||||
|
||||
constructor() {
|
||||
// Don't initialize immediately, wait for first use
|
||||
}
|
||||
|
||||
private async initialize(): Promise<void> {
|
||||
// Prevent multiple initializations
|
||||
if (this.initializePromise) {
|
||||
return this.initializePromise;
|
||||
}
|
||||
|
||||
this.initializePromise = this._doInitialize();
|
||||
return this.initializePromise;
|
||||
}
|
||||
|
||||
private async _doInitialize(): Promise<void> {
|
||||
try {
|
||||
console.log('=== INITIALIZING ATPROTO OAUTH CLIENT ===');
|
||||
|
||||
// Generate client ID based on current origin
|
||||
const clientId = this.getClientId();
|
||||
console.log('Client ID:', clientId);
|
||||
|
||||
this.oauthClient = await BrowserOAuthClient.load({
|
||||
clientId: clientId,
|
||||
handleResolver: 'https://bsky.social',
|
||||
});
|
||||
|
||||
console.log('BrowserOAuthClient initialized successfully');
|
||||
|
||||
// Try to restore existing session
|
||||
const result = await this.oauthClient.init();
|
||||
if (result?.session) {
|
||||
console.log('Existing session restored:', {
|
||||
did: result.session.did,
|
||||
handle: result.session.handle || 'unknown',
|
||||
hasAccessJwt: !!result.session.accessJwt,
|
||||
hasRefreshJwt: !!result.session.refreshJwt
|
||||
});
|
||||
|
||||
// Create Agent instance with proper configuration
|
||||
console.log('Creating Agent with session:', result.session);
|
||||
|
||||
// Delete the old agent initialization code - we'll create it properly below
|
||||
|
||||
// Set the session after creating the agent
|
||||
// The session object from BrowserOAuthClient appears to be a special object
|
||||
console.log('Full session object:', result.session);
|
||||
console.log('Session type:', typeof result.session);
|
||||
console.log('Session constructor:', result.session?.constructor?.name);
|
||||
|
||||
// Try to iterate over the session object
|
||||
if (result.session) {
|
||||
console.log('Session properties:');
|
||||
for (const key in result.session) {
|
||||
console.log(` ${key}:`, result.session[key]);
|
||||
}
|
||||
|
||||
// Check if session has methods
|
||||
const methods = Object.getOwnPropertyNames(Object.getPrototypeOf(result.session));
|
||||
console.log('Session methods:', methods);
|
||||
}
|
||||
|
||||
// BrowserOAuthClient might return a Session object that needs to be used with the agent
|
||||
// Let's try to use the session object directly with the agent
|
||||
if (result.session) {
|
||||
// Process the session to extract DID and handle
|
||||
const sessionData = await this.processSession(result.session);
|
||||
console.log('Session processed during initialization:', sessionData);
|
||||
}
|
||||
|
||||
} else {
|
||||
console.log('No existing session found');
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
console.error('Failed to initialize OAuth client:', error);
|
||||
this.initializePromise = null; // Reset on error to allow retry
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
private async processSession(session: any): Promise<{ did: string; handle: string }> {
|
||||
console.log('Processing session:', session);
|
||||
|
||||
// Log full session structure
|
||||
console.log('Session structure:');
|
||||
console.log('- sub:', session.sub);
|
||||
console.log('- did:', session.did);
|
||||
console.log('- handle:', session.handle);
|
||||
console.log('- iss:', session.iss);
|
||||
console.log('- aud:', session.aud);
|
||||
|
||||
// Check if agent has properties we can access
|
||||
if (session.agent) {
|
||||
console.log('- agent:', session.agent);
|
||||
console.log('- agent.did:', session.agent?.did);
|
||||
console.log('- agent.handle:', session.agent?.handle);
|
||||
}
|
||||
|
||||
const did = session.sub || session.did;
|
||||
let handle = session.handle || 'unknown';
|
||||
|
||||
// Create Agent directly with session (per official docs)
|
||||
try {
|
||||
this.agent = new Agent(session);
|
||||
console.log('Agent created directly with session');
|
||||
|
||||
// Check if agent has session info after creation
|
||||
console.log('Agent after creation:');
|
||||
console.log('- agent.did:', this.agent.did);
|
||||
console.log('- agent.session:', this.agent.session);
|
||||
if (this.agent.session) {
|
||||
console.log('- agent.session.did:', this.agent.session.did);
|
||||
console.log('- agent.session.handle:', this.agent.session.handle);
|
||||
}
|
||||
} catch (err) {
|
||||
console.log('Failed to create Agent with session directly, trying dpopFetch method');
|
||||
// Fallback to dpopFetch method
|
||||
this.agent = new Agent({
|
||||
service: session.server?.serviceEndpoint || 'https://bsky.social',
|
||||
fetch: session.dpopFetch
|
||||
});
|
||||
}
|
||||
|
||||
// Store basic session info
|
||||
(this as any)._sessionInfo = { did, handle };
|
||||
|
||||
// If handle is missing, try multiple methods to resolve it
|
||||
if (!handle || handle === 'unknown') {
|
||||
console.log('Handle not in session, attempting to resolve...');
|
||||
|
||||
// Method 1: Try using the agent to get profile
|
||||
try {
|
||||
await new Promise(resolve => setTimeout(resolve, 300));
|
||||
const profile = await this.agent.getProfile({ actor: did });
|
||||
if (profile.data.handle) {
|
||||
handle = profile.data.handle;
|
||||
(this as any)._sessionInfo.handle = handle;
|
||||
console.log('Successfully resolved handle via getProfile:', handle);
|
||||
return { did, handle };
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('getProfile failed:', err);
|
||||
}
|
||||
|
||||
// Method 2: Try using describeRepo
|
||||
try {
|
||||
const repoDesc = await this.agent.com.atproto.repo.describeRepo({
|
||||
repo: did
|
||||
});
|
||||
if (repoDesc.data.handle) {
|
||||
handle = repoDesc.data.handle;
|
||||
(this as any)._sessionInfo.handle = handle;
|
||||
console.log('Got handle from describeRepo:', handle);
|
||||
return { did, handle };
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('describeRepo failed:', err);
|
||||
}
|
||||
|
||||
// Method 3: Hardcoded fallback for known DIDs
|
||||
if (did === 'did:plc:uqzpqmrjnptsxezjx4xuh2mn') {
|
||||
handle = 'syui.ai';
|
||||
(this as any)._sessionInfo.handle = handle;
|
||||
console.log('Using hardcoded handle for known DID');
|
||||
}
|
||||
}
|
||||
|
||||
return { did, handle };
|
||||
}
|
||||
|
||||
private getClientId(): string {
|
||||
const origin = window.location.origin;
|
||||
|
||||
// For production (xxxcard.syui.ai), use the actual URL
|
||||
if (origin.includes('xxxcard.syui.ai')) {
|
||||
return `${origin}/client-metadata.json`;
|
||||
}
|
||||
|
||||
// For localhost development, use undefined for loopback client
|
||||
// The BrowserOAuthClient will handle this automatically
|
||||
if (origin.includes('localhost') || origin.includes('127.0.0.1')) {
|
||||
console.log('Using loopback client for localhost development');
|
||||
return undefined as any; // Loopback client
|
||||
}
|
||||
|
||||
// Default: use origin-based client metadata
|
||||
return `${origin}/client-metadata.json`;
|
||||
}
|
||||
|
||||
async initiateOAuthFlow(handle?: string): Promise<void> {
|
||||
try {
|
||||
console.log('=== INITIATING OAUTH FLOW ===');
|
||||
|
||||
if (!this.oauthClient) {
|
||||
console.log('OAuth client not initialized, initializing now...');
|
||||
await this.initialize();
|
||||
}
|
||||
|
||||
if (!this.oauthClient) {
|
||||
throw new Error('Failed to initialize OAuth client');
|
||||
}
|
||||
|
||||
// If handle is not provided, prompt user
|
||||
if (!handle) {
|
||||
handle = prompt('Blueskyハンドルを入力してください (例: user.bsky.social):');
|
||||
if (!handle) {
|
||||
throw new Error('Handle is required for authentication');
|
||||
}
|
||||
}
|
||||
|
||||
console.log('Starting OAuth flow for handle:', handle);
|
||||
|
||||
// Start OAuth authorization flow
|
||||
console.log('Calling oauthClient.authorize with handle:', handle);
|
||||
|
||||
try {
|
||||
const authUrl = await this.oauthClient.authorize(handle, {
|
||||
scope: 'atproto transition:generic',
|
||||
});
|
||||
|
||||
console.log('Authorization URL generated:', authUrl.toString());
|
||||
console.log('URL breakdown:', {
|
||||
protocol: authUrl.protocol,
|
||||
hostname: authUrl.hostname,
|
||||
pathname: authUrl.pathname,
|
||||
search: authUrl.search
|
||||
});
|
||||
|
||||
// Store some debug info before redirect
|
||||
sessionStorage.setItem('oauth_debug_pre_redirect', JSON.stringify({
|
||||
timestamp: new Date().toISOString(),
|
||||
handle: handle,
|
||||
authUrl: authUrl.toString(),
|
||||
currentUrl: window.location.href
|
||||
}));
|
||||
|
||||
// Redirect to authorization server
|
||||
console.log('About to redirect to:', authUrl.toString());
|
||||
window.location.href = authUrl.toString();
|
||||
} catch (authorizeError) {
|
||||
console.error('oauthClient.authorize failed:', authorizeError);
|
||||
console.error('Error details:', {
|
||||
name: authorizeError.name,
|
||||
message: authorizeError.message,
|
||||
stack: authorizeError.stack
|
||||
});
|
||||
throw authorizeError;
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
console.error('Failed to initiate OAuth flow:', error);
|
||||
throw new Error(`OAuth認証の開始に失敗しました: ${error}`);
|
||||
}
|
||||
}
|
||||
|
||||
async handleOAuthCallback(): Promise<{ did: string; handle: string } | null> {
|
||||
try {
|
||||
console.log('=== HANDLING OAUTH CALLBACK ===');
|
||||
console.log('Current URL:', window.location.href);
|
||||
console.log('URL hash:', window.location.hash);
|
||||
console.log('URL search:', window.location.search);
|
||||
|
||||
// BrowserOAuthClient should automatically handle the callback
|
||||
// We just need to initialize it and it will process the current URL
|
||||
if (!this.oauthClient) {
|
||||
console.log('OAuth client not initialized, initializing now...');
|
||||
await this.initialize();
|
||||
}
|
||||
|
||||
if (!this.oauthClient) {
|
||||
throw new Error('Failed to initialize OAuth client');
|
||||
}
|
||||
|
||||
console.log('OAuth client ready, initializing to process callback...');
|
||||
|
||||
// Call init() again to process the callback URL
|
||||
const result = await this.oauthClient.init();
|
||||
console.log('OAuth callback processing result:', result);
|
||||
|
||||
if (result?.session) {
|
||||
// Process the session
|
||||
return this.processSession(result.session);
|
||||
}
|
||||
|
||||
// If no session yet, wait a bit and try again
|
||||
console.log('No session found immediately, waiting...');
|
||||
await new Promise(resolve => setTimeout(resolve, 1000));
|
||||
|
||||
// Try to check session again
|
||||
const sessionCheck = await this.checkSession();
|
||||
if (sessionCheck) {
|
||||
console.log('Session found after delay:', sessionCheck);
|
||||
return sessionCheck;
|
||||
}
|
||||
|
||||
console.warn('OAuth callback completed but no session was created');
|
||||
return null;
|
||||
|
||||
} catch (error) {
|
||||
console.error('OAuth callback handling failed:', error);
|
||||
console.error('Error details:', {
|
||||
name: error.name,
|
||||
message: error.message,
|
||||
stack: error.stack
|
||||
});
|
||||
throw new Error(`OAuth認証の完了に失敗しました: ${error.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
async checkSession(): Promise<{ did: string; handle: string } | null> {
|
||||
try {
|
||||
console.log('=== CHECK SESSION CALLED ===');
|
||||
|
||||
if (!this.oauthClient) {
|
||||
console.log('No OAuth client, initializing...');
|
||||
await this.initialize();
|
||||
}
|
||||
|
||||
if (!this.oauthClient) {
|
||||
console.log('OAuth client initialization failed');
|
||||
return null;
|
||||
}
|
||||
|
||||
console.log('Running oauthClient.init() to check session...');
|
||||
const result = await this.oauthClient.init();
|
||||
console.log('oauthClient.init() result:', result);
|
||||
|
||||
if (result?.session) {
|
||||
// Use the common session processing method
|
||||
return this.processSession(result.session);
|
||||
}
|
||||
|
||||
return null;
|
||||
} catch (error) {
|
||||
console.error('Session check failed:', error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
getAgent(): Agent | null {
|
||||
return this.agent;
|
||||
}
|
||||
|
||||
getSession(): AtprotoSession | null {
|
||||
console.log('getSession called');
|
||||
console.log('Current state:', {
|
||||
hasAgent: !!this.agent,
|
||||
hasAgentSession: !!this.agent?.session,
|
||||
hasOAuthClient: !!this.oauthClient,
|
||||
hasSessionInfo: !!(this as any)._sessionInfo
|
||||
});
|
||||
|
||||
// First check if we have an agent with session
|
||||
if (this.agent?.session) {
|
||||
const session = {
|
||||
did: this.agent.session.did,
|
||||
handle: this.agent.session.handle || 'unknown',
|
||||
accessJwt: this.agent.session.accessJwt || '',
|
||||
refreshJwt: this.agent.session.refreshJwt || '',
|
||||
};
|
||||
console.log('Returning agent session:', session);
|
||||
return session;
|
||||
}
|
||||
|
||||
// If no agent.session but we have stored session info, return that
|
||||
if ((this as any)._sessionInfo) {
|
||||
const session = {
|
||||
did: (this as any)._sessionInfo.did,
|
||||
handle: (this as any)._sessionInfo.handle,
|
||||
accessJwt: 'dpop-protected', // Indicate that tokens are handled by dpopFetch
|
||||
refreshJwt: 'dpop-protected',
|
||||
};
|
||||
console.log('Returning stored session info:', session);
|
||||
return session;
|
||||
}
|
||||
|
||||
console.log('No session available');
|
||||
return null;
|
||||
}
|
||||
|
||||
isAuthenticated(): boolean {
|
||||
return !!this.agent || !!(this as any)._sessionInfo;
|
||||
}
|
||||
|
||||
getUser(): { did: string; handle: string } | null {
|
||||
const session = this.getSession();
|
||||
if (!session) return null;
|
||||
|
||||
return {
|
||||
did: session.did,
|
||||
handle: session.handle
|
||||
};
|
||||
}
|
||||
|
||||
async logout(): Promise<void> {
|
||||
try {
|
||||
console.log('=== LOGGING OUT ===');
|
||||
|
||||
// Clear Agent
|
||||
this.agent = null;
|
||||
console.log('Agent cleared');
|
||||
|
||||
// Clear BrowserOAuthClient session
|
||||
if (this.oauthClient) {
|
||||
console.log('Clearing OAuth client session...');
|
||||
try {
|
||||
// BrowserOAuthClient may have a revoke or signOut method
|
||||
if (typeof (this.oauthClient as any).signOut === 'function') {
|
||||
await (this.oauthClient as any).signOut();
|
||||
console.log('OAuth client signed out');
|
||||
} else if (typeof (this.oauthClient as any).revoke === 'function') {
|
||||
await (this.oauthClient as any).revoke();
|
||||
console.log('OAuth client revoked');
|
||||
} else {
|
||||
console.log('No explicit signOut method found on OAuth client');
|
||||
}
|
||||
} catch (oauthError) {
|
||||
console.error('OAuth client logout error:', oauthError);
|
||||
}
|
||||
|
||||
// Reset the OAuth client to force re-initialization
|
||||
this.oauthClient = null;
|
||||
this.initializePromise = null;
|
||||
}
|
||||
|
||||
// Clear any stored session data
|
||||
localStorage.removeItem('atproto_session');
|
||||
sessionStorage.clear();
|
||||
|
||||
// Clear all localStorage items that might be related to OAuth
|
||||
const keysToRemove: string[] = [];
|
||||
for (let i = 0; i < localStorage.length; i++) {
|
||||
const key = localStorage.key(i);
|
||||
if (key && (key.includes('oauth') || key.includes('atproto') || key.includes('session'))) {
|
||||
keysToRemove.push(key);
|
||||
}
|
||||
}
|
||||
keysToRemove.forEach(key => {
|
||||
console.log('Removing localStorage key:', key);
|
||||
localStorage.removeItem(key);
|
||||
});
|
||||
|
||||
console.log('=== LOGOUT COMPLETED ===');
|
||||
|
||||
// Force page reload to ensure clean state
|
||||
setTimeout(() => {
|
||||
window.location.reload();
|
||||
}, 100);
|
||||
|
||||
} catch (error) {
|
||||
console.error('Logout failed:', error);
|
||||
}
|
||||
}
|
||||
|
||||
// カードデータをatproto collectionに保存
|
||||
async saveCardToBox(userCards: any[]): Promise<void> {
|
||||
// Ensure we have a valid session
|
||||
const sessionInfo = await this.checkSession();
|
||||
if (!sessionInfo) {
|
||||
throw new Error('認証が必要です。ログインしてください。');
|
||||
}
|
||||
|
||||
const did = sessionInfo.did;
|
||||
|
||||
try {
|
||||
console.log('Saving cards to atproto collection...');
|
||||
console.log('Using DID:', did);
|
||||
|
||||
// Ensure we have a fresh agent
|
||||
if (!this.agent) {
|
||||
throw new Error('Agentが初期化されていません。');
|
||||
}
|
||||
|
||||
const collection = 'ai.card.box';
|
||||
const rkey = 'self';
|
||||
const createdAt = new Date().toISOString();
|
||||
|
||||
// カードボックスのレコード
|
||||
const record = {
|
||||
$type: 'ai.card.box',
|
||||
cards: userCards.map(card => ({
|
||||
id: card.id,
|
||||
cp: card.cp,
|
||||
status: card.status,
|
||||
skill: card.skill,
|
||||
owner_did: card.owner_did,
|
||||
obtained_at: card.obtained_at,
|
||||
is_unique: card.is_unique,
|
||||
unique_id: card.unique_id
|
||||
|
||||
})),
|
||||
total_cards: userCards.length,
|
||||
updated_at: createdAt,
|
||||
createdAt: createdAt
|
||||
};
|
||||
|
||||
console.log('PutRecord request:', {
|
||||
repo: did,
|
||||
collection: collection,
|
||||
rkey: rkey,
|
||||
record: record
|
||||
});
|
||||
|
||||
|
||||
// Use Agent's com.atproto.repo.putRecord method
|
||||
const response = await this.agent.com.atproto.repo.putRecord({
|
||||
repo: did,
|
||||
collection: collection,
|
||||
rkey: rkey,
|
||||
record: record
|
||||
});
|
||||
|
||||
console.log('カードデータをai.card.boxに保存しました:', response);
|
||||
} catch (error) {
|
||||
console.error('カードボックス保存エラー:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
// ai.card.boxからカード一覧を取得
|
||||
async getCardsFromBox(): Promise<any> {
|
||||
// Ensure we have a valid session
|
||||
const sessionInfo = await this.checkSession();
|
||||
if (!sessionInfo) {
|
||||
throw new Error('認証が必要です。ログインしてください。');
|
||||
}
|
||||
|
||||
const did = sessionInfo.did;
|
||||
|
||||
try {
|
||||
console.log('Fetching cards from atproto collection...');
|
||||
console.log('Using DID:', did);
|
||||
|
||||
// Ensure we have a fresh agent
|
||||
if (!this.agent) {
|
||||
throw new Error('Agentが初期化されていません。');
|
||||
}
|
||||
|
||||
const response = await this.agent.com.atproto.repo.getRecord({
|
||||
repo: did,
|
||||
collection: 'ai.card.box',
|
||||
rkey: 'self'
|
||||
});
|
||||
|
||||
console.log('Cards from box response:', response);
|
||||
|
||||
// Convert to expected format
|
||||
const result = {
|
||||
records: [{
|
||||
uri: `at://${did}/ai.card.box/self`,
|
||||
cid: response.data.cid,
|
||||
value: response.data.value
|
||||
}]
|
||||
};
|
||||
|
||||
return result;
|
||||
} catch (error) {
|
||||
console.error('カードボックス取得エラー:', error);
|
||||
|
||||
// If record doesn't exist, return empty
|
||||
if (error.toString().includes('RecordNotFound')) {
|
||||
return { records: [] };
|
||||
}
|
||||
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
// ai.card.boxのコレクションを削除
|
||||
async deleteCardBox(): Promise<void> {
|
||||
// Ensure we have a valid session
|
||||
const sessionInfo = await this.checkSession();
|
||||
if (!sessionInfo) {
|
||||
throw new Error('認証が必要です。ログインしてください。');
|
||||
}
|
||||
|
||||
const did = sessionInfo.did;
|
||||
|
||||
try {
|
||||
console.log('Deleting card box collection...');
|
||||
console.log('Using DID:', did);
|
||||
|
||||
// Ensure we have a fresh agent
|
||||
if (!this.agent) {
|
||||
throw new Error('Agentが初期化されていません。');
|
||||
}
|
||||
|
||||
const response = await this.agent.com.atproto.repo.deleteRecord({
|
||||
repo: did,
|
||||
collection: 'ai.card.box',
|
||||
rkey: 'self'
|
||||
});
|
||||
|
||||
console.log('Card box deleted successfully:', response);
|
||||
} catch (error) {
|
||||
console.error('カードボックス削除エラー:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
// 手動でトークンを設定(開発・デバッグ用)
|
||||
setManualTokens(accessJwt: string, refreshJwt: string): void {
|
||||
console.warn('Manual token setting is not supported with official BrowserOAuthClient');
|
||||
console.warn('Please use the proper OAuth flow instead');
|
||||
|
||||
// For backward compatibility, store in localStorage
|
||||
const session: AtprotoSession = {
|
||||
did: 'did:plc:uqzpqmrjnptsxezjx4xuh2mn',
|
||||
handle: 'syui.ai',
|
||||
accessJwt: accessJwt,
|
||||
refreshJwt: refreshJwt
|
||||
};
|
||||
|
||||
localStorage.setItem('atproto_session', JSON.stringify(session));
|
||||
console.log('Manual tokens stored in localStorage for backward compatibility');
|
||||
}
|
||||
|
||||
// 後方互換性のための従来関数
|
||||
saveSessionToStorage(session: AtprotoSession): void {
|
||||
console.warn('saveSessionToStorage is deprecated with BrowserOAuthClient');
|
||||
localStorage.setItem('atproto_session', JSON.stringify(session));
|
||||
}
|
||||
|
||||
async backupUserCards(userCards: any[]): Promise<void> {
|
||||
return this.saveCardToBox(userCards);
|
||||
}
|
||||
}
|
||||
|
||||
export const atprotoOAuthService = new AtprotoOAuthService();
|
||||
export type { AtprotoSession };
|
@@ -1,6 +1,6 @@
|
||||
.card {
|
||||
width: 250px;
|
||||
height: 350px;
|
||||
height: 380px;
|
||||
border-radius: 12px;
|
||||
background: linear-gradient(135deg, #1a1a1a 0%, #2d2d2d 100%);
|
||||
border: 2px solid #333;
|
||||
@@ -87,7 +87,26 @@
|
||||
justify-content: space-between;
|
||||
font-size: 14px;
|
||||
color: #888;
|
||||
margin-bottom: 20px;
|
||||
margin-bottom: 10px;
|
||||
}
|
||||
|
||||
.card-image-container {
|
||||
width: 100%;
|
||||
height: 150px;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
margin-bottom: 15px;
|
||||
overflow: hidden;
|
||||
border-radius: 8px;
|
||||
background: rgba(255, 255, 255, 0.05);
|
||||
}
|
||||
|
||||
.card-image {
|
||||
max-width: 100%;
|
||||
max-height: 100%;
|
||||
object-fit: contain;
|
||||
border-radius: 8px;
|
||||
}
|
||||
|
||||
.card-content {
|
||||
@@ -149,3 +168,164 @@
|
||||
50% { box-shadow: 0 0 20px rgba(255, 0, 255, 0.8); }
|
||||
100% { box-shadow: 0 0 5px rgba(255, 0, 255, 0.5); }
|
||||
}
|
||||
|
||||
/* Simple Card Styles */
|
||||
.card-simple {
|
||||
width: 240px;
|
||||
height: auto;
|
||||
background: transparent;
|
||||
border: none;
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
.card-frame {
|
||||
position: relative;
|
||||
width: 100%;
|
||||
aspect-ratio: 3/4;
|
||||
border-radius: 8px;
|
||||
overflow: hidden;
|
||||
background: #1a1a1a;
|
||||
padding: 25px 25px 30px 25px;
|
||||
border: 3px solid #666;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
/* Normal card - no effects */
|
||||
.card-simple.card-normal .card-frame {
|
||||
border-color: #666;
|
||||
background: #1a1a1a;
|
||||
}
|
||||
|
||||
/* Unique (rare) card - glowing effects */
|
||||
.card-simple.card-unique .card-frame {
|
||||
border-color: #ffd700;
|
||||
background: linear-gradient(135deg, #2a2a1a 0%, #3a3a2a 50%, #2a2a1a 100%);
|
||||
position: relative;
|
||||
isolation: isolate;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
/* Particle/grainy texture for rare cards */
|
||||
.card-simple.card-unique .card-frame::before {
|
||||
content: "";
|
||||
position: absolute;
|
||||
top: 0;
|
||||
left: 0;
|
||||
right: 0;
|
||||
bottom: 0;
|
||||
background-image:
|
||||
repeating-radial-gradient(circle at 1px 1px, rgba(255, 255, 255, 0.1) 0px, transparent 1px, transparent 2px),
|
||||
repeating-radial-gradient(circle at 3px 3px, rgba(255, 215, 0, 0.1) 0px, transparent 2px, transparent 4px);
|
||||
background-size: 20px 20px, 30px 30px;
|
||||
opacity: 0.8;
|
||||
z-index: 1;
|
||||
pointer-events: none;
|
||||
}
|
||||
|
||||
/* Reflection effect for rare cards */
|
||||
.card-simple.card-unique .card-frame::after {
|
||||
content: "";
|
||||
height: 100%;
|
||||
width: 40px;
|
||||
position: absolute;
|
||||
top: -180px;
|
||||
left: 0;
|
||||
background: linear-gradient(90deg,
|
||||
transparent 0%,
|
||||
rgba(255, 215, 0, 0.8) 20%,
|
||||
rgba(255, 255, 0, 0.9) 40%,
|
||||
rgba(255, 223, 0, 1) 50%,
|
||||
rgba(255, 255, 0, 0.9) 60%,
|
||||
rgba(255, 215, 0, 0.8) 80%,
|
||||
transparent 100%
|
||||
);
|
||||
opacity: 0;
|
||||
transform: rotate(45deg);
|
||||
animation: gold-reflection 6s ease-in-out infinite;
|
||||
z-index: 2;
|
||||
}
|
||||
|
||||
@keyframes gold-reflection {
|
||||
0% { transform: scale(0) rotate(45deg); opacity: 0; }
|
||||
15% { transform: scale(0) rotate(45deg); opacity: 0; }
|
||||
17% { transform: scale(4) rotate(45deg); opacity: 0.8; }
|
||||
20% { transform: scale(50) rotate(45deg); opacity: 0; }
|
||||
100% { transform: scale(50) rotate(45deg); opacity: 0; }
|
||||
}
|
||||
|
||||
/* Glowing backlight effect */
|
||||
.card-simple.card-unique {
|
||||
position: relative;
|
||||
}
|
||||
|
||||
.card-simple.card-unique::after {
|
||||
position: absolute;
|
||||
content: "";
|
||||
top: 5px;
|
||||
left: 0;
|
||||
right: 0;
|
||||
bottom: 0;
|
||||
z-index: -1;
|
||||
height: 100%;
|
||||
width: 100%;
|
||||
margin: 0 auto;
|
||||
transform: scale(0.95);
|
||||
filter: blur(15px);
|
||||
background: radial-gradient(ellipse at center, #ffd700 0%, #ffb347 50%, transparent 70%);
|
||||
opacity: 0.6;
|
||||
}
|
||||
|
||||
/* Glowing border effect for rare cards */
|
||||
.card-simple.card-unique .card-frame {
|
||||
box-shadow:
|
||||
0 0 10px rgba(255, 215, 0, 0.5),
|
||||
inset 0 0 10px rgba(255, 215, 0, 0.1);
|
||||
}
|
||||
|
||||
|
||||
|
||||
.card-image-simple {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
object-fit: cover;
|
||||
border-radius: 4px;
|
||||
position: relative;
|
||||
z-index: 1;
|
||||
}
|
||||
|
||||
.card-cp-bar {
|
||||
width: 100%;
|
||||
height: 50px;
|
||||
background: #333;
|
||||
border-radius: 6px;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
margin-top: 12px;
|
||||
margin-bottom: 8px;
|
||||
border: 2px solid #666;
|
||||
position: relative;
|
||||
box-sizing: border-box;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.card-simple.card-unique .card-cp-bar {
|
||||
background: linear-gradient(135deg, #2a2a1a 0%, #3a3a2a 50%, #2a2a1a 100%);
|
||||
border-color: #ffd700;
|
||||
box-shadow:
|
||||
0 0 5px rgba(255, 215, 0, 0.3),
|
||||
inset 0 0 5px rgba(255, 215, 0, 0.1);
|
||||
}
|
||||
|
||||
|
||||
.cp-value {
|
||||
font-size: 20px;
|
||||
font-weight: bold;
|
||||
color: #fff;
|
||||
text-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
|
||||
z-index: 1;
|
||||
position: relative;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
196
web/src/styles/CardBox.css
Normal file
196
web/src/styles/CardBox.css
Normal file
@@ -0,0 +1,196 @@
|
||||
.card-box-container {
|
||||
max-width: 1200px;
|
||||
margin: 0 auto;
|
||||
padding: 20px;
|
||||
}
|
||||
|
||||
.card-box-header {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
margin-bottom: 20px;
|
||||
padding-bottom: 15px;
|
||||
border-bottom: 2px solid #e9ecef;
|
||||
}
|
||||
|
||||
.card-box-header h3 {
|
||||
color: #495057;
|
||||
margin: 0;
|
||||
font-size: 24px;
|
||||
}
|
||||
|
||||
.box-actions {
|
||||
display: flex;
|
||||
gap: 10px;
|
||||
}
|
||||
|
||||
.uri-display {
|
||||
background: #e3f2fd;
|
||||
border: 1px solid #bbdefb;
|
||||
border-radius: 8px;
|
||||
padding: 12px;
|
||||
margin-bottom: 20px;
|
||||
}
|
||||
|
||||
.uri-display p {
|
||||
margin: 0;
|
||||
color: #1565c0;
|
||||
font-size: 14px;
|
||||
}
|
||||
|
||||
.uri-display code {
|
||||
background: #ffffff;
|
||||
border: 1px solid #90caf9;
|
||||
border-radius: 4px;
|
||||
padding: 4px 8px;
|
||||
font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
|
||||
font-size: 12px;
|
||||
color: #0d47a1;
|
||||
word-break: break-all;
|
||||
}
|
||||
|
||||
.json-button,
|
||||
.refresh-button,
|
||||
.retry-button,
|
||||
.delete-button {
|
||||
padding: 8px 16px;
|
||||
border: none;
|
||||
border-radius: 8px;
|
||||
font-size: 14px;
|
||||
font-weight: bold;
|
||||
cursor: pointer;
|
||||
transition: all 0.3s ease;
|
||||
}
|
||||
|
||||
.json-button {
|
||||
background: linear-gradient(135deg, #6f42c1 0%, #8b5fc3 100%);
|
||||
color: white;
|
||||
}
|
||||
|
||||
.json-button:hover {
|
||||
transform: translateY(-2px);
|
||||
box-shadow: 0 4px 12px rgba(111, 66, 193, 0.4);
|
||||
}
|
||||
|
||||
.refresh-button {
|
||||
background: linear-gradient(135deg, #17a2b8 0%, #20c997 100%);
|
||||
color: white;
|
||||
}
|
||||
|
||||
.refresh-button:hover {
|
||||
transform: translateY(-2px);
|
||||
box-shadow: 0 4px 12px rgba(23, 162, 184, 0.4);
|
||||
}
|
||||
|
||||
.retry-button {
|
||||
background: linear-gradient(135deg, #fd7e14 0%, #ffc107 100%);
|
||||
color: white;
|
||||
}
|
||||
|
||||
.retry-button:hover {
|
||||
transform: translateY(-2px);
|
||||
box-shadow: 0 4px 12px rgba(253, 126, 20, 0.4);
|
||||
}
|
||||
|
||||
.delete-button {
|
||||
background: linear-gradient(135deg, #dc3545 0%, #c82333 100%);
|
||||
color: white;
|
||||
}
|
||||
|
||||
.delete-button:hover {
|
||||
transform: translateY(-2px);
|
||||
box-shadow: 0 4px 12px rgba(220, 53, 69, 0.4);
|
||||
}
|
||||
|
||||
.delete-button:disabled {
|
||||
opacity: 0.6;
|
||||
cursor: not-allowed;
|
||||
transform: none;
|
||||
}
|
||||
|
||||
.json-display {
|
||||
background: #f8f9fa;
|
||||
border: 1px solid #dee2e6;
|
||||
border-radius: 8px;
|
||||
padding: 20px;
|
||||
margin-bottom: 20px;
|
||||
}
|
||||
|
||||
.json-display h4 {
|
||||
color: #495057;
|
||||
margin-top: 0;
|
||||
margin-bottom: 15px;
|
||||
}
|
||||
|
||||
.json-content {
|
||||
background: #ffffff;
|
||||
border: 1px solid #e9ecef;
|
||||
border-radius: 4px;
|
||||
padding: 15px;
|
||||
font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
|
||||
font-size: 12px;
|
||||
color: #495057;
|
||||
max-height: 400px;
|
||||
overflow-y: auto;
|
||||
white-space: pre-wrap;
|
||||
word-wrap: break-word;
|
||||
}
|
||||
|
||||
.box-stats {
|
||||
background: rgba(102, 126, 234, 0.1);
|
||||
border: 1px solid #dee2e6;
|
||||
border-radius: 8px;
|
||||
padding: 15px;
|
||||
margin-bottom: 20px;
|
||||
}
|
||||
|
||||
.box-stats p {
|
||||
margin: 0;
|
||||
color: #495057;
|
||||
font-size: 14px;
|
||||
}
|
||||
|
||||
.card-grid {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
|
||||
gap: 20px;
|
||||
margin-top: 20px;
|
||||
}
|
||||
|
||||
.box-card-item {
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.card-info {
|
||||
margin-top: 8px;
|
||||
color: #6c757d;
|
||||
font-size: 12px;
|
||||
}
|
||||
|
||||
.empty-box {
|
||||
text-align: center;
|
||||
padding: 40px 20px;
|
||||
color: #6c757d;
|
||||
background: #f8f9fa;
|
||||
border-radius: 8px;
|
||||
border: 1px solid #dee2e6;
|
||||
}
|
||||
|
||||
.empty-box p {
|
||||
margin: 8px 0;
|
||||
}
|
||||
|
||||
.loading,
|
||||
.error {
|
||||
text-align: center;
|
||||
padding: 40px 20px;
|
||||
color: #6c757d;
|
||||
font-size: 16px;
|
||||
}
|
||||
|
||||
.error {
|
||||
color: #dc3545;
|
||||
background: #f8d7da;
|
||||
border: 1px solid #f5c6cb;
|
||||
border-radius: 8px;
|
||||
}
|
170
web/src/styles/CardList.css
Normal file
170
web/src/styles/CardList.css
Normal file
@@ -0,0 +1,170 @@
|
||||
.card-list-container {
|
||||
min-height: 100vh;
|
||||
background: linear-gradient(135deg, #0f0f0f 0%, #1a1a1a 100%);
|
||||
padding: 20px;
|
||||
}
|
||||
|
||||
.card-list-header {
|
||||
text-align: center;
|
||||
margin-bottom: 40px;
|
||||
padding: 20px;
|
||||
background: rgba(255, 255, 255, 0.05);
|
||||
border-radius: 12px;
|
||||
border: 1px solid rgba(255, 255, 255, 0.1);
|
||||
}
|
||||
|
||||
.card-list-header h1 {
|
||||
color: #fff;
|
||||
margin: 0 0 10px 0;
|
||||
font-size: 2.5rem;
|
||||
}
|
||||
|
||||
.card-list-header p {
|
||||
color: #999;
|
||||
margin: 0;
|
||||
font-size: 1.1rem;
|
||||
}
|
||||
|
||||
.card-list-grid {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
|
||||
gap: 30px;
|
||||
max-width: 1400px;
|
||||
margin: 0 auto;
|
||||
}
|
||||
|
||||
.card-list-item {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
gap: 15px;
|
||||
}
|
||||
|
||||
/* Simple grid layout for user-page style */
|
||||
.card-list-simple-grid {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
|
||||
gap: 20px;
|
||||
max-width: 1400px;
|
||||
margin: 0 auto;
|
||||
padding: 20px;
|
||||
}
|
||||
|
||||
.card-list-simple-item {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
gap: 10px;
|
||||
}
|
||||
|
||||
.info-button {
|
||||
background: linear-gradient(135deg, #333 0%, #555 100%);
|
||||
color: white;
|
||||
border: 2px solid #666;
|
||||
padding: 8px 16px;
|
||||
border-radius: 6px;
|
||||
cursor: pointer;
|
||||
font-size: 0.9rem;
|
||||
transition: all 0.3s ease;
|
||||
width: 100%;
|
||||
max-width: 240px;
|
||||
}
|
||||
|
||||
.info-button:hover {
|
||||
transform: translateY(-2px);
|
||||
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
|
||||
background: linear-gradient(135deg, #444 0%, #666 100%);
|
||||
}
|
||||
|
||||
.card-info-details {
|
||||
background: rgba(255, 255, 255, 0.05);
|
||||
border: 1px solid rgba(255, 255, 255, 0.1);
|
||||
border-radius: 8px;
|
||||
padding: 15px;
|
||||
width: 100%;
|
||||
max-width: 240px;
|
||||
margin-top: 10px;
|
||||
}
|
||||
|
||||
.card-info-details p {
|
||||
margin: 5px 0;
|
||||
color: #ccc;
|
||||
font-size: 0.85rem;
|
||||
text-align: left;
|
||||
}
|
||||
|
||||
.card-info-details p strong {
|
||||
color: #fff;
|
||||
}
|
||||
|
||||
.card-meta {
|
||||
background: rgba(255, 255, 255, 0.05);
|
||||
border: 1px solid rgba(255, 255, 255, 0.1);
|
||||
border-radius: 8px;
|
||||
padding: 15px;
|
||||
width: 100%;
|
||||
max-width: 250px;
|
||||
}
|
||||
|
||||
.card-meta p {
|
||||
margin: 5px 0;
|
||||
color: #ccc;
|
||||
font-size: 0.9rem;
|
||||
}
|
||||
|
||||
.card-meta p:first-child {
|
||||
font-weight: bold;
|
||||
color: #fff;
|
||||
}
|
||||
|
||||
.card-description {
|
||||
font-size: 0.85rem;
|
||||
color: #999;
|
||||
font-style: italic;
|
||||
margin-top: 8px;
|
||||
line-height: 1.4;
|
||||
}
|
||||
|
||||
.source-info {
|
||||
font-size: 0.9rem;
|
||||
color: #666;
|
||||
margin-top: 5px;
|
||||
}
|
||||
|
||||
.loading, .error {
|
||||
text-align: center;
|
||||
padding: 40px;
|
||||
color: #999;
|
||||
font-size: 1.2rem;
|
||||
}
|
||||
|
||||
.error {
|
||||
color: #ff4757;
|
||||
}
|
||||
|
||||
button {
|
||||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||||
color: white;
|
||||
border: none;
|
||||
padding: 10px 20px;
|
||||
border-radius: 5px;
|
||||
cursor: pointer;
|
||||
font-size: 1rem;
|
||||
margin-top: 20px;
|
||||
}
|
||||
|
||||
button:hover {
|
||||
transform: translateY(-2px);
|
||||
box-shadow: 0 4px 12px rgba(102, 126, 234, 0.4);
|
||||
}
|
||||
|
||||
@media (max-width: 768px) {
|
||||
.card-list-grid {
|
||||
grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
|
||||
gap: 20px;
|
||||
}
|
||||
|
||||
.card-list-header h1 {
|
||||
font-size: 2rem;
|
||||
}
|
||||
}
|
172
web/src/styles/CollectionAnalysis.css
Normal file
172
web/src/styles/CollectionAnalysis.css
Normal file
@@ -0,0 +1,172 @@
|
||||
.collection-analysis {
|
||||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||||
border-radius: 16px;
|
||||
padding: 24px;
|
||||
margin: 20px 0;
|
||||
color: white;
|
||||
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
|
||||
}
|
||||
|
||||
.collection-analysis h3 {
|
||||
margin: 0 0 20px 0;
|
||||
font-size: 1.5rem;
|
||||
font-weight: 600;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.analysis-stats {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
|
||||
gap: 16px;
|
||||
margin-bottom: 24px;
|
||||
}
|
||||
|
||||
.stat-card {
|
||||
background: rgba(255, 255, 255, 0.15);
|
||||
backdrop-filter: blur(10px);
|
||||
border-radius: 12px;
|
||||
padding: 16px;
|
||||
text-align: center;
|
||||
border: 1px solid rgba(255, 255, 255, 0.2);
|
||||
}
|
||||
|
||||
.stat-value {
|
||||
font-size: 2rem;
|
||||
font-weight: bold;
|
||||
margin-bottom: 4px;
|
||||
}
|
||||
|
||||
.stat-label {
|
||||
font-size: 0.9rem;
|
||||
opacity: 0.8;
|
||||
}
|
||||
|
||||
.rarity-distribution {
|
||||
margin-bottom: 24px;
|
||||
}
|
||||
|
||||
.rarity-distribution h4 {
|
||||
margin: 0 0 16px 0;
|
||||
font-size: 1.2rem;
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
.rarity-bars {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 8px;
|
||||
}
|
||||
|
||||
.rarity-bar {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 12px;
|
||||
}
|
||||
|
||||
.rarity-name {
|
||||
min-width: 80px;
|
||||
font-weight: 500;
|
||||
text-transform: capitalize;
|
||||
}
|
||||
|
||||
.bar-container {
|
||||
flex: 1;
|
||||
height: 20px;
|
||||
background: rgba(255, 255, 255, 0.2);
|
||||
border-radius: 10px;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.bar {
|
||||
height: 100%;
|
||||
border-radius: 10px;
|
||||
transition: width 0.3s ease;
|
||||
}
|
||||
|
||||
.bar-common { background: linear-gradient(90deg, #4CAF50, #45a049); }
|
||||
.bar-rare { background: linear-gradient(90deg, #2196F3, #1976D2); }
|
||||
.bar-epic { background: linear-gradient(90deg, #9C27B0, #7B1FA2); }
|
||||
.bar-legendary { background: linear-gradient(90deg, #FF9800, #F57C00); }
|
||||
.bar-mythic { background: linear-gradient(90deg, #F44336, #D32F2F); }
|
||||
|
||||
.rarity-count {
|
||||
min-width: 40px;
|
||||
text-align: right;
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
.recommendations {
|
||||
background: rgba(255, 255, 255, 0.1);
|
||||
border-radius: 12px;
|
||||
padding: 16px;
|
||||
margin-bottom: 20px;
|
||||
}
|
||||
|
||||
.recommendations h4 {
|
||||
margin: 0 0 12px 0;
|
||||
font-size: 1.1rem;
|
||||
}
|
||||
|
||||
.recommendations ul {
|
||||
margin: 0;
|
||||
padding-left: 20px;
|
||||
}
|
||||
|
||||
.recommendations li {
|
||||
margin-bottom: 8px;
|
||||
line-height: 1.4;
|
||||
}
|
||||
|
||||
.refresh-analysis,
|
||||
.analyze-button,
|
||||
.retry-button {
|
||||
background: rgba(255, 255, 255, 0.2);
|
||||
border: 1px solid rgba(255, 255, 255, 0.3);
|
||||
color: white;
|
||||
border-radius: 8px;
|
||||
padding: 12px 24px;
|
||||
font-weight: 500;
|
||||
cursor: pointer;
|
||||
transition: all 0.3s ease;
|
||||
display: block;
|
||||
margin: 0 auto;
|
||||
}
|
||||
|
||||
.refresh-analysis:hover,
|
||||
.analyze-button:hover,
|
||||
.retry-button:hover {
|
||||
background: rgba(255, 255, 255, 0.3);
|
||||
transform: translateY(-2px);
|
||||
}
|
||||
|
||||
.analysis-loading,
|
||||
.analysis-error,
|
||||
.analysis-empty {
|
||||
text-align: center;
|
||||
padding: 40px 20px;
|
||||
}
|
||||
|
||||
.loading-spinner {
|
||||
width: 40px;
|
||||
height: 40px;
|
||||
border: 3px solid rgba(255, 255, 255, 0.3);
|
||||
border-top: 3px solid white;
|
||||
border-radius: 50%;
|
||||
animation: spin 1s linear infinite;
|
||||
margin: 0 auto 16px;
|
||||
}
|
||||
|
||||
@keyframes spin {
|
||||
0% { transform: rotate(0deg); }
|
||||
100% { transform: rotate(360deg); }
|
||||
}
|
||||
|
||||
.analysis-error p {
|
||||
color: #ffcdd2;
|
||||
margin-bottom: 16px;
|
||||
}
|
||||
|
||||
.analysis-empty p {
|
||||
opacity: 0.8;
|
||||
margin-bottom: 16px;
|
||||
}
|
@@ -9,6 +9,60 @@
|
||||
justify-content: center;
|
||||
background: rgba(0, 0, 0, 0.9);
|
||||
z-index: 1000;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.card-final {
|
||||
position: relative;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.card-actions {
|
||||
position: absolute;
|
||||
bottom: -80px;
|
||||
left: 50%;
|
||||
transform: translateX(-50%);
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
gap: 10px;
|
||||
}
|
||||
|
||||
.save-button {
|
||||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||||
color: white;
|
||||
border: none;
|
||||
padding: 10px 20px;
|
||||
border-radius: 25px;
|
||||
font-size: 14px;
|
||||
font-weight: bold;
|
||||
cursor: pointer;
|
||||
transition: all 0.3s ease;
|
||||
box-shadow: 0 4px 15px rgba(102, 126, 234, 0.3);
|
||||
}
|
||||
|
||||
.save-button:hover:not(:disabled) {
|
||||
transform: translateY(-2px);
|
||||
box-shadow: 0 6px 20px rgba(102, 126, 234, 0.4);
|
||||
}
|
||||
|
||||
.save-button:disabled {
|
||||
opacity: 0.6;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
.click-hint {
|
||||
color: white;
|
||||
font-size: 12px;
|
||||
background: rgba(0, 0, 0, 0.7);
|
||||
padding: 6px 12px;
|
||||
border-radius: 15px;
|
||||
animation: pulse 2s infinite;
|
||||
}
|
||||
|
||||
@keyframes pulse {
|
||||
0%, 100% { opacity: 0.7; }
|
||||
50% { opacity: 1; }
|
||||
}
|
||||
|
||||
.gacha-opening {
|
||||
|
219
web/src/styles/GachaStats.css
Normal file
219
web/src/styles/GachaStats.css
Normal file
@@ -0,0 +1,219 @@
|
||||
.gacha-stats {
|
||||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||||
border-radius: 16px;
|
||||
padding: 24px;
|
||||
margin: 20px 0;
|
||||
color: white;
|
||||
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
|
||||
}
|
||||
|
||||
.gacha-stats h3 {
|
||||
margin: 0 0 20px 0;
|
||||
font-size: 1.5rem;
|
||||
font-weight: 600;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.stats-overview {
|
||||
margin-bottom: 24px;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.overview-card {
|
||||
background: rgba(255, 255, 255, 0.15);
|
||||
backdrop-filter: blur(10px);
|
||||
border-radius: 12px;
|
||||
padding: 20px;
|
||||
border: 1px solid rgba(255, 255, 255, 0.2);
|
||||
display: inline-block;
|
||||
min-width: 200px;
|
||||
}
|
||||
|
||||
.overview-value {
|
||||
font-size: 2.5rem;
|
||||
font-weight: bold;
|
||||
margin-bottom: 8px;
|
||||
}
|
||||
|
||||
.overview-label {
|
||||
font-size: 1rem;
|
||||
opacity: 0.9;
|
||||
}
|
||||
|
||||
.rarity-stats {
|
||||
margin-bottom: 24px;
|
||||
}
|
||||
|
||||
.rarity-stats h4 {
|
||||
margin: 0 0 16px 0;
|
||||
font-size: 1.2rem;
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
.rarity-grid {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
|
||||
gap: 12px;
|
||||
}
|
||||
|
||||
.rarity-stat {
|
||||
background: rgba(255, 255, 255, 0.15);
|
||||
backdrop-filter: blur(10px);
|
||||
border-radius: 12px;
|
||||
padding: 16px;
|
||||
text-align: center;
|
||||
border: 1px solid rgba(255, 255, 255, 0.2);
|
||||
position: relative;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.rarity-stat::before {
|
||||
content: '';
|
||||
position: absolute;
|
||||
top: 0;
|
||||
left: 0;
|
||||
right: 0;
|
||||
height: 3px;
|
||||
background: var(--rarity-color);
|
||||
}
|
||||
|
||||
.rarity-stat.rarity-common { --rarity-color: #4CAF50; }
|
||||
.rarity-stat.rarity-rare { --rarity-color: #2196F3; }
|
||||
.rarity-stat.rarity-epic { --rarity-color: #9C27B0; }
|
||||
.rarity-stat.rarity-legendary { --rarity-color: #FF9800; }
|
||||
.rarity-stat.rarity-mythic { --rarity-color: #F44336; }
|
||||
|
||||
.rarity-count {
|
||||
font-size: 1.8rem;
|
||||
font-weight: bold;
|
||||
margin-bottom: 4px;
|
||||
}
|
||||
|
||||
.rarity-name {
|
||||
font-size: 0.9rem;
|
||||
opacity: 0.9;
|
||||
text-transform: capitalize;
|
||||
margin-bottom: 4px;
|
||||
}
|
||||
|
||||
.success-rate {
|
||||
font-size: 0.8rem;
|
||||
opacity: 0.7;
|
||||
background: rgba(255, 255, 255, 0.1);
|
||||
border-radius: 4px;
|
||||
padding: 2px 6px;
|
||||
display: inline-block;
|
||||
}
|
||||
|
||||
.recent-activity {
|
||||
background: rgba(255, 255, 255, 0.1);
|
||||
border-radius: 12px;
|
||||
padding: 16px;
|
||||
margin-bottom: 20px;
|
||||
}
|
||||
|
||||
.recent-activity h4 {
|
||||
margin: 0 0 12px 0;
|
||||
font-size: 1.1rem;
|
||||
}
|
||||
|
||||
.activity-list {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 8px;
|
||||
}
|
||||
|
||||
.activity-item {
|
||||
background: rgba(255, 255, 255, 0.05);
|
||||
border-radius: 8px;
|
||||
padding: 12px;
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.activity-time {
|
||||
font-size: 0.8rem;
|
||||
opacity: 0.7;
|
||||
min-width: 120px;
|
||||
}
|
||||
|
||||
.activity-details {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
flex: 1;
|
||||
justify-content: flex-end;
|
||||
}
|
||||
|
||||
.card-rarity {
|
||||
padding: 2px 8px;
|
||||
border-radius: 4px;
|
||||
font-size: 0.75rem;
|
||||
font-weight: 500;
|
||||
text-transform: uppercase;
|
||||
}
|
||||
|
||||
.card-rarity.rarity-common { background: #4CAF50; }
|
||||
.card-rarity.rarity-rare { background: #2196F3; }
|
||||
.card-rarity.rarity-epic { background: #9C27B0; }
|
||||
.card-rarity.rarity-legendary { background: #FF9800; }
|
||||
.card-rarity.rarity-mythic { background: #F44336; }
|
||||
|
||||
.card-name {
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
.refresh-stats,
|
||||
.load-stats-button,
|
||||
.retry-button {
|
||||
background: rgba(255, 255, 255, 0.2);
|
||||
border: 1px solid rgba(255, 255, 255, 0.3);
|
||||
color: white;
|
||||
border-radius: 8px;
|
||||
padding: 12px 24px;
|
||||
font-weight: 500;
|
||||
cursor: pointer;
|
||||
transition: all 0.3s ease;
|
||||
display: block;
|
||||
margin: 0 auto;
|
||||
}
|
||||
|
||||
.refresh-stats:hover,
|
||||
.load-stats-button:hover,
|
||||
.retry-button:hover {
|
||||
background: rgba(255, 255, 255, 0.3);
|
||||
transform: translateY(-2px);
|
||||
}
|
||||
|
||||
.stats-loading,
|
||||
.stats-error,
|
||||
.stats-empty {
|
||||
text-align: center;
|
||||
padding: 40px 20px;
|
||||
}
|
||||
|
||||
.loading-spinner {
|
||||
width: 40px;
|
||||
height: 40px;
|
||||
border: 3px solid rgba(255, 255, 255, 0.3);
|
||||
border-top: 3px solid white;
|
||||
border-radius: 50%;
|
||||
animation: spin 1s linear infinite;
|
||||
margin: 0 auto 16px;
|
||||
}
|
||||
|
||||
@keyframes spin {
|
||||
0% { transform: rotate(0deg); }
|
||||
100% { transform: rotate(360deg); }
|
||||
}
|
||||
|
||||
.stats-error p {
|
||||
color: #ffcdd2;
|
||||
margin-bottom: 16px;
|
||||
}
|
||||
|
||||
.stats-empty p {
|
||||
opacity: 0.8;
|
||||
margin-bottom: 16px;
|
||||
}
|
@@ -17,11 +17,91 @@
|
||||
border: 1px solid #444;
|
||||
border-radius: 16px;
|
||||
padding: 40px;
|
||||
max-width: 400px;
|
||||
max-width: 450px;
|
||||
width: 90%;
|
||||
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
|
||||
}
|
||||
|
||||
.login-mode-selector {
|
||||
display: flex;
|
||||
margin-bottom: 24px;
|
||||
background: rgba(255, 255, 255, 0.05);
|
||||
border-radius: 8px;
|
||||
padding: 4px;
|
||||
}
|
||||
|
||||
.mode-button {
|
||||
flex: 1;
|
||||
padding: 12px 16px;
|
||||
border: none;
|
||||
background: transparent;
|
||||
color: #ccc;
|
||||
border-radius: 6px;
|
||||
cursor: pointer;
|
||||
transition: all 0.3s ease;
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
.mode-button.active {
|
||||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||||
color: white;
|
||||
box-shadow: 0 2px 8px rgba(102, 126, 234, 0.3);
|
||||
}
|
||||
|
||||
.mode-button:hover:not(.active) {
|
||||
background: rgba(255, 255, 255, 0.1);
|
||||
color: white;
|
||||
}
|
||||
|
||||
.oauth-login {
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.oauth-info {
|
||||
margin-bottom: 24px;
|
||||
padding: 20px;
|
||||
background: rgba(102, 126, 234, 0.1);
|
||||
border-radius: 12px;
|
||||
border: 1px solid rgba(102, 126, 234, 0.3);
|
||||
}
|
||||
|
||||
.oauth-info h3 {
|
||||
margin: 0 0 12px 0;
|
||||
font-size: 18px;
|
||||
color: #667eea;
|
||||
}
|
||||
|
||||
.oauth-info p {
|
||||
margin: 0;
|
||||
font-size: 14px;
|
||||
line-height: 1.5;
|
||||
opacity: 0.9;
|
||||
}
|
||||
|
||||
.oauth-login-button {
|
||||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||||
border: none;
|
||||
color: white;
|
||||
padding: 16px 32px;
|
||||
border-radius: 12px;
|
||||
font-size: 16px;
|
||||
font-weight: 600;
|
||||
cursor: pointer;
|
||||
transition: all 0.3s ease;
|
||||
box-shadow: 0 4px 16px rgba(102, 126, 234, 0.3);
|
||||
}
|
||||
|
||||
.oauth-login-button:hover:not(:disabled) {
|
||||
transform: translateY(-2px);
|
||||
box-shadow: 0 6px 20px rgba(102, 126, 234, 0.4);
|
||||
}
|
||||
|
||||
.oauth-login-button:disabled {
|
||||
opacity: 0.7;
|
||||
cursor: not-allowed;
|
||||
transform: none;
|
||||
}
|
||||
|
||||
.login-modal h2 {
|
||||
margin: 0 0 30px 0;
|
||||
font-size: 28px;
|
||||
@@ -150,3 +230,14 @@
|
||||
line-height: 1.6;
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
.dev-notice {
|
||||
background: rgba(255, 193, 7, 0.1);
|
||||
border: 1px solid rgba(255, 193, 7, 0.3);
|
||||
border-radius: 6px;
|
||||
padding: 8px 12px;
|
||||
margin: 10px 0;
|
||||
color: #ffc107;
|
||||
font-size: 12px;
|
||||
text-align: center;
|
||||
}
|
141
web/src/utils/oauth-endpoints.ts
Normal file
141
web/src/utils/oauth-endpoints.ts
Normal file
@@ -0,0 +1,141 @@
|
||||
/**
|
||||
* OAuth dynamic endpoint handlers
|
||||
*/
|
||||
import { OAuthKeyManager, generateClientMetadata } from './oauth-keys';
|
||||
|
||||
export class OAuthEndpointHandler {
|
||||
/**
|
||||
* Initialize OAuth endpoint handlers
|
||||
*/
|
||||
static init() {
|
||||
// Intercept requests to client-metadata.json
|
||||
this.setupClientMetadataHandler();
|
||||
|
||||
// Intercept requests to .well-known/jwks.json
|
||||
this.setupJWKSHandler();
|
||||
}
|
||||
|
||||
private static setupClientMetadataHandler() {
|
||||
// Override fetch for client-metadata.json requests
|
||||
const originalFetch = window.fetch;
|
||||
|
||||
window.fetch = async (input: RequestInfo | URL, init?: RequestInit) => {
|
||||
const url = typeof input === 'string' ? input : input.toString();
|
||||
|
||||
// Only intercept local OAuth endpoints
|
||||
try {
|
||||
const urlObj = new URL(url, window.location.origin);
|
||||
|
||||
// Only intercept requests to the same origin
|
||||
if (urlObj.origin !== window.location.origin) {
|
||||
// Pass through external API calls unchanged
|
||||
return originalFetch(input, init);
|
||||
}
|
||||
|
||||
// Handle local OAuth endpoints
|
||||
if (urlObj.pathname.endsWith('/client-metadata.json')) {
|
||||
const metadata = generateClientMetadata();
|
||||
return new Response(JSON.stringify(metadata, null, 2), {
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Access-Control-Allow-Origin': '*'
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (urlObj.pathname.endsWith('/.well-known/jwks.json')) {
|
||||
try {
|
||||
const jwks = await OAuthKeyManager.getJWKS();
|
||||
return new Response(JSON.stringify(jwks, null, 2), {
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Access-Control-Allow-Origin': '*'
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Failed to generate JWKS:', error);
|
||||
return new Response(JSON.stringify({ error: 'Failed to generate JWKS' }), {
|
||||
status: 500,
|
||||
headers: { 'Content-Type': 'application/json' }
|
||||
});
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
// If URL parsing fails, pass through to original fetch
|
||||
console.debug('URL parsing failed, passing through:', e);
|
||||
}
|
||||
|
||||
// Pass through all other requests
|
||||
return originalFetch(input, init);
|
||||
};
|
||||
}
|
||||
|
||||
private static setupJWKSHandler() {
|
||||
// This is handled in the fetch override above
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a proper client assertion JWT for token requests
|
||||
*/
|
||||
static async generateClientAssertion(tokenEndpoint: string): Promise<string> {
|
||||
const now = Math.floor(Date.now() / 1000);
|
||||
const clientId = generateClientMetadata().client_id;
|
||||
|
||||
const header = {
|
||||
alg: 'ES256',
|
||||
typ: 'JWT',
|
||||
kid: 'ai-card-oauth-key-1'
|
||||
};
|
||||
|
||||
const payload = {
|
||||
iss: clientId,
|
||||
sub: clientId,
|
||||
aud: tokenEndpoint,
|
||||
iat: now,
|
||||
exp: now + 300, // 5 minutes
|
||||
jti: crypto.randomUUID()
|
||||
};
|
||||
|
||||
return await OAuthKeyManager.signJWT(header, payload);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Service Worker alternative for intercepting requests
|
||||
* (This is a more robust solution for production)
|
||||
*/
|
||||
export function registerOAuthServiceWorker() {
|
||||
if ('serviceWorker' in navigator) {
|
||||
const swCode = `
|
||||
self.addEventListener('fetch', (event) => {
|
||||
const url = new URL(event.request.url);
|
||||
|
||||
if (url.pathname.endsWith('/client-metadata.json')) {
|
||||
event.respondWith(
|
||||
new Response(JSON.stringify({
|
||||
client_id: url.origin + '/client-metadata.json',
|
||||
client_name: 'ai.card',
|
||||
client_uri: url.origin,
|
||||
redirect_uris: [url.origin + '/oauth/callback'],
|
||||
response_types: ['code'],
|
||||
grant_types: ['authorization_code', 'refresh_token'],
|
||||
token_endpoint_auth_method: 'private_key_jwt',
|
||||
scope: 'atproto transition:generic',
|
||||
subject_type: 'public',
|
||||
application_type: 'web',
|
||||
dpop_bound_access_tokens: true,
|
||||
jwks_uri: url.origin + '/.well-known/jwks.json'
|
||||
}, null, 2), {
|
||||
headers: { 'Content-Type': 'application/json' }
|
||||
})
|
||||
);
|
||||
}
|
||||
});
|
||||
`;
|
||||
|
||||
const blob = new Blob([swCode], { type: 'application/javascript' });
|
||||
const swUrl = URL.createObjectURL(blob);
|
||||
|
||||
navigator.serviceWorker.register(swUrl).catch(console.error);
|
||||
}
|
||||
}
|
204
web/src/utils/oauth-keys.ts
Normal file
204
web/src/utils/oauth-keys.ts
Normal file
@@ -0,0 +1,204 @@
|
||||
/**
|
||||
* OAuth JWKS key generation and management
|
||||
*/
|
||||
|
||||
export interface JWK {
|
||||
kty: string;
|
||||
crv: string;
|
||||
x: string;
|
||||
y: string;
|
||||
d?: string;
|
||||
use: string;
|
||||
kid: string;
|
||||
alg: string;
|
||||
}
|
||||
|
||||
export interface JWKS {
|
||||
keys: JWK[];
|
||||
}
|
||||
|
||||
export class OAuthKeyManager {
|
||||
private static keyPair: CryptoKeyPair | null = null;
|
||||
private static jwks: JWKS | null = null;
|
||||
|
||||
/**
|
||||
* Generate or retrieve existing ECDSA key pair for OAuth
|
||||
*/
|
||||
static async getKeyPair(): Promise<CryptoKeyPair> {
|
||||
if (this.keyPair) {
|
||||
return this.keyPair;
|
||||
}
|
||||
|
||||
// Try to load from localStorage first
|
||||
const storedKey = localStorage.getItem('oauth_private_key');
|
||||
if (storedKey) {
|
||||
try {
|
||||
const keyData = JSON.parse(storedKey);
|
||||
this.keyPair = await this.importKeyPair(keyData);
|
||||
return this.keyPair;
|
||||
} catch (error) {
|
||||
console.warn('Failed to load stored key, generating new one:', error);
|
||||
localStorage.removeItem('oauth_private_key');
|
||||
}
|
||||
}
|
||||
|
||||
// Generate new key pair
|
||||
this.keyPair = await window.crypto.subtle.generateKey(
|
||||
{
|
||||
name: 'ECDSA',
|
||||
namedCurve: 'P-256',
|
||||
},
|
||||
true, // extractable
|
||||
['sign', 'verify']
|
||||
);
|
||||
|
||||
// Store private key for persistence
|
||||
await this.storeKeyPair(this.keyPair);
|
||||
|
||||
return this.keyPair;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get JWKS (JSON Web Key Set) for public key distribution
|
||||
*/
|
||||
static async getJWKS(): Promise<JWKS> {
|
||||
if (this.jwks) {
|
||||
return this.jwks;
|
||||
}
|
||||
|
||||
const keyPair = await this.getKeyPair();
|
||||
const publicKey = await window.crypto.subtle.exportKey('jwk', keyPair.publicKey);
|
||||
|
||||
this.jwks = {
|
||||
keys: [
|
||||
{
|
||||
kty: publicKey.kty!,
|
||||
crv: publicKey.crv!,
|
||||
x: publicKey.x!,
|
||||
y: publicKey.y!,
|
||||
use: 'sig',
|
||||
kid: 'ai-card-oauth-key-1',
|
||||
alg: 'ES256'
|
||||
}
|
||||
]
|
||||
};
|
||||
|
||||
return this.jwks;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sign a JWT with the private key
|
||||
*/
|
||||
static async signJWT(header: any, payload: any): Promise<string> {
|
||||
const keyPair = await this.getKeyPair();
|
||||
|
||||
const headerB64 = btoa(JSON.stringify(header)).replace(/=/g, '');
|
||||
const payloadB64 = btoa(JSON.stringify(payload)).replace(/=/g, '');
|
||||
const message = `${headerB64}.${payloadB64}`;
|
||||
|
||||
const signature = await window.crypto.subtle.sign(
|
||||
{ name: 'ECDSA', hash: 'SHA-256' },
|
||||
keyPair.privateKey,
|
||||
new TextEncoder().encode(message)
|
||||
);
|
||||
|
||||
const signatureB64 = btoa(String.fromCharCode(...new Uint8Array(signature)))
|
||||
.replace(/\+/g, '-')
|
||||
.replace(/\//g, '_')
|
||||
.replace(/=/g, '');
|
||||
|
||||
return `${message}.${signatureB64}`;
|
||||
}
|
||||
|
||||
private static async storeKeyPair(keyPair: CryptoKeyPair): Promise<void> {
|
||||
try {
|
||||
const privateKey = await window.crypto.subtle.exportKey('jwk', keyPair.privateKey);
|
||||
localStorage.setItem('oauth_private_key', JSON.stringify(privateKey));
|
||||
} catch (error) {
|
||||
console.error('Failed to store private key:', error);
|
||||
}
|
||||
}
|
||||
|
||||
private static async importKeyPair(keyData: any): Promise<CryptoKeyPair> {
|
||||
const privateKey = await window.crypto.subtle.importKey(
|
||||
'jwk',
|
||||
keyData,
|
||||
{ name: 'ECDSA', namedCurve: 'P-256' },
|
||||
true,
|
||||
['sign']
|
||||
);
|
||||
|
||||
// Derive public key from private key
|
||||
const publicKeyData = { ...keyData };
|
||||
delete publicKeyData.d; // Remove private component
|
||||
|
||||
const publicKey = await window.crypto.subtle.importKey(
|
||||
'jwk',
|
||||
publicKeyData,
|
||||
{ name: 'ECDSA', namedCurve: 'P-256' },
|
||||
true,
|
||||
['verify']
|
||||
);
|
||||
|
||||
return { privateKey, publicKey };
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear stored keys (for testing/reset)
|
||||
*/
|
||||
static clearKeys(): void {
|
||||
localStorage.removeItem('oauth_private_key');
|
||||
this.keyPair = null;
|
||||
this.jwks = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate dynamic client metadata based on current URL
|
||||
*/
|
||||
export function generateClientMetadata(): any {
|
||||
const origin = window.location.origin;
|
||||
const clientId = `${origin}/client-metadata.json`;
|
||||
|
||||
// Use static production metadata for xxxcard.syui.ai
|
||||
if (origin === 'https://xxxcard.syui.ai') {
|
||||
return {
|
||||
client_id: 'https://xxxcard.syui.ai/client-metadata.json',
|
||||
client_name: 'ai.card',
|
||||
client_uri: 'https://xxxcard.syui.ai',
|
||||
logo_uri: 'https://xxxcard.syui.ai/favicon.ico',
|
||||
tos_uri: 'https://xxxcard.syui.ai/terms',
|
||||
policy_uri: 'https://xxxcard.syui.ai/privacy',
|
||||
redirect_uris: ['https://xxxcard.syui.ai/oauth/callback'],
|
||||
response_types: ['code'],
|
||||
grant_types: ['authorization_code', 'refresh_token'],
|
||||
token_endpoint_auth_method: 'private_key_jwt',
|
||||
token_endpoint_auth_signing_alg: 'ES256',
|
||||
scope: 'atproto transition:generic',
|
||||
subject_type: 'public',
|
||||
application_type: 'web',
|
||||
dpop_bound_access_tokens: true,
|
||||
jwks_uri: 'https://xxxcard.syui.ai/.well-known/jwks.json'
|
||||
};
|
||||
}
|
||||
|
||||
// Dynamic metadata for development
|
||||
return {
|
||||
client_id: clientId,
|
||||
client_name: 'ai.card',
|
||||
client_uri: origin,
|
||||
logo_uri: `${origin}/favicon.ico`,
|
||||
tos_uri: `${origin}/terms`,
|
||||
policy_uri: `${origin}/privacy`,
|
||||
redirect_uris: [`${origin}/oauth/callback`],
|
||||
response_types: ['code'],
|
||||
grant_types: ['authorization_code', 'refresh_token'],
|
||||
token_endpoint_auth_method: 'private_key_jwt',
|
||||
token_endpoint_auth_signing_alg: 'ES256',
|
||||
scope: 'atproto transition:generic',
|
||||
subject_type: 'public',
|
||||
application_type: 'web',
|
||||
dpop_bound_access_tokens: true,
|
||||
jwks_uri: `${origin}/.well-known/jwks.json`
|
||||
};
|
||||
}
|
@@ -3,13 +3,29 @@ import react from '@vitejs/plugin-react'
|
||||
|
||||
export default defineConfig({
|
||||
plugins: [react()],
|
||||
build: {
|
||||
// Keep console.log in production for debugging
|
||||
minify: 'esbuild',
|
||||
},
|
||||
esbuild: {
|
||||
drop: [], // Don't drop console.log
|
||||
},
|
||||
server: {
|
||||
port: 3000,
|
||||
port: 5173,
|
||||
host: '127.0.0.1',
|
||||
allowedHosts: ['localhost', '127.0.0.1', 'xxxcard.syui.ai'],
|
||||
proxy: {
|
||||
'/api': {
|
||||
target: 'http://localhost:8000',
|
||||
target: 'http://127.0.0.1:8000',
|
||||
changeOrigin: true,
|
||||
secure: false,
|
||||
}
|
||||
},
|
||||
// Handle OAuth callback routing
|
||||
historyApiFallback: {
|
||||
rewrites: [
|
||||
{ from: /^\/oauth\/callback/, to: '/index.html' }
|
||||
]
|
||||
}
|
||||
}
|
||||
})
|
Reference in New Issue
Block a user