unit unt_game_clock;
interface
uses Windows, Dialogs, SysUtils, ExtCtrls, unt_penalties, unt_penalty, myGlobals,
ShellApi;
type
TGameClock =
class
private
GameClock : TTimer;
//this is the timer
max_RunningPenalties : byte;
//this is a number of max. running penalties per team
public
Direction : byte;
//this is the direction of the running clock
IsPowerplay : Boolean;
//this is a flag for powerplay
PP_OnTeam : byte;
//this is a flag for the team, which is under powerplay
constructor Create;
Procedure StartClock;
Procedure StopClock;
Procedure GameClockTimer(Sender: TObject);
end;
implementation
uses Math;
constructor TGameClock.Create;
begin
inherited;
//create timer and init
GameClock := TTimer.Create(
nil);
max_RunningPenalties := 2;
isPowerplay := False;
end;
procedure TGameClock.GameClockTimer(Sender: TObject);
var
i, ListOffset : integer;
TimeHolder : TSystemTime;
h,m,s,msec : Word;
IncreaseOffset : Boolean;
int_team_a, int_team_b : byte;
begin
//init of variables
int_team_a := 0;
int_team_b := 0;
ListOffset := 0;
//walk over all penalties
for i := 0
to PenaltyList.Count -1
do
begin
if not(PenaltyList.Items[i-ListOffset] <>
nil)
then exit;
if PenaltyList.Items[i-ListOffset].Team = '
A'
then inc(int_team_a);
if PenaltyList.Items[i-ListOffset].Team = '
B'
then inc(int_team_b);
//here we check, if we have a powerplay-situation
if ((int_team_a <= max_RunningPenalties)
and (int_team_b <= max_RunningPenalties))
then
begin
//set isPowerplay true or false
if int_team_a <> int_team_b
then
IsPowerplay := True
else
IsPowerplay := False;
//set the team, which is on powerplay
if int_team_a > int_team_b
then
PP_OnTeam := 1
else
PP_OnTeam := 2;
end;
//here we check, if a team has more then the allowed max number of penalties
//if yes => we dont count the timing for this penalty
if ((PenaltyList.Items[i-ListOffset].Team = '
A')
and (int_team_a <= max_RunningPenalties))
or
((PenaltyList.Items[i-ListOffset].Team = '
B')
and (int_team_b <= max_RunningPenalties))
then
begin
IncreaseOffset := False;
DecodeTime(StrToTime(PenaltyList.Items[i-ListOffset].CTime),h,m,s,msec);
TimeHolder.wHour := h;
TimeHolder.wMinute := m;
TimeHolder.wSecond := s;
TimeHolder.wMilliseconds := msec;
case PenaltyList.Items[i-ListOffset].Direction
of
0 :
begin // clock should run asc
TimeHolder.wsecond:= TimeHolder.wSecond + 1;
if TimeHolder.wSecond = 60
then begin
TimeHolder.wSecond :=0;
TimeHolder.wMinute :=TimeHolder.wMinute + 1;
end;
end;
1 :
begin // clock should run desc
//delete that penalty, cause time is over
if (TimeHolder.wSecond = 0)
and (TimeHolder.wMinute = 0)
then
begin
//delete it later, after sending out the 0:00
IncreaseOffset := True;
end
else
begin
if TimeHolder.wSecond = 0
then begin
TimeHolder.wSecond := 59;
TimeHolder.wMinute := TimeHolder.wMinute - 1;
end
else
TimeHolder.wsecond := TimeHolder.wSecond - 1;
end;
end;
end;
//adwise the new CurrentTime
PenaltyList.Items[i-ListOffset].CTime := Format('
%.2d:%.2d:%.2d', [TimeHolder.wHour,TimeHolder.wMinute,TimeHolder.wSecond]);
//we have to delete an item
if IncreaseOffset
then
begin
PenaltyList.Delete(i-ListOffset);
inc(ListOffset);
end;
end;
end;
end;
Procedure TGameClock.StartClock;
begin
GameClock.OnTimer := GameClockTimer;
GameClock.Enabled := True;
end;
Procedure TGameClock.StopClock;
begin
GameClock.Enabled := False;
end;
end.