Skip to content

Commit f7e8dd5

Browse files
committed
add chjallenge lib
1 parent bae8d8e commit f7e8dd5

File tree

13 files changed

+149
-116
lines changed

13 files changed

+149
-116
lines changed

.github/FUNDING.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ community_bridge: # Replace with a single Community Bridge project-name e.g., cl
99
liberapay: # Replace with a single Liberapay username
1010
issuehunt: # Replace with a single IssueHunt username
1111
otechie: # Replace with a single Otechie username
12-
custom: ['https://www.paypal.com/donate?hosted_button_id=WF2CRKMVKH2J8']
12+
custom: [ 'https://www.paypal.com/donate?hosted_button_id=WF2CRKMVKH2J8' ]

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,6 @@
1414
# Dependency directories (remove the comment below to include it)
1515
# vendor/
1616
.env
17-
tmp/
17+
tmp/
18+
19+
.idea

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626

2727
## TODO:
2828

29-
- [ ] Algoritmo de respuesta para challenges aleatorios dependiendo de:
29+
- [ ] Algoritmo de respuesta para challenges aleatorios dependiendo de:
3030

31-
1. **level**: ["easy", "medium", "hard"]
31+
1. **level**: ["easy", "medium", "hard"]
3232
2. **challenge_type**: ["algorithm", "concurrency, "database", "web", "cli", "core"]
3333
3. **id** hint: get random id between total records count

botservice/messages/messageCreate.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,20 @@ func MessageCmd(s *discordgo.Session, m *discordgo.MessageCreate) {
1616

1717
if !strings.Contains(m.Content, ".go challenge") {
1818

19-
mysql := storage.RespuestasCmd{}
19+
mysql := storage.ResponseCMD{}
2020
msg, err := mysql.GetCmd(m.Content)
2121
if err != nil {
2222
if err != sql.ErrNoRows {
2323
_, _ = s.ChannelMessageSend(m.ChannelID, `**No sé, error en comando**`)
2424
}
2525
} else {
26-
_, _ = s.ChannelMessageSend(m.ChannelID, msg.Respuesta)
26+
_, _ = s.ChannelMessageSend(m.ChannelID, msg.Res)
2727
}
2828

2929
} else {
3030

31-
mysql := storage.RespuestasChallenge{}
32-
msg, err := mysql.GetChallenge(m.Content)
31+
r := storage.ChallengeResponse{}
32+
msg, err := r.GetChallenge(m.Content)
3333
if err != nil {
3434
if err != sql.ErrNoRows {
3535
_, _ = s.ChannelMessageSend(m.ChannelID, `**Ups, intenta de nuevo, sin espacios extras**`)

botservice/storage/models.go

+29-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,37 @@
11
package storage
22

3-
type RespuestasCmd struct {
4-
ID int
5-
Comando string
6-
Respuesta string
3+
import (
4+
"log"
5+
"strings"
6+
)
7+
8+
type ResponseCMD struct {
9+
ID int
10+
CMD string
11+
Res string
712
}
813

9-
type RespuestasChallenge struct {
14+
type ChallengeResponse struct {
1015
Description string
1116
Level string
1217
ChallengeType string
1318
}
19+
20+
func BuildResponse(res string) *ChallengeResponse {
21+
values := strings.Split(res, ",")
22+
23+
// the response from the lib is
24+
// type,description,level
25+
26+
if len(values) != 3 {
27+
log.Println("response broken from lib")
28+
return &ChallengeResponse{}
29+
}
30+
31+
return &ChallengeResponse{
32+
ChallengeType: values[0],
33+
Description: values[1],
34+
Level: values[2],
35+
}
36+
37+
}

botservice/storage/querys.go

+14-15
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,37 @@
11
package storage
22

33
import (
4+
"github.com/tomiok/challenge-lib"
5+
"log"
46
"math/rand"
7+
"strings"
58
"time"
69
)
710

8-
func (res *RespuestasCmd) GetCmd(cmd string) (*RespuestasCmd, error) {
11+
func (res *ResponseCMD) GetCmd(cmd string) (*ResponseCMD, error) {
912
db := MySqlConn()
1013

1114
q := `SELECT id, comando, respuesta FROM messages WHERE comando = ?`
12-
err := db.QueryRow(q, cmd).Scan(&res.ID, &res.Comando, &res.Respuesta)
15+
err := db.QueryRow(q, cmd).Scan(&res.ID, &res.CMD, &res.Res)
1316
defer db.Close()
1417
if err != nil {
15-
return &RespuestasCmd{}, err
18+
return &ResponseCMD{}, err
1619
}
1720

1821
return res, err
1922
}
2023

21-
func (res *RespuestasChallenge) GetChallenge(cmd string) (*RespuestasChallenge, error) {
22-
db := MySqlConn()
23-
24-
var rows int
25-
_ = db.QueryRow("SELECT COUNT(*) FROM challenges").Scan(&rows)
26-
id := randomId(rows)
24+
func (res *ChallengeResponse) GetChallenge(cmd string) (*ChallengeResponse, error) {
25+
values := strings.Split(cmd, " ")
2726

28-
q := `SELECT description, level, challenge_type FROM challenges WHERE id = ?`
29-
err := db.QueryRow(q, id).Scan(&res.Description, &res.Level, &res.ChallengeType)
30-
defer db.Close()
31-
if err != nil {
32-
return &RespuestasChallenge{}, err
27+
if len(values) == 0 || len(values) == 1 {
28+
log.Println("using default values for find a challenge")
29+
message := challengelib.FindChallenge("easy", "backend")
30+
return BuildResponse(message), nil
3331
}
3432

35-
return res, err
33+
message := challengelib.FindChallenge(values[0], values[1])
34+
return BuildResponse(message), nil
3635
}
3736

3837
func randomId(count int) int {

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ require (
66
github.com/bwmarrin/discordgo v0.23.2
77
github.com/go-sql-driver/mysql v1.6.0
88
github.com/gofiber/fiber/v2 v2.17.0
9+
github.com/tomiok/challenge-lib v0.0.2
910
gorm.io/driver/mysql v1.1.2
1011
gorm.io/gorm v1.21.13
1112
)

go.sum

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
github.com/andybalholm/brotli v1.0.2 h1:JKnhI/XQ75uFBTiuzXpzFrUriDPiZjlOSzh6wXogP0E=
22
github.com/andybalholm/brotli v1.0.2/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu3qAvBg8x/Y=
3+
github.com/buger/jsonparser v1.1.1 h1:2PnMjfWD7wBILjqQbt530v576A/cAbQvEW9gGIpYMUs=
4+
github.com/buger/jsonparser v1.1.1/go.mod h1:6RYKKt7H4d4+iWqouImQ9R2FZql3VbhNgx27UK13J/0=
35
github.com/bwmarrin/discordgo v0.23.2 h1:BzrtTktixGHIu9Tt7dEE6diysEF9HWnXeHuoJEt2fH4=
46
github.com/bwmarrin/discordgo v0.23.2/go.mod h1:c1WtWUGN6nREDmzIpyTp/iD3VYt4Fpx+bVyfBG7JE+M=
57
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
@@ -15,6 +17,8 @@ github.com/jinzhu/now v1.1.2 h1:eVKgfIdy9b6zbWBMgFpfDPoAMifwSZagU9HmEU6zgiI=
1517
github.com/jinzhu/now v1.1.2/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
1618
github.com/klauspost/compress v1.12.2 h1:2KCfW3I9M7nSc5wOqXAlW2v2U6v+w6cbjvbfp+OykW8=
1719
github.com/klauspost/compress v1.12.2/go.mod h1:8dP1Hq4DHOhN9w426knH3Rhby4rFm6D8eO+e+Dq5Gzg=
20+
github.com/tomiok/challenge-lib v0.0.2 h1:rqtZRekEVussXQxV80dHJZHBYLmEAcXsTPAL1qqvUbE=
21+
github.com/tomiok/challenge-lib v0.0.2/go.mod h1:cqcMANTp4P8KnhiOy6EgcryjupblrlzPYECah2V/Sp4=
1822
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
1923
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
2024
github.com/valyala/fasthttp v1.26.0 h1:k5Tooi31zPG/g8yS6o2RffRO2C9B9Kah9SY8j/S7058=

vendor/modules.txt

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# github.com/andybalholm/brotli v1.0.2
22
github.com/andybalholm/brotli
3+
# github.com/buger/jsonparser v1.1.1
4+
github.com/buger/jsonparser
35
# github.com/bwmarrin/discordgo v0.23.2
46
## explicit
57
github.com/bwmarrin/discordgo
@@ -27,8 +29,9 @@ github.com/jinzhu/now
2729
github.com/klauspost/compress/flate
2830
github.com/klauspost/compress/gzip
2931
github.com/klauspost/compress/zlib
30-
# github.com/mattn/go-sqlite3 v1.14.8
32+
# github.com/tomiok/challenge-lib v0.0.1
3133
## explicit
34+
github.com/tomiok/challenge-lib
3235
# github.com/valyala/bytebufferpool v1.0.0
3336
github.com/valyala/bytebufferpool
3437
# github.com/valyala/fasthttp v1.26.0
@@ -51,8 +54,6 @@ golang.org/x/sys/windows
5154
# gorm.io/driver/mysql v1.1.2
5255
## explicit
5356
gorm.io/driver/mysql
54-
# gorm.io/driver/sqlite v1.1.4
55-
## explicit
5657
# gorm.io/gorm v1.21.13
5758
## explicit
5859
gorm.io/gorm

webservice/frontend/css/style.css

+14-14
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
html, body {
2-
height: 100%;
3-
padding-top: 8px;
2+
height: 100%;
3+
padding-top: 8px;
44
}
55

66
.full-height {
7-
height: 75%;
8-
align-content: center;
9-
padding-left: 3%;
10-
padding-right: 3%;
7+
height: 75%;
8+
align-content: center;
9+
padding-left: 3%;
10+
padding-right: 3%;
1111
}
1212

1313
.p-center {
14-
text-align: center;
15-
}
14+
text-align: center;
15+
}
1616

1717
.f20 {
18-
font-size: 20px;
18+
font-size: 20px;
1919
}
2020

2121
.f16 {
22-
font-size: 16px;
22+
font-size: 16px;
2323
}
2424

25-
p{
26-
color: #00001a;
25+
p {
26+
color: #00001a;
2727
}
2828

29-
.alink{
30-
color: #8c1aff;
29+
.alink {
30+
color: #8c1aff;
3131
}

webservice/frontend/index.html

+63-63
Original file line numberDiff line numberDiff line change
@@ -12,73 +12,73 @@
1212
<link rel="stylesheet" href="./css/style.css"/>
1313
</head>
1414
<body>
15-
<div class="container col-sm-6 col-sm-offset-3">
16-
<center>
17-
<h2>Envía un challenge al Gopherbot</h2>
18-
<img src="./img/gophers-latam.png" height="90" alt="Gophers LATAM">
19-
</center>
15+
<div class="container col-sm-6 col-sm-offset-3">
16+
<center>
17+
<h2>Envía un challenge al Gopherbot</h2>
18+
<img src="./img/gophers-latam.png" height="90" alt="Gophers LATAM">
19+
</center>
20+
21+
<form>
22+
<div id="description-group" class="form-group">
23+
<label for="description">Descripción:</label>
24+
<textarea class="form-control" id="description" rows="3" maxlength="5000" required></textarea>
25+
</div>
26+
27+
<div id="level-group" class="form-group">
28+
<label for="level">Nivel de dificultad:</label>
29+
<select class="form-select" id="level" required>
30+
<option value="">- Seleccionar -</option>
31+
<option value="easy">Easy</option>
32+
<option value="medium">Medium</option>
33+
<option value="hard">Hard</option>
34+
</select>
35+
</div>
2036

21-
<form>
22-
<div id="description-group" class="form-group">
23-
<label for="description">Descripción:</label>
24-
<textarea class="form-control" id="description" rows="3" maxlength="5000" required></textarea>
25-
</div>
26-
27-
<div id="level-group" class="form-group">
28-
<label for="level">Nivel de dificultad:</label>
29-
<select class="form-select" id="level" required>
30-
<option value="">- Seleccionar -</option>
31-
<option value="easy">Easy</option>
32-
<option value="medium">Medium</option>
33-
<option value="hard">Hard</option>
34-
</select>
35-
</div>
36-
37-
<div id="challengetype-group" class="form-group">
38-
<label for="challengetype">Tipo relacionado de desafío:</label>
39-
<select class="form-select" id="challengetype" required>
40-
<option value="">- Seleccionar -</option>
41-
<option value="algorithm">Algorithm</option>
42-
<option value="concurrency">Concurrency</option>
43-
<option value="database">Database</option>
44-
<option value="web">Web</option>
45-
<option value="cli">CLI</option>
46-
<option value="core">Core</option>
47-
</select>
48-
</div>
49-
<br>
50-
<center>
51-
<button type="submit" class="btn btn-success">Enviar</button>
52-
</center>
53-
</form>
37+
<div id="challengetype-group" class="form-group">
38+
<label for="challengetype">Tipo relacionado de desafío:</label>
39+
<select class="form-select" id="challengetype" required>
40+
<option value="">- Seleccionar -</option>
41+
<option value="algorithm">Algorithm</option>
42+
<option value="concurrency">Concurrency</option>
43+
<option value="database">Database</option>
44+
<option value="web">Web</option>
45+
<option value="cli">CLI</option>
46+
<option value="core">Core</option>
47+
</select>
48+
</div>
5449
<br>
50+
<center>
51+
<button type="submit" class="btn btn-success">Enviar</button>
52+
</center>
53+
</form>
54+
<br>
5555

56-
<div class="full-height">
57-
<div align="center">
58-
<p class="p-center f16">
59-
<a class="alink"
60-
href="https://discord.com/api/oauth2/authorize?client_id=767979098768408586&permissions=67624001&scope=bot"
61-
target="_blank"><b><u>+add Gopherbot</u></b></a>
62-
</p>
63-
<a href="https://discord.com/api/oauth2/authorize?client_id=767979098768408586&permissions=67624001&scope=bot"
64-
target="_blank"><img src="./img/gopherbot.png" height="100" /></a>
65-
</div>
66-
<br>
67-
<p class="p-center"><b>Información y comandos (en desarrollo)</b></p>
68-
<ul>
69-
<li><b>bot</b> - llamar al bot</li>
70-
<li><b>.go</b> - saludo del bot</li>
71-
<li><b>.go ayuda</b> - ver comandos generales disponibles</li>
72-
<li><b>.go libros</b> - enlaces de libros</li>
73-
<li><b>.go recursos</b> - enlaces de recursos</li>
74-
<li><b>.go challenge</b> - pedir desafios para practicar (en desarrollo)</li>
75-
<li>...</li>
76-
<li><b>Server:</b>
77-
<a class="alink" href="https://discord.io/go-latam" target="_blank">https://discord.io/go-latam</a>
78-
</li>
79-
</ul>
80-
<br>
56+
<div class="full-height">
57+
<div align="center">
58+
<p class="p-center f16">
59+
<a class="alink"
60+
href="https://discord.com/api/oauth2/authorize?client_id=767979098768408586&permissions=67624001&scope=bot"
61+
target="_blank"><b><u>+add Gopherbot</u></b></a>
62+
</p>
63+
<a href="https://discord.com/api/oauth2/authorize?client_id=767979098768408586&permissions=67624001&scope=bot"
64+
target="_blank"><img src="./img/gopherbot.png" height="100"/></a>
8165
</div>
66+
<br>
67+
<p class="p-center"><b>Información y comandos (en desarrollo)</b></p>
68+
<ul>
69+
<li><b>bot</b> - llamar al bot</li>
70+
<li><b>.go</b> - saludo del bot</li>
71+
<li><b>.go ayuda</b> - ver comandos generales disponibles</li>
72+
<li><b>.go libros</b> - enlaces de libros</li>
73+
<li><b>.go recursos</b> - enlaces de recursos</li>
74+
<li><b>.go challenge</b> - pedir desafios para practicar (en desarrollo)</li>
75+
<li>...</li>
76+
<li><b>Server:</b>
77+
<a class="alink" href="https://discord.io/go-latam" target="_blank">https://discord.io/go-latam</a>
78+
</li>
79+
</ul>
80+
<br>
8281
</div>
82+
</div>
8383
</body>
8484
</html>

0 commit comments

Comments
 (0)