Skip to content

Commit 19117e6

Browse files
committed
add template package, add events, update dependencies
1 parent e6b5dba commit 19117e6

File tree

6 files changed

+248
-19
lines changed

6 files changed

+248
-19
lines changed

.github/workflows/ci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111
fetch-depth: 2
1212
- uses: actions/setup-go@v2
1313
with:
14-
go-version: '1.19'
14+
go-version: '1.21'
1515
- name: Run coverage
1616
run: go test -race -coverpkg=./... -coverprofile=coverage.txt -covermode=atomic ./...
1717
- name: Upload coverage to Codecov

go.mod

+5-5
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@ require (
88
github.com/jmoiron/sqlx v1.3.5
99
github.com/json-iterator/go v1.1.12
1010
github.com/parkervcp/crocgodyl v0.1.1-0.20221213180216-7db80a34cb87
11-
github.com/rs/zerolog v1.31.0
11+
github.com/rs/zerolog v1.32.0
1212
github.com/stretchr/testify v1.8.4
13-
go.mongodb.org/mongo-driver v1.13.1
13+
go.mongodb.org/mongo-driver v1.14.0
1414
)
1515

1616
require (
1717
github.com/davecgh/go-spew v1.1.1 // indirect
1818
github.com/golang/snappy v0.0.4 // indirect
1919
github.com/google/go-cmp v0.5.8 // indirect
20-
github.com/klauspost/compress v1.17.4 // indirect
20+
github.com/klauspost/compress v1.17.7 // indirect
2121
github.com/mattn/go-colorable v0.1.13 // indirect
2222
github.com/mattn/go-isatty v0.0.20 // indirect
2323
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
@@ -28,9 +28,9 @@ require (
2828
github.com/xdg-go/scram v1.1.2 // indirect
2929
github.com/xdg-go/stringprep v1.0.4 // indirect
3030
github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a // indirect
31-
golang.org/x/crypto v0.18.0 // indirect
31+
golang.org/x/crypto v0.19.0 // indirect
3232
golang.org/x/sync v0.6.0 // indirect
33-
golang.org/x/sys v0.16.0 // indirect
33+
golang.org/x/sys v0.17.0 // indirect
3434
golang.org/x/text v0.14.0 // indirect
3535
gopkg.in/yaml.v3 v3.0.1 // indirect
3636
)

go.sum

+10
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ github.com/kisielk/sqlstruct v0.0.0-20201105191214-5f3e10d3ab46/go.mod h1:yyMNCy
2424
github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
2525
github.com/klauspost/compress v1.17.4 h1:Ej5ixsIri7BrIjBkRZLTo6ghwrEtHFk7ijlczPW4fZ4=
2626
github.com/klauspost/compress v1.17.4/go.mod h1:/dCuZOvVtNoHsyb+cuJD3itjs3NbnF6KH9zAO4BDxPM=
27+
github.com/klauspost/compress v1.17.7 h1:ehO88t2UGzQK66LMdE8tibEd1ErmzZjNEqWkjLAKQQg=
28+
github.com/klauspost/compress v1.17.7/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
2729
github.com/lib/pq v1.2.0 h1:LXpIM/LZ5xGFhOpXAQUIMM1HdyqzVYM13zNdjCEEcA0=
2830
github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
2931
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
@@ -50,6 +52,8 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
5052
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
5153
github.com/rs/zerolog v1.31.0 h1:FcTR3NnLWW+NnTwwhFWiJSZr4ECLpqCm6QsEnyvbV4A=
5254
github.com/rs/zerolog v1.31.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
55+
github.com/rs/zerolog v1.32.0 h1:keLypqrlIjaFsbmJOBdB/qvyF8KEtCWHwobLp5l/mQ0=
56+
github.com/rs/zerolog v1.32.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
5357
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
5458
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
5559
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
@@ -66,12 +70,16 @@ github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a/go.mod h1:ul22v+Nro/
6670
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
6771
go.mongodb.org/mongo-driver v1.13.1 h1:YIc7HTYsKndGK4RFzJ3covLz1byri52x0IoMB0Pt/vk=
6872
go.mongodb.org/mongo-driver v1.13.1/go.mod h1:wcDf1JBCXy2mOW0bWHwO/IOYqdca1MPCwDtFu/Z9+eo=
73+
go.mongodb.org/mongo-driver v1.14.0 h1:P98w8egYRjYe3XDjxhYJagTokP/H6HzlsnojRgZRd80=
74+
go.mongodb.org/mongo-driver v1.14.0/go.mod h1:Vzb0Mk/pa7e6cWw85R4F/endUC3u0U9jGcNU603k65c=
6975
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
7076
golang.org/x/crypto v0.0.0-20200302210943-78000ba7a073/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
7177
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
7278
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
7379
golang.org/x/crypto v0.18.0 h1:PGVlW0xEltQnzFZ55hkuX5+KLyrMYhHld1YHO4AKcdc=
7480
golang.org/x/crypto v0.18.0/go.mod h1:R0j02AL6hcrfOiy9T4ZYp/rcWeMxM3L6QYxlOuEG1mg=
81+
golang.org/x/crypto v0.19.0 h1:ENy+Az/9Y1vSrlrvBSyna3PITt4tiZLf7sgCjZBX7Wo=
82+
golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU=
7583
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
7684
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
7785
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
@@ -94,6 +102,8 @@ golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
94102
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
95103
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
96104
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
105+
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
106+
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
97107
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
98108
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
99109
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=

src/go/messagebroker/match.go

+13-13
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ const (
2020
UNWINDIA_MATCH_READY_B
2121
UNWINDIA_MATCH_READY_ALL
2222
UNWINDIA_MATCH_SERVER_READY
23+
UNWINDIA_MATCH_RESULT_UPDATED
24+
UNWINDIA_MATCH_RESULT_FINISHED
2325
UNWINDIA_MATCH_FINISHED
2426
_maxEventid
2527
)
@@ -30,16 +32,20 @@ var EventsName = map[int]string{
3032
2: "UNWINDIA_MATCH_READY_B",
3133
3: "UNWINDIA_MATCH_READY_ALL",
3234
4: "UNWINDIA_MATCH_SERVER_READY",
33-
5: "UNWINDIA_MATCH_FINISHED",
35+
5: "UNWINDIA_MATCH_RESULT_UPDATED",
36+
6: "UNWINDIA_MATCH_RESULT_FINISHED",
37+
7: "UNWINDIA_MATCH_FINISHED",
3438
}
3539

3640
var EventsValue = map[string]MatchEvent{
37-
"UNWINDIA_MATCH_NEW": UNWINDIA_MATCH_NEW,
38-
"UNWINDIA_MATCH_READY_A": UNWINDIA_MATCH_READY_A,
39-
"UNWINDIA_MATCH_READY_B": UNWINDIA_MATCH_READY_B,
40-
"UNWINDIA_MATCH_SERVER_READY": UNWINDIA_MATCH_SERVER_READY,
41-
"UNWINDIA_MATCH_READY_ALL": UNWINDIA_MATCH_READY_ALL,
42-
"UNWINDIA_MATCH_FINISHED": UNWINDIA_MATCH_FINISHED,
41+
EventsName[0]: UNWINDIA_MATCH_NEW,
42+
EventsName[1]: UNWINDIA_MATCH_READY_A,
43+
EventsName[2]: UNWINDIA_MATCH_READY_B,
44+
EventsName[3]: UNWINDIA_MATCH_READY_ALL,
45+
EventsName[4]: UNWINDIA_MATCH_SERVER_READY,
46+
EventsName[4]: UNWINDIA_MATCH_RESULT_UPDATED,
47+
EventsName[4]: UNWINDIA_MATCH_RESULT_FINISHED,
48+
EventsName[5]: UNWINDIA_MATCH_FINISHED,
4349
}
4450

4551
func (e MatchEvent) String() string {
@@ -50,12 +56,6 @@ func (e MatchEvent) String() string {
5056
return strconv.Itoa(int(e))
5157
}
5258

53-
type NewContest struct {
54-
}
55-
type Response struct {
56-
value interface{}
57-
}
58-
5959
// UnmarshalJSON unmarshals b into MatchEvent.
6060
func (e *MatchEvent) UnmarshalJSON(b []byte) error {
6161
// From json.Unmarshaler: By convention, to approximate the behavior of

src/go/template/template.go

+35
Original file line numberDiff line numberDiff line change
@@ -1 +1,36 @@
11
package template
2+
3+
import (
4+
"errors"
5+
"github.com/GSH-LAN/Unwindia_common/src/go/matchservice"
6+
"github.com/rs/zerolog/log"
7+
"strings"
8+
"text/template"
9+
)
10+
11+
func ParseTemplateForMatch(tpl string, matchinfo *matchservice.MatchInfo) (string, error) {
12+
if matchinfo == nil {
13+
return "", errors.New("empty matchinfo")
14+
}
15+
16+
funcs := map[string]any{
17+
"contains": strings.Contains,
18+
"hasPrefix": strings.HasPrefix,
19+
"hasSuffix": strings.HasSuffix,
20+
}
21+
22+
tmpl, err := template.New("match").Option("missingkey=error").Funcs(funcs).Parse(tpl)
23+
if err != nil {
24+
log.Err(err).Msg("Error parsing template")
25+
return "", err
26+
}
27+
28+
parsedTemplate := strings.Builder{}
29+
err = tmpl.Execute(&parsedTemplate, matchinfo)
30+
if err != nil {
31+
log.Err(err).Msg("Error parsing matchinfo into template")
32+
return "", err
33+
}
34+
35+
return parsedTemplate.String(), nil
36+
}

src/go/template/template_test.go

+184
Original file line numberDiff line numberDiff line change
@@ -1 +1,185 @@
11
package template
2+
3+
import (
4+
"github.com/GSH-LAN/Unwindia_common/src/go/matchservice"
5+
"testing"
6+
)
7+
8+
const (
9+
templateText1 = `This match is managed by UNWINDIA
10+
{{if not (and .Team1.Ready .Team2.Ready) -}}
11+
As soon both teams are ready your gameserver will be genereated.
12+
{{ else -}}
13+
{{ if ne .ServerAddress "" -}}
14+
Your server is ready, find the connection details below:
15+
IP-Address: {{ .ServerAddress }}
16+
Password: {{ .ServerPassword }}
17+
RCON-Password: {{ .ServerPasswordMgmt }}
18+
<a href="steam://connect/{{ .ServerAddress -}}/{{ .ServerPassword -}}">connect {{ .ServerAddress -}};password {{ .ServerPassword -}}</a>
19+
{{ else -}}
20+
Since both Teams are ready now your server is getting prepared. This process will take a few minutes.
21+
{{ end -}}
22+
{{ end -}}
23+
`
24+
25+
expectedTemplateText1NewMatch = `This match is managed by UNWINDIA
26+
As soon both teams are ready your gameserver will be genereated.
27+
`
28+
29+
expectedTemplateText1TeamsReady = `This match is managed by UNWINDIA
30+
Since both Teams are ready now your server is getting prepared. This process will take a few minutes.
31+
`
32+
33+
expectedTemplateText1TeamsAndServerReady = `This match is managed by UNWINDIA
34+
Your server is ready, find the connection details below:
35+
IP-Address: 127.0.0.1:27015
36+
Password: password
37+
RCON-Password: secret
38+
<a href="steam://connect/127.0.0.1:27015/password">connect 127.0.0.1:27015;password password</a>
39+
`
40+
41+
templateTextBroken = ` This is a broken template {{ .UnKnownAttribute}} `
42+
)
43+
44+
var matchNew = matchservice.MatchInfo{
45+
Id: "123",
46+
MsID: "456",
47+
Team1: matchservice.Team{
48+
Id: "team1id",
49+
Name: "cool-team",
50+
Players: nil,
51+
Picture: nil,
52+
Ready: false,
53+
},
54+
Team2: matchservice.Team{
55+
Id: "team2id",
56+
Name: "nice-teams",
57+
Players: nil,
58+
Picture: nil,
59+
Ready: false,
60+
},
61+
PlayerAmount: 10,
62+
Game: "csgo",
63+
Map: "",
64+
ServerAddress: "",
65+
ServerPassword: "",
66+
ServerPasswordMgmt: "",
67+
ServerTvAddress: "",
68+
ServerTvPassword: "",
69+
}
70+
71+
var matchTeamsReady = matchservice.MatchInfo{
72+
Id: "123",
73+
MsID: "456",
74+
Team1: matchservice.Team{
75+
Id: "team1id",
76+
Name: "cool-team",
77+
Players: nil,
78+
Picture: nil,
79+
Ready: true,
80+
},
81+
Team2: matchservice.Team{
82+
Id: "team2id",
83+
Name: "nice-teams",
84+
Players: nil,
85+
Picture: nil,
86+
Ready: true,
87+
},
88+
PlayerAmount: 10,
89+
Game: "csgo",
90+
Map: "",
91+
ServerAddress: "",
92+
ServerPassword: "",
93+
ServerPasswordMgmt: "",
94+
ServerTvAddress: "",
95+
ServerTvPassword: "",
96+
}
97+
98+
var matchTeamsAndServerReady = matchservice.MatchInfo{
99+
Id: "123",
100+
MsID: "456",
101+
Team1: matchservice.Team{
102+
Id: "team1id",
103+
Name: "cool-team",
104+
Players: nil,
105+
Picture: nil,
106+
Ready: true,
107+
},
108+
Team2: matchservice.Team{
109+
Id: "team2id",
110+
Name: "nice-teams",
111+
Players: nil,
112+
Picture: nil,
113+
Ready: true,
114+
},
115+
PlayerAmount: 10,
116+
Game: "csgo",
117+
Map: "",
118+
ServerAddress: "127.0.0.1:27015",
119+
ServerPassword: "password",
120+
ServerPasswordMgmt: "secret",
121+
ServerTvAddress: "127.0.0.1:27115",
122+
ServerTvPassword: "tvpassword",
123+
}
124+
125+
func TestParseTemplateForMatch(t *testing.T) {
126+
type args struct {
127+
tpl string
128+
matchinfo *matchservice.MatchInfo
129+
}
130+
tests := []struct {
131+
name string
132+
args args
133+
want string
134+
wantErr bool
135+
}{
136+
{
137+
name: "ok-template_new_match",
138+
args: args{
139+
tpl: templateText1,
140+
matchinfo: &matchNew,
141+
},
142+
want: expectedTemplateText1NewMatch,
143+
wantErr: false,
144+
},
145+
{
146+
name: "ok-template_teams_ready",
147+
args: args{
148+
tpl: templateText1,
149+
matchinfo: &matchTeamsReady,
150+
},
151+
want: expectedTemplateText1TeamsReady,
152+
wantErr: false,
153+
},
154+
{
155+
name: "ok-template_teams_and_server_ready",
156+
args: args{
157+
tpl: templateText1,
158+
matchinfo: &matchTeamsAndServerReady,
159+
},
160+
want: expectedTemplateText1TeamsAndServerReady,
161+
wantErr: false,
162+
},
163+
{
164+
name: "err-template_text_broken",
165+
args: args{
166+
tpl: templateTextBroken,
167+
matchinfo: &matchNew,
168+
},
169+
want: "",
170+
wantErr: true,
171+
},
172+
}
173+
for _, tt := range tests {
174+
t.Run(tt.name, func(t *testing.T) {
175+
got, err := ParseTemplateForMatch(tt.args.tpl, tt.args.matchinfo)
176+
if (err != nil) != tt.wantErr {
177+
t.Errorf("ParseTemplateForMatch() error = %v, wantErr %v", err, tt.wantErr)
178+
return
179+
}
180+
if got != tt.want {
181+
t.Errorf("ParseTemplateForMatch() got = %v, want %v", got, tt.want)
182+
}
183+
})
184+
}
185+
}

0 commit comments

Comments
 (0)