Skip to content

Commit bd9a9cb

Browse files
committed
2 parents bc37c0c + 7f96c1f commit bd9a9cb

File tree

3 files changed

+58
-42
lines changed

3 files changed

+58
-42
lines changed

backend/blockchain.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
import json
2+
from functools import lru_cache
3+
4+
from eth_account import Account
15
from web3 import Web3
26
from web3.middleware import SignAndSendRawMiddlewareBuilder
3-
from eth_account import Account
7+
48
from game_settings import settings
5-
import json
69

710
CONTRACT_ABI = None
811

frontend/src/App.jsx

Lines changed: 48 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,86 @@
1-
import { useEffect, useState } from "react";
1+
import { useEffect, useState, useCallback } from "react";
22
import './styles.css';
33

4+
const LOCATION_NAMES = ["inn", "fairy_village"];
5+
const PLAYER_ID = 0;
6+
47
export default function App() {
58
const [message, setMessage] = useState("");
6-
const [dialogue, setDialogue] = useState("");
9+
const [dialogue, setDialogue] = useState("");
710
const [activeCharacter, setActiveCharacter] = useState(0);
811
const [isSending, setIsSending] = useState(false);
912
const [location, setLocation] = useState(0);
1013
const [agentsIds, setAgentsIds] = useState([]);
1114

1215
const API_URL = import.meta.env.VITE_BACKEND_API_URL;
1316

17+
const locationIdToResourceName = useCallback((id) => LOCATION_NAMES[id], []);
18+
1419
const sendMessage = async (e) => {
1520
if (e.key === "Enter" && message.trim()) {
16-
setIsSending(true);
21+
setIsSending(true);
22+
try {
23+
const response = await fetch(`${API_URL}/say`, {
24+
method: "POST",
25+
headers: { "Content-Type": "application/json" },
26+
body: JSON.stringify({ player_id: PLAYER_ID, agent_id: activeCharacter, message: message }),
27+
});
28+
29+
if (!response.ok) throw new Error(`Error: ${response.status}`);
30+
31+
const data = await response.json();
32+
33+
setDialogue(data.message);
34+
setActiveCharacter(data.responder_id);
35+
setMessage("");
36+
} catch (error) {
37+
console.error(error);
38+
} finally {
39+
setIsSending(false);
40+
}
41+
}
42+
};
1743

18-
const response = await fetch(`${API_URL}/say`, {
44+
const enterLocation = async () => {
45+
try {
46+
const response = await fetch(`${API_URL}/enterLocation`, {
1947
method: "POST",
2048
headers: { "Content-Type": "application/json" },
21-
body: JSON.stringify({ player_id: 0, agent_id: activeCharacter, message: message }),
49+
body: JSON.stringify({ player_id: PLAYER_ID, location_id: location }),
2250
});
2351

2452
if (!response.ok) throw new Error(`Error: ${response.status}`);
2553

2654
const data = await response.json();
55+
setAgentsIds(data.agents_ids);
56+
setActiveCharacter(data.agents_ids[0]);
2757

28-
setDialogue(data.message);
29-
setActiveCharacter(data.responder_id);
30-
setMessage("");
31-
setIsSending(false);
58+
} catch (error) {
59+
console.error(error);
3260
}
3361
};
3462

35-
const locationIdToResourceName = (id) => ["inn", "fairy_village"][id];
36-
37-
useEffect(() => {
38-
const enterLocation = async () => {
39-
try {
40-
const response = await fetch(`${API_URL}/enterLocation`,
41-
{
42-
method: "POST",
43-
headers: { "Content-Type": "application/json" },
44-
body: JSON.stringify({ player_id: 0, location_id: location }),
45-
});
46-
47-
if (!response.ok) throw new Error(`Error: ${response.status}`);
48-
49-
const data = await response.json();
50-
51-
setAgentsIds(data.agents_ids);
52-
setActiveCharacter(data.agents_ids[0]);
53-
54-
} catch (error) {
55-
console.error(error);
56-
}
57-
};
63+
useEffect(() => {
5864
enterLocation();
5965
}, [location]);
6066

67+
const changeLocation = () => {
68+
setLocation((prevLocation) => (prevLocation + 1) % LOCATION_NAMES.length);
69+
setDialogue("");
70+
}
71+
6172
return (
6273
<div className="app-container"
63-
style={{backgroundImage: `url('/images/${locationIdToResourceName(location)}/background.jpg')`}}>
64-
{dialogue ? <div className="dialogue-box">{dialogue}</div> : <></>}
74+
style={{ backgroundImage: `url('/images/${locationIdToResourceName(location)}/background.jpg')` }}>
75+
{dialogue && <div className="dialogue-box">{dialogue}</div>}
6576
<div className="content">
6677
{agentsIds.map((char, i) => (
6778
<div key={char} className="column">
6879
<img
6980
src={`/images/${locationIdToResourceName(location)}/character_0${i + 1}.png`}
7081
className={`character-img ${activeCharacter === char ? "talking" : ""}`}
7182
alt="Character"
72-
onClick={() => {
73-
if (!isSending) setActiveCharacter(char)
74-
}}
83+
onClick={() => !isSending && setActiveCharacter(char)}
7584
/>
7685
</div>
7786
))}
@@ -86,9 +95,9 @@ export default function App() {
8695
/>
8796
<button
8897
className="change-location-button"
89-
onClick={() => {setLocation((location + 1) % 2)}}
98+
onClick={changeLocation}
9099
>
91-
{(location === 0) ? "Leave Inn" : "Go back to Inn"}
100+
{location === 0 ? "Leave Inn" : "Go back to Inn"}
92101
</button>
93102
</div>
94103
);

frontend/src/styles.css

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ html, body {
3333
font-size: 3em;
3434
white-space: pre-wrap;
3535
z-index: 10;
36+
position: absolute;
37+
left: 12px;
38+
right: 12px;
3639
}
3740

3841
.content {
@@ -75,7 +78,7 @@ html, body {
7578

7679
.change-location-button {
7780
position: fixed;
78-
top: 20px;
81+
bottom: 50%;
7982
right: 20px;
8083
height: 50px;
8184
border-radius: 5px;
@@ -89,6 +92,7 @@ html, body {
8992
display: flex;
9093
align-items: center;
9194
justify-content: center;
95+
z-index: 50;
9296
}
9397

9498
.change-location-button:hover {

0 commit comments

Comments
 (0)