Advertisement
Starkkz

User stats editor

Nov 5th, 2014
3,156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Import MaxGUI.Drivers
  2.  
  3. Type TStat
  4.     Global list:TList
  5.     Global saved = True
  6.    
  7.     Function ResetList()
  8.         list = CreateList()
  9.     EndFunction
  10.    
  11.     Function AddList(name$,u,s,k,d,t)
  12.         TStat.saved = False
  13.    
  14.         Local stat:TStat = New TStat
  15.        
  16.         stat.name = name
  17.         stat.usgn = u
  18.         stat.score = s
  19.         stat.kills = k
  20.         stat.deaths = d
  21.         stat.time = t
  22.         ListAddLast(list, stat)
  23.     EndFunction
  24.    
  25.     Function FindUSGN:TStat(u)
  26.         For s:TStat = EachIn TStat.list
  27.             If s.usgn = u Then Return s
  28.         Next
  29.     EndFunction
  30.  
  31.     Field name$
  32.     Field usgn
  33.     Field score
  34.     Field kills
  35.     Field deaths
  36.     Field time
  37. EndType
  38.  
  39. Function SaveStats()
  40.     If TStat.list Then
  41.         Local stream:TStream = WriteStream("sys/stats/userstats.dat")
  42.         If stream Then
  43.             TStat.saved = True
  44.        
  45.             WriteLine stream, "userstats alpha"
  46.            
  47.             For stat:TStat = EachIn TStat.list
  48.                 WriteLine stream, stat.name
  49.                 WriteInt stream, stat.usgn
  50.                 WriteInt stream, stat.score
  51.                 WriteInt stream, stat.kills
  52.                 WriteInt stream, stat.deaths
  53.                 WriteInt stream, stat.time
  54.             Next
  55.            
  56.             CloseStream(stream)
  57.             Notify("Stats saved succesfully!")
  58.         Else
  59.             Notify("Failed to save stats, cannot write in file", True)
  60.         EndIf
  61.     Else
  62.         Notify("Failed to save stats, load stats first", True)
  63.     EndIf
  64. EndFunction
  65.  
  66. Function ReadStats()
  67.     TStat.ResetList()
  68.  
  69.     If FileType("sys/stats/stats.dat") = 1 Then
  70.         DeleteFile("sys/stats/stats.dat")
  71.     EndIf
  72.    
  73.     Local stream:TStream = ReadStream("sys/stats/userstats.dat")
  74.     If stream Then
  75.         If ReadLine(stream) <> "userstats alpha" Then
  76.             Return Null
  77.         EndIf
  78.    
  79.         While Not Eof(stream)
  80.             Local name$ = ReadLine(stream)
  81.             Local size = StreamSize(stream) - StreamPos(stream)
  82.            
  83.             If size >= 20 Then
  84.                 Local usgn = ReadInt(stream)
  85.                 Local score = ReadInt(stream)
  86.                 Local kills = ReadInt(stream)
  87.                 Local deaths = ReadInt(stream)
  88.                 Local time = ReadInt(stream)
  89.                
  90.                 TStat.AddList(name, usgn, score, kills, deaths, time)
  91.             Else
  92.                 Exit
  93.             EndIf
  94.         Wend
  95.        
  96.         TStat.saved = True
  97.         CloseStream(stream)
  98.         Return True
  99.     EndIf
  100. EndFunction
  101.  
  102. Function LoadStatsMenu()
  103.     If ReadStats() Then
  104.         current_stats = Null
  105.         SetGadgetText(stat_name, "")
  106.         SetGadgetText(stat_usgn, "")
  107.         SetGadgetText(stat_score, "")
  108.         SetGadgetText(stat_kills, "")
  109.         SetGadgetText(stat_deaths, "")
  110.         SetGadgetText(stat_time, "")
  111.         ClearGadgetItems(stat_player_menu)
  112.        
  113.         For stat:TStat = EachIn TStat.list
  114.             AddGadgetItem(stat_player_menu, stat.name, 0, -1, "#"+stat.usgn)
  115.         Next
  116.        
  117.         EnableGadget(add_entry_button)
  118.         EnableGadget(create_entry_button)
  119.         EnableGadget(button_save_stats)
  120.         EnableGadget(entry_remove_button)
  121.     Else
  122.         Notify("Failed to load stats", True)
  123.     EndIf
  124. EndFunction
  125.  
  126. Function stats_id:TStat(id)
  127.     Local i = 0
  128.     For stat:TStat = EachIn TStat.list
  129.         If i = id Then
  130.             Return stat
  131.         EndIf
  132.         i = i + 1
  133.     Next
  134. EndFunction
  135.  
  136. Function open_stats_id(id)
  137.     current_stats_id = id
  138.     current_stats = stats_id(id)
  139.     If current_stats Then
  140.         SetGadgetText(stat_name, current_stats.name)
  141.         SetGadgetText(stat_usgn, current_stats.usgn)
  142.         SetGadgetText(stat_score, current_stats.score)
  143.         SetGadgetText(stat_kills, current_stats.kills)
  144.         SetGadgetText(stat_deaths, current_stats.deaths)
  145.         SetGadgetText(stat_time, current_stats.time)
  146.     EndIf
  147. EndFunction
  148.  
  149. Function update_stats()
  150.     If current_stats Then      
  151.         Local name$ = GadgetText(stat_name)
  152.         Local usgn = Int(GadgetText(stat_usgn))
  153.         Local score = Int(GadgetText(stat_score))
  154.         Local kills = Int(GadgetText(stat_kills))
  155.         Local deaths = Int(GadgetText(stat_deaths))
  156.         Local time = Int(GadgetText(stat_time))
  157.        
  158.         Local modified = False
  159.         If name <> current_stats.name Then
  160.             modified = True
  161.         ElseIf usgn <> current_stats.usgn Then
  162.             modified = True
  163.         ElseIf score <> current_stats.score Then
  164.             modified = True
  165.         ElseIf kills <> current_stats.kills Then
  166.             modified = True
  167.         ElseIf deaths <> current_stats.deaths Then
  168.             modified = True
  169.         ElseIf time <> current_stats.time Then
  170.             modified = True
  171.         EndIf
  172.    
  173.         If modified Then
  174.             current_stats.name = GadgetText(stat_name)
  175.             current_stats.usgn = Int(GadgetText(stat_usgn))
  176.             current_stats.score = Int(GadgetText(stat_score))
  177.             current_stats.kills = Int(GadgetText(stat_kills))
  178.             current_stats.deaths = Int(GadgetText(stat_deaths))
  179.             current_stats.time = Int(GadgetText(stat_time))
  180.            
  181.             ModifyGadgetItem(stat_player_menu, current_stats_id, name)
  182.             TStat.saved = False
  183.         EndIf
  184.     EndIf
  185. EndFunction
  186.  
  187. Function remove_current_entry()
  188.     If current_stats And TStat.list Then
  189.         ListRemove(TStat.list, current_stats)
  190.         RemoveGadgetItem(stat_player_menu, current_stats_id)
  191.        
  192.         current_stats_id = Null
  193.         current_stats = Null
  194.        
  195.         SetGadgetText(stat_name, "")
  196.         SetGadgetText(stat_usgn, "")
  197.         SetGadgetText(stat_score, "")
  198.         SetGadgetText(stat_kills, "")
  199.         SetGadgetText(stat_deaths, "")
  200.         SetGadgetText(stat_time, "")
  201.     EndIf
  202. EndFunction
  203.  
  204. Function TextFieldIntFilter(e:TEvent,context:Object)
  205.     Select e.id
  206.         Case EVENT_KEYCHAR
  207.             If e.data = 8 Then Return 1
  208.             Return Instr("0123456789", Chr(e.data))
  209.     EndSelect
  210. EndFunction
  211.  
  212. Function add_stat_list()
  213.     Local name$ = GadgetText(e_stat_name)
  214.     Local usgn = Int(GadgetText(e_stat_usgn))
  215.     Local score = Int(GadgetText(e_stat_score))
  216.     Local kills = Int(GadgetText(e_stat_kills))
  217.     Local deaths = Int(GadgetText(e_stat_deaths))
  218.     Local time = Int(GadgetText(e_stat_time))
  219.    
  220.     If usgn > 0 Then
  221.         If Len(name) > 0 Then
  222.             If Not TStat.FindUSGN(usgn) Then
  223.                 TStat.AddList(name, usgn, score, kills, deaths, time)
  224.                 AddGadgetItem(stat_player_menu, name, 0, -1, "#"+usgn)
  225.                 Return True
  226.             Else
  227.                 Notify("This U.S.G.N ID is already in the stats list!", True)
  228.             EndIf
  229.         Else
  230.             Notify("You must specify a name!", True)
  231.         EndIf
  232.     Else
  233.         Notify("You must specify an usgn id!", True)
  234.     EndIf
  235. EndFunction
  236.  
  237. AppTitle = "Stats Editor by Starkkz"
  238.  
  239. Global current_stats_id
  240. Global current_stats:TStat
  241.  
  242. Global main_window:TGadget = CreateWindow("Stats Editor by Starkkz", DesktopWidth()/2 - 150, DesktopHeight()/2 - 200, 300, 400, Null, WINDOW_TITLEBAR)
  243. Global button_load_stats:TGadget = CreateButton("Load Stats", 10, 15, 100, 25, main_window)
  244. Global button_save_stats:TGadget = CreateButton("Save Stats", 10, 45, 100, 25, main_window)
  245.  
  246. Global entry_remove_button:TGadget = CreateButton("Remove Entry", 120, 45, 100, 25, main_window)
  247. Global create_entry_button:TGadget = CreateButton("Create Entry", 120, 15, 100, 25, main_window)
  248.  
  249. Global stat_player_menu:TGadget = CreateComboBox(120, 75, 165, 25, main_window)
  250.  
  251. Global entry_window:TGadget = CreateWindow("Create Entry", DesktopWidth()/2 - 125, DesktopHeight()/2 - 175, 250, 350, main_window, WINDOW_TITLEBAR|WINDOW_HIDDEN)
  252. Global add_entry_button:TGadget = CreateButton("Add Entry", 10, 285, 100, 25, entry_window)
  253.  
  254. SetGadgetAlpha(entry_window, 0.9)
  255.  
  256. DisableGadget(add_entry_button)
  257. DisableGadget(create_entry_button)
  258. DisableGadget(button_save_stats)
  259. DisableGadget(entry_remove_button)
  260.  
  261. CreateLabel("Player Name", 10, 90, 100, 20, main_window)
  262. CreateLabel("USGN ID", 10, 135, 100, 20, main_window)
  263. CreateLabel("Score", 10, 180, 100, 20, main_window)
  264. CreateLabel("Kills", 10, 225, 100, 20, main_window)
  265. CreateLabel("Deaths", 10, 270, 100, 20, main_window)
  266. CreateLabel("Server time", 10, 315, 100, 20, main_window)
  267.  
  268. CreateLabel("Player Name", 10, 10, 100, 20, entry_window)
  269. CreateLabel("USGN ID", 10, 55, 100, 20, entry_window)
  270. CreateLabel("Score", 10, 100, 100, 20, entry_window)
  271. CreateLabel("Kills", 10, 145, 100, 20, entry_window)
  272. CreateLabel("Deaths", 10, 190, 100, 20, entry_window)
  273. CreateLabel("Server time", 10, 235, 100, 20, entry_window)
  274.  
  275. Global stat_name:TGadget = CreateTextField(10, 110, 170, 25, main_window)
  276. Global stat_usgn:TGadget = CreateTextField(10, 155, 170, 25, main_window)
  277. Global stat_score:TGadget = CreateTextField(10, 200, 170, 25, main_window)
  278. Global stat_kills:TGadget = CreateTextField(10, 245, 170, 25, main_window)
  279. Global stat_deaths:TGadget = CreateTextField(10, 290, 170, 25, main_window)
  280. Global stat_time:TGadget = CreateTextField(10, 335, 170, 25, main_window)
  281.  
  282. Global e_stat_name:TGadget = CreateTextField(10, 30, 170, 25, entry_window)
  283. Global e_stat_usgn:TGadget = CreateTextField(10, 75, 170, 25, entry_window)
  284. Global e_stat_score:TGadget = CreateTextField(10, 120, 170, 25, entry_window)
  285. Global e_stat_kills:TGadget = CreateTextField(10, 165, 170, 25, entry_window)
  286. Global e_stat_deaths:TGadget = CreateTextField(10, 210, 170, 25, entry_window)
  287. Global e_stat_time:TGadget = CreateTextField(10, 255, 170, 25, entry_window)
  288.  
  289. SetGadgetFilter(stat_usgn, TextFieldIntFilter)
  290. SetGadgetFilter(stat_score, TextFieldIntFilter)
  291. SetGadgetFilter(stat_kills, TextFieldIntFilter)
  292. SetGadgetFilter(stat_deaths, TextFieldIntFilter)
  293. SetGadgetFilter(stat_time, TextFieldIntFilter)
  294.  
  295. SetGadgetFilter(e_stat_usgn, TextFieldIntFilter)
  296. SetGadgetFilter(e_stat_score, TextFieldIntFilter)
  297. SetGadgetFilter(e_stat_kills, TextFieldIntFilter)
  298. SetGadgetFilter(e_stat_deaths, TextFieldIntFilter)
  299. SetGadgetFilter(e_stat_time, TextFieldIntFilter)
  300.  
  301. Repeat
  302.     WaitEvent()
  303.     Select EventID()
  304.         Case EVENT_GADGETACTION
  305.             Select EventSource()
  306.                 Case button_load_stats
  307.                     LoadStatsMenu()
  308.                 Case button_save_stats
  309.                     update_stats()
  310.                     SaveStats()
  311.                 Case stat_player_menu
  312.                     update_stats()
  313.                     open_stats_id(EventData())
  314.                 Case entry_remove_button
  315.                     remove_current_entry()
  316.                 Case create_entry_button
  317.                     ShowGadget(entry_window)
  318.                 Case add_entry_button
  319.                     If add_stat_list() Then
  320.                         HideGadget(entry_window)
  321.                        
  322.                         SetGadgetText(e_stat_name, "")
  323.                         SetGadgetText(e_stat_usgn, "")
  324.                         SetGadgetText(e_stat_score, "")
  325.                         SetGadgetText(e_stat_kills, "")
  326.                         SetGadgetText(e_stat_deaths, "")
  327.                         SetGadgetText(e_stat_time, "")
  328.                     EndIf
  329.             EndSelect
  330.         Case EVENT_WINDOWCLOSE
  331.             Select EventSource()
  332.                 Case main_window
  333.                     If Not TStat.saved Then
  334.                         If Proceed("Stats haven't been saved, do you wish to save them?") Then
  335.                             SaveStats()
  336.                         EndIf
  337.                     EndIf
  338.                     End
  339.                 Case entry_window
  340.                     HideGadget(entry_window)
  341.                    
  342.                     SetGadgetText(e_stat_name, "")
  343.                     SetGadgetText(e_stat_usgn, "")
  344.                     SetGadgetText(e_stat_score, "")
  345.                     SetGadgetText(e_stat_kills, "")
  346.                     SetGadgetText(e_stat_deaths, "")
  347.                     SetGadgetText(e_stat_time, "")
  348.             EndSelect
  349.     EndSelect
  350. Forever
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement