-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.html
159 lines (138 loc) · 5.04 KB
/
main.html
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Game Chat</title>
<style>
body {
font-family: Arial, sans-serif;
display: flex;
flex-direction: column;
height: 100vh;
margin: 0;
padding: 0;
background-color: #f4f4f9;
}
#chat-container {
display: flex;
flex-direction: column;
flex-grow: 1;
padding: 20px;
overflow-y: auto;
background-color: #fff;
border-bottom: 2px solid #ddd;
}
#chat-box {
flex-grow: 1;
margin-bottom: 20px;
}
#button-panel {
display: flex;
justify-content: space-around; /* Distribute buttons evenly */
flex-wrap: wrap; /* Allow wrapping if necessary */
padding: 10px;
background-color: #f9f9f9;
border-top: 2px solid #ddd;
}
.button-container {
display: flex;
flex-direction: column; /* Stack button and description vertically */
align-items: center; /* Center align the button and description */
margin: 10px;
}
button {
padding: 10px 20px;
font-size: 14px;
cursor: pointer;
background-color: #007bff;
color: white;
border: none;
border-radius: 5px;
}
button:hover {
background-color: #0056b3;
}
#chat-box div {
padding: 10px;
margin-bottom: 5px;
background-color: #e9ecef;
border-radius: 5px;
}
.button-description {
font-size: 12px;
color: #555;
margin-top: 5px;
}
</style>
</head>
<body>
<div id="chat-container">
<div id="chat-box">
<!-- Messages will appear here -->
</div>
</div>
<div id="button-panel">
<!-- Buttons will appear here -->
</div>
<script>
// Function to fetch chat messages from Flask
async function fetchMessages() {
const response = await fetch("http://127.0.0.1:5000/get_messages");
const data = await response.json();
const chatBox = document.getElementById("chat-box");
chatBox.innerHTML = ''; // Clear current messages
data.messages.forEach(message => {
let messageDiv = document.createElement("div");
messageDiv.textContent = `${message.sender}: ${message.message}`;
chatBox.appendChild(messageDiv);
});
}
// Function to fetch buttons from Flask
async function fetchButtons() {
const response = await fetch("http://127.0.0.1:5000/get_buttons");
const data = await response.json();
const buttonPanel = document.getElementById("button-panel");
buttonPanel.innerHTML = ''; // Clear current buttons and descriptions
data.buttons.forEach(button => {
let buttonContainer = document.createElement("div");
buttonContainer.classList.add("button-container");
// Create the button element
let buttonElement = document.createElement("button");
buttonElement.textContent = button.label;
buttonElement.onclick = () => handleButtonClick(button.action);
// Create the description element
let descriptionElement = document.createElement("p");
descriptionElement.classList.add("button-description");
descriptionElement.textContent = button.description;
// Append the button and its description to the container
buttonContainer.appendChild(buttonElement);
buttonContainer.appendChild(descriptionElement);
// Append the container to the panel
buttonPanel.appendChild(buttonContainer);
});
}
// Function to handle button clicks
async function handleButtonClick(action) {
const response = await fetch("http://127.0.0.1:5000/button_click", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({ action })
});
const data = await response.json();
console.log(data.response);
fetchMessages(); // Refresh chat after button click
}
// Initial fetch of messages and buttons
fetchMessages();
fetchButtons();
// Optionally set an interval to update the UI periodically (e.g., every 5 seconds)
setInterval(() => {
fetchMessages();
fetchButtons();
}, 1000);
</script>
</body>
</html>