-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProg_Unit.pas
1382 lines (1210 loc) · 42 KB
/
Prog_Unit.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
unit Prog_Unit;
interface
uses
Windows, Sysutils, classes, Forms, IniFiles, graphics, Dialogs, spielerdaten,
FileCtrl, scktcomp, ImportProgress, stdctrls, buttons,
menus, comctrls, OGame_Types, VirtualTrees, SelectPlugin, Stat_Points, TThreadSocketSplitter,
ThreadProtocolObject, IdBaseComponent, IdComponent,
IdTCPConnection, IdTCPClient, IdHTTP, DateUtils, LibXmlParser, cS_DB, cS_DB_reportFile,
SelectUSer, UniTree, cS_networking, MergeSocket, SplitSocket, cS_DB_solsysFile, TIReadPlugin,
SyncObjs, RaidBoard, frm_pos_size_ini, OGameData;
const
//Notes
pi_Note = 5; //-> siehe TThreadSocketSplitter!
//noneThread <= 1024 //DatenPakete werden mit dem Hauptthread Synchronisiert zur Verarbeitung aufgerufen
pi_nTScans = 10;
pi_nTSysteme = 11;
pi_nTRaids = 12;
pi_nTDelRaids = 13;
pi_nTChat = 14;
pi_nTSpieler = 15;
pi_nTStats = 16;
pi_nTFleetStats = 17;
pi_nTAllyStats = 18;
//ExtraThread >= 512 //DatenPakete werden direkt vom ReadThread der Verbindung zur Verarbeitung aufgerufen
pi_ExTOrderSystem = 540;
pi_ExTOrderScan = 545;
pi_Statistic_start = 600;
//hier nach Statistic mindestend 20 abstand halten!!
//Thread > 1024 //Datenpakete warten auf Verarbeitung! d.h. der ReadThread wird solange angehalten, bis ein anderer Thread diese Daten abruft und verarbeitet.
pi_TMain = 1025;
pi_TSyncScans = 1026;
pi_TSyncSystems = 1027;
pi_TSyncRaids = 1029;
pi_TSyncStats = 1030;
const main_ver = '2.1';
const release_number = 'a';
{$ifdef PRERELEASE}
const VNumber = 'PRE-' + main_ver + release_number;
{$else}
{$ifdef RELEASE}
const VNumber = main_ver + release_number;
{$else}
const VNumber = 'DEV-' + main_ver + release_number;
{$endif}
{$endif}
{$DEFINE oanzahl} //ohne Anzahl!! -- brauchts nirgends mehr!
type
TProgressEvent = procedure(Sender: TObject; ClientThread: TSplitSocket; Prozent: Byte) of object;
TProgressNewAction = procedure(Sender: TObject; ClientThread: TSplitSocket; Action: String) of object;
TPlanet = record
ScanBericht : Integer;
{$IFNDEF oanzahl} Anzahl : word; {$ENDIF}
end;
PSonnenSystem = ^TSonnenSystem;
TSonnenSystem = record
Planeten : Array[1..max_Planeten] of array[false..true] of TPlanet;
//MondeReached: array of TPlanetPosition;
SystemCopy : integer;
end;
TRaidPlayer = record
Name: TPlayerName;
Forschungen: array[0..2] of integer;
end;
TRaidAusgang = (kaUnentschieden,kaAngreifer,kaVerteidiger);
TRaidInfo = Record
Zeit: TDateTime;
AngreiferUnits, VerteidigerUnits: word;
Mondentstehung: Integer;
TFMetall, TFKristall: Word;
Ausgang: TRaidAusgang;
Ende: String[255];
end;
TRaidHead = Record
Angreifer: array of TRaidPlayer;
Verteidiger: array of TRaidPlayer;
Info: TRaidInfo;
end;
TNREvent = procedure(nr: integer) of object;
TOgameDataBase = class;
TOGameDataBase_AskMoon = procedure(Sender: TOGameDataBase; const
Report: TScanBericht; var isMoon: Boolean; var Handled: Boolean) of object;
EUserCanceledInit = class(Exception);
TOgameDataBase = class(TObject)
protected
cS_NetServ: TcSServer;
function FUniRead(G, S: Integer): TSonnensystem;
function InitStatFiles: Boolean;
private
xml_data_file: string;
function LoadInitFiles: boolean;
function initSysFile: Boolean;
function initScanFile: Boolean;
procedure ImportSys(Filename: String);
procedure ImportScans(Filename: string);
function GetStats: TStatPoints;
function GetFleetStats: TStatPoints;
function GetAllyStats: TStatPoints;
procedure ApplicationOnIdle(Sender: TObject; var Done: Boolean);
function initFleetBoard: Boolean;
procedure langplugin_onaskmoonprocedure(Sender: TOGameDataBase; const Report: TScanBericht;
var isMoon: Boolean; var Handled: Boolean);
public
game_data: TGameData;
//Alte Symbole: IMMER UniTree verwenden!
Berichte: TcSReportDB;
Systeme: TcSSolSysDB;
//--------------------------------------
UniTree: TUniverseTree;
UserPosition: TPlanetPosition;
Username: TPlayerName;
game_domain: string;
UniDomain: string; // andromeda, X -> domain can be pinged, but in url there is an other name like uniXXX.ogame.de
UniCheckName: string;
SaveDir, PlayerInf: String;
Importing: Boolean;
DeleteScansWhenAddSys: Boolean;
redHours: TRedHours;
MaxTimeToAdd: TDateTime;
OnRaidInfo: TNREvent;
LanguagePlugIn: TLangPlugIn;
Statistic: TStatisticDB;
Stats_own, FleetStats_own, AllyStats_own: Cardinal;
FleetBoard: TFleetBoard;
SpeedFactor: Single;
OnAskMoon: TOGameDataBase_AskMoon;
check_solsys_data_before_askMoon: boolean;
property Uni[g,s: Integer]: TSonnenSystem read FUniRead;
property Stats: TStatPoints read GetStats;
property FleetStats: TStatPoints read GetFleetStats;
property AllyStats: TStatPoints read GetAllyStats;
function GetSystemTime_u(p1, p2: word): int64;
function global_domain(): string;
function LeseFleets(handle: integer): Boolean;
function GetPlayerAtPos(planet: TPlanetPosition; addstatus: boolean = true): string;
constructor Create(UserDir: string; ACLHost: TcSServer;
xml_data_file: string);
function doInitialisation: Boolean;
destructor Destroy; override;
procedure ImportFile(Filename: String);
procedure DeleteDeletedPlanets(Sys: integer);
procedure DeleteScansOfPlanet(Pos: TPlanetPosition);
function GetSystemCopyNR(Pos: TPlanetposition): integer; overload;
function GetSystemCopyNR(g,s: Integer): integer; overload;
function LeseMehrereScanBerichte(handle: integer): Integer;
function LeseSystem(handle: integer): Boolean;
function SelectPlugIn(ForceDialog: boolean): Boolean;
procedure SaveUserOptions;
function CheckUserOptions(ForceDialog: Boolean): boolean;
procedure ImportXML(Filename: String);
function LeseStats(handle: integer): Boolean;
function GetLastActivity(pos: TPlanetPosition): int64;
function GetPlayerStatusAtPos(pos: TPlanetPosition): string;
function Time_To_AgeStr(time: TDateTime): String;
procedure updateGameData;
procedure calcScanRess_NowScan(const report: TScanBericht;
out mine_production: TSetRessources;
out prod_faktor: single;
out needed_energy: Integer;
out solsatenergy: integer;
out planettemp: single;
out ress_now: TSetRessources);
end;
TFUni = (fsAsk,fsNone,fsDeleteAll);
var ODataBase: TOgameDataBase;
login: boolean; //wird verwendet um beim beenden weider neu zum starten mit login!
cSServer: TcSServer;
Protocol: TThreadProtocol;
userdatadir: string;
var
DefaultUser: String;
LastUser: String;
LangFile: String;
//function LeseSystemPlanet(s: string; var SP: TSystemPlanet): boolean;
//function ScanToFileName(Scan: TScanBericht): string;
function AlterToColor_dt(Alter: TDateTime; maxhours: integer): TColor;
function CheckNewestVersion: string;
function dPunkteToColor(dPunkte: Integer; redgreenLine: Integer): TColor;
function ReadSysFromStream_1_0(stream: TStream): TSystemCopy;
procedure WriteSysToStream_1_0(Sys: TSystemCopy; stream: TStream);
procedure WriteStringToStream(s: shortstring; stream: TStream);
function ReadStringFromStream(stream: TStream): ShortString;
function LogSenderToStr(Sender, SenderToIdentify: TObject;
var SenderName: string): Boolean;
function Time_To_AgeStr_Ex(a_now, time: TDateTime): String;
implementation
uses Languages, Connections, cS_XML, Export,
SDBFile, OtherTime, Math;
function TOgameDataBase.LeseFleets(handle: integer): Boolean;
var count, i, j: integer;
fleet: TFleetEvent;
info: TFleetsInfoSource;
begin
info := LanguagePlugIn.ReadPhalanxScan(handle);
count := info.count;
Result := (count > 0)and(info.typ = fist_events);
if Result then
begin
//Vorher alle eingelesenen Flotten löschen:
// (damit alle "frisch" wieder eingelesen werden)
i := 0;
while (i < FleetBoard.Fleets.Count) do
begin
with FleetBoard.Fleets[i] do
begin
if (head.player = Username)and
(head.unique_id >= 0)and
(
(GetPlayerAtPos(head.origin, false) = Username)or
(GetPlayerAtPos(head.target, false) = Username)
) then
FleetBoard.DeleteFleet(i)
else
inc(i);
end;
end;
end;
for i := 0 to count -1 do
begin
if LanguagePlugIn.ReadPhalanxScanGet(fleet) then
begin
if (fleet.head.eventtype <> fet_espionage){ and
(not ((fef_return in fleet.head.eventflags) and
(fef_own in fleet.head.eventflags)))} then
begin
if (fleet.head.eventtype = fet_attack) then
begin
j := UniTree.UniReport(fleet.head.target);
if j >= 0 then
begin
fleet.ress[0] := Berichte[j].Bericht[sg_Rohstoffe,0] div 2;
fleet.ress[1] := Berichte[j].Bericht[sg_Rohstoffe,1] div 2;
fleet.ress[2] := Berichte[j].Bericht[sg_Rohstoffe,2] div 2;
end;
end;
fleet.head.player := Username;
FleetBoard.AddFleet(fleet);
end;
end;
end;
end;
function TOgameDataBase.GetPlayerAtPos(planet: TPlanetPosition;
addstatus: boolean = true): string;
var i: integer;
begin
i := UniTree.UniSys(planet.P[0],planet.P[1]);
if i >= 0 then
with Systeme[i] do
begin
Result := Planeten[planet.P[2]].Player;
if addstatus and (Planeten[planet.P[2]].Status <> []) then
Result := Result + ' (' +
LanguagePlugIn.StatusToStr(Planeten[planet.P[2]].Status) +
')';
end
else
begin
i := UniTree.UniReport(planet);
if i >= 0 then
begin
Result := Berichte[i].Head.Spieler;
end
else
Result := '???';
if addstatus then
Result := Result + ' (?)';
end;
end;
function LogSenderToStr(Sender, SenderToIdentify: TObject;
var SenderName: string): Boolean;
begin
Result := True;
if (SenderToIdentify is TSocketMultiplex) then
begin
with SenderToIdentify as TSocketMultiplex do
if Master then
SenderName := 'NCon_M_' + IntToHex(Handle,6)
else
SenderName := 'NCon_S_' + IntToHex(Handle,6);
end
else
if (SenderToIdentify is TThread) then
SenderName := 'Thread_' + IntToHex(TThread(SenderToIdentify).Handle,6)
else
Result := False;
end;
function TOgameDataBase.FUniRead(G, S: Integer): TSonnensystem;
var i: Integer;
m: Boolean;
begin
//ToDo: Diese Funktion komplett entfernen/ rausoptimieren/ ersetzen
Result.SystemCopy := UniTree.UniSys(G,S);
for i := 1 to max_planeten do
for m := false to true do
Result.Planeten[i,m].ScanBericht := UniTree.UniReport(G,S,i,m);
end;
constructor TOgameDataBase.Create(UserDir: string;
ACLHost: TcSServer; xml_data_file: string);
begin
inherited Create;
OnAskMoon := nil;
check_solsys_data_before_askMoon := false;
cS_NetServ := ACLHost;
self.xml_data_file := xml_data_file;
{CThreadHost.AddNoThreadDataProc(pi_nTScans,CThreadHostNoThreadScan_R);
CThreadHost.AddNoThreadDataProc(pi_nTSysteme,CThreadHostNoThreadSystem_R);
CThreadHost.AddNoThreadDataProc(pi_ExTOrderSystem,CThreadHostAsyncOrderSystem_R);
CThreadHost.AddNoThreadDataProc(pi_ExTOrderScan,CThreadHostAsyncOrderScan_R); }
LanguagePlugIn := TLangPlugIn.Create;
SaveDir := UserDir;
if not DirectoryExists(SaveDir) then CreateDir(SaveDir);
PlayerInf := SaveDir + 'options.inf';
if not doInitialisation then
raise Exception.Create('TOgameDataBase.Create: Init failed!');
UniTree := TNetUniTree.Create(max_Galaxy, max_Systems, max_Planeten,
Systeme, Berichte, cS_NetServ);
// assign this event after initialisation:
Application.OnIdle := ApplicationOnIdle;
end;
procedure TOgameDataBase.SaveUserOptions;
var i: integer;
ini: TMemIniFile;
rh: TredHoursTypes;
begin
//Es gibt hier zwei Proceduren!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//SaveOptions -> wird von Self.Destroy aufgerufen!
//SaveUserOptions -> wird bei Änderung der Spieler daten über "Einstellungen" aufgerufen
// und direkt nach den reinladen, in LoadInitFiles!
ini := TMemIniFile.Create(PlayerInf);
ini.WriteString('UserOptions', 'OwnName',Username);
for i := 0 to 2 do
ini.WriteInteger('StartPosition', 'Pos' + inttostr(i),UserPosition.P[i]);
ini.WriteString('UserOptions', 'UniDomain', UniDomain);
ini.WriteString('UserOptions', 'UniCheckName', UniCheckName);
ini.WriteFloat('UserOptions', 'SpeedFactor', SpeedFactor);
ini.WriteString('UserOptions', 'PluginFile', LanguagePlugIn.PluginFilename);
ini.WriteString('UserOptions', 'ogame_domain', game_domain);
ini.WriteInteger('UserOptions', 'Galaxy_Count', max_Galaxy);
ini.WriteInteger('UserOptions', 'System_Count', max_Systems);
ini.WriteFloat('UserOptions', 'tf_factor_fleet', TF_faktor_Fleet);
ini.WriteFloat('UserOptions', 'tf_factor_def', TF_faktor_Def);
ini.WriteBool('UserOptions', 'BetaUni', OGame_IsBetaUni);
ini.WriteBool('UserOptions','AutoDeleteScans',DeleteScansWhenAddSys);
for rh := low(rh) to high(rh) do
ini.WriteInteger('UserOptions','redHours'+IntToStr(integer(rh)),redHours[rh]);
ini.WriteDateTime('UserOptions','MaxTimeToAdd',MaxTimeToAdd);
ini.WriteInteger('UserOptions','Punkte',Stats_own);
ini.WriteInteger('UserOptions','FleetPunkte',FleetStats_own);
ini.WriteInteger('UserOptions','AllyPunkte',AllyStats_own);
ini.UpdateFile;
ini.free;
end;
function TOgameDataBase.LoadInitFiles: boolean;
var i: integer;
ini: TMemIniFile;
rh: TredHoursTypes;
f: single;
begin
//Laden der Daten
ini := TMemIniFile.Create(PlayerInf);
max_Galaxy := ini.ReadInteger('UserOptions','Galaxy_Count',0);
max_Systems := ini.ReadInteger('UserOptions','System_Count',0);
TF_faktor_Fleet := ini.ReadFloat('UserOptions', 'tf_factor_fleet', TF_faktor_Fleet);
TF_faktor_Def := ini.ReadFloat('UserOptions', 'tf_factor_def', TF_faktor_Def);
f := ini.ReadFloat('UserOptions', 'tf_factor', -1); // import old
if (f > 0) then
begin
TF_faktor_Fleet := f;
ini.DeleteKey('UserOptions', 'tf_factor');
ini.UpdateFile;
end;
i := ini.ReadInteger('UserOptions','DefInTF', -1); // import old
if (i = 1) then // wert für "true"
begin
TF_faktor_Def := TF_faktor_Fleet;
ini.DeleteKey('UserOptions','DefInTF');
ini.UpdateFile;
end;
OGame_IsBetaUni := ini.ReadBool('UserOptions', 'BetaUni', OGame_IsBetaUni);
for i := 0 to 2 do
UserPosition.P[i] := ini.ReadInteger('StartPosition','Pos' + inttostr(i),0);
UserPosition.Mond := false;
Username := ini.ReadString('UserOptions','OwnName','');
UniDomain := ini.ReadString('UserOptions','UniDomain','');
if UniDomain = '' then
UniDomain := 'uni' + ini.ReadString('UserOptions','Uni',''); // import old settings
UniCheckName := ini.ReadString('UserOptions','UniCheckName','');
if UniCheckName = '' then
UniCheckName := UniDomain;
SpeedFactor := ini.ReadFloat('UserOptions','SpeedFactor', 1);
game_domain := ini.ReadString('UserOptions','ogame_domain','--n/a--');
if game_domain = '--n/a--' then // for compatibility to old version
game_domain := game_sites_OLD[ini.ReadInteger('UserOptions','LanguageIndex',0)];
LanguagePlugIn.PluginFilename := ini.ReadString('UserOptions','PluginFile','');
DeleteScansWhenAddSys := ini.ReadBool('UserOptions','AutoDeleteScans',true);
Stats_own := ini.ReadInteger('UserOptions','Punkte',0);
FleetStats_own := ini.ReadInteger('UserOptions','FleetPunkte',0);
AllyStats_own := ini.ReadInteger('UserOptions','AllyPunkte',0);
redHours[rh_Scans] := 340;
redHours[rh_Systems] := 340;
redHours[rh_Stats] := 340;
redHours[rh_Points] := 100000;
for rh := low(rh) to high(rh) do
redHours[rh] := ini.ReadInteger('UserOptions','redHours'+IntToStr(integer(rh)),redHours[rh]);
MaxTimeToAdd := ini.ReadDateTime('UserOptions','MaxTimeToAdd',2);
ini.free;
Result := CheckUserOptions(False);
if Result then
begin {dann hatt alles funzt- >}
SelectPlugIn(False);
SaveUserOptions;
end;
end;
function AlterToColor_dt(Alter: TDateTime; maxhours: integer): TColor;
var r,g,b : integer;
begin
if Alter < 0 then //Zukunft!?
begin
Result := clsilver;
exit;
end;
alter := (alter * 255) * (24/maxhours); //aufteilen in 255 stufen
r := trunc(alter); if r > 255 then r := 255;
g := 255 - trunc(alter); if g < 0 then g := 0;
b := (g-r);
if b<0 then b := -b;
b := 255-b;
Result := rgb(r,g,b);
end;
function TOgameDataBase.doInitialisation: Boolean;
begin
try
game_data := TGameData.Create(xml_data_file);
finally
// ... TODO
end;
Result := LoadInitFiles and
initScanFile and
initSysFile and
InitStatFiles and
initFleetBoard;
end;
destructor TOgameDataBase.Destroy;
begin
Statistic.Free;
FleetBoard.Free;
UniTree.Free;
Systeme.Free;
Berichte.Free;
LanguagePlugIn.Free;
game_data.Free;
inherited Destroy;
end;
function TOgameDataBase.initSysFile: Boolean;
(* Allgemein: Öffnet die Sonnensystem-Datenbank
Speziell: Öffnet die Datei für die Sonnensystem-Datenbank.
Falls diese ein altes Format besitzt, wird
eine neue Datenbank erstellt und die alte Datei importiert,
dannach gelöscht! *)
function __importold(filename: string): Boolean;
var old: TcSSolSysDB_for_File;
i: integer;
begin
old := TcSSolSysDB_for_File.Create(filename, UniDomain);
try
for i := 0 to old.Count-1 do
begin
Systeme.AddSolSys(old[i]);
end;
finally
old.Free;
end;
Result := True;
end;
var Filename, msg: String;
fileacc: TcSSolSysDBFile;
begin
Result := True; // Errors are reported through excpetions
Filename := SaveDir + 'solsys.cssys';
try
Systeme := TcSSolSysDB_for_File.Create(Filename, UniDomain);
except
on E: EcSDBFFSysUniDiffers do
begin
Systeme.Free;
msg := 'The SolSys-DB file is tagged for an other universe: '
+ E.file_universe_name + #10 + #13
+ 'This may occour if you updated creatureScan or changed your profile.' + #10 + #13
+ 'Shall we use this file despite the warning?' + #10 + #13
+ 'If you press YES, the uni-tag of the file will be changed.' + #10 + #13
+ 'If you press NO, the file will be deleted.' + #10 + #13
+ 'If you press CANCEL, the application terminates and you can do with your file what you like' + #10 + #13;
case Application.MessageBox(PChar(msg), 'Warning', MB_YESNOCANCEL) of
IDYES:
begin
// change uni tag
fileacc := TcSSolSysDBFile.Create(Filename);
fileacc.UniDomain := UniDomain;
fileacc.Free;
// try to load DB again
Systeme := TcSSolSysDB_for_File.Create(Filename, UniDomain);
end;
IDNO:
begin
// delete file
DeleteFile(Filename);
// try to load DB again (create new one in this case!)
Systeme := TcSSolSysDB_for_File.Create(Filename, UniDomain);
end;
else
raise;
end;
end;
end;
with Systeme as TcSSolSysDB_for_File do
begin
if IsOldFormat then
begin
Systeme.Free;
RenameFile(Filename,Filename+'.old');
Systeme := TcSSolSysDB_for_File.Create(Filename, UniDomain);
if __importold(Filename+'.old') then
DeleteFile(Filename+'.old');
end;
end;
end;
function TOgameDataBase.initScanFile: boolean;
(* Allgemein: Öffnet die Berichte-Datenbank
Speziell: Öffnet die Datei für die Berichte-Datenbank.
Falls diese ein altes Format besitzt, wird
eine neue Datenbank erstellt und die alte Datei importiert,
dannach gelöscht! *)
function __importold(filename: string): Boolean;
var old: TcSReportDB_for_File;
i: integer;
begin
old := TcSReportDB_for_File.Create(filename, UniDomain);
try
for i := 0 to old.Count-1 do
begin
Berichte.AddReport(old[i])
end;
finally
old.Free;
end;
Result := True;
end;
var Filename, msg: String;
fileacc: TcSReportDBFile;
begin
Result := True; // Error are reported through exceptions
Filename := SaveDir + 'reports.csscan';
try
Berichte := TcSReportDB_for_File.Create(Filename, UniDomain);
except
on E: EcSDBFFReportUniDiffers do
begin
Berichte.Free;
msg := 'The Report-DB file is tagged for an other universe: '
+ E.file_universe_name + #10 + #13
+ 'This may occour if you updated creatureScan or changed your profile.' + #10 + #13
+ 'Shall we use this file despite the warning?' + #10 + #13
+ 'If you press YES, the uni-tag of the file will be changed.' + #10 + #13
+ 'If you press NO, the file will be deleted.' + #10 + #13
+ 'If you press CANCEL, the application terminates and you can do with your file what you like' + #10 + #13;
case Application.MessageBox(PChar(msg), 'Warning', MB_YESNOCANCEL) of
IDYES:
begin
// change uni tag
fileacc := TcSReportDBFile.Create(Filename);
fileacc.UniDomain := UniDomain;
fileacc.Free;
// try to load DB again
Berichte := TcSReportDB_for_File.Create(Filename, UniDomain);
end;
IDNO:
begin
// delete file
DeleteFile(Filename);
// try to load DB again (create new one in this case!)
Berichte := TcSReportDB_for_File.Create(Filename, UniDomain);
end;
else
raise;
end;
end;
end;
with Berichte as TcSReportDB_for_File do
begin
if IsOldFormat then
begin
Berichte.Free;
RenameFile(Filename,Filename+'.old');
Berichte := TcSReportDB_for_File.Create(Filename, UniDomain);
if __importold(Filename+'.old') then
DeleteFile(Filename+'.old');
end;
end;
end;
procedure TOgameDataBase.ImportFile(Filename: String);
//Mit dieser Procedure wird ein Importvorgang gestartet. Sie ermittelt, ob Scan oder Sytem-Datei und leitet die internen Importfunktionen ein!
var datei: TFileStream;
V: String[255]; // einfach mal mehr einlesen, mach ja nix
sysFF: TcSSolSysFileFormat;
scanFF: TcSReportFileFormat;
isValidFormat: Boolean;
begin
//Lesen der Versionsimformationen:
datei := TFileStream.Create(Filename,fmOpenRead);
datei.Read(V,sizeof(V));
datei.free;
isValidFormat := false;
//Auswahl und Start der iternen Funktionen (ImportSys / ImportScans)
for sysFF := TcSSolSysFileFormat(1) to high(sysFF) do
if V = cSSolSysFileFormatstr[sysFF] then
isValidFormat := True;
if (isValidFormat) then
begin
ImportSys(Filename);
Exit;
end;
for scanFF := TcSReportFileFormat(1) to high(scanFF) do
if V = cSReportFileFormatstr[scanFF] then
isValidFormat := True;
if (isValidFormat) then
begin
ImportScans(Filename);
Exit;
end;
if (copy(V,1,6) = 'export')or(copy(V,1,4) = '?xml') then
ImportXML(Filename)
else
if (copy(V,1,3) = 'cSe') then
FRM_Export.ImportcSe_1_0(Filename)
else
ShowMessage(STR_MSG_keine_creatureScan_datei);
end;
procedure TOgameDataBase.ImportSys(Filename: String);
//Wird normalerweise nur durch die Pulic-Procedure ImportFile aufgerufen. Importiert alle neueren Sonnensysteme in der Datei (filename)
var ImportSysFile: TcSSolSysDBFile;
i: integer;
FRM_Progress: TFRM_ImportProgress;
e: Boolean;
begin
ImportSysFile := TcSSolSysDBFile.Create(Filename);
try
//überprüfung auf Uni (nicht auf Sprache, da nicht in der Dateiinformation enthalten!)
if (*(ImportSysFile.Universe = UserUni)*) (true) or // TODO
(Application.MessageBox(
PChar(STR_MSG_Falsches_Universum + ImportSysFile.UniDomain),
PChar(Application.Title),MB_YESNO or MB_ICONQUESTION) = idYes) then
begin
//erstellen und initialisieren des FortschrittFenster
FRM_Progress := TFRM_ImportProgress.Create(Application);
FRM_Progress.GroupBox1.Caption := STR_Importiere_Sonnensysteme;
FRM_Progress.Gauge1.MaxValue := ImportSysFile.Count;
//Ausschalter
FRM_Progress.ende := @e;
e := false;
FRM_Progress.show;
for i := 0 to ImportSysFile.Count-1 do
begin
try
//AddSysToList überprüft selbst, ob die Daten neuer sind oder nicht!
UniTree.AddNewSolSys(ImportSysFile.SolSys[i]);
except
Application.HandleException(self);
if Application.MessageBox(Pchar('Error importing solsys #' + IntToStr(i)),'cS Import',MB_OKCANCEL) = IDCancel then
e := True;
end;
FRM_Progress.Label1.Caption := STR_Systeme + ': ' + IntToStr(i);
FRM_Progress.Gauge1.Progress := i+1;
//wird in keinem eigenen Thread ausgeführt!
Application.ProcessMessages;
if e then Break;
end;
FRM_Progress.ende := nil;
FRM_Progress.Release;
end;
finally
ImportSysFile.Free;
end;
end;
procedure TOgameDataBase.ImportScans(Filename: string);
var ImportScanFile: TcSReportDBFile;
i: integer;
FRM_Progress: TFRM_ImportProgress;
e: boolean;
begin
ImportScanFile := TcSReportDBFile.Create(Filename);
//überprüfung auf Uni (nicht auf Sprache, da nicht in der Dateiinformation enthalten!)
if (*(ImportScanFile.Universe = UserUni)*) (true)or // TODO
(Application.MessageBox(PChar(STR_MSG_Falsches_Universum + ImportScanFile.UniDomain),PChar(Application.Title),MB_YESNO or MB_ICONQUESTION) = idYes) then
begin
//erstellen und initialisieren des FortschrittsFenster
FRM_Progress := TFRM_ImportProgress.Create(Application);
FRM_Progress.GroupBox1.Caption := STR_Importiere_Scanberichte;
FRM_Progress.Gauge1.MaxValue := ImportScanFile.Count;
//Ausschalter
FRM_Progress.ende := @e;
e := False;
FRM_Progress.show;
for i := 0 to ImportScanFile.Count-1 do
begin
Importing := true;
try
UniTree.AddNewReport(ImportScanFile.Reports[i]);
except
ShowMessage('Error importing report #' + IntToStr(i));
end;
Importing := false;
FRM_Progress.Label1.Caption := STR_Scanberichte + ': ' + IntToStr(i+1);
FRM_Progress.Gauge1.Progress := i+1;
//wird in keinem eigenen Thread ausgeführt!
Application.ProcessMessages;
if e then Break;
end;
FRM_Progress.ende := nil;
FRM_Progress.Release;
end;
ImportScanFile.Free;
end;
procedure TOgameDataBase.DeleteDeletedPlanets(Sys: integer);
var p: integer;
m: boolean;
pos: TPlanetPosition;
begin
with Systeme[sys] do
for p := 1 to max_Planeten do for m := false to true do
if (Uni[System.P[0],System.P[1]].Planeten[p,m].ScanBericht >= 0)and(Planeten[p].Player = '') then
begin
pos := system;
pos.P[2] := p;
pos.Mond := m;
DeleteScansOfPlanet(pos);
end;
end;
procedure TOgameDataBase.DeleteScansOfPlanet(Pos: TPlanetPosition);
var i: integer;
begin
i := UniTree.UniReport(Pos);
while (i >= 0) do
begin
UniTree.DeleteReport(i);
i := UniTree.UniReport(Pos);
end;
end;
function CheckNewestVersion: string;
var net: TIdHTTP;
mem: TMemoryStream;
ende: byte;
parser: TXmlParser;
tag_cS, tag_version_main: Boolean;
begin
net := TIdHTTP.Create(Application);
mem := TMemoryStream.Create;
try
net.Get(UpdateCheckUrl,mem);
ende := 0;
mem.Write(ende,sizeof(ende));
parser := TXmlParser.Create;
parser.SetBuffer(mem.Memory);
tag_cS := False;
tag_version_main := False;
parser.StartScan;
while parser.Scan do
begin
if parser.CurName = 'creaturescan' then
tag_cS := parser.CurPartType = ptStartTag;
case parser.CurPartType of
ptStartTag: if (parser.CurName = 'version')and
(parser.CurAttr.Value('type') = 'main') then
begin
tag_version_main := true;
end;
ptContent: if tag_cS and tag_version_main then
begin
Result := trim(parser.CurContent);
break;
end;
end;
end;
parser.Free;
except
Result := '';
end;
net.free;
mem.Free;
end;
function TOgameDataBase.GetSystemCopyNR(Pos: TPlanetposition): integer;
//kürzerer weg zum erfahren der systemcopyNR
begin
if ValidPosition(pos) then
Result := Uni[pos.p[0],pos.p[1]].SystemCopy
else Result := -1;
end;
function TOgameDataBase.GetSystemCopyNR(g,s: Integer): integer;
//kürzerer weg zum erfahren der systemcopyNR
var pos: TPlanetPosition;
begin
pos.P[0] := g;
pos.P[1] := s;
pos.P[2] := 1;
pos.Mond := False;
Result := GetSystemCopyNR(pos);
end;
function TOgameDataBase.LeseMehrereScanBerichte(handle: integer): Integer;
var Scan: TScanBericht;
c: integer;
moon_unknown, phandled: boolean;
begin
Result := 0;
c := LanguagePlugIn.ReadReports(handle);
Scan := TScanBericht.Create;
try
while LanguagePlugIn.GetReport(handle, Scan, moon_unknown) do
begin
Scan.Head.Creator := Username;
if moon_unknown then
begin
phandled := false;
langplugin_onaskmoonprocedure(Self, Scan,
Scan.Head.Position.Mond,
phandled);
if phandled then
UniTree.AddNewReport(Scan);
// else: throw away! (maybee the onaskmoonporcedure saved the scan somewhere in order to add it later
end
else
UniTree.AddNewReport(Scan);
inc(Result);
end;
finally
Scan.Free;
end;
if c <> Result then
raise Exception.Create('TOgameDataBase.LeseMehrereScanBerichte: Plugin fehlerhaft,' +
' Anzahl einegelesener Scans stimmt nicht mit Rückgabewert überein!');
end;
function TOgameDataBase.LeseSystem(handle: integer): Boolean;
var Sys: TSystemCopy;
begin
Result := LanguagePlugIn.ReadSystem(handle, Sys, Username);
if Result then
begin
UniTree.AddNewSolSys(sys);
end;
end;
function TOgameDataBase.SelectPlugIn(ForceDialog: boolean): Boolean;
var dialog: TFRM_SelectPlugin;
serverURL: string;
begin
serverURL := UniCheckName + '.' + game_domain;
chdir(ExtractFilePath(Application.ExeName));
LanguagePlugIn.LoadPluginFile(LanguagePlugIn.PluginFilename,
serverURL, PlayerInf);
Result := (not ForceDialog) and
LanguagePlugIn.ValidFile and
(game_domain = LanguagePlugIn.configGameDomain);
if not Result then
begin
dialog := TFRM_SelectPlugin.Create(Application,game_domain);
dialog.PluginFile := LanguagePlugIn.PluginFilename;
if dialog.ShowModal = IDOK then
begin
LanguagePlugIn.LoadPluginFile(dialog.PluginFile, serverURL, PlayerInf);
if not LanguagePlugIn.ValidFile then
ShowMessage('Die Plugindatei ist fehlerhaft!');
Result := LanguagePlugIn.ValidFile and
(game_domain = LanguagePlugIn.configGameDomain);
end;
dialog.free;
end;
end;
function TOgameDataBase.CheckUserOptions(ForceDialog: Boolean): boolean;