#!/usr/bin/tclsh
# OpenVerse Server Program
# Modified by Andy Goth <unununium@openverse.org>
# Now it runs a GothChess room!
# 
#
# Module Name		- GothChess chess timer procs
# Current Maintainter 	- Andy Goth <unununium@openverse.org>
# Sourced By		- gothchess.tcl
#
# Copyright (C) 1999 David Gale <cruise@openverse.org>
# For more information visit http://OpenVerse.org/
#
# 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 Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
# USA.

global chess MVS

# show_chess_timer who which
#
# Send $who $which's timer
proc show_chess_timer {who which} {
    global chess

    # Maybe show player 1's timer
    if {($which == 1) || ([string compare $which "all"] == 0)} {
	if {$chess(initial_time) != 0} {
	    if {$chess(players.1.time) >= 0} {
		# Figure out which color to use
		if {($chess(chess_timer_running) == 0) || ($chess(turn) != 1)} {
		    set color "cyan"
		} elseif {$chess(players.1.time) >= 60} {
		    set color "white"
		} else {
		    set color "yellow"
		}
		
		# Formulate the time remaining
		set msg "TEXT player_1_timer 560 190 $color 2 0 |[expr $chess(players.1.time) / 3600]:"
		if {($chess(players.1.time) / 60) % 60 < 10} {
		    append msg "0"
		}
		append msg "[expr ($chess(players.1.time) / 60) % 60]:"
		if {$chess(players.1.time) % 60 < 10} {
		    append msg "0"
		}
		append msg [expr $chess(players.1.time) % 60]

		# Send to one or all players?
		if {[string compare $who "all"] == 0} {
		    send_to_all_users $msg
		} else {
		    send_to_user $who $msg
		}
	    
		unset color msg
	    } else {
		if {[string compare $who "all"] == 0} {
		    send_to_all_users "TEXT player_1_timer 560 190 red 2 0 |Time Expired"
		} else {
		    send_to_user $who "TEXT player_1_timer 560 190 red 2 0 |Time Expired"
		}
	    }
	} else {
	    if {[string compare $who "all"] == 0} {
		send_to_all_users "TEXT player_1_timer 560 190 cyan 2 0 |Unlimited Time"
	    } else {
		send_to_user $who "TEXT player_1_timer 560 190 cyan 2 0 |Unlimited Time"
	    }
	}
    }

    # Maybe show player 2's timer
    if {($which == 2) || ([string compare $which "all"] == 0)} {
	if {$chess(initial_time) != 0} {
	    if {$chess(players.2.time) >= 0} {
		# Figure out which color to use
		if {($chess(chess_timer_running) == 0) || ($chess(turn) != 1)} {
		    set color "cyan"
		} elseif {$chess(players.2.time) >= 60} {
		    set color "white"
		} else {
		    set color "yellow"
		}
	    
		# Formulate the time remaining
		set msg "TEXT player_2_timer 560 290 $color 2 0 |[expr $chess(players.2.time) / 3600]:"
		if {($chess(players.2.time) / 60) % 60 < 10} {
		    append msg "0"
		}
		append msg "[expr ($chess(players.2.time) / 60) % 60]:"
		if {$chess(players.2.time) % 60 < 10} {
		    append msg "0"
		}
		append msg [expr $chess(players.2.time) % 60]

		# Send to one or all players?
		if {[string compare $who "all"] == 0} {
		    send_to_all_users $msg
		} else {
		    send_to_user $who $msg
		}

		unset color msg
	    } else {
		if {[string compare $who "all"] == 0} {
		    send_to_all_users "TEXT player_2_timer 560 290 red 2 0 |Time Expired"
		} else {
		    send_to_user $who "TEXT player_2_timer 560 290 red 2 0 |Time Expired"
		}
	    }
	} else {
	    if {[string compare $who "all"] == 0} {
		send_to_all_users "TEXT player_2_timer 560 290 cyan 2 0 |Unlimited Time"
	    } else {
		send_to_user $who "TEXT player_2_timer 560 290 cyan 2 0 |Unlimited Time"
	    }
	}
    }

    unset who which
}

# reset_chess_timers
#
# Sets both players' timers to initial values (does not stop them)
proc reset_chess_timers {} {
    global chess

    set chess(players.1.time) $chess(initial_time)
    set chess(players.2.time) $chess(initial_time)
}

# start_chess_timer
#
# Starts the chess timer
proc start_chess_timer {} {
    global chess

    if {$chess(chess_timer_running) == 0} {
	# Start the timer
	set chess(chess_timer_running) 1
	chess_timer

	if {$chess(turn) == 0} {
	    log_it "(start_chess_timer): Chess timer started but it is no one's turn (shouldn't happen)"
	} else {
	    log_it "(start_chess_timer): Chess timer started (turn: $chess(turn); time: $chess(players.$chess(turn).time))"
	}
    } else {
	# It's already going!
	log_it "(start_chess_timer): Chess timer started while it is already running (shouldn't happen)"
    }
}

# stop_chess_timer
#
# Stops the chess timer (does not reset it)
proc stop_chess_timer {} {
    global chess

    if {$chess(chess_timer_running) == 1} {
	# Cancel the timer
	set chess(chess_timer_running) 0
	after cancel $chess(chess_timer_id)

	if {$chess(turn) == 0} {
	    log_it "(stop_chess_timer): Chess timer stopped but it is no one's turn (shouldn't happen)"
	} else {
	    log_it "(stop_chess_timer): Chess timer stopped (turn: $chess(turn); time: $chess(players.$chess(turn).time))"
	}
    } else {
	# It's not running!
	log_it "(stop_chess_timer): Chess timer stopped even though it is not running (shouldn't happen)"
    }
}

# chess_timer
#
# Calls itself every second if game is running
# Handles the chess timers
proc chess_timer {} {
    global MVS chess

    # Sanity check... see if a game is going
    if {$chess(turn) == 0} {
	log_it "(chess_timer): Chess timer running while game is stopped (shouldn't happen)"
	set chess_timer_running 0
	return
    }

    # Decrement the current player's time
    incr chess(players.$chess(turn).time) -1

    # Show the chess timer
    show_chess_timer "all" $chess(turn)

    if {$chess(players.$chess(turn).time) < 0} {
	# This player loses
	set chess(chess_timer_running) 0
	show_message "all" "orange" "Player [expr 3 - $chess(turn)] wins (time)"
	game_end [expr 3 - $chess(turn)]
	return
    }

    # Arrange to have this proc called later
    set chess(chess_timer_id) [after 1000 "chess_timer"]
}

