DPTW/Public/ecma/DPTW.Scores.ecma.js

342 lines
10 KiB
JavaScript

DPTW.Scores = function(dptw, inputs){
const self = this;
let started = false,
nick_maximum_length, zeros_fill_score,
pages,
scores_show_save_ok_message,
scores_show_save_error_message,
scores_show_load_ok_message,
scores_show_load_error_message,
game_mode_name;
const SETTER = 10;
const construct = () => {};
this.start = callback => {
const end = status => typeof callback == "function" && callback(status);
if(started){
end(false);
return false;
};
started = true;
nick_maximum_length = dptw.settings("nick_maximum_length");
zeros_fill_score = dptw.settings("zeros_fill_score");
scores_show_save_ok_message = dptw.settings("scores_show_save_ok_message");
scores_show_save_error_message = dptw.settings("scores_show_save_error_message");
scores_show_load_ok_message = dptw.settings("scores_show_load_ok_message");
scores_show_load_error_message = dptw.settings("scores_show_load_error_message");
game_mode_name = dptw.settings("game_mode_name");
end(true);
return true;
};
const button = (key, x, y, width, height) => ({
type : "rectangle",
x : x,
y : y,
width : width,
height : height,
background : "rgba(255, 255, 255, .3)",
border_color : "#000",
border_width : .1,
childs : [{
type : "text",
x : width / 2,
y : height / 2,
baseline : "middle",
align : "center",
text : dptw.i18n.get(key),
size : height,
background : "#FFF",
shadow : [[0, 0, .2, "#000"]]
}],
on_click : "{object_name}.scores." + key
});
const label_text = (x, y, align, text) => ({
type : "text",
x : x,
y : y,
baseline : "middle",
align : align,
size : 1.5,
style : "bold",
background : "#FFF",
shadow : [[0, 0, .2, "#000"]],
text : text
});
this.build = () => {
const half = dptw.kanvas.cells / 2;
dptw.kanvas.map[SETTER] = {type : "block", x : -half, y : -half, childs : [
{type : "rectangle", x : 0, y : 0, width : dptw.kanvas.cells, height : dptw.kanvas.cells, background : "#000", alpha : .8},
button("previous", 2, dptw.kanvas.cells - 4, 11, 2.5),
button("next", dptw.kanvas.cells - 13, dptw.kanvas.cells - 4, 11, 2.5),
button("back", 14, dptw.kanvas.cells - 4, 12, 2.5),
label_text(2, dptw.kanvas.cells - 5.5, "left", dptw.i18n.get("page")),
label_text(dptw.kanvas.cells - 2, dptw.kanvas.cells - 5.5, "right", 1),
{type : "block", x : 2, y : 2, childs : []}
]};
load_scores();
};
this.build_add = () => {
const half = dptw.kanvas.cells / 2,
key_size = dptw.kanvas.cells / 12,
margin = dptw.kanvas.cells - (key_size * 10),
items = [],
key_margin = key_size * .05,
key_width = key_size - (2 * key_margin),
character_position = key_width / 2,
half_key = key_size / 2;
let keys_x = half_key,
keys_y = key_size;
items.push({
type : "rectangle",
x : key_size - .1,
y : key_size * 6,
width : key_size * 10,
height : key_size,
background : "rgba(255, 255, 255, .3)",
border_color : "#000",
border_width : .1,
childs : [{
type : "text",
x : key_size * 5,
y : half_key,
baseline : "middle",
align : "center",
text : "",
size : key_width,
background : "#FFF",
shadow : [[0, 0, .2, "#000"]]
}]
});
[
"1234567890<",
"qwertyuiop",
"asdfghjkl",
"zxcvbnm"
].forEach((set, i) => set.split("").forEach((character, j) => items.push({
type : "rectangle",
x : (i * half_key) + keys_x + ((j * key_size) + key_margin),
y : keys_y + ((i * key_size) + key_margin),
width : key_width,
height : key_width,
background : "rgba(255, 255, 255, .3)",
border_color : "#000",
border_width : .1,
childs : [{
type : "text",
x : character_position,
y : character_position,
baseline : "middle",
align : "center",
text : (character = character.toUpperCase()),
size : key_width,
background : "#FFF",
shadow : [[0, 0, .2, "#000"]]
}],
on_click : "(() => {object_name}.scores.set_nick('" + character + "'))"
})));
["not_save", "save"].forEach((key, i) => items.push({
type : "rectangle",
x : (key_size * 5 * i) + half_key + (key_size * i),
y : key_size * 8,
width : key_size * 5,
height : key_size,
background : "rgba(255, 255, 255, .3)",
border_color : "#000",
border_width : .1,
childs : [{
type : "text",
x : key_size * 2.5,
y : half_key,
baseline : "middle",
align : "center",
text : dptw.i18n.get(key),
size : key_width,
background : "#FFF",
shadow : [[0, 0, .2, "#000"]]
}],
on_click : "{object_name}.scores." + key
}));
dptw.kanvas.map[SETTER] = {type : "block", x : -half, y : -half, childs : items};
};
this.set_nick = character => {
if(character == "<")
with(dptw.kanvas.map[SETTER].childs[0].childs[0])text = text.substr(0, text.length - 1);
else
with(dptw.kanvas.map[SETTER].childs[0].childs[0])text.length < nick_maximum_length && (text += character);
};
this.save = () => {
const nick = dptw.kanvas.map[SETTER].childs[0].childs[0].text;
if(!nick){
alert("need_nick");
return;
};
const variables = {
mode : game_mode_name,
nick : nick,
score : Number(dptw.kanvas.map[dptw.SCORE].text)
}
dptw.load_file("/api/scores/set?v=" + btoa(JSON.stringify(variables)), response => {
response = JSON.parse(response);
if(dptw.errors.validate(
response.content.error,
[].concat(
dptw.sql_session_errors,
[
"mode_null",
"mode_empty",
"mode_not_exists",
"nick_null",
"nick_empty",
"score_null",
"score_lower_0"
]
),
variables,
scores_show_save_error_message && "save_scores_error",
scores_show_save_ok_message && "save_scores_ok"
)){
dptw.kanvas.map[SETTER] = null;
dptw.show_menu();
};
});
};
this.not_save = () => {
dptw.kanvas.map[SETTER] = null;
dptw.show_menu();
};
this.back = () => {
dptw.kanvas.map[SETTER] = null;
dptw.show_menu();
};
this.next = () => {
const page_block = dptw.kanvas.map[SETTER].childs[5],
page = Number(page_block.text);
if(page < pages){
page_block.text = page + 1;
load_scores();
};
};
this.previous = () => {
const page_block = dptw.kanvas.map[SETTER].childs[5];
if(page_block.text != 1){
page_block.text = Number(page_block.text) - 1;
load_scores();
};
};
const score_text = (x, y, align, text) => ({
type : "text",
x : x,
y : y,
baseline : "middle",
align : align,
size : 2.5,
style : "bold",
background : "#FFF",
shadow : [[0, 0, .2, "#000"]],
text : text
});
const load_scores = () => {
const variables = {
mode : "testing_game",
page : Number(dptw.kanvas.map[SETTER].childs[5].text),
items_per_page : 15,
nicks : ""
};
dptw.load_file("/api/scores/list?v=" + btoa(JSON.stringify(variables)), response => {
response = JSON.parse(response);
dptw.kanvas.map[SETTER].childs[6].childs = [];
pages = response.content.pages;
dptw.errors.validate(
response.content.error,
[].concat(
dptw.sql_session_errors,
[
"mode_null",
"mode_empty",
"mode_not_exists",
"page_null",
"page_lower_1",
"items_per_page_null",
"items_per_page_lower_1",
"nicks_null"
]
),
variables,
scores_show_load_error_message && "load_scores_error",
scores_show_load_ok_message && "load_scores_ok"
) &&
response.content.scores.forEach((score, i) => {
const y = 1 + (i * 2);
dptw.kanvas.map[SETTER].childs[6].childs.push(
score_text(0, y, "left", (zeros_fill_score + score.position).slice(-zeros_fill_score.length)),
score_text(10, y, "left", score.nick),
score_text(20, y, "left", score.date_in.replace(/^[0-9]{2}([0-9]{2})\-([0-9]{2})\-([0-9]{2}) .+$/, "$1$2$3")),
score_text(dptw.kanvas.cells - 4, y, "right", (zeros_fill_score + score.score).slice(-zeros_fill_score.length))
);
});
});
};
construct();
};