-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcomment_console.go
179 lines (158 loc) · 6.23 KB
/
comment_console.go
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
package main
import (
"fmt"
"strings"
"time"
"github.com/gdamore/tcell/v2"
"github.com/mattn/go-runewidth"
)
var showConfigLines = false
func dumpTable() {
fmt.Println()
for _, line := range sessionTable() {
fmt.Println(line)
}
}
func printTable() {
table := sessionTableDecorated(true)
tHeight := len(table)
tWidth := len([]rune(table[0]))
title := locale.Label("title") //"FPVCombat Announcer"
state := ""
switch {
case session.IsRegistration():
elapsed := time.Since(session.Timestamps.RegStarted)
state = fmt.Sprintf("[R %02d:%02d] %c C %c B %c E", int(elapsed.Minutes()), int(elapsed.Seconds())%60, tcell.RuneRArrow, tcell.RuneRArrow, tcell.RuneRArrow)
case session.IsCountdown():
timer := time.Since(session.Timestamps.RegEnded)
if config.DurationCountdown > 0 {
timer = time.Duration(config.DurationCountdown+2)*time.Second - timer // adding 2 seconds to make it more sync with voice over
if int(timer.Seconds()) > config.DurationCountdown {
timer = time.Duration(config.DurationCountdown) * time.Second
}
}
state = fmt.Sprintf("[R] %c [C 00:%02d] %c B %c E", tcell.RuneRArrow, int(timer.Seconds()), tcell.RuneRArrow, tcell.RuneRArrow)
case session.IsBattle():
timer := time.Since(session.Timestamps.BatStarted)
if config.DurationBattle > 0 {
timer = time.Duration(config.DurationBattle)*time.Minute - timer
}
timerFormatted := fmt.Sprintf("%02d:%02d", int(timer.Minutes()), int(timer.Seconds())%60)
state = fmt.Sprintf("R %c C %c [B %s] %c E", tcell.RuneRArrow, tcell.RuneRArrow, timerFormatted, tcell.RuneRArrow)
case session.IsEnded():
state = fmt.Sprintf("R %c C %c B %c [E]", tcell.RuneRArrow, tcell.RuneRArrow, tcell.RuneRArrow)
}
header := fmt.Sprintf("%s%s%s", title, strings.Repeat(" ", tWidth-2-len([]rune(title))-len([]rune(state))), strings.ToUpper(state))
configLines := []string{
locale.Label("configSource") + ":\"" + config.Source + "\" " + locale.Label("configLocale") + ":\"" + config.Locale + "\" " + locale.Label("configSpeech") + ":\"" + config.SpeakCommand + "\"",
}
if showConfigLines {
configLines = []string{
locale.Label("configSource") + ":" + config.Source,
locale.Label("configLocale") + ":" + config.Locale,
locale.Label("configSpeech") + ":" + config.SpeakCommand,
locale.Label("configScoreHit") + ":" + config.ScoreHit,
locale.Label("configScoreDamage") + ":" + config.ScoreDamage,
locale.Label("configCheers") + ":" + onoff(config.SpeakCheers),
locale.Label("configLives") + ":" + onoff(config.SpeakLives),
}
}
cHeight := len(configLines)
bullet := " " + string(tcell.RuneBullet) + " "
keys := "[ESC/Q] " + locale.Label("keysQuit") +
bullet + "[Space] " + locale.Label("keysAdvance") +
bullet + "[R] " + locale.Label("keysRestart") +
bullet + "[C] " + locale.Label("keysToggleCheers") +
bullet + "[L] " + locale.Label("keysToggleLives") +
bullet + "[X] " + locale.Label("keysToggleConfig")
version := "v" + Version
footer := fmt.Sprintf("%s%s%s", keys, strings.Repeat(" ", tWidth-2-len([]rune(keys))-len([]rune(version))), version)
// header
printStr(screen, 0, 0, fmt.Sprintf("┌%s┐", strings.Repeat("─", tWidth)))
printStr(screen, 0, 1, fmt.Sprintf("│ %s │", header))
printStr(screen, 0, 2, fmt.Sprintf("├%s┤", strings.Repeat("─", tWidth)))
// table
for i, line := range table {
printStr(screen, 0, i+3, "│"+line+"│")
}
printStr(screen, 0, 3+tHeight+0, fmt.Sprintf("├%s┤", strings.Repeat("─", tWidth)))
// config
if showConfigLines {
for i, line := range configLines {
parts := strings.SplitN(line, ":", 2)
fmtLine := fmt.Sprintf("%-20s %s", parts[0], parts[1])
padLine := fmt.Sprintf("%s%s", fmtLine, strings.Repeat(" ", tWidth-2-len([]rune(fmtLine))))
printStr(screen, 0, 3+tHeight+i+1, fmt.Sprintf("│ %s │", padLine))
}
} else {
padLine := fmt.Sprintf("%s%s^", configLines[0], strings.Repeat(" ", tWidth-2-1-len([]rune(configLines[0]))))
printStr(screen, 0, 3+tHeight+1, fmt.Sprintf("│ %s │", padLine))
}
printStr(screen, 0, 3+tHeight+cHeight+1, fmt.Sprintf("├%s┤", strings.Repeat("─", tWidth)))
// footer
printStr(screen, 0, 3+tHeight+cHeight+2, fmt.Sprintf("│ %s │", footer))
printStr(screen, 0, 3+tHeight+cHeight+3, fmt.Sprintf("└%s┘", strings.Repeat("─", tWidth)))
screen.Show()
}
func sessionTable() []string {
return sessionTableDecorated(false)
}
func sessionTableDecorated(decorate bool) []string {
table := []string{}
header := fmt.Sprintf(
" ID | %-10s | %-20s | %-16s || %12s | %12s | %10s || %10s ",
strings.TrimSpace(locale.Label("tableName")),
strings.TrimSpace(locale.Label("tableDescription")),
strings.TrimSpace(locale.Label("tableUpdated")),
strings.TrimSpace(locale.Label("tableHits")),
strings.TrimSpace(locale.Label("tableDamage")),
strings.TrimSpace(locale.Label("tableLives")),
strings.TrimSpace(locale.Label("tableScore")),
)
table = append(table, header)
table = append(table, " -- | ---------- | -------------------- | ---------------- || ------------ | ------------ | ---------- || ---------- ")
for _, p := range session.Players {
updated := p.Updated.Format("15:04:05.000")
hits := fmt.Sprintf("%d", p.Hits)
damage := fmt.Sprintf("%d", p.Damage)
if decorate && len(session.Hits) > 0 {
lastHit := session.Hits[len(session.Hits)-1]
if lastHit.AttackerID != nil && p.ID == *lastHit.AttackerID {
hits = fmt.Sprintf("%c %s", tcell.RuneRArrow, hits)
}
if lastHit.VictimID != nil && p.ID == *lastHit.VictimID {
damage = fmt.Sprintf("%c %s", tcell.RuneRArrow, damage)
}
}
table = append(table, fmt.Sprintf(" %X | %-10s | %-20s | %-16s || %12s | %12s | %10d || %10d ", p.ID, printableString(p.Name), printableString(p.Description), updated, hits, damage, p.Lives, p.Score))
}
return table
}
func printableString(str string) string {
result := str
for i := 0; i < len(result); i++ {
if result[i] < 0x20 || result[i] > 0x7E {
result = result[:i] + "?" + result[i+1:]
}
}
return result
}
func printStr(s tcell.Screen, x, y int, str string) {
for _, c := range str {
var comb []rune
w := runewidth.RuneWidth(c)
if w == 0 {
comb = []rune{c}
c = ' '
w = 1
}
s.SetContent(x, y, c, comb, tcell.StyleDefault)
x += w
}
}
func onoff(b bool) string {
if b {
return locale.Label("on")
}
return locale.Label("off")
}