-
Notifications
You must be signed in to change notification settings - Fork 789
/
cl_scoring.lua
562 lines (449 loc) · 16 KB
/
cl_scoring.lua
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
-- Game report
include("cl_awards.lua")
local table = table
local string = string
local vgui = vgui
local pairs = pairs
CLSCORE = {}
CLSCORE.Events = {}
CLSCORE.Scores = {}
CLSCORE.TraitorIDs = {}
CLSCORE.DetectiveIDs = {}
CLSCORE.Players = {}
CLSCORE.StartTime = 0
CLSCORE.Panel = nil
CLSCORE.EventDisplay = {}
local skull_icon = Material("HUD/killicons/default")
surface.CreateFont("WinHuge", {font = "Trebuchet24",
size = 72,
weight = 1000,
shadow = true})
-- so much text here I'm using shorter names than usual
local T = LANG.GetTranslation
local PT = LANG.GetParamTranslation
function CLSCORE:GetDisplay(key, event)
local displayfns = self.EventDisplay[event.id]
if not displayfns then return end
local keyfn = displayfns[key]
if not keyfn then return end
return keyfn(event)
end
function CLSCORE:TextForEvent(e)
return self:GetDisplay("text", e)
end
function CLSCORE:IconForEvent(e)
return self:GetDisplay("icon", e)
end
function CLSCORE:TimeForEvent(e)
local t = e.t - self.StartTime
if t >= 0 then
return util.SimpleTime(t, "%02i:%02i")
else
return " "
end
end
-- Tell CLSCORE how to display an event. See cl_scoring_events for examples.
-- Pass an empty table to keep an event from showing up.
function CLSCORE.DeclareEventDisplay(event_id, event_fns)
-- basic input vetting, can't check returned value types because the
-- functions may be impure
if not tonumber(event_id) then
Error("Event ??? display: invalid event id\n")
end
if (not event_fns) or type(event_fns) != "table" then
Error(Format("Event %d display: no display functions found.\n", event_id))
end
if not event_fns.text then
Error(Format("Event %d display: no text display function found.\n", event_id))
end
if not event_fns.icon then
Error(Format("Event %d display: no icon and tooltip display function found.\n", event_id))
end
CLSCORE.EventDisplay[event_id] = event_fns
end
function CLSCORE:FillDList(dlst)
for k, e in pairs(self.Events) do
local etxt = self:TextForEvent(e)
local eicon, ttip = self:IconForEvent(e)
local etime = self:TimeForEvent(e)
if etxt then
if eicon then
local mat = eicon
eicon = vgui.Create("DImage")
eicon:SetMaterial(mat)
eicon:SetTooltip(ttip)
eicon:SetKeepAspect(true)
eicon:SizeToContents()
end
dlst:AddLine(etime, eicon, " " .. etxt)
end
end
end
function CLSCORE:BuildEventLogPanel(dpanel)
local margin = 10
local w, h = dpanel:GetSize()
local dlist = vgui.Create("DListView", dpanel)
dlist:SetPos(0, 0)
dlist:SetSize(w, h - margin*2)
dlist:SetSortable(true)
dlist:SetMultiSelect(false)
local timecol = dlist:AddColumn(T("col_time"))
local iconcol = dlist:AddColumn("")
local eventcol = dlist:AddColumn(T("col_event"))
iconcol:SetFixedWidth(16)
timecol:SetFixedWidth(40)
-- If sortable is off, no background is drawn for the headers which looks
-- terrible. So enable it, but disable the actual use of sorting.
iconcol.Header:SetDisabled(true)
timecol.Header:SetDisabled(true)
eventcol.Header:SetDisabled(true)
self:FillDList(dlist)
end
function CLSCORE:BuildScorePanel(dpanel)
local margin = 10
local w, h = dpanel:GetSize()
local dlist = vgui.Create("DListView", dpanel)
dlist:SetPos(0, 0)
dlist:SetSize(w, h)
dlist:SetSortable(true)
dlist:SetMultiSelect(false)
dlist:SetPaintBackground(false)
local colnames = {"", "col_player", "col_role", "col_kills1", "col_kills2", "col_points", "col_team", "col_total"}
for k, name in pairs(colnames) do
if name == "" then
-- skull icon column
local c = dlist:AddColumn("")
c:SetFixedWidth(18)
else
dlist:AddColumn(T(name))
end
end
-- the type of win condition triggered is relevant for team bonus
local wintype = WIN_NONE
for i=#self.Events, 1, -1 do
local e = self.Events[i]
if e.id == EVENT_FINISH then
wintype = e.win
break
end
end
local scores = self.Scores
local nicks = self.Players
local bonus = ScoreTeamBonus(scores, wintype)
for id, s in pairs(scores) do
if id != -1 then
local was_traitor = s.was_traitor
local role = was_traitor and T("traitor") or (s.was_detective and T("detective") or "")
local surv = ""
if s.deaths > 0 then
surv = vgui.Create("ColoredBox", dlist)
surv:SetColor(Color(150, 50, 50))
surv:SetBorder(false)
surv:SetSize(18,18)
local skull = vgui.Create("DImage", surv)
skull:SetMaterial(skull_icon)
skull:SetTooltip("Dead")
skull:SetKeepAspect(true)
skull:SetSize(18,18)
end
local points_own = KillsToPoints(s, was_traitor)
local points_team = (was_traitor and bonus.traitors or bonus.innos)
local points_total = points_own + points_team
local l = dlist:AddLine(surv, nicks[id], role, s.innos, s.traitors, points_own, points_team, points_total)
-- center align
for k, col in pairs(l.Columns) do
col:SetContentAlignment(5)
end
-- when sorting on the column showing survival, we would get an error
-- because images can't be sorted, so instead hack in a dummy value
local surv_col = l.Columns[1]
if surv_col then
surv_col.Value = type(surv_col.Value) == "Panel" and "1" or "0"
end
end
end
dlist:SortByColumn(6)
end
function CLSCORE:AddAward(y, pw, award, dpanel)
local nick = award.nick
local text = award.text
local title = string.upper(award.title)
local titlelbl = vgui.Create("DLabel", dpanel)
titlelbl:SetText(title)
titlelbl:SetFont("TabLarge")
titlelbl:SizeToContents()
local tiw, tih = titlelbl:GetSize()
local nicklbl = vgui.Create("DLabel", dpanel)
nicklbl:SetText(nick)
nicklbl:SetFont("DermaDefaultBold")
nicklbl:SizeToContents()
local nw, nh = nicklbl:GetSize()
local txtlbl = vgui.Create("DLabel", dpanel)
txtlbl:SetText(text)
txtlbl:SetFont("DermaDefault")
txtlbl:SizeToContents()
local tw, th = txtlbl:GetSize()
titlelbl:SetPos((pw - tiw) / 2, y)
y = y + tih + 2
local fw = nw + tw + 5
local fx = ((pw - fw) / 2)
nicklbl:SetPos(fx, y)
txtlbl:SetPos(fx + nw + 5, y)
y = y + nh
return y
end
-- double check that we have no nils
local function ValidAward(a)
return a and a.nick and a.text and a.title and a.priority
end
local wintitle = {
[WIN_TRAITOR] = {txt = "hilite_win_traitors", c = Color(190, 5, 5, 255)},
[WIN_INNOCENT] = {txt = "hilite_win_innocent", c = Color(5, 190, 5, 255)}
}
function CLSCORE:BuildHilitePanel(dpanel)
local w, h = dpanel:GetSize()
local title = wintitle[WIN_INNOCENT]
local endtime = self.StartTime
for i=#self.Events, 1, -1 do
local e = self.Events[i]
if e.id == EVENT_FINISH then
endtime = e.t
-- when win is due to timeout, innocents win
local wintype = e.win
if wintype == WIN_TIMELIMIT then wintype = WIN_INNOCENT end
title = wintitle[wintype]
break
end
end
local roundtime = endtime - self.StartTime
local numply = table.Count(self.Players)
local numtr = table.Count(self.TraitorIDs)
local bg = vgui.Create("ColoredBox", dpanel)
bg:SetColor(Color(50, 50, 50, 255))
bg:SetSize(w,h)
bg:SetPos(0,0)
local winlbl = vgui.Create("DLabel", dpanel)
winlbl:SetFont("WinHuge")
winlbl:SetText( T(title.txt) )
winlbl:SetTextColor(COLOR_WHITE)
winlbl:SizeToContents()
local xwin = (w - winlbl:GetWide())/2
local ywin = 30
winlbl:SetPos(xwin, ywin)
bg.PaintOver = function()
draw.RoundedBox(8, xwin - 15, ywin - 5, winlbl:GetWide() + 30, winlbl:GetTall() + 10, title.c)
end
local ysubwin = ywin + winlbl:GetTall()
local partlbl = vgui.Create("DLabel", dpanel)
local plytxt = PT(numtr == 1 and "hilite_players2" or "hilite_players1",
{numplayers = numply, numtraitors = numtr})
partlbl:SetText(plytxt)
partlbl:SizeToContents()
partlbl:SetPos(xwin, ysubwin + 8)
local timelbl = vgui.Create("DLabel", dpanel)
timelbl:SetText(PT("hilite_duration", {time= util.SimpleTime(roundtime, "%02i:%02i")}))
timelbl:SizeToContents()
timelbl:SetPos(xwin + winlbl:GetWide() - timelbl:GetWide(), ysubwin + 8)
-- Awards
local wa = math.Round(w * 0.9)
local ha = h - ysubwin - 40
local xa = (w - wa) / 2
local ya = h - ha
local awardp = vgui.Create("DPanel", dpanel)
awardp:SetSize(wa, ha)
awardp:SetPos(xa, ya)
awardp:SetPaintBackground(false)
-- Before we pick awards, seed the rng in a way that is the same on all
-- clients. We can do this using the round start time. To make it a bit more
-- random, involve the round's duration too.
math.randomseed(self.StartTime + endtime)
-- Attempt to generate every award, then sort the succeeded ones based on
-- priority/interestingness
local award_choices = {}
for k, afn in pairs(AWARDS) do
local a = afn(self.Events, self.Scores, self.Players, self.TraitorIDs, self.DetectiveIDs)
if ValidAward(a) then
table.insert(award_choices, a)
end
end
local num_choices = table.Count(award_choices)
local max_awards = 5
-- sort descending by priority
table.SortByMember(award_choices, "priority")
-- put the N most interesting awards in the menu
for i=1,max_awards do
local a = award_choices[i]
if a then
self:AddAward((i - 1) * 42, wa, a, awardp)
end
end
end
function CLSCORE:ShowPanel()
local margin = 15
local dpanel = vgui.Create("DFrame")
local w, h = 700, 500
dpanel:SetSize(700, 500)
dpanel:Center()
dpanel:SetTitle(T("report_title"))
dpanel:SetVisible(true)
dpanel:ShowCloseButton(true)
dpanel:SetMouseInputEnabled(true)
dpanel:SetKeyboardInputEnabled(true)
dpanel.OnKeyCodePressed = util.BasicKeyHandler
-- keep it around so we can reopen easily
dpanel:SetDeleteOnClose(false)
self.Panel = dpanel
local dbut = vgui.Create("DButton", dpanel)
local bw, bh = 100, 25
dbut:SetSize(bw, bh)
dbut:SetPos(w - bw - margin, h - bh - margin/2)
dbut:SetText(T("close"))
dbut.DoClick = function() dpanel:Close() end
local dsave = vgui.Create("DButton", dpanel)
dsave:SetSize(bw,bh)
dsave:SetPos(margin, h - bh - margin/2)
dsave:SetText(T("report_save"))
dsave:SetTooltip(T("report_save_tip"))
dsave:SetConsoleCommand("ttt_save_events")
local dtabsheet = vgui.Create("DPropertySheet", dpanel)
dtabsheet:SetPos(margin, margin + 15)
dtabsheet:SetSize(w - margin*2, h - margin*3 - bh)
local padding = dtabsheet:GetPadding()
-- Highlight tab
local dtabhilite = vgui.Create("DPanel", dtabsheet)
dtabhilite:SetPaintBackground(false)
dtabhilite:StretchToParent(padding,padding,padding,padding)
self:BuildHilitePanel(dtabhilite)
dtabsheet:AddSheet(T("report_tab_hilite"), dtabhilite, "icon16/star.png", false, false, T("report_tab_hilite_tip"))
-- Event log tab
local dtabevents = vgui.Create("DPanel", dtabsheet)
-- dtab1:SetSize(650, 450)
dtabevents:StretchToParent(padding, padding, padding, padding)
self:BuildEventLogPanel(dtabevents)
dtabsheet:AddSheet(T("report_tab_events"), dtabevents, "icon16/application_view_detail.png", false, false, T("report_tab_events_tip"))
-- Score tab
local dtabscores = vgui.Create("DPanel", dtabsheet)
dtabscores:SetPaintBackground(false)
dtabscores:StretchToParent(padding, padding, padding, padding)
self:BuildScorePanel(dtabscores)
dtabsheet:AddSheet(T("report_tab_scores"), dtabscores, "icon16/user.png", false, false, T("report_tab_scores_tip"))
dpanel:MakePopup()
-- makepopup grabs keyboard, whereas we only need mouse
dpanel:SetKeyboardInputEnabled(false)
end
function CLSCORE:ClearPanel()
if self.Panel then
-- move the mouse off any tooltips and then remove the panel next tick
-- we need this hack as opposed to just calling Remove because gmod does
-- not offer a means of killing the tooltip, and doesn't clean it up
-- properly on Remove
gui.SetMousePos( ScrW()/2, ScrH()/2 )
local pnl = self.Panel
timer.Simple(0, function() pnl:Remove() end)
end
end
function CLSCORE:SaveLog()
if self.Events and #self.Events <= 0 then
chat.AddText(COLOR_WHITE, T("report_save_error"))
return
end
local logdir = "ttt/logs"
if not file.IsDir(logdir, "DATA") then
file.CreateDir(logdir)
end
local logname = logdir .. "/ttt_events_" .. os.time() .. ".txt"
local log = "Trouble in Terrorist Town - Round Events Log\n".. string.rep("-", 50) .."\n"
log = log .. string.format("%s | %-25s | %s\n", " TIME", "TYPE", "WHAT HAPPENED") .. string.rep("-", 50) .."\n"
for _, e in pairs(self.Events) do
local etxt = self:TextForEvent(e)
local etime = self:TimeForEvent(e)
local _, etype = self:IconForEvent(e)
if etxt then
log = log .. string.format("%s | %-25s | %s\n", etime, etype, etxt)
end
end
file.Write(logname, log)
chat.AddText(COLOR_WHITE, T("report_save_result"), COLOR_GREEN, " /garrysmod/data/" .. logname)
end
function CLSCORE:Reset()
self.Events = {}
--self.StoredEvents = nil
self.TraitorIDs = {}
self.DetectiveIDs = {}
self.Scores = {}
self.Players = {}
self.RoundStarted = 0
self:ClearPanel()
end
function CLSCORE:Init(events)
-- Get start time and traitors
local starttime = nil
local traitors = nil
local detectives = nil
for k, e in pairs(events) do
if e.id == EVENT_GAME and e.state == ROUND_ACTIVE then
starttime = e.t
elseif e.id == EVENT_SELECTED then
traitors = e.traitor_ids
detectives = e.detective_ids
end
if starttime and traitors then
break
end
end
-- Get scores and players
local scores = {}
local nicks = {}
for k, e in pairs(events) do
if e.id == EVENT_SPAWN then
scores[e.sid] = ScoreInit()
nicks[e.sid] = e.ni
end
end
scores = ScoreEventLog(events, scores, traitors, detectives)
self.Players = nicks
self.Scores = scores
self.TraitorIDs = traitors
self.DetectiveIDs = detectives
self.StartTime = starttime
self.Events = events
end
function CLSCORE:ReportEvents(events)
self:Reset()
self:Init(events)
self:ShowPanel()
end
function CLSCORE:Reopen()
if self.Panel and self.Panel:IsValid() and not self.Panel:IsVisible() then
self.Panel:SetVisible(true)
end
end
local buff = ""
local function ReceiveReportStream(len)
local cont = net.ReadBit() == 1
buff = buff .. net.ReadString()
if cont then
return
else
-- do stuff with buffer contents
local json_events = buff -- util.Decompress(buff)
if not json_events then
ErrorNoHalt("Round report decompression failed!\n")
else
-- convert the json string back to a table
local events = util.JSONToTable(json_events)
if istable(events) then
CLSCORE:ReportEvents(events)
else
ErrorNoHalt("Round report event decoding failed!\n")
end
end
-- flush
buff = ""
end
end
net.Receive("TTT_ReportStream", ReceiveReportStream)
local function SaveLog(ply, cmd, args)
CLSCORE:SaveLog()
end
concommand.Add("ttt_save_events", SaveLog)