# Phase 1-2 Backend Integration TODO List

This file lists all the integration points needed to activate the monetization and retention systems.

## CRITICAL: These systems are NOT YET INTEGRATED

All Phase 1-2 systems have been implemented but are **NOT connected to the game**.
The following integrations are REQUIRED before the features can be used by players.

---

## Priority 1: WebSocket Message Handlers (CRITICAL)

**File to modify:** `/home/user/lichun/ws/app.py`

### Add imports at top of file:
```python
from monetization import (
    handle_purchase_energy_refill,
    handle_purchase_time_skip,
    handle_validate_purchase
)
from retention import (
    handle_daily_login_check,
    handle_daily_quest_check
)
```

### Add message handlers in consumer() function:
```python
# In consumer() function, add these cases:

elif message_type == 'purchaseEnergyRefill':
    from monetization import handle_purchase_energy_refill
    handle_purchase_energy_refill(player_id, message_data, send_to_client_func)

elif message_type == 'purchaseTimeSkip':
    from monetization import handle_purchase_time_skip
    handle_purchase_time_skip(player_id, message_data, send_to_client_func)

elif message_type == 'validatePurchase':
    from monetization import handle_validate_purchase
    handle_validate_purchase(player_id, message_data, send_to_client_func)

elif message_type == 'claimDailyReward':
    from retention.daily_rewards import claim_daily_reward
    result = claim_daily_reward(player_id)
    await send_to_client({'type': 'dailyRewardClaimed', **result})
```

### Add on player connection (after player loads):
```python
# Check daily login
from retention import handle_daily_login_check, handle_daily_quest_check
handle_daily_login_check(player_id, send_to_client_func)
handle_daily_quest_check(player_id, send_to_client_func)
```

---

## Priority 2: Diamond Economy Integration (CRITICAL)

**Files to modify:** `ws/events.py`, `ws/dayEvents.py`

### Add diamond rewards to existing events:

**Graduation events:**
```python
# In graduation event handler
from monetization.diamond_economy import award_diamonds

if education_level == 'high_school':
    award_diamonds(player_id, 'graduate_high_school', 25)
elif education_level == 'college':
    award_diamonds(player_id, 'graduate_college', 50)
```

**Birthday events:**
```python
# In birthday handler
if new_age % 10 == 0 and new_age >= 10:
    award_diamonds(player_id, f'reach_age_{new_age}', 20)
```

**Job/promotion events:**
```python
# In first job handler
if player.job_count == 1:
    award_diamonds(player_id, 'get_first_job', 15)

# In promotion handler
award_diamonds(player_id, 'promotion', 20)
if new_title == 'CEO':
    award_diamonds(player_id, 'become_ceo', 100)
```

**Marriage events:**
```python
# In marriage handler
award_diamonds(player_id, 'get_married', 25)
```

**Child birth events:**
```python
# In child birth handler
if children_count == 1:
    award_diamonds(player_id, 'have_first_child', 30)
```

---

## Priority 3: Achievement Integration (HIGH)

**Files to modify:** `ws/events.py`, `ws/dayEvents.py`

### Add achievement checks to major events:

```python
from retention.achievements import check_achievements

# In graduation event:
check_achievements(player_id, 'graduate', {'level': education_level, 'gpa': gpa})

# In job event:
check_achievements(player_id, 'get_job', {})

# In promotion event:
check_achievements(player_id, 'promotion', {'title': new_title})

# In marriage event:
check_achievements(player_id, 'marriage', {})

# In birth event:
check_achievements(player_id, 'birth_child', {})

# In birthday event:
check_achievements(player_id, 'birthday', {'age': new_age})

# In death event:
check_achievements(player_id, 'death', {})

# In friendship event:
check_achievements(player_id, 'make_friend', {})

# In item purchase event:
check_achievements(player_id, 'purchase_item', {})
```

---

## Priority 4: Quest Progress Integration (HIGH)

**Files to modify:** `ws/intradayActivity.py`, `ws/conversationEvents.py`

### Add quest progress tracking:

```python
from retention.daily_quests import update_quest_progress

# In work/activity handlers:
update_quest_progress(player_id, 'work_hours', hours_worked)
update_quest_progress(player_id, 'complete_activities', 1)
update_quest_progress(player_id, 'spend_energy', energy_spent)

# In conversation handler:
update_quest_progress(player_id, 'talk_to_characters', 1)

# In study/class handler:
update_quest_progress(player_id, 'attend_class', 1)
update_quest_progress(player_id, 'study', hours_studied)

# In shopping handler:
update_quest_progress(player_id, 'buy_item', 1)

# In dating handler:
update_quest_progress(player_id, 'go_on_date', 1)

# In money earning:
update_quest_progress(player_id, 'earn_money', amount_earned)

# In affinity increase:
update_quest_progress(player_id, 'increase_affinity', affinity_change)

# In social activities:
update_quest_progress(player_id, 'socialize', 1)
```

---

## Priority 5: Statistics Tracking (MEDIUM)

**Files to modify:** `ws/events.py`, `ws/intradayActivity.py`

### Add statistics tracking:

```python
from retention.statistics import (
    increment_stat,
    update_stat,
    set_boolean_stat,
    capture_photo_memory
)

# Track money:
increment_stat(player_id, 'lifetime_earnings', amount)
increment_stat(player_id, 'lifetime_spending', cost)

# Track activities:
increment_stat(player_id, 'total_activities', 1)
increment_stat(player_id, 'total_conversations', 1)
increment_stat(player_id, 'total_relationships', 1)

# Track career:
increment_stat(player_id, 'job_count', 1)
increment_stat(player_id, 'times_fired', 1)
update_stat(player_id, 'highest_job_level', job_level)

# Track relationships:
increment_stat(player_id, 'people_dated', 1)
increment_stat(player_id, 'children_count', 1)
increment_stat(player_id, 'friends_count', 1)
set_boolean_stat(player_id, 'ever_married', True)

# Capture photo memories:
capture_photo_memory(player_id, 'graduation', f'Graduated from {school}', {
    'level': education_level,
    'gpa': gpa
})
```

---

## Priority 6: Server Initialization (MEDIUM)

**File to modify:** `ws/app.py`

### Add system initialization on server startup:

```python
# At server startup (before accepting connections):

from retention.achievements import initialize_achievements
from retention.daily_quests import initialize_quest_templates
from retention.daily_rewards import initialize_daily_rewards

# Initialize achievement definitions (44 achievements)
initialize_achievements()

# Initialize quest templates (11 quests)
initialize_quest_templates()

# Initialize daily rewards (7-day cycle)
initialize_daily_rewards()

logging.info("Monetization and retention systems initialized")
```

---

## Priority 7: Player Initialization (MEDIUM)

**File to modify:** `ws/functions.py` or wherever new players are created

### Initialize player statistics on account creation:

```python
from retention.statistics import initialize_player_statistics

# When creating new player:
player_id = create_new_player(...)
initialize_player_statistics(player_id)
```

---

## Testing Integration

After integration, test these flows:

1. **Energy Refill:**
   - Player sends `purchaseEnergyRefill` message
   - Balance deducted, energy increased
   - Transaction logged

2. **Daily Login:**
   - Player connects
   - Receives `dailyRewardAvailable` message
   - Claims reward
   - Streak incremented

3. **Quest Progress:**
   - Player performs action (work, talk, etc.)
   - Quest progress updates
   - Quest completes, diamonds awarded

4. **Achievement Unlock:**
   - Player triggers event (graduation, marriage)
   - Achievement unlocked
   - Diamonds awarded
   - Notification sent

5. **Statistics:**
   - Player performs actions
   - Statistics increment
   - Photo memories captured

---

## Estimated Integration Time

- **Priority 1 (WebSocket):** 2-3 hours
- **Priority 2 (Diamond Economy):** 3-4 hours
- **Priority 3 (Achievements):** 2-3 hours
- **Priority 4 (Quests):** 4-5 hours
- **Priority 5 (Statistics):** 2-3 hours
- **Priority 6-7 (Initialization):** 1-2 hours

**Total: 14-20 hours of integration work**

---

## Database Migration

Before testing, run the migration:

```bash
mysql -u [username] -p [database_name] < ws/migrations/001_monetization_and_retention.sql
```

This creates all 14 tables and adds columns to the players table.
