-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayerView.js
123 lines (115 loc) · 4.15 KB
/
PlayerView.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import React, { useState, useEffect } from 'react';
import { useRouter } from 'next/router';
import styles from '../styles/PlayerView.module.css';
import { Skull, Sword } from 'lucide-react';
const getHealthStatus = (currentHP, maxHP) => {
const percentage = (currentHP / maxHP) * 100;
if (currentHP === 0) return { text: 'Dead', color: 'grey' };
if (percentage <= 24) return { text: 'Near Death', color: 'darkred' };
if (percentage <= 49) return { text: 'Bloodied', color: 'red' };
if (percentage <= 74) return { text: 'Wounded', color: 'darkorange' };
if (percentage <= 99) return { text: 'Barely Wounded', color: 'orange' };
return { text: 'Healthy', color: 'green' };
};
const PlayerView = () => {
const router = useRouter();
const { sessionId } = router.query;
const [combatants, setCombatants] = useState([]);
const [currentTurn, setCurrentTurn] = useState(0);
const [isCombatActive, setIsCombatActive] = useState(false);
const [isLoading, setIsLoading] = useState(true);
const [error, setError] = useState(null);
useEffect(() => {
const fetchState = async () => {
if (!sessionId) return;
try {
const response = await fetch(`/api/combatants?sessionId=${sessionId}`);
if (response.ok) {
const data = await response.json();
console.log('PlayerView: Received state:', data);
if (Array.isArray(data.combatants)) {
setCombatants(data.combatants);
setCurrentTurn(data.currentTurn);
setIsCombatActive(data.isCombatActive);
} else {
setError('Invalid data format received from server');
}
} else {
setError('Failed to fetch state');
}
} catch (error) {
console.error('Error fetching state:', error);
setError('Error fetching state');
} finally {
setIsLoading(false);
}
};
fetchState();
const intervalId = setInterval(fetchState, 2000);
return () => clearInterval(intervalId);
}, [sessionId]);
if (isLoading) {
return <div>Loading...</div>;
}
if (error) {
return <div>Error: {error}</div>;
}
return (
<div className={styles.playerView}>
<h1>Combat Status</h1>
{!isCombatActive ? (
<div>
<p>Combat is not currently active.</p>
{combatants.length > 0 && (
<div>
<h2>Prepared Combatants:</h2>
<ul className={styles.combatantList}>
{combatants.map((combatant) => (
<li key={combatant.id} className={styles.combatant}>
{combatant.type === 'enemy' ? (
<Skull className={styles.entityIcon} />
) : (
<Sword className={styles.entityIcon} />
)}
<span className={styles.name}>{combatant.name}</span>
</li>
))}
</ul>
</div>
)}
</div>
) : (
<div>
{Array.isArray(combatants) && combatants.length > 0 ? (
<ul className={styles.combatantList}>
{combatants.map((combatant, index) => (
<li
key={combatant.id}
className={`${styles.combatant} ${index === currentTurn ? styles.active : ''}`}
>
{combatant.type === 'enemy' ? (
<Skull className={styles.entityIcon} />
) : (
<Sword className={styles.entityIcon} />
)}
<span className={styles.name}>{combatant.name}</span>
{combatant.type === 'enemy' && (
<span
className={styles.healthStatus}
style={{ color: getHealthStatus(combatant.currentHP, combatant.maxHP).color }}
>
{getHealthStatus(combatant.currentHP, combatant.maxHP).text}
</span>
)}
</li>
))}
</ul>
) : (
<p>No combatants available</p>
)}
</div>
)}
</div>
);
};
export default PlayerView;