forked from coandco/TSSSFF-Generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
maker.js
243 lines (222 loc) · 8.7 KB
/
maker.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
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
240
241
242
243
//After a page is updated this stores the edit key so we can modify it again
var EDIT_KEY = null;
//Display error
function mayError(errObj){
if (errObj.error){
$("#error strong").text(errObj.error);
$("#error em").text(errObj.details);
$("#error").show()
return 1;
} else {
return 0;
}
}
//Blanks the cards
function newCard(){
$(".card").attr("class","card pony maleFemale unicorn s0");
$(".card .nameInput").val("");
$(".card .attrs").val("");
$(".card .effect").val("").change();
$(".card .flavour").val("").change();
$(".card .copyright").val("");
EDIT_KEY = null;
document.location.hash = "";
$("#editUrl,#shareUrl,#image").val("").change().addClass("empty")
$("#error").hide();
}
//Loads a card
function load(kind,id){
var o={};o[kind]=id
$.get("dbInterface.php",o,function(r){
var d = JSON.parse(r);
EDIT_KEY = null;
if(mayError(d)) {return;}
$(".card button").each(function(){
$(".card").removeClass($(this).attr("value"))
})
$(".card").addClass(d.classes);
$(".card .nameInput").val(d.name).change();
$(".card .attrs").val(d.attr);
$(".card .effect").val(d.effect);
$(".card .flavour").val(d.flavour);
$("#image").val(d.image).change();
$(".card .copyright").val(d.copyright);
$(".card textarea").change();
document.location.hash = "."
document.location.hash = ""
$("#editUrl,#shareUrl").removeClass("empty") //Bodge fix for placeholder overlay
$("#shareUrl").val(document.location+"view:"+d["viewkey"]);
if(d.editkey){
$("#editUrl").val(document.location+"edit:"+d["editkey"]);
document.location.hash = "edit:"+d["editkey"]
EDIT_KEY = d["editkey"];
} else {
$("#editUrl").val("Cannot edit");
document.location.hash = "view:"+d["viewkey"]
}
})
}
//Saves a card
function save(){
$.post("dbInterface.php",{
editkey:EDIT_KEY,
classes:$(".card").attr("class"),
name:$(".card .nameInput").val(),
attr:$(".card .attrs").val(),
effect:$(".card .effect").val(),
flavour:$(".card .flavour").val(),
copyright:$(".card .copyright").val(),
image:$("#image").val()
},function(r){
var d = JSON.parse(r);
if(mayError(d)) {return;}
document.location.hash = "."
document.location.hash = ""
$("#editUrl").val(document.location+"edit:"+d["editkey"]);
$("#shareUrl").val(document.location+"view:"+d["viewkey"]);
$("#editUrl,#shareUrl").removeClass("empty") //Bodge fix for placeholder overlay
EDIT_KEY = d["editkey"];
document.location.hash = "edit:"+d["editkey"]
})
}
function exportCard(toShipbooru){
$.post("../CardMachine/TSSSF/ponyimage.php",{
classes:$(".card").attr("class"),
name:$(".card .nameInput").val(),
attr:$(".card .attrs").val(),
effect:$(".card .effect").val(),
flavour:$(".card .flavour").val(),
copyright:$(".card .copyright").val(),
image:$("#image").val()
},function(r){
var d = JSON.parse(r);
if(mayError(d)) {return;}
/*if(toShipbooru){
var data = new FormData();
data.append("upload",new Blob([d.img_url],{type:"image/png"}))
data.append("title",$(".card .name").val());
data.append("attr",$(".card .attrs").val());
data.append("rating","q");
data.append("submit","Upload");
$.ajax({
url:"http://secretshipfic.booru.org/index.php?page=post&s=list",
type:"POST",
data: data,
processData: false,
contentType: false,
xhrFields: {
withCredentials: true
},
complete:function(n,c,d){
console.log(n,c,d)
}
})
} else {*/
open("data:image/png;base64,"+d.img_url);
//}
})
}
function cardSetup(){
//On card button clicks, remove other classes and add new ones.
//Unless it is changeling, special case, just toggle.
$(".card button").click(function(){
if ($(this).attr("value") == "changeling"){
$(".card").toggleClass($(this).attr("value"));
} else {
$(this).parent().children("button").each(function(){
if ($(this).attr("value") != "changeling"){
$(".card").removeClass($(this).attr("value"));
}
})
$(".card").addClass($(this).attr("value"));
}
})
//On Window resize we use css transformation to scale the card to fix
//Yes it seems horrible but the alternative was somehting even more horrible!
$(window).resize(function(){
var f = ($(".cardwrapper").width())/788;
$(".card").css("transform","scale("+f+")")
$(".cardwrapper").height(1088*f);
});
//Constant infomation for special escape code handling.
var SPECIAL_REGEX = /\\(malefemale|unicorn|pegasus|earth|alicorn|goal|time|female|male|ship|replace|swap|draw|newgoal|search|copy|changeling)/g
var SPECIAL_REPLACE = {
"\\male":"\u2642",
"\\female":"\u2640",
"\\malefemale":"\u26A4",
"\\ship":"\u2764",
"\\earth":"\uE000",
"\\unicorn":"\uE001",
"\\pegasus":"\uE002",
"\\alicorn":"\uE003",
"\\time":"\uE004",
"\\replace":"(Replace): While in your hand, you may discard a Pony card from the grid and play this card in its place. This power cannot be copied.",
"\\swap":"(Swap): You may swap 2 Pony cards on the shipping grid.",
"\\draw":"(Draw): You may draw a card from the Ship or Pony deck.",
"\\newgoal":"(New Goal): You may discard a Goal and draw a new one to replace it.",
"\\search":"(Search): You may search the Ship or Pony discard pile for a card of your choice and play it.",
"\\copy":"(Copy): You may copy the power of any Pony card currently on the shipping grid, except for Changelings.",
"\\changeling":"Gains the name, keywords and symbols of any single [race] of your choice until the end of the turn. If this card is moved to a new place on the grid, the current player must select a new disguise that will last until the end of their turn, even if other cards say its power would not activate."
}
//Replace special escape codes when an input is updated
$(".card input[type=text], .card textarea").on("change",function(){
var txt = $(this).val();
txt = txt.replace(SPECIAL_REGEX,function(t){
return SPECIAL_REPLACE[t];
});
$(this).val(txt)
})
//Replace and create tooltip hints
$.each(SPECIAL_REPLACE,function(key,replace){
console.log([key,replace,"dt[data-original-title='\\"+key+"']",$("dt[data-original-title='\\"+key+"']")]);
$("dt[data-original-title='\\"+key+"']").attr("data-original-title",replace).tooltip();
})
//When a text editor is updated resize it's helper to clone back the height.
//This is because CSS Really hates working vertically
$(".card textarea").on("change keyup paste",function(){
var t = $(this),
o = $(".cardHelper ." + t.attr("class"));
o.text(t.val());
t.height(o.height());
});
//We also use a simular system for the name, but since we dont need manual
//line breaks it gets easiers
$(".card .nameInput").on("change keyup paste",function(){
var t = $(this),
o = $(".card .name");
o.toggleClass("small",t[0].scrollWidth > t.width()+1)
o.text(t.val());
});
//Update image
$("#image").change(function(){
$(".card .image").css("background-image","url('"+$(this).val()+"')")
})
//Save, New & Export buttons
$("#save").click(save)
$("#new").click(newCard)
$("#export").click(exportCard)
//$("#exportTo").click(function(){exportCard(1)})
//Log number of ajax events for the spinner
var AJAX_EVENTS = 0
$( document ).ajaxSend(function(){
AJAX_EVENTS++;
$("#working").show();
$("#error").hide();
}).ajaxComplete(function() {
if ( --AJAX_EVENTS == 0 ) {
$("#working").hide();
}
});
//Inital call setup functions
$(window).resize();
$(".card textarea").change();
//Check the hash to see if we are loading something
if(document.location.hash){
hs = document.location.hash.substr(1).split(":")
if (hs.length==2 && (hs[0] == "view" || hs[0] == "edit")){
load(hs[0],hs[1]);
} else {
document.location.hash = ""
}
}
};