]> Sergey Matveev's repositories - vors.git/blob - cmd/server/room.go
0be24b25d24697e39460d00b3b604f4b11820a72f13202bf289f7552704d8024
[vors.git] / cmd / server / room.go
1 package main
2
3 import (
4         "fmt"
5         "sort"
6         "sync"
7         "time"
8
9         "github.com/dustin/go-humanize"
10         vors "go.stargrave.org/vors/v3/internal"
11 )
12
13 var (
14         Rooms  = map[string]*Room{}
15         RoomsM sync.Mutex
16 )
17
18 type Room struct {
19         name  string
20         key   string
21         peers map[byte]*Peer
22         alive chan struct{}
23 }
24
25 func (room *Room) Stats(now time.Time) []string {
26         sids := make([]int, 0, len(room.peers))
27         for sid := range room.peers {
28                 sids = append(sids, int(sid))
29         }
30         sort.Ints(sids)
31         lines := make([]string, 0, len(sids))
32         for _, sid := range sids {
33                 peer := room.peers[byte(sid)]
34                 if peer == nil {
35                         continue
36                 }
37                 line := fmt.Sprintf(
38                         "%12s  |  %s | Rx/Tx/Bad: %s / %s / %s |  %s / %s",
39                         peer.name,
40                         peer.addr,
41                         humanize.Comma(peer.stats.pktsRx),
42                         humanize.Comma(peer.stats.pktsTx),
43                         humanize.Comma(peer.stats.bads),
44                         humanize.IBytes(peer.stats.bytesRx),
45                         humanize.IBytes(peer.stats.bytesTx),
46                 )
47                 if peer.muted {
48                         line += "  |  " + vors.CRed + "MUTE" + vors.CReset
49                 }
50                 if peer.stats.last.Add(vors.ScreenRefresh).After(now) {
51                         line += "  |  " + vors.CGreen + "TALK" + vors.CReset
52                 }
53                 lines = append(lines, line)
54         }
55         return lines
56 }