## 2026-10-02T02:46:48Z
You are Worker M2 (teamwork_preview_worker) implementing Milestone 2: Party Engine & Scaling System for FreeExile.
Working Directory: c:\Projects\FreeExile\.agents\teamwork\worker_m2
Parent Conversation ID: 914399d4-059b-422e-a9e6-44e1005de38a

MANDATORY FIRST STEP:
Read:
- c:\Projects\FreeExile\.agents\teamwork\ORIGINAL_REQUEST.md (specifically ## 2026-10-02T02:12:28Z)
- c:\Projects\FreeExile\PROJECT.md
- c:\Projects\FreeExile\TEST_READY.md
- c:\Projects\FreeExile\.agents\teamwork\explorer_survey_1\survey_report.md
- c:\Projects\FreeExile\.agents\teamwork\worker_m1\handoff.md

MANDATORY INTEGRITY WARNING:
DO NOT CHEAT. All implementations must be genuine. DO NOT hardcode test results, create dummy/facade implementations, or circumvent the intended task. A teamwork_preview_auditor will independently verify your work. Integrity violations WILL be detected and your work WILL be rejected.

YOUR WRITE OWNERSHIP:
You own exclusively:
- `server/party/party_types.py`
- `server/party/party_scaling_calculator.py`
- `server/party/party_service.py`
- `server/inventory/loot_filter_types.py` (extend GroundLootDrop with allocation fields)
- `server/inventory/loot_drop_service.py` (integrate loot allocation & multipliers)
- `server/combat/monster_scaling.py` (integrate party scaling)
- `server/world/game_design_matrix_service.py` (minor: add verify_matrix_integrity alias and currency/rarity checks)
- `tests/unit/test_party_scaling.py`

YOUR TASKS:
1. Implement `server/party/party_types.py`:
   - `LootAllocationMode` (PERMANENT, SHORT, FREE_FOR_ALL)
   - `PartyRole` (LEADER, MEMBER)
   - `PartyMemberState` (@dataclass(slots=True, frozen=True): player_id, character_name, level, zone_id, instance_id, wx, wy, role, is_online, hp, max_hp)
   - `PartyState` (party_id, leader_id, members: Dict[str, PartyMemberState], loot_mode: LootAllocationMode, created_at)
   - Enums and DTOs for party invites and actions.
2. Implement `server/party/party_scaling_calculator.py`:
   - `calculate_nearby_members(members: List[PartyMemberState], ref_wx: float, ref_wy: float, ref_zone: str, ref_inst: str, radius: float = 15.0) -> List[PartyMemberState]`
   - `calculate_party_exp(base_exp: int, nearby_members: List[PartyMemberState]) -> Dict[str, int]`:
     Total pool = base_exp * (1.0 + 0.30 * (N - 1)). Share weight W_i = (Level_i + 10)^2.71. Apply LevelProgressionService level gap multiplier per member.
   - `calculate_loot_multipliers(nearby_count: int) -> Tuple[float, float, float]`:
     Item Qty = 1.0 + 0.50 * (N - 1), Currency Qty = 1.0 + 0.50 * (N - 1), Rarity = 1.0 + 0.30 * (N - 1).
   - `calculate_monster_scaling(base_hp: int, rank: str, party_count: int) -> Dict[str, Any]`:
     Common/Magic +50% HP/player, Rare +70% HP/player, Boss +100% HP/player. Armor +10%/player, Res +2%/player (cap 75%), Poise 15% Max HP.
3. Implement `server/party/party_service.py`:
   - Full 6-player lifecycle: `create_party`, `invite_player`, `accept_invite`, `decline_invite`, `promote_leader`, `leave_party`, `kick_member`, `disband_party`.
   - Max 6 players validation.
   - Leader disconnect / leave auto-promotes next senior member.
   - Set loot allocation mode (Leader only).
   - Thread-safe concurrency.
4. Integrate with `GroundLootDrop` and `LootDropService`:
   - Extend `GroundLootDrop` with `allocated_player_id: str = ""`, `allocation_mode: str = "FREE_FOR_ALL"`, `allocation_expires_at: float = 0.0`.
   - Method `can_player_pickup(player_id: str, current_time: float) -> bool`:
     If PERMANENT: player_id == allocated_player_id.
     If SHORT: player_id == allocated_player_id or current_time >= allocation_expires_at.
     If FREE_FOR_ALL: True.
   - Update `LootDropService.generate_monster_drops` to accept allocation mode and party members, assigning items round-robin to nearby members.
5. Create comprehensive unit tests in `tests/unit/test_party_scaling.py`.
6. Adhere strictly to GEMINI.md: all files <= 350 lines (hard cap 500), strictly typed, memory efficient.
7. Verification:
   - `pytest tests/unit/test_party_scaling.py -v`
   - `pytest tests/e2e_social_party/test_tier1_party_and_scaling.py -v`
   - `pytest tests/e2e_social_party/ -v`
   - `python tools/lint/check_code_and_doc_hygiene.py --strict`
8. Write handoff to `c:\Projects\FreeExile\.agents\teamwork\worker_m2\handoff.md`.
Send message to parent when done.
