Skip to content
Snippets Groups Projects
Commit 6f29769d authored by Lucy ⁢'s avatar Lucy ⁢
Browse files

Merge branch 'fix-30' into 'master'

added autoreload of the leaderboard

Closes #30

See merge request !14
parents b2c2511b da1a5f20
No related branches found
No related tags found
1 merge request!14added autoreload of the leaderboard
Pipeline #1791 passed with warnings
...@@ -8,6 +8,7 @@ require ( ...@@ -8,6 +8,7 @@ require (
github.com/gorilla/mux v1.7.3 github.com/gorilla/mux v1.7.3
github.com/gorilla/securecookie v1.1.1 github.com/gorilla/securecookie v1.1.1
github.com/gorilla/sessions v1.2.0 github.com/gorilla/sessions v1.2.0
github.com/gorilla/websocket v1.4.1
github.com/mattn/go-sqlite3 v1.11.0 github.com/mattn/go-sqlite3 v1.11.0
golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586 golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586
xorm.io/core v0.7.0 xorm.io/core v0.7.0
......
{{ template "header" . }} {{ template "header" . }}
<main class="flexcell gridcontainer-leader"> <main class="flexcell gridcontainer-leader">
<div id="table" class="col-0" style="width: 20%;"> <div id="table" class="col-0 row-0" style="width: 20%;">
<table> <table>
<tbody>
<tr> <tr>
<th>Name</th> <th>Name</th>
<th>Points</th> <th>Points</th>
...@@ -12,22 +13,27 @@ ...@@ -12,22 +13,27 @@
<td>{{ .Points }}</td> <td>{{ .Points }}</td>
</tr> </tr>
{{ end }} {{ end }}
</tbody>
</table> </table>
</div> </div>
<div class="col-1" style="height: 100%;"> <div class="col-1 row-0">
<canvas id="graph"></canvas> <canvas id="graph"></canvas>
</div> </div>
<script src="static/Chart.bundle.min.js" type="text/javascript"></script> <script src="static/Chart.bundle.min.js" type="text/javascript"></script>
<script> <script>
var score = {name: [{{ range .AllUsers }}{{ .DisplayName }},{{end}}], points: [ { label: "Points", data: [{{ range .AllUsers }}{{ .Points }},{{end}}]}] };
var chart;
(function(){ (function(){
var c = document.getElementById("graph").getContext('2d'); var c = document.getElementById("graph").getContext('2d');
c.height = 800; c.height = 800;
var chart = new Chart(c, {
chart = new Chart(c, {
type: 'horizontalBar', type: 'horizontalBar',
data: { data: {
labels: [{{ range .AllUsers }}{{ .DisplayName }},{{end}}], labels: score.name,
datasets: [ { label: "Points", data: [{{ range .AllUsers }}{{ .Points }},{{end}}]}] datasets: score.points
}, },
options: { options: {
scales: { scales: {
...@@ -45,6 +51,38 @@ ...@@ -45,6 +51,38 @@
responsive: true, responsive: true,
}, },
}); });
ws = new WebSocket("ws://"+window.location.host+"/ws");
ws.onopen = function() {
// Web Socket is connected, send data uting send()
console.log("ws connected");
};
ws.onclose = function() {
alert("WS Disconnected, reload the page")
};
ws.onmessage = (evt)=>{
var rec = evt.data;
console.log(evt.data);
score = JSON.parse(rec);
chart.data = {
labels: score.name,
datasets: [{data: score.points, label: "Solved Challenges" }]
};
chart.update();
table = document.getElementsByTagName("tbody")[0];
table.innerHTML = "<tr><th>Name</th><th>Points</th></tr>";
for(i=0; i < score.name.length; i++){
row = table.insertRow();
namecell = row.insertCell();
namecell.innerHTML = score.name[i];
pointcell = row.insertCell();
pointcell.innerHTML = score.points[i];
}
};
})(); })();
</script> </script>
</main> </main>
......
package wtfd
import (
"encoding/json"
"github.com/gorilla/websocket"
"log"
"net/http"
)
var (
serverChan = make(chan chan string, 4)
messageChan = make(chan string, 1)
upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
)
func leaderboardMessageServer(serverChan chan chan string) {
var clients []chan string
// And now we listen to new clients and new messages:
for {
select {
case client, _ := <-serverChan:
clients = append(clients, client)
case msg, _ := <-messageChan:
// Send the uptime to all connected clients:
for _, c := range clients {
c <- msg
}
}
}
}
func leaderboardServer(serverChan chan chan string) {
var clients []chan string
for {
select {
case client, _ := <-serverChan:
clients = append(clients, client)
}
}
}
func leaderboardWS(w http.ResponseWriter, r *http.Request) {
ws, err := upgrader.Upgrade(w, r, nil)
if err != nil {
if _, ok := err.(websocket.HandshakeError); !ok {
log.Println(err)
}
return
}
client := make(chan string, 1)
serverChan <- client // i have no idea what this go magic is
for {
select {
case text, _ := <-client:
writer, _ := ws.NextWriter(websocket.TextMessage)
writer.Write([]byte(text))
writer.Close()
}
}
}
func updateScoreboard() error {
log.Printf("Scoreboard Update\n")
type userNamePoints struct {
Name []string `json:"name"`
Points []int `json:"points"`
}
var name []string
var points []int
allu, err := ormAllUsersSortedByPoints()
if err != nil {
log.Printf("Scoreboard Update Error: %v\n", err)
return err
}
for _, u := range allu {
name = append(name, u.DisplayName)
points = append(points, u.Points)
}
json, err := json.Marshal(&userNamePoints{Name: name, Points: points})
if err != nil {
log.Printf("Scoreboard Update Error: %v\n", err)
return err
}
messageChan <- string(json)
log.Printf("Scoreboard Update String: %s\n", string(json))
return nil
}
package wtfd package wtfd
import ( import (
"encoding/base64"
"encoding/gob" "encoding/gob"
"encoding/json" "encoding/json"
"encoding/base64"
"errors" "errors"
"fmt" "fmt"
"github.com/gomarkdown/markdown" "github.com/gomarkdown/markdown"
"github.com/gorilla/mux" "github.com/gorilla/mux"
"github.com/gorilla/sessions"
"github.com/gorilla/securecookie" "github.com/gorilla/securecookie"
"github.com/gorilla/sessions"
"golang.org/x/crypto/bcrypt" "golang.org/x/crypto/bcrypt"
"html/template" "html/template"
"io/ioutil" "io/ioutil"
...@@ -497,6 +497,7 @@ func submitFlag(w http.ResponseWriter, r *http.Request) { ...@@ -497,6 +497,7 @@ func submitFlag(w http.ResponseWriter, r *http.Request) {
} }
_, _ = fmt.Fprintf(w, "correct") _, _ = fmt.Fprintf(w, "correct")
_ = updateScoreboard()
} else { } else {
_, _ = fmt.Fprintf(w, "not correct") _, _ = fmt.Fprintf(w, "not correct")
...@@ -534,6 +535,7 @@ func register(w http.ResponseWriter, r *http.Request) { ...@@ -534,6 +535,7 @@ func register(w http.ResponseWriter, r *http.Request) {
} else { } else {
_ = ormNewUser(u) _ = ormNewUser(u)
login(w, r) login(w, r)
_ = updateScoreboard()
} }
...@@ -732,6 +734,7 @@ func Server() error { ...@@ -732,6 +734,7 @@ func Server() error {
if err != nil { if err != nil {
return err return err
} }
go leaderboardMessageServer(serverChan)
// Http sturf // Http sturf
r := mux.NewRouter() r := mux.NewRouter()
r.HandleFunc("/", mainpage) r.HandleFunc("/", mainpage)
...@@ -741,6 +744,7 @@ func Server() error { ...@@ -741,6 +744,7 @@ func Server() error {
r.HandleFunc("/logout", logout) r.HandleFunc("/logout", logout)
r.HandleFunc("/register", register) r.HandleFunc("/register", register)
r.HandleFunc("/submitflag", submitFlag) r.HandleFunc("/submitflag", submitFlag)
r.HandleFunc("/ws", leaderboardWS)
r.HandleFunc("/{chall}", mainpage) r.HandleFunc("/{chall}", mainpage)
r.HandleFunc("/detailview/{chall}", detailview) r.HandleFunc("/detailview/{chall}", detailview)
r.HandleFunc("/solutionview/{chall}", solutionview) r.HandleFunc("/solutionview/{chall}", solutionview)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment