Entschuldigt wenn ich mich einmische, aber ich hätte da ein paar Ideen einzuwerfen.
@TE: in der TPenalty Klasse speichers du die Zeit als string
Zitat:
4. Ja. Das ist aktuell meinem internen Zeitverfahren geschuldet und der Frage, an welcher Stelle ich denn dann nun die Zeiten (aktuell Spielzeit und jede einzelne laufende Strafzeit) intern und später dann auch extern aktualisieren lassen soll. Aktuell läuft auf dem Hauptform einfach nur ein Timer, der die Sekunden und Minuten hoch- oder runterzählt.
, aber bei deinem Timer benutzt du original.wSecond / wMinute. Da hast du deine eigene Argumentation wiederlegt.
Aber jetzt zu meinem Vorschlag:
Delphi-Quellcode:
TPenalty = class(TObject)
public
Team : boolean; // Team (A oder B) (there will be no more than 2 teams a game)
Start, // start of penalty
Ende : LongInt; // end of penalty (oder auch Dauer, wenn man will)
constructor Create; overload;
constructor Create(fTeam : boolean; fStart, fEnde : LongInt); overload;
end;
TPenalties = class(TObjectList)
private
function GetItem(Index: integer): TPenalty;
procedure SetItem(Index: integer; Value: TPenalty);
public
property Items[Index: integer]: TPenalty read GetItem write SetItem; default;
end;
TGameClock = class
private
PenaltyList : TPenalties;
max_RunningPenalties : byte;
isPowerplay : boolean;
GameClock : TTimer;
Clock : LongInt;
procedure Tick(Sender: TObject);
public
constructor Create;
procedure StartGame;
procedure StartClock;
procedure StopClock;
// will hold the logic of no more than 2 penalties a team
procedure AddPenalty(fTeam : boolean; fDuration : LongInt);
end;
implementation
{$R *.dfm}
function TPenalties.GetItem(Index: Integer): TPenalty;
begin
Result := (inherited GetItem(Index)) as TPenalty;
end;
procedure TPenalties.SetItem(Index: Integer; Value: TPenalty);
begin
inherited SetItem(Index, Value);
end;
constructor TGameClock.Create;
begin
inherited;
//create timer and init
GameClock := TTimer.Create(nil);
max_RunningPenalties := 2;
isPowerplay := False;
end;
procedure TGameClock.StartClock;
begin
GameClock.OnTimer := Tick;
GameClock.Enabled := True;
end;
procedure TGameClock.StartGame;
begin
Clock := 0;
GameClock.Enabled := True;
end;
procedure TGameClock.StopClock;
begin
GameClock.Enabled := False;
end;
procedure TGameClock.AddPenalty(fTeam : boolean; fDuration : LongInt);
var
i : integer;
FirstRunning, Last, New : TPenalty;
begin
for i := 0 to PenaltyList.Count - 1 do
begin
if (PenaltyList.Items[i].Start < Clock) and
(PenaltyList.Items[i].Ende > Clock) and
(PenaltyList.Items[i].Team = fTeam) then
begin
if not Assigned(FirstRunning) then
begin
FirstRunning := PenaltyList.Items[i];
end
else
begin
Last := PenaltyList.Items[i];
end;
end;
end;
// FirstRunning is now the first running penalty and
// Last is the last given penalty besides the first running
// first set standard values
New := TPenalty.Create(fTeam, Clock, Clock + fDuration);
if Assigned(FirstRunning) then
begin
if Assigned(Last) then
begin
// if two or more add penalty time at end of last penalty
New.Start := Last.Ende;
New.Ende := Last.Ende + fDuration;
end;
end;
PenaltyList.Add(New);
end;
procedure TGameClock.Tick(Sender: TObject);
var
i, ListOffset : integer;
int_team_a, int_team_b : byte;
PP_OnTeam : byte;
begin
//init of variables
int_team_a := 0;
int_team_b := 0;
// next Tick of clock
Clock := Clock + GameClock.Interval; // add intervall to our clock
for i := 0 to PenaltyList.Count - 1 do
begin
if (PenaltyList.Items[i].Start <= Clock) and
(PenaltyList.Items[i].Ende > Clock) then
begin
if PenaltyList.Items[i].Team then
inc(int_team_a)
else
inc(int_team_b);
end;
end;
//here we check, if we have a powerplay-situation
if ((int_team_a <= max_RunningPenalties) and (int_team_b <= max_RunningPenalties)) then
begin
IsPowerplay := int_team_a <> int_team_b;
//set the team, which is on powerplay
if IsPowerplay then
begin
if int_team_a > int_team_b then
PP_OnTeam := 1
else
PP_OnTeam := 2;
end;
end;
//walk over all penalties
for i := 0 to PenaltyList.Count - 1 do
begin
// only running ones
if (PenaltyList.Items[i].Start <= Clock) and
(PenaltyList.Items[i].Ende > Clock) then
begin
// show penalties
// assign to whatever it should display (differentiation not done)
// start of penalty
TimeToStr(PenaltyList.Items[i].Start / 1000); // "/ 1000" because all times are stored in milliseconds
// start of penalty
TimeToStr(PenaltyList.Items[i].Ende / 1000);
// if the clock is running the backwards then
// TimeToStr(gamelength - PenaltyList.Items[i].Ende / 1000); // gamelength is the length of the game in milliseconds
end;
end;
// at the end show current time
TimeToStr(Clock / 1000);
end;
{ TPenalty }
constructor TPenalty.Create;
begin
inherited;
end;
constructor TPenalty.Create(fTeam: boolean; fStart, fEnde: LongInt);
begin
Create;
Team := fTeam;
Start := fStart;
Ende := fEnde;
end;
Was haltet ihr davon?
Ich gehe die Sache andersherum an. Die Penalty Klassen sind nur für die "Datenablage" und die Timerklasse koordiniert alles. Damit habe ich dann auch gleich eine Historie aller Strafen und kann später eine Auswertung implementieren wenn ich will.
Gruß David