-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchat.html
134 lines (126 loc) · 5.27 KB
/
chat.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
<!DOCTYPE html>
<html>
<head>
<title>Example chat - Parking Master</title>
<script src="https://cdn.jsdelivr.net/gh/Parking-Master/Gametime.js@latest/Gametime.min.js"></script>
<style>
body { margin: 0; padding-bottom: 3rem; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; }
#form { background: rgba(0, 0, 0, 0.15); padding: 0.25rem; position: fixed; bottom: 0; left: 0; right: 0; display: flex; height: 3rem; box-sizing: border-box; } #input { border: none; padding: 0 1rem; flex-grow: 1; border-radius: 2rem; margin: 0.25rem; }
#input:focus { outline: none; } #form button { background: #333; border: none; padding: 0 1rem; margin: 0.25rem; border-radius: 3px; outline: none; color: #fff; }
#messages { list-style-type: none; margin: 0; padding: 0; }
#messages > li { padding: 0.5rem 1rem; }
#messages > li:nth-child(odd) { background: #efefef; }
#input:disabled, form button:disabled { opacity: 0.5; }
</style>
<script type="text/javascript" src="https://cdn.jsdelivr.net/gh/Parking-Master/Gametime.js@latest/Gametime.js"></script>
</head>
<body>
<ul id="messages"></ul>
<form id="form" action="">
<input id="input" autocomplete="off" /><button class="send">Send</button><button class="disconnect">Disconnect</button>
</form>
<script>
function getCookie(cname) {
let name = cname + "=";
let decodedCookie = decodeURIComponent(document.cookie);
let ca = decodedCookie.split(";");
for (let i = 0; i < ca.length; i++) {
let c = ca[i];
while (c.charAt(0) == " ") {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
var username = !!(getCookie("user-email") && getCookie("user-pass")) ? getCookie("user-name") : (localStorage["uname"]) || "Unknown";
var CurrentUserID = [];
gametime.set("key", "pub-c-c44c8fc4-612e-4fc3-b875-4398f01da63c", "sub-c-b6832794-3c08-11ec-b2c1-a25c7fcd9558");
gametime.set("channel", "example-channel123");
gametime.make("message");
gametime.make("postID");
gametime.make("checkUserID");
gametime.make("isGuest");
function sendMessage(msg) {
var item = document.createElement("li");
item.textContent = msg;
messages.appendChild(item);
window.scrollTo(0, document.body.scrollHeight);
var chatHistory = localStorage["history"];
var noBug = chatHistory === "" ? "" : ",";
// localStorage.setItem("history", (chatHistory + noBug + encodeURIComponent(msg.split(":").splice(1).join(":").trim())).split(",").toString());
}
gametime.on("message", sendMessage);
var messages = document.getElementById("messages");
var form = document.getElementById("form");
var input = document.getElementById("input");
if (!localStorage["history"]) {
localStorage.setItem("history", "");
} else {
for (var i = 0; i < localStorage["history"].split(",").length; i++) {
var item = document.createElement("li");
item.textContent = (username + ": ") + decodeURIComponent(localStorage["history"].split(",")[i]);
messages.appendChild(item);
window.scrollTo(0, document.body.scrollHeight);
}
}
form.onsubmit = function(event) {
event.preventDefault();
if (input.value) {
gametime.run("message", [(username + ": ") + input.value]);
input.value = "";
}
};
document.querySelector(".disconnect").onclick = function(event) {
event.preventDefault();
if (confirm("Are you sure?")) {
this.textContent = "Reconnect";
gametime.disconnect();
localStorage.removeItem("history");
messages.innerHTML = "";
document.querySelector(".disconnect").disabled = "disabled", document.querySelector(".send").disabled = "disabled", input.disabled = "disabled";
}
};
function postUserID(id) {
CurrentUserID.unshift(id.toString());
}
gametime.on("postID", postUserID);
window.onload = function() {
setTimeout(() => gametime.run("postID", [gametime.user.id]), 2500);
setInterval(() => {
setTimeout(function() {
if (CurrentUserID[0] == gametime.user.id) {
isGuest = true;
isHost = false;
team = "red";
} else if (CurrentUserID[1] == gametime.user.id) {
isHost = true;
isGuest = false;
team = "blue";
}
}, 800);
}, 4000);
}
setInterval(() => {
function removeDuplicate(arr) {
var tmp = [];
for (var i = 0; i < arr.length; i++){
if (tmp.indexOf(arr[i]) == -1) {
tmp.push(arr[i]);
}
}
return tmp;
}
CurrentUserID = removeDuplicate(CurrentUserID);
}, 100);
let checkIfUserJoined = setInterval(() => {
if (CurrentUserID.length >= 2) {
clearInterval(checkIfUserJoined);
gametime.run("postID", [gametime.user.id])
}
}, 1500);
</script>
</body>
</html>