-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.html
239 lines (220 loc) · 10.4 KB
/
index.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
<!DOCTYPE html>
<html lang="en-us">
<!DOCTYPE html>
<html lang="en-us">
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Unity WebGL Player | SkyGameSDK Example</title>
<script src="Build/UnityLoader.js"></script>
<script src="https://skynet-js.hns.siasky.net/4.0-beta/index.js"></script>
<script src="https://siasky.net/_AAnblJDpo1aB9bjN5VFsZUTPBasmujkcQuiEDibAO6rQQ"></script>
<script>
//THESE VALUES SHOULD BE MODIFIED FOR YOUR LIKING!!!
const client = new skynet.SkynetClient("https://siasky.net"); //siasky portal by default, feel free to change
const dataKey = "myApp"; //enter your game name here if you want - shouldn't be any issues leaving it as is, though
var gameInstance = UnityLoader.instantiate("unityContainer", "Build/GameName.json"); //Build/GameName.json will probably need to be adjusted if you change the name of your project.
var hnsDomain = "abughadiyah.hns"; //hns domain string
//THESE TOO SHOULD BE CHANGED, IF YOU WANT A STANDALONE GAME
var unityCommunicatorObject = "GameMaster"; //object to recieve browser communication
var unityRecieverFunction = "DisposeOf"; //function in script to handle incoming data from browser
//GENERIC, CAN BE IGNORED IN MOST CASES
const contentRecord = new contentrecord.ContentRecordDAC(); //makes contentrecord is accessable through contentrecord.*
var revisions = 0; //revision number, gets added on every revision in the revision function
var data = Math.random().toString(); //random string
var friendsPublicKey;
var canRead = true; //disable if you want to enforce mysky login
var hasInitialized = false;
//TO BE IGNORED
var skylink = "sia://AABr1uIDTBGLAW4E2m_PVdo2DJij5EwASLsqcTCAxkB8Tg"; //generate a new one using the setAndGetSkylink function
var oldData;
function generateLobbyURL(){ //if the link includes a hashtag, it will change the random value already assigned, if not, create a link + some logic to get friends public key
if (document.location.toString().includes("#")){
console.log("Includes a hashtag!");
data = document.location.toString().split('#/')[1] + "1";
data2 = document.location.toString().split('#/')[1];
const { publicKey } = skynet.genKeyPairFromSeed(data2);
skylink = "sia://" + hnsDomain + "#/" + "#/" + data2; //edits the skylink so that it may work for the contentrecord DAC
friendsPublicKey = publicKey;
} else {
var random = data + "1"; //random string to make a new key for player2
var random2 = "#/" + data; //adding our random data as to value convenience/how short the link is over how secure the keys are - can be changed
document.location = document.location + "#/" + data;
skylink = "sia://" + hnsDomain + "/#/" + random + "#/" + data; //edits the skylink so that it may work for the contentrecord DAC
const { publicKey } = skynet.genKeyPairFromSeed(random);
friendsPublicKey = publicKey;
}
}
generateLobbyURL(); //remove this line if you want to do lobbies from inside unity, keep it otherwise as otherwise each restart will lead to a new link
//useful for own functions
const { publicKey, privateKey, seed } = skynet.genKeyPairFromSeed(data); //generates publickey, private key and seed from randomness
async function getSlowData() { //retrieve slow data from skydb, any amount of data is fine
if (canRead == true){
try {
const { data, skylink } = await client.db.getJSON(friendsPublicKey, dataKey); //gets data and skylink from skyDB
if (data != null){ //simple check not to spam logs & contentrecord
gameInstance.SendMessage(unityCommunicatorObject, unityRecieverFunction, data.example); //sends data.example(json) to Unity
console.log("retrieved slow data: " + data.example);
//seperate data fields
//gameInstance.SendMessage(unityCommunicatorObject, unityRecieverFunction, data.example2); //sends data.example2(json) to Unity
//console.log("retrieved slow data: " + data.example2);
}
} catch (error) {
console.log(error);
}
}
}
async function setSlowData(_data) { //set slow data, any amount of data is fine
const json = { example: _data };
//seperate data fields
//const json = { example: _data, example2: _data2 };
try {
await client.db.setJSON(privateKey, dataKey, json);
console.log("sent slow data: " + json.example);
} catch (error) {
console.log(error);
}
}
async function setFastData(_data){ //fast data(should be obsolete soon, just a workaround), don't send more than 70 bytes
var revision = BigInt(revisions);
var data = _data;
revisions++;
const entry = { dataKey, data, revision };
try {
await client.registry.setEntry(privateKey, entry);
console.log("sent fast data " + data);
} catch (error) {
console.log(error);
}
}
async function getFastData(){ //get fast data =)
if (canRead == true){
try {
const { entry, signature } = await client.registry.getEntry(friendsPublicKey, dataKey);
if (entry != null){ //simple check not to spam logs & contentrecord
gameInstance.SendMessage(unityCommunicatorObject, unityRecieverFunction, entry.data); //sends data to unity
console.log("recieved data " + entry.data);
}
} catch (error) {
console.log(error);
}
}
}
async function setCloudData(_data) {
if (hasInitialized == true){
try {
const { data, skylink } = await mySkyObject.setJSON(hnsDomain + "/path/file.json", { message: _data });
//for seperate data fields
//const { data, skylink } = await mySkyObject.setJSON(hnsDomain + "/path/file.json", { message: _data, message2 _data2 });
} catch (error) {
console.log(error)
}
}
}
async function getCloudData() {
if (hasInitialized == true){
try {
const { data, skylink } = await mySkyObject.getJSON(hnsDomain + "/path/file.json");
if (data == null){
setCloudData("0"); //change 0 to default value
} else {
gameInstance.SendMessage(unityCommunicatorObject, unityRecieverFunction, data.message);
//for seperate data fields
//gameInstance.SendMessage(unityCommunicatorObject, unityRecieverFunction, data.message);
}
} catch (error) {
console.log(error)
}
}
}
async function autoLogin() { //checks if we're already logged in
if (hasInitialized == false){
try {
const mySky = await client.loadMySky(hnsDomain, {debug: true});
const loggedIn = await mySky.checkLogin();
if (loggedIn){
hasInitialized = true;
canRead = true;
await mySky.loadDacs(contentRecord);
mySkyObject = mySky;
}
} catch (error) {
console.log(error)
}
}
}
async function manualLogin(){
if (hasInitialized == false){
hasInitialized = true;
const mySky = await client.loadMySky(hnsDomain, {debug: true});
const isLoggedIn = await mySky.checkLogin();
await mySky.loadDacs(contentRecord);
if (!isLoggedIn) {
await mySky.requestLoginAccess();
}
if (isLoggedIn){
canRead = true;
}
}
}
async function setLeaderBoardData(_data){ //example "finishedGame"
if (oldData != _data && canRead == true){ //we need to make sure mysky is initialized
oldData = _data;
await contentRecord.recordNewContent({
skylink,
metadata: {"gameData": _data} //exchange to your own personal needs - this is just an example and is generic
});
console.log("added data " + _data + " to contentrecord with skylink " + skylink);
}
}
function setBrowserStorage(_item, _data){ //example ("name", "Frank")
localStorage.setItem(_item, _data);
}
function getBrowserStorage(_item){ //example ("name"); returns const "Frank"
const item = localStorage.getItem(_item);
console.log("item " + _item + ": " + item);
gameInstance.SendMessage(unityCommunicatorObject, unityRecieverFunction, item);
}
async function setAndGetSkylink() { //function for random skylink, can be altered to work in "rooms" or be made deterministically from the host
const json = { example: "skylink" };
var random = Math.random().toString();
const { publicKey, privateKey } = skynet.genKeyPairFromSeed(random);
try {
await client.db.setJSON(privateKey, dataKey, json);
console.log("created skylink entry");
setTimeout(async function () {
const { entry, signature } = await client.registry.getEntry(publicKey, dataKey);
skylink = "sia://" + entry.data;
}, 20000); //might want to be changed, but skydb is quite laggy
} catch (error) {
console.log(error);
}
}
function copyToClipboard(text) {
var toCopy = document.createElement("textarea");
document.body.appendChild(toCopy);
toCopy.value = text;
toCopy.select();
document.execCommand("copy");
document.body.removeChild(toCopy);
}
function copyURL() {
var toCopy = document.createElement("textarea");
document.body.appendChild(toCopy);
toCopy.value = document.location;
toCopy.select();
document.execCommand("copy");
document.body.removeChild(toCopy);
}
</script>
</head>
<body>
<div class="webgl-content">
<div id="unityContainer" style="width: 960px; height: 600px"></div>
<div class="footer">
<div class="webgl-logo"></div>
<div class="fullscreen" onclick="unityInstance.SetFullscreen(1)"></div>
</div>
</div>
</body>
</html>