-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from ahmed-debbech/dev
Add seen and solved mechanisms
- Loading branch information
Showing
22 changed files
with
534 additions
and
22 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,41 @@ | ||
# go_chess_puzzle | ||
A Chess puzzle generator from Lichess.com match databases using Stockfish to calculate best moves. | ||
# Pichess | ||
###### You like Lichess? You will love Pichess ❤️ | ||
|
||
 | ||
|
||
A Chess puzzle generator from Lichess.org match databases using Stockfish to calculate best moves. | ||
|
||
### Demo | ||
|
||
 | ||
|
||
### How does it work? | ||
|
||
##### Generation phase: | ||
I built three simple programs: | ||
**1 (Cutter)** - Parses the downloaded file from [Lichess database](https://database.lichess.org/) (a huge text file of several GBs). | ||
The file consists of thousands of PGN games and Cutter is responsible for splitting every game's PGN to a seperate file inside a specified output directory and naming them with unique IDS. | ||
**2 (Generator)** - The main core that produces puzzle candidates. | ||
- Generator selects and opens a PGN file with a given random id (say 15260) from the output directory. | ||
- Generator then reads the PGN and jumps to also a random position played already in the game (say 15). | ||
- At this point Generator Calls stockfish chess engine and tells it "Hey, here is a chess game at the position (say 15) and white plays now, can you finish the game for me?". | ||
- Note that Generator asks stockfish to play at depth 24 and depth 4 so that we can reach to a checkmate. | ||
- Eventually Generator points to the last 2 to 5 moves in the game that is finished by stockfish. | ||
- Now we have a new puzzle candidate | ||
|
||
**3 (Storer)** - Stores puzzle candidates coming from generator to MongoDB | ||
Storer accepts any puzzle candidate coming from Generator to save it directly into `puzzles` collection in MongoDB | ||
|
||
|
||
##### Serving phase: | ||
**The backend** | ||
The actual program that you need to deploy is `backend` that will serve all web pages content and do everything related to checking if client solved or seen a puzzle. | ||
The rest three programs we talked about above are like a toolbox for generating/storing puzzles forbackend to be able to serve them. | ||
|
||
### We depend on these amazing libraries... | ||
* [notnil/chess](https://github.com/notnil/chess) library for easier manipulation of chess games in Golang. | ||
* [chessboardjs](https://chessboardjs.com/) the library that draws chess the board you see in your browser. | ||
|
||
### Want to contribute? | ||
Please feel free to fork this repo and to open a new pull request with your own modifications. | ||
Contact me at [email protected] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
* implement a way to store current unsolved puzzle | ||
* when press New dont refresh page | ||
* implement a way to store current unsolved puzzle |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/solved?cid=44-44-444&pid=1234&sh=okokokokkokkookok | ||
1) check the hash received against the ramstore | ||
2) hit mongo with uuid and add it to set | ||
|
||
/seen?cid=44-44-444&pid=1234 | ||
1) directly hit mongo with uuid and store it as a set | ||
|
||
/load | ||
1) set pid to ramstore and calculate hash | ||
|
||
|
||
hash equation: | ||
exp : d5f7, h1h4, a8g2 [arr] | ||
5*7 + 1*4 + 8*2 = 55 [uniq] | ||
123455d5f7h1h4a8g2 [pid][uniq][arr] | ||
db2bea72fe15509fe830982d9b057f2b4a873b6714e45d5d6430707e24ebc7c3 [hash SHA256] | ||
|
||
|
||
the ramstore: | ||
key: pid, value: hash | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package logic | ||
|
||
import ( | ||
"strconv" | ||
"encoding/json" | ||
"github.com/ahmed-debbech/go_chess_puzzle/generator/data" | ||
"github.com/ahmed-debbech/go_chess_puzzle/generator/config" | ||
) | ||
|
||
type PuzzleDto struct { | ||
ID string | ||
FEN string | ||
BestMoves [config.BestMovesNumber]string | ||
GenTime string | ||
CurrentPlayer int | ||
SolveCount int | ||
MatchLink string | ||
SeenCount int | ||
FirstSeenTime string | ||
} | ||
|
||
func (p PuzzleDto) String() string{ | ||
return p.ID + " FEN: " + p.FEN + " BestMove: " + strconv.Itoa(len(p.BestMoves)) + " GenTime: " + p.GenTime + " CurrentPlayer: "+ strconv.Itoa(p.CurrentPlayer) +" SolveCount: " + strconv.Itoa(p.SolveCount) + " MatchLink: " + p.MatchLink + " SeenCount: " + strconv.Itoa(p.SeenCount) + " FirstSeenTime: " + p.FirstSeenTime; | ||
} | ||
|
||
func fromPuzzleDao(puzzleDao *data.Puzzle) *PuzzleDto{ | ||
return &PuzzleDto{ | ||
ID: puzzleDao.ID, | ||
FEN: puzzleDao.FEN, | ||
BestMoves: puzzleDao.BestMoves, | ||
GenTime: puzzleDao.GenTime, | ||
CurrentPlayer: puzzleDao.CurrentPlayer, | ||
SolveCount: puzzleDao.SolveCount, | ||
MatchLink: puzzleDao.MatchLink, | ||
SeenCount: len(puzzleDao.SeenCount), | ||
FirstSeenTime: puzzleDao.FirstSeenTime, | ||
} | ||
} | ||
|
||
func (p PuzzleDto) ToJson() ([]byte, error){ | ||
return json.Marshal(p) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package ramstore | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
"crypto/sha256" | ||
"encoding/hex" | ||
"errors" | ||
|
||
"github.com/ahmed-debbech/go_chess_puzzle/generator/config" | ||
) | ||
|
||
type RamStore struct{ | ||
store map[string]string | ||
} | ||
|
||
var ramStoreInstance *RamStore = nil | ||
|
||
func newRamStore() *RamStore{ | ||
fmt.Println("Creating new RamStore") | ||
return &RamStore{ store: make(map[string]string) } | ||
} | ||
|
||
func GetRamStoreInstance() *RamStore{ | ||
if ramStoreInstance == nil { | ||
ramStoreInstance = newRamStore() | ||
} | ||
return ramStoreInstance | ||
} | ||
|
||
func Set(pid string, hash string){ | ||
ramStoreInstance.store[pid] = hash | ||
fmt.Println(ramStoreInstance) | ||
fmt.Println("LEN: ", len(ramStoreInstance.store)) | ||
} | ||
|
||
func extractDigits(move string) int{ | ||
p := 1 | ||
for _, c := range move { | ||
if ('0'<=c) && ('9' >= c) { | ||
ss, _ := strconv.Atoi(string(c)) | ||
p *= ss | ||
} | ||
} | ||
return p | ||
} | ||
|
||
func doHash(toHash string) string { | ||
hash := sha256.New() | ||
hash.Write([]byte(toHash)) | ||
hashBytes := hash.Sum(nil) | ||
hashString := hex.EncodeToString(hashBytes) | ||
return hashString | ||
} | ||
|
||
func Calculate(pid string, bestmove [config.BestMovesNumber]string) string{ | ||
hash := pid | ||
|
||
sum := 0 | ||
necessary_moves := make([]string, 0) | ||
for i:=1; i<=len(bestmove)-1; i+=2 { | ||
necessary_moves = append(necessary_moves, bestmove[i]) | ||
sum += extractDigits(bestmove[i]) | ||
} | ||
|
||
hash += strconv.Itoa(sum) | ||
hash += strings.Join(necessary_moves, "") | ||
return doHash(hash) | ||
} | ||
|
||
func Get(pid string) (string, error) { | ||
|
||
i, ok := ramStoreInstance.store[pid] | ||
if ok { | ||
return i , nil | ||
} | ||
return "", errors.New("hash is not found") | ||
|
||
} | ||
|
||
func Delete(pid string) { | ||
delete(ramStoreInstance.store, pid) | ||
} |
Oops, something went wrong.