Skip to content

Commit

Permalink
Add human players as pre selectable
Browse files Browse the repository at this point in the history
  • Loading branch information
windler committed Apr 14, 2022
1 parent e5c541b commit 1bba676
Show file tree
Hide file tree
Showing 9 changed files with 149 additions and 103 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ games/*.pgn
games/archive/*.pgn
.env
dist/*
configs/chesspal.dev.yaml
configs/chesspal.*.yaml
github_token
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Chesspal
Chesspal is a responsive web server for [USB DGT e-Boards](https://digitalgametechnology.com/products/home-use-e-boards). Its core features are to record chess games and play against configurable AIs.
Chesspal is a responsive webserver for [USB DGT e-Boards](https://digitalgametechnology.com/products/home-use-e-boards). Its core features are to record chess games and play against configurable AIs.

Special thanks to:
- @notnil ([https://github.com/notnil/chess](https://github.com/notnil/chess)) for an amazing golang based chess package that is used as a basis
Expand Down Expand Up @@ -53,7 +53,7 @@ make release
## Raspberry pi setup
The makefile ships with targets to easily setup a fresh Raspian installation. However, the arm binaries can be used for manual installation as well.

To setup a fresh Raspberry pi using makefile targets, add a `.env` based on `.env.example`. A working `golang` and `npm` setup is required. Then you can run the following commands:
To setup a fresh Raspberry pi using makefile targets, add a `.env` based on `.env.example`. Further, create a config file `configs/chesspal.raspi.yaml`. A working `golang` and `npm` setup is required. Then you can run the following commands:

```bash
make release # to generate a new release bundle locally
Expand Down
31 changes: 21 additions & 10 deletions cmd/chesspal/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ type StartOptions struct {
}

type Player struct {
Name string `json:"name"`
Type int `json:"type"`
IsHuman bool `json:"isHuman"`
Type int `json:"type"`
}

type Config struct {
Expand All @@ -64,10 +64,14 @@ type Config struct {
DgtPort string `yaml:"dgtPort"`
Engines map[string]string `yaml:"engines"`
Bots []player.BotOptions `yaml:"bots"`
Humans []Human `yaml:"humans"`
Eval Eval `yaml:"eval"`
RClone Rclone `yaml:"rclone"`
}

type Human struct {
Name string `yaml:"name" json:"name"`
}
type Rclone struct {
Remote string `yaml:"remote"`
Games bool `yaml:"games"`
Expand All @@ -88,7 +92,8 @@ var engine *player.DGTEngine
var currentBoard chess.Board

type WSResponse struct {
Bots []player.BotOptions `json:"bots"`
Bots []player.BotOptions `json:"bots"`
Humans []Human `json:"humans"`
}

type GameHistory struct {
Expand Down Expand Up @@ -237,7 +242,7 @@ func main() {
}
defer ws.Close()

if err := ws.WriteJSON(WSResponse{Bots: config.Bots}); !errors.Is(err, nil) {
if err := ws.WriteJSON(WSResponse{Bots: config.Bots, Humans: config.Humans}); !errors.Is(err, nil) {
log.Printf("error occurred: %v", err)
}

Expand Down Expand Up @@ -355,19 +360,25 @@ func startGame(msg *Message, ui *ui.WSUI, cfg Config, ws *websocket.Conn) {
engine.Reset()
engine.SetUpsideDown(msg.Options.UpsideDown)

log.Printf("Black: %+v, White: %+v", msg.Options.Black, msg.Options.White)

var white, black game.Player
if msg.Options.Black.Type == 0 {
black = player.NewDGTPlayer(msg.Options.Black.Name, engine)
if msg.Options.Black.IsHuman {
i := msg.Options.Black.Type
human := cfg.Humans[i]
black = player.NewDGTPlayer(human.Name, engine)
} else {
i := msg.Options.Black.Type - 1
i := msg.Options.Black.Type
options := cfg.Bots[i]
options.Path = cfg.Engines[options.Engine]
black = player.NewUCIPlayer(options)
}
if msg.Options.White.Type == 0 {
white = player.NewDGTPlayer(msg.Options.White.Name, engine)
if msg.Options.White.IsHuman {
i := msg.Options.White.Type
human := cfg.Humans[i]
white = player.NewDGTPlayer(human.Name, engine)
} else {
i := msg.Options.White.Type - 1
i := msg.Options.White.Type
options := cfg.Bots[i]
options.Path = cfg.Engines[options.Engine]
white = player.NewUCIPlayer(options)
Expand Down
3 changes: 3 additions & 0 deletions configs/chesspal.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ rclone:
engines:
stockfish_12: /usr/games/stockfish
fairy_stockfish: usr/local/bin/stockfish
humans:
- name: White
- name: Black
bots:
- name: Adrian (fairy 400)
engine: fairy_stockfish
Expand Down
7 changes: 4 additions & 3 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,16 @@ raspi-install-chesspal:
&& mkdir ~/chesspal \
&& mkdir -p ~/games/archive \
&& tar -xf /tmp/$(ARM_RELEASE) -C ~/chesspal \
&& sudo update-rc.d $(INIT_SCRIPT) defaults \
&& sudo shutdown -r 0")
&& sudo update-rc.d $(INIT_SCRIPT) defaults")
$(call ssh-copy,configs/chesspal.raspi.yaml,/home/pi/chesspal/configs/chesspal.yaml)
$(call ssh-cmd,"sudo shutdown -r 0")

raspi-restart-chesspal:
$(call ssh-cmd,"sudo service $(INIT_SCRIPT) stop")
$(call ssh-cmd,"sudo service $(INIT_SCRIPT) start")

raspi-apply-config:
$(call ssh-copy,configs/chesspal.yaml,/home/pi/chesspal/configs/chesspal.yaml)
$(call ssh-copy,configs/chesspal.raspi.yaml,/home/pi/chesspal/configs/chesspal.yaml)
$(call ssh-cmd,"sudo shutdown -r 0")

raspi-logs-follow:
Expand Down
143 changes: 85 additions & 58 deletions web/vue-frontend/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@
:fen="fen"
:outcome="outcome"
:pgn="pgn"
:black="black.name"
:white="white.name"
class="my-4"
/>
</v-col>
Expand Down Expand Up @@ -82,7 +84,6 @@
>
<template v-slot:activator="{ on, attrs }">
<v-btn
:disabled="!connected"
color="primary"
width="100%"
v-bind="attrs"
Expand All @@ -91,56 +92,64 @@
>
New game
</v-btn>

</template>
<v-container>
<v-row class="justify-center">
<v-col cols="12">
<ChessPlayer
v-on:nameChange="white.name = $event"
v-on:modeChange="white.mode = $event"
:locked="started"
color="white"
:name="white.name"
:bots="bots"
class="my-4"
/>
<ChessPlayer
v-on:nameChange="black.name = $event"
v-on:modeChange="black.mode = $event"
:locked="started"
color="black"
:name="black.name"
class="my-4"
:bots="bots"
/>
<SettingsCard
:locked="started"
v-on:upsideDownChange="upsideDown = $event"
v-on:speakChange="
white.speak = Boolean($event);
black.speak = Boolean($event);
"
:speak="white.speak || black.speak ? 'true' : 'false'"
class="my-4"
/>
<v-btn
color="primary"
width="100%"
@click.stop="startGame()"
>
Start game
</v-btn>
<v-progress-linear
indeterminate
width="100%"
color="primary"
v-if="startSend"
height="20"
></v-progress-linear>
</v-col>
</v-row>
</v-container>
<v-card outlined dense>
<v-card-title primary-title class="justify-center">
New game
</v-card-title>
<v-card-actions>
<v-container>
<v-row class="justify-center">
<v-col cols="12">
<ChessPlayer
v-on:modeChange="setPlayer('white', $event)"
:locked="started"
color="white"
:name="white.name"
:bots="bots"
:humans="humans"
class="my-4"
/>
<ChessPlayer
v-on:modeChange="setPlayer('black', $event)"
:locked="started"
color="black"
:name="black.name"
class="my-4"
:bots="bots"
:humans="humans"
/>
<SettingsCard
:locked="started"
v-on:upsideDownChange="upsideDown = $event"
v-on:speakChange="
white.speak = Boolean($event);
black.speak = Boolean($event);
"
:speak="
white.speak || black.speak ? 'true' : 'false'
"
class="my-4"
/>
<v-btn
color="primary"
width="100%"
@click.stop="startGame()"
>
Start game
</v-btn>
<v-progress-linear
indeterminate
width="100%"
color="primary"
v-if="startSend"
height="20"
></v-progress-linear>
</v-col>
</v-row>
</v-container>
</v-card-actions>
</v-card>
</v-dialog>
<div class="my-12"></div>
</v-col>
Expand Down Expand Up @@ -207,23 +216,37 @@ export default {
pawn: 50.0,
turn: "w",
black: {
name: "black",
mode: 0,
isHuman: true,
speak: false,
},
white: {
name: "white",
mode: 0,
isHuman: true,
speak: false,
},
evalMode: 0,
pgn: "",
fen: "r5nr/ppk2pp1/7p/2Bp1b2/8/7P/PPP1PPP1/RN2KB1R",
outcome: "*",
bots: [],
humans: [],
}),
methods: {
setPlayer: function(color, event) {
console.log(event.name, event.isHuman, event.value)
if (color == "black") {
this.black.name = event.name
this.black.isHuman = event.isHuman
this.black.mode = event.mode
}
if (color == "white") {
this.white.name = event.name
this.white.isHuman = event.isHuman
this.white.mode = event.mode
}
},
toggleDarkTheme() {
this.$vuetify.theme.dark = !this.$vuetify.theme.dark;
},
Expand Down Expand Up @@ -268,10 +291,12 @@ export default {
white: {
name: this.white.name,
type: Number(this.white.mode),
isHuman: this.white.isHuman,
},
black: {
name: this.black.name,
type: Number(this.black.mode),
isHuman: this.black.isHuman,
},
evalMode: 1, //always use eval but only show based on ui // Number(this.evalMode),
upsideDown: Boolean(this.upsideDown),
Expand Down Expand Up @@ -312,12 +337,12 @@ export default {
},
mounted() {
if (localStorage.white) {
this.white.name = localStorage.white;
}
if (localStorage.black) {
this.black.name = localStorage.black;
}
// if (localStorage.white) {
// this.white.name = localStorage.white;
// }
// if (localStorage.black) {
// this.black.name = localStorage.black;
// }
},
watch: {
Expand Down Expand Up @@ -346,8 +371,10 @@ export default {
this.connection.onmessage = function (event) {
var data = JSON.parse(event.data);
console.log(data);
if (data.bots != null) {
that.bots = data.bots;
that.humans = data.humans;
return;
}
Expand Down
15 changes: 11 additions & 4 deletions web/vue-frontend/src/components/ChessBoard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
<v-icon color="grey">fas fa-chess-board</v-icon>
</v-card-title>

<div style="position:relative;">
<div style="position: relative">
<p class="text-center ma-2">
{{ black }}
</p>
<v-overlay
:absolute="absolute"
:value="outcome != '*' && outcome != ''"
Expand All @@ -18,6 +21,10 @@
</v-overlay>

<div v-html="svg" :class="boardClass() + ' ma-auto'"></div>

<p class="text-center ma-3">
{{ white }}
</p>
</div>

<v-divider class="ma-4"></v-divider>
Expand Down Expand Up @@ -59,7 +66,7 @@
export default {
name: "ChessBoard",
props: ["svg", "fen", "outcome", "pgn"],
props: ["svg", "fen", "outcome", "pgn", "white", "black"],
methods: {
boardClass: function () {
switch (this.$vuetify.breakpoint.name) {
Expand All @@ -77,7 +84,7 @@ export default {
return "board";
},
importLichess: async function () {
var win = window.open('', '_blank');
var win = window.open("", "_blank");
const requestOptions = {
method: "POST",
headers: { "Content-Type": "application/x-www-form-urlencoded" },
Expand All @@ -88,7 +95,7 @@ export default {
requestOptions
);
const data = await response.json();
win.location = data.url
win.location = data.url;
},
copy: function () {
navigator.clipboard.writeText(this.pgn).then(
Expand Down
Loading

0 comments on commit 1bba676

Please sign in to comment.