-
Notifications
You must be signed in to change notification settings - Fork 3
/
gameUpload.js
93 lines (82 loc) · 3.69 KB
/
gameUpload.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
var uploadingGame = false;
function toDataUrl(url, callback) {
if (url == null) {
callback(null);
} else if (url.startsWith("data:")) {
callback(url);
} else {
var xhr = new XMLHttpRequest();
xhr.onload = function() {
var reader = new FileReader();
reader.onloadend = function() {
callback(reader.result);
}
reader.readAsDataURL(xhr.response);
};
xhr.open("GET", url);
xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
xhr.responseType = "blob";
xhr.send();
}
}
function formatDate(date) {
var monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var day = date.getDate();
var monthIndex = date.getMonth();
var year = date.getFullYear();
return day + " " + monthNames[monthIndex] + " " + year;
}
function gameUpload() {
if ($("#gameLink").val().startsWith("http://") || $("#gameLink").val().startsWith("https://")) {
if (!uploadingGame) {
uploadingGame = true;
$(".uploadGame").text("Uploading...");
$(".uploadGame").css({
backgroundColor: "#7e7e7e",
color: "black",
cursor: "default"
});
firebase.database().ref("users/" + currentUid + "/_settings/name").once("value", function(snapshot) {
var name = snapshot.val();
var attributionText = document.getElementById("attributionText").value;
var attributionLink = document.getElementById("attributionLink").value;
var title = profanity.clean($("#gameTitle").val());
toDataUrl(
$("#gameLink").val().startsWith("https://scratch.mit.edu/projects/")?
"https://cors-anywhere.herokuapp.com/" + "https://cdn2.scratch.mit.edu/get_image/project/" + $("#gameLink").val().split("/")[4] + "_288x216.png"
: (
$("#gameThumbnail").val() != "" ?
"https://cors-anywhere.herokuapp.com/" + $("#gameThumbnail").val()
: null
)
,
function(base64Img) {
var newGame = firebase.database().ref("games").push()
newGame.set({
title: capitalizeText(title),
thumbnail: base64Img,
src: $("#gameLink").val().replace(/http:\/\//g, "https://"),
description: profanity.clean($("#gameDescription").val()),
metrics: {likes: 0},
dateAdded: formatDate(new Date()),
category: $("#gameTag").val(),
uid: currentUid,
by: name,
byStaff: isStaff(currentUid),
verified: isStaff(currentUid),
attributionText: attributionText,
attributionLink: attributionLink
}).then(function() {
window.location.href = "game?play=" + newGame.key;
});
}
);
});
}
} else {
dialog("Please check your info", `
In the Game File Link section, make sure that you add a <code>https://</code>
at the start to validate the link!
`, [{text: "OK", onclick: "closeDialog();", type: "primary"}]);
}
}