Files
ChessCubing/www/js/backend.js

284 lines
7.2 KiB
JavaScript

let White = {
moveLeft : 0,
Time : 0,
TimeStartMove : 0
}
let Black = {
moveLeft : 0,
Time : 0,
TimeStartMove : 0
}
let has_start = false
Block = {
Type : false,
Time : 0,
last_time : 0
}
Timer = {
White_Has_started : false,
Black_Has_started : false,
Black_time : 0,
White_time : 0,
White_start_time : 0,
Black_start_time : 0,
coldown_White : 0,
coldown_Black : 0,
coldown_Start_White : 0,
coldown_Start_Black : 0,
White_Has_Finish : false,
Black_Has_Finish : false
}
config = {
StartTimeWhite : 600000,
StartTimeBlack:600000,
StartMoveLeft : 8,
BlockTime : 180000,
last_block : true,
trais : false,
MaxIncrement : 120000,
first_time : true
}
function load_endgame_scene(){
hide_scene("Menu")
hide_scene("Clock")
hide_scene("Cube")
show_scene("EndGame")
}
function load_Clock_scene(){
has_start = false
hide_scene("Menu")
hide_scene("EndGame")
hide_scene("Cube")
inizalize_clock()
show_scene("Clock")
}
function load_menu_scene(){
hide_scene("EndGame")
hide_scene("Clock")
hide_scene("Cube")
if (typeof sync_menu_with_config === "function"){
sync_menu_with_config()
}
show_scene("Menu")
}
function inizalize_clock(){
trais = !config.trais
toggle_trais()
White.Time = config.StartTimeWhite
White.moveLeft = config.StartMoveLeft
Black.Time = config.StartTimeBlack
Black.moveLeft = config.StartMoveLeft
Block.Time = config.BlockTime
Block.Type = !config.last_block
if (config.first_time) {
config.first_time = false
show_start_clock_button()
}else{
start()
}
change_move_left_black(Black.moveLeft)
change_move_left_white(White.moveLeft)
change_time_black(Black.Time)
change_time_white(White.Time)
change_time_block(Block.Time)
set_block_type(Block.Type)
}
function start(){
White.TimeStartMove = Date.now()
Black.TimeStartMove = Date.now()
Block.last_time = Date.now()
has_start = true
}
function load_cube_scene(){
has_start = false
config.StartTimeWhite = White.Time
config.StartTimeBlack = Black.Time
config.last_block = Block.Type
Timer = {
White_Has_started : false,
Black_Has_started : false,
Black_time : 0,
White_time : 0,
White_start_time : 0,
Black_start_time : 0,
coldown_White : 0,
coldown_Black : 0,
coldown_Start_White : 0,
coldown_Start_Black : 0,
White_Has_Finish : false,
Black_Has_Finish : false
}
Set_block_type_Timer(config.last_block)
Set_White_Time_Cube(0)
Set_Black_Time_Cube(0)
hide_scene("Clock")
hideGlow()
show_scene("Cube")
}
function black_timer_touch(){
if (!Timer.Black_Has_started){
Timer.coldown_Start_Black = Date.now()
black_Timer.classList.add("RedClick")
}else if(!Timer.Black_Has_Finish){
Timer.Black_Has_Finish = true
black_Timer.classList.add("GreenClick")
}
}
function black_timer_release(){
if (Timer.coldown_Black > 2000) {
Timer.Black_Has_started = true
Timer.Black_start_time = Date.now()
black_Timer.classList.remove("GreenClick")
}else{
black_Timer.classList.remove("RedClick")
black_Timer.classList.remove("GreenClick")
Timer.coldown_Start_Black = 0;
}
}
function on_start_clock_button_press(){
start()
hide_start_clock_button()
}
function white_timer_touch(){
if (!Timer.White_Has_started){
Timer.coldown_Start_White = Date.now()
white_Timer.classList.add("RedClick")
}else if(!Timer.White_Has_Finish){
Timer.White_Has_Finish = true
white_Timer.classList.add("GreenClick")
}
}
function white_timer_release(){
if (Timer.coldown_White > 2000) {
Timer.White_Has_started = true
Timer.White_start_time = Date.now()
white_Timer.classList.remove("GreenClick")
}else{
white_Timer.classList.remove("RedClick")
white_Timer.classList.remove("GreenClick")
Timer.coldown_Start_White = 0;
}
}
function white_touch(){
if (trais) return null
if (!has_start) return null
toggle_trais()
Black.TimeStartMove = Date.now()
White.moveLeft -= 1
change_move_left_white(White.moveLeft)
}
function black_touch(){
if (!trais) return null
if (!has_start) return null
Black.moveLeft -= 1
if (Black.moveLeft == 0){
load_cube_scene()
}else{
toggle_trais()
White.TimeStartMove = Date.now()
change_move_left_black(Black.moveLeft)
}
}
function update() {
if (last_scene == "Clock" && has_start){
if (!trais) {// trais au blanc
White.Time -= (Date.now() - White.TimeStartMove)
White.TimeStartMove = Date.now()
if (White.Time < 0){
change_winner(false)
load_endgame_scene()
}
}else{
Black.Time -= (Date.now() - Black.TimeStartMove)
Black.TimeStartMove = Date.now()
if(Black.Time < 0){
change_winner(true)
load_endgame_scene()
}
}
Block.Time -= (Date.now() - Block.last_time)
Block.last_time = Date.now()
if (Block.Time <= 0){
load_cube_scene()
}
change_time_white(White.Time)
change_time_black(Black.Time)
change_time_block(Block.Time)
}else if(last_scene == "Cube"){
if (!Timer.Black_Has_started){ // noire a pas commencer le cube
if (Timer.coldown_Start_Black != 0){ // coldown lancer
Timer.coldown_Black = Date.now() - Timer.coldown_Start_Black
if (Timer.coldown_Black > 2000){
black_Timer.classList.remove("RedClick")
black_Timer.classList.add("GreenClick")
}
}
}else if(!Timer.Black_Has_Finish){
Timer.Black_time = Date.now() - Timer.Black_start_time
Set_Black_Time_Cube(Timer.Black_time)
}
if (!Timer.White_Has_started){ // blanc a pas commencer le cube
if (Timer.coldown_Start_White != 0){ // coldown lancer
Timer.coldown_White = Date.now() - Timer.coldown_Start_White
if (Timer.coldown_White > 2000){
white_Timer.classList.remove("RedClick")
white_Timer.classList.add("GreenClick")
}
}
}else if (!Timer.White_Has_Finish){
Timer.White_time = Date.now() - Timer.White_start_time
Set_White_Time_Cube(Timer.White_time)
}
if (Timer.Black_Has_Finish && Timer.White_Has_Finish){
if (config.last_block){
if (Timer.Black_time > config.MaxIncrement) Timer.Black_time = config.MaxIncrement
config.StartTimeWhite += Timer.Black_time
config.StartTimeBlack += Timer.Black_time
}else{
if (Timer.White_time > config.MaxIncrement) Timer.White_time = config.MaxIncrement
config.StartTimeWhite -= Timer.White_time
config.StartTimeBlack -= Timer.White_time
}
load_Clock_scene()
}
}
requestAnimationFrame(update);
}