2014-01-07 02:19:07 +00:00
|
|
|
/* -*- Mode: vala; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
*
|
2013-07-07 21:14:29 +00:00
|
|
|
* Copyright (C) 2010-2013 Robert Ancell
|
2016-03-15 17:12:58 +00:00
|
|
|
* Copyright (C) 2013-2014 Michael Catanzaro
|
|
|
|
* Copyright (C) 2015-2016 Sahil Sareen
|
2013-07-07 21:14:29 +00:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify it under
|
|
|
|
* the terms of the GNU General Public License as published by the Free Software
|
2016-02-15 20:53:54 +00:00
|
|
|
* Foundation, either version 3 of the License, or (at your option) any later
|
2013-07-07 21:14:29 +00:00
|
|
|
* version. See http://www.gnu.org/copyleft/gpl.html the full text of the
|
|
|
|
* license.
|
|
|
|
*/
|
|
|
|
|
2015-02-16 04:07:37 +00:00
|
|
|
public enum ClockType
|
|
|
|
{
|
|
|
|
SIMPLE,
|
|
|
|
FISCHER,
|
2015-03-14 15:34:16 +00:00
|
|
|
BRONSTEIN,
|
|
|
|
INVALID;
|
2015-02-16 04:07:37 +00:00
|
|
|
|
|
|
|
public string to_string ()
|
|
|
|
{
|
|
|
|
switch (this)
|
|
|
|
{
|
|
|
|
case SIMPLE:
|
|
|
|
return "simple";
|
|
|
|
case FISCHER:
|
|
|
|
return "fischer";
|
|
|
|
case BRONSTEIN:
|
|
|
|
return "bronstein";
|
|
|
|
default:
|
|
|
|
assert_not_reached ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static ClockType string_to_enum (string s)
|
|
|
|
{
|
|
|
|
switch (s)
|
|
|
|
{
|
|
|
|
case "simple":
|
|
|
|
return SIMPLE;
|
|
|
|
case "fischer":
|
|
|
|
return FISCHER;
|
|
|
|
case "bronstein":
|
|
|
|
return BRONSTEIN;
|
|
|
|
default:
|
2015-03-14 15:34:16 +00:00
|
|
|
return INVALID;
|
2015-02-16 04:07:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-06 22:10:29 +00:00
|
|
|
public class ChessClock : Object
|
|
|
|
{
|
2018-07-27 22:51:59 +00:00
|
|
|
private int white_initial_seconds;
|
|
|
|
private int black_initial_seconds;
|
2013-08-01 16:12:49 +00:00
|
|
|
|
2018-07-27 22:51:59 +00:00
|
|
|
private int white_seconds_used = 0;
|
|
|
|
private int black_seconds_used = 0;
|
2011-01-07 04:20:27 +00:00
|
|
|
|
2018-07-27 22:51:59 +00:00
|
|
|
private int white_prev_move_seconds = 0;
|
|
|
|
private int black_prev_move_seconds = 0;
|
2011-01-07 04:20:27 +00:00
|
|
|
|
2018-07-27 22:51:59 +00:00
|
|
|
private int white_extra_seconds = 0;
|
|
|
|
private int black_extra_seconds = 0;
|
2015-02-16 04:07:37 +00:00
|
|
|
|
|
|
|
public int extra_seconds { get; set; default = 0; }
|
|
|
|
|
2018-07-27 22:51:59 +00:00
|
|
|
public int white_remaining_seconds
|
2015-02-16 04:07:37 +00:00
|
|
|
{
|
2018-07-27 22:51:59 +00:00
|
|
|
get { return white_initial_seconds + white_extra_seconds - white_seconds_used; }
|
2015-02-16 04:07:37 +00:00
|
|
|
}
|
|
|
|
|
2018-07-27 22:51:59 +00:00
|
|
|
public int black_remaining_seconds
|
2015-02-16 04:07:37 +00:00
|
|
|
{
|
2018-07-27 22:51:59 +00:00
|
|
|
get { return black_initial_seconds + black_extra_seconds - black_seconds_used; }
|
2015-02-16 04:07:37 +00:00
|
|
|
}
|
|
|
|
|
2018-07-27 22:51:59 +00:00
|
|
|
public ClockType clock_type { get; set; default = ClockType.SIMPLE; }
|
|
|
|
|
|
|
|
private Color _active_color = Color.WHITE;
|
2011-01-07 04:20:27 +00:00
|
|
|
public Color active_color
|
|
|
|
{
|
|
|
|
get { return _active_color; }
|
|
|
|
set
|
|
|
|
{
|
|
|
|
if (value == active_color)
|
|
|
|
return;
|
|
|
|
|
|
|
|
stop ();
|
|
|
|
_active_color = value;
|
2015-02-16 04:07:37 +00:00
|
|
|
|
|
|
|
// This is a move switch
|
|
|
|
// Update the clocks for Fischer and Bronstein mode
|
|
|
|
update_extra_seconds ();
|
|
|
|
update_prev_move_time ();
|
|
|
|
|
2011-01-07 04:20:27 +00:00
|
|
|
start ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-21 23:55:28 +00:00
|
|
|
private Timer? timer;
|
2013-08-01 16:12:49 +00:00
|
|
|
private uint tick_timeout_id = 0;
|
2011-01-06 22:10:29 +00:00
|
|
|
|
|
|
|
public signal void tick ();
|
|
|
|
public signal void expired ();
|
|
|
|
|
2014-09-15 23:13:02 +00:00
|
|
|
private bool is_active = false;
|
|
|
|
|
2013-11-02 21:22:57 +00:00
|
|
|
public ChessClock (int white_initial_seconds, int black_initial_seconds)
|
2011-01-06 22:10:29 +00:00
|
|
|
{
|
2013-11-02 21:22:57 +00:00
|
|
|
this.white_initial_seconds = white_initial_seconds;
|
|
|
|
this.black_initial_seconds = black_initial_seconds;
|
2011-01-06 22:10:29 +00:00
|
|
|
}
|
2013-08-01 16:12:49 +00:00
|
|
|
|
2011-01-06 22:10:29 +00:00
|
|
|
public void start ()
|
|
|
|
{
|
2013-08-03 14:20:09 +00:00
|
|
|
if (is_active)
|
2011-01-06 22:10:29 +00:00
|
|
|
return;
|
|
|
|
|
2014-09-15 23:13:02 +00:00
|
|
|
is_active = true;
|
|
|
|
|
2013-05-21 23:55:28 +00:00
|
|
|
if (timer == null)
|
|
|
|
{
|
|
|
|
/* Starts automatically */
|
|
|
|
timer = new Timer ();
|
|
|
|
}
|
|
|
|
else
|
2013-08-01 16:12:49 +00:00
|
|
|
{
|
2013-05-21 23:55:28 +00:00
|
|
|
timer.start ();
|
2013-08-01 16:12:49 +00:00
|
|
|
}
|
2011-01-07 04:20:27 +00:00
|
|
|
|
2013-08-01 16:12:49 +00:00
|
|
|
watch_timer ();
|
2011-01-06 22:10:29 +00:00
|
|
|
}
|
|
|
|
|
2011-01-07 04:20:27 +00:00
|
|
|
private bool tick_cb ()
|
2011-01-06 22:10:29 +00:00
|
|
|
{
|
|
|
|
if (active_color == Color.WHITE)
|
2013-11-02 18:44:56 +00:00
|
|
|
white_seconds_used++;
|
2011-01-06 22:10:29 +00:00
|
|
|
else
|
2013-11-02 18:44:56 +00:00
|
|
|
black_seconds_used++;
|
2011-01-07 04:20:27 +00:00
|
|
|
|
2013-11-02 18:44:56 +00:00
|
|
|
tick ();
|
|
|
|
|
2017-03-12 15:51:32 +00:00
|
|
|
if (white_seconds_used >= white_initial_seconds + white_extra_seconds ||
|
|
|
|
black_seconds_used >= black_initial_seconds + black_extra_seconds)
|
2014-09-15 23:13:02 +00:00
|
|
|
{
|
|
|
|
stop ();
|
|
|
|
expired ();
|
|
|
|
}
|
|
|
|
|
2014-12-31 03:37:03 +00:00
|
|
|
return Source.CONTINUE;
|
2011-01-06 22:10:29 +00:00
|
|
|
}
|
|
|
|
|
2011-01-07 04:20:27 +00:00
|
|
|
public void stop ()
|
2011-01-06 22:10:29 +00:00
|
|
|
{
|
2013-08-03 14:20:09 +00:00
|
|
|
if (!is_active)
|
2011-01-06 22:10:29 +00:00
|
|
|
return;
|
|
|
|
|
2011-01-07 04:20:27 +00:00
|
|
|
timer.stop ();
|
2013-11-02 18:44:56 +00:00
|
|
|
stop_watching_timer ();
|
2014-09-15 23:13:02 +00:00
|
|
|
is_active = false;
|
2011-01-06 22:10:29 +00:00
|
|
|
}
|
2013-07-15 11:33:49 +00:00
|
|
|
|
2013-08-01 16:12:49 +00:00
|
|
|
public void pause ()
|
2013-07-15 11:33:49 +00:00
|
|
|
{
|
2013-08-03 14:20:09 +00:00
|
|
|
if (!is_active)
|
2013-07-15 11:33:49 +00:00
|
|
|
return;
|
|
|
|
|
2013-08-03 13:20:09 +00:00
|
|
|
timer.stop ();
|
2013-11-02 18:44:56 +00:00
|
|
|
stop_watching_timer ();
|
2014-12-17 01:07:00 +00:00
|
|
|
is_active = false;
|
2013-08-01 16:12:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void unpause ()
|
|
|
|
{
|
2013-08-03 21:54:52 +00:00
|
|
|
if (timer == null || is_active)
|
2013-08-01 16:12:49 +00:00
|
|
|
return;
|
|
|
|
|
2013-08-03 13:20:09 +00:00
|
|
|
timer.@continue ();
|
|
|
|
watch_timer ();
|
2014-12-17 01:07:00 +00:00
|
|
|
is_active = true;
|
2013-08-01 16:12:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void watch_timer ()
|
|
|
|
{
|
|
|
|
/* Wake up each second */
|
2015-02-11 09:01:47 +00:00
|
|
|
tick_timeout_id = Timeout.add (1000, tick_cb);
|
2013-08-01 16:12:49 +00:00
|
|
|
}
|
|
|
|
|
2013-11-02 18:44:56 +00:00
|
|
|
private void stop_watching_timer ()
|
2013-08-01 16:12:49 +00:00
|
|
|
{
|
|
|
|
Source.remove (tick_timeout_id);
|
|
|
|
tick_timeout_id = 0;
|
|
|
|
}
|
2018-07-27 22:51:59 +00:00
|
|
|
|
|
|
|
public void update_prev_move_time ()
|
|
|
|
{
|
|
|
|
if (active_color == Color.WHITE)
|
|
|
|
black_prev_move_seconds = black_seconds_used;
|
|
|
|
else
|
|
|
|
white_prev_move_seconds = white_seconds_used;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void update_extra_seconds ()
|
|
|
|
{
|
|
|
|
int white_move_used = 0, black_move_used = 0;
|
|
|
|
switch (clock_type)
|
|
|
|
{
|
|
|
|
case ClockType.FISCHER:
|
|
|
|
if (active_color == Color.WHITE)
|
|
|
|
white_extra_seconds += extra_seconds;
|
|
|
|
else
|
|
|
|
black_extra_seconds += extra_seconds;
|
|
|
|
break;
|
|
|
|
case ClockType.BRONSTEIN:
|
|
|
|
white_move_used = white_seconds_used - white_prev_move_seconds;
|
|
|
|
black_move_used = black_seconds_used - black_prev_move_seconds;
|
|
|
|
if (active_color != Color.WHITE)
|
|
|
|
white_extra_seconds += int.min (extra_seconds, white_move_used);
|
|
|
|
else
|
|
|
|
black_extra_seconds += int.min (extra_seconds, black_move_used);
|
|
|
|
break;
|
2020-12-05 15:59:08 +00:00
|
|
|
default:
|
|
|
|
break;
|
2018-07-27 22:51:59 +00:00
|
|
|
}
|
|
|
|
}
|
2011-01-06 22:10:29 +00:00
|
|
|
}
|