Skip to content

Commit 7e591d7

Browse files
make ramstore available for same puzzle but different uuids (#7)
1 parent b525344 commit 7e591d7

File tree

5 files changed

+22
-9
lines changed

5 files changed

+22
-9
lines changed

backend/TODO

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
* implement a way to store current unsolved puzzle
2-
* check that solved api call uuid is found in the seen list
1+
* implement a way to store current unsolved puzzle

backend/main.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ func loadPage(title string) *Page {
4141
func loadPuzzleHandle(w http.ResponseWriter, r *http.Request){
4242
if(r.Method != "GET"){http.Error(w, "Invalid", http.StatusMethodNotAllowed); return;}
4343

44+
query := r.URL.Query()
45+
uuid := query.Get("u")
46+
if uuid == "" {
47+
w.Write([]byte("false"))
48+
}
49+
4450
puzzle, err := logic.GetRandomPuzzle()
4551
if err != nil {
4652
http.Error(w, err.Error(), http.StatusNotFound)
@@ -52,7 +58,7 @@ func loadPuzzleHandle(w http.ResponseWriter, r *http.Request){
5258

5359
w.Write([]byte(`{"error": "`+err.Error()+`"}`))
5460
}
55-
ramstore.SetToRamStore(puzzle)
61+
ramstore.SetToRamStore(puzzle, uuid)
5662
w.Header().Set("Content-Type", "application/json")
5763
w.Write(serial)
5864
}
@@ -63,8 +69,13 @@ func solvedHandler(w http.ResponseWriter, r *http.Request){
6369
query := r.URL.Query()
6470
puzzleId := query.Get("pid")
6571
hash := query.Get("h")
72+
uuid := query.Get("u")
6673

67-
solved := ramstore.CheckRamStore(puzzleId, hash)
74+
if hash == "" || puzzleId == "" || uuid == "" {
75+
w.Write([]byte("false"))
76+
}
77+
78+
solved := ramstore.CheckRamStore(fmt.Sprintf("%s/%s", puzzleId, uuid), hash)
6879

6980
if solved {
7081
go logic.IncrementSolvedCounter(puzzleId)
@@ -79,6 +90,9 @@ func seenHandler(w http.ResponseWriter, r *http.Request) {
7990

8091
query := r.URL.Query()
8192
pid := query.Get("pid")
93+
if pid == "" {
94+
w.Write([]byte("false"))
95+
}
8296

8397
cookie, err := r.Cookie("chess_uuid")
8498
if err != nil {

backend/ramstore/ramfunc.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import (
55
"github.com/ahmed-debbech/go_chess_puzzle/backend/logic"
66
)
77

8-
func SetToRamStore(puzzle *logic.PuzzleDto){
8+
func SetToRamStore(puzzle *logic.PuzzleDto, uuid string){
99
fmt.Println("Setting puzzle to RamStore")
1010
GetRamStoreInstance()
11-
Set(puzzle.ID, Calculate(puzzle.ID, puzzle.BestMoves))
11+
Set(fmt.Sprintf("%s/%s", puzzle.ID, uuid), Calculate(puzzle.ID, puzzle.BestMoves))
1212
}
1313

1414
func CheckRamStore(pid string, hash string) bool{

backend/views/404.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<head>
44
<meta charset="UTF-8">
55
<meta content="width=device-width, initial-scale=1, maximum-scale=1, shrink-to-fit=no" name="viewport">
6-
<title>404 — Stisla</title>
6+
<title>Pichess</title>
77

88
<!-- General CSS Files -->
99
<link rel="stylesheet" href="assets/modules/bootstrap/css/bootstrap.min.css">

backend/views/assets/js/tools.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ function requestNew(obj){
66
document.getElementById('board1').innerHTML = '';
77

88
$.ajax({
9-
url: '/load',
9+
url: '/load?u=' + getUuid() ,
1010
type: "GET",
1111
success: function (data) {
1212
if (data.ID == undefined) return
@@ -117,7 +117,7 @@ function solved(id){
117117
content_hash += createdMoves
118118
let hash = calculateHash(content_hash)
119119
$.ajax({
120-
url: '/solved?pid=' + id + '&h=' + hash,
120+
url: '/solved?pid=' + id + '&h=' + hash + '&u=' + getUuid(),
121121
type: "GET",
122122
success: function (data) {
123123
console.log(data)

0 commit comments

Comments
 (0)