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
Import MaxGUI.Drivers
Type TStat
	Global list:TList
	Global saved = True
	
	Function ResetList()
		list = CreateList()
	EndFunction
	
	Function AddList(name$,u,s,k,d,t)
		TStat.saved = False
	
		Local stat:TStat = New TStat
		
		stat.name = name
		stat.usgn = u
		stat.score = s
		stat.kills = k
		stat.deaths = d
		stat.time = t
		ListAddLast(list, stat)
	EndFunction
	
	Function FindUSGN:TStat(u)
		For s:TStat = EachIn TStat.list
			If s.usgn = u Then Return s
		Next
	EndFunction
	Field name$
	Field usgn
	Field score
	Field kills
	Field deaths
	Field time
EndType
Function SaveStats()
	If TStat.list Then
		Local stream:TStream = WriteStream("sys/stats/userstats.dat")
		If stream Then
			TStat.saved = True
		
			WriteLine stream, "userstats alpha"
			
			For stat:TStat = EachIn TStat.list
				WriteLine stream, stat.name
				WriteInt stream, stat.usgn
				WriteInt stream, stat.score
				WriteInt stream, stat.kills
				WriteInt stream, stat.deaths
				WriteInt stream, stat.time
			Next
			
			CloseStream(stream)
			Notify("Stats saved succesfully!")
		Else
			Notify("Failed to save stats, cannot write in file", True)
		EndIf
	Else
		Notify("Failed to save stats, load stats first", True)
	EndIf
EndFunction
Function ReadStats()
	TStat.ResetList()
	If FileType("sys/stats/stats.dat") = 1 Then
		DeleteFile("sys/stats/stats.dat")
	EndIf
	
	Local stream:TStream = ReadStream("sys/stats/userstats.dat")
	If stream Then
		If ReadLine(stream) <> "userstats alpha" Then
			Return Null
		EndIf
	
		While Not Eof(stream)
			Local name$ = ReadLine(stream)
			Local size = StreamSize(stream) - StreamPos(stream)
			
			If size >= 20 Then
				Local usgn = ReadInt(stream)
				Local score = ReadInt(stream)
				Local kills = ReadInt(stream)
				Local deaths = ReadInt(stream)
				Local time = ReadInt(stream)
				
				TStat.AddList(name, usgn, score, kills, deaths, time)
			Else
				Exit
			EndIf
		Wend
		
		TStat.saved = True
		CloseStream(stream)
		Return True
	EndIf
EndFunction
Function LoadStatsMenu()
	If ReadStats() Then
		current_stats = Null
		SetGadgetText(stat_name, "")
		SetGadgetText(stat_usgn, "")
		SetGadgetText(stat_score, "")
		SetGadgetText(stat_kills, "")
		SetGadgetText(stat_deaths, "")
		SetGadgetText(stat_time, "")
		ClearGadgetItems(stat_player_menu)
		
		For stat:TStat = EachIn TStat.list
			AddGadgetItem(stat_player_menu, stat.name, 0, -1, "#"+stat.usgn)
		Next
		
		EnableGadget(add_entry_button)
		EnableGadget(create_entry_button)
		EnableGadget(button_save_stats)
		EnableGadget(entry_remove_button)
	Else
		Notify("Failed to load stats", True)
	EndIf
EndFunction
Function stats_id:TStat(id)
	Local i = 0
	For stat:TStat = EachIn TStat.list
		If i = id Then
			Return stat
		EndIf
		i = i + 1
	Next
EndFunction
Function open_stats_id(id)
	current_stats_id = id
	current_stats = stats_id(id)
	If current_stats Then
		SetGadgetText(stat_name, current_stats.name)
		SetGadgetText(stat_usgn, current_stats.usgn)
		SetGadgetText(stat_score, current_stats.score)
		SetGadgetText(stat_kills, current_stats.kills)
		SetGadgetText(stat_deaths, current_stats.deaths)
		SetGadgetText(stat_time, current_stats.time)
	EndIf
EndFunction
Function update_stats()
	If current_stats Then		
		Local name$ = GadgetText(stat_name)
		Local usgn = Int(GadgetText(stat_usgn))
		Local score = Int(GadgetText(stat_score))
		Local kills = Int(GadgetText(stat_kills))
		Local deaths = Int(GadgetText(stat_deaths))
		Local time = Int(GadgetText(stat_time))
		
		Local modified = False
		If name <> current_stats.name Then
			modified = True
		ElseIf usgn <> current_stats.usgn Then
			modified = True
		ElseIf score <> current_stats.score Then
			modified = True
		ElseIf kills <> current_stats.kills Then
			modified = True
		ElseIf deaths <> current_stats.deaths Then
			modified = True
		ElseIf time <> current_stats.time Then
			modified = True
		EndIf
	
		If modified Then
			current_stats.name = GadgetText(stat_name)
			current_stats.usgn = Int(GadgetText(stat_usgn))
			current_stats.score = Int(GadgetText(stat_score))
			current_stats.kills = Int(GadgetText(stat_kills))
			current_stats.deaths = Int(GadgetText(stat_deaths))
			current_stats.time = Int(GadgetText(stat_time))
			
			ModifyGadgetItem(stat_player_menu, current_stats_id, name)
			TStat.saved = False
		EndIf
	EndIf
EndFunction
Function remove_current_entry()
	If current_stats And TStat.list Then
		ListRemove(TStat.list, current_stats)
		RemoveGadgetItem(stat_player_menu, current_stats_id)
		
		current_stats_id = Null
		current_stats = Null
		
		SetGadgetText(stat_name, "")
		SetGadgetText(stat_usgn, "")
		SetGadgetText(stat_score, "")
		SetGadgetText(stat_kills, "")
		SetGadgetText(stat_deaths, "")
		SetGadgetText(stat_time, "")
	EndIf
EndFunction
Function TextFieldIntFilter(e:TEvent,context:Object)
	Select e.id
		Case EVENT_KEYCHAR
			If e.data = 8 Then Return 1
			Return Instr("0123456789", Chr(e.data))
	EndSelect
EndFunction
Function add_stat_list()
	Local name$ = GadgetText(e_stat_name)
	Local usgn = Int(GadgetText(e_stat_usgn))
	Local score = Int(GadgetText(e_stat_score))
	Local kills = Int(GadgetText(e_stat_kills))
	Local deaths = Int(GadgetText(e_stat_deaths))
	Local time = Int(GadgetText(e_stat_time))
	
	If usgn > 0 Then
		If Len(name) > 0 Then
			If Not TStat.FindUSGN(usgn) Then
				TStat.AddList(name, usgn, score, kills, deaths, time)
				AddGadgetItem(stat_player_menu, name, 0, -1, "#"+usgn)
				Return True
			Else
				Notify("This U.S.G.N ID is already in the stats list!", True)
			EndIf
		Else
			Notify("You must specify a name!", True)
		EndIf
	Else
		Notify("You must specify an usgn id!", True)
	EndIf
EndFunction
AppTitle = "Stats Editor by Starkkz"
Global current_stats_id
Global current_stats:TStat
Global main_window:TGadget = CreateWindow("Stats Editor by Starkkz", DesktopWidth()/2 - 150, DesktopHeight()/2 - 200, 300, 400, Null, WINDOW_TITLEBAR)
Global button_load_stats:TGadget = CreateButton("Load Stats", 10, 15, 100, 25, main_window)
Global button_save_stats:TGadget = CreateButton("Save Stats", 10, 45, 100, 25, main_window)
Global entry_remove_button:TGadget = CreateButton("Remove Entry", 120, 45, 100, 25, main_window)
Global create_entry_button:TGadget = CreateButton("Create Entry", 120, 15, 100, 25, main_window)
Global stat_player_menu:TGadget = CreateComboBox(120, 75, 165, 25, main_window)
Global entry_window:TGadget = CreateWindow("Create Entry", DesktopWidth()/2 - 125, DesktopHeight()/2 - 175, 250, 350, main_window, WINDOW_TITLEBAR|WINDOW_HIDDEN)
Global add_entry_button:TGadget = CreateButton("Add Entry", 10, 285, 100, 25, entry_window)
SetGadgetAlpha(entry_window, 0.9)
DisableGadget(add_entry_button)
DisableGadget(create_entry_button)
DisableGadget(button_save_stats)
DisableGadget(entry_remove_button)
CreateLabel("Player Name", 10, 90, 100, 20, main_window)
CreateLabel("USGN ID", 10, 135, 100, 20, main_window)
CreateLabel("Score", 10, 180, 100, 20, main_window)
CreateLabel("Kills", 10, 225, 100, 20, main_window)
CreateLabel("Deaths", 10, 270, 100, 20, main_window)
CreateLabel("Server time", 10, 315, 100, 20, main_window)
CreateLabel("Player Name", 10, 10, 100, 20, entry_window)
CreateLabel("USGN ID", 10, 55, 100, 20, entry_window)
CreateLabel("Score", 10, 100, 100, 20, entry_window)
CreateLabel("Kills", 10, 145, 100, 20, entry_window)
CreateLabel("Deaths", 10, 190, 100, 20, entry_window)
CreateLabel("Server time", 10, 235, 100, 20, entry_window)
Global stat_name:TGadget = CreateTextField(10, 110, 170, 25, main_window)
Global stat_usgn:TGadget = CreateTextField(10, 155, 170, 25, main_window)
Global stat_score:TGadget = CreateTextField(10, 200, 170, 25, main_window)
Global stat_kills:TGadget = CreateTextField(10, 245, 170, 25, main_window)
Global stat_deaths:TGadget = CreateTextField(10, 290, 170, 25, main_window)
Global stat_time:TGadget = CreateTextField(10, 335, 170, 25, main_window)
Global e_stat_name:TGadget = CreateTextField(10, 30, 170, 25, entry_window)
Global e_stat_usgn:TGadget = CreateTextField(10, 75, 170, 25, entry_window)
Global e_stat_score:TGadget = CreateTextField(10, 120, 170, 25, entry_window)
Global e_stat_kills:TGadget = CreateTextField(10, 165, 170, 25, entry_window)
Global e_stat_deaths:TGadget = CreateTextField(10, 210, 170, 25, entry_window)
Global e_stat_time:TGadget = CreateTextField(10, 255, 170, 25, entry_window)
SetGadgetFilter(stat_usgn, TextFieldIntFilter)
SetGadgetFilter(stat_score, TextFieldIntFilter)
SetGadgetFilter(stat_kills, TextFieldIntFilter)
SetGadgetFilter(stat_deaths, TextFieldIntFilter)
SetGadgetFilter(stat_time, TextFieldIntFilter)
SetGadgetFilter(e_stat_usgn, TextFieldIntFilter)
SetGadgetFilter(e_stat_score, TextFieldIntFilter)
SetGadgetFilter(e_stat_kills, TextFieldIntFilter)
SetGadgetFilter(e_stat_deaths, TextFieldIntFilter)
SetGadgetFilter(e_stat_time, TextFieldIntFilter)
Repeat
	WaitEvent()
	Select EventID()
		Case EVENT_GADGETACTION
			Select EventSource()
				Case button_load_stats
					LoadStatsMenu()
				Case button_save_stats
					update_stats()
					SaveStats()
				Case stat_player_menu
					update_stats()
					open_stats_id(EventData())
				Case entry_remove_button
					remove_current_entry()
				Case create_entry_button
					ShowGadget(entry_window)
				Case add_entry_button
					If add_stat_list() Then
						HideGadget(entry_window)
						
						SetGadgetText(e_stat_name, "")
						SetGadgetText(e_stat_usgn, "")
						SetGadgetText(e_stat_score, "")
						SetGadgetText(e_stat_kills, "")
						SetGadgetText(e_stat_deaths, "")
						SetGadgetText(e_stat_time, "")
					EndIf
			EndSelect
		Case EVENT_WINDOWCLOSE
			Select EventSource()
				Case main_window
					If Not TStat.saved Then
						If Proceed("Stats haven't been saved, do you wish to save them?") Then
							SaveStats()
						EndIf
					EndIf
					End
				Case entry_window
					HideGadget(entry_window)
					
					SetGadgetText(e_stat_name, "")
					SetGadgetText(e_stat_usgn, "")
					SetGadgetText(e_stat_score, "")
					SetGadgetText(e_stat_kills, "")
					SetGadgetText(e_stat_deaths, "")
					SetGadgetText(e_stat_time, "")
			EndSelect
	EndSelect
Forever