-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
471 additions
and
427 deletions.
There are no files selected for viewing
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,4 +1,4 @@ | ||
{ | ||
"singleQuote": true, | ||
"singleQuote": false, | ||
"semi": true | ||
} |
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,8 +1,8 @@ | ||
export default { | ||
providers: [ | ||
export default { | ||
providers: [ | ||
{ | ||
"domain": "https://convexdev.us.auth0.com/", | ||
"applicationID": "tPilWp4HDDF4yoi3D995XMrVIJhflD4V" | ||
} | ||
domain: "https://convexdev.us.auth0.com/", | ||
applicationID: "tPilWp4HDDF4yoi3D995XMrVIJhflD4V", | ||
}, | ||
], | ||
}; | ||
}; |
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,45 +1,45 @@ | ||
'use node' | ||
"use node"; | ||
|
||
import { api, internal } from './_generated/api' | ||
import { Id } from './_generated/dataModel' | ||
import { internalAction } from './_generated/server' | ||
const jsChessEngine = require('js-chess-engine') | ||
import { Chess } from 'chess.js' | ||
import { api, internal } from "./_generated/api"; | ||
import { Id } from "./_generated/dataModel"; | ||
import { internalAction } from "./_generated/server"; | ||
const jsChessEngine = require("js-chess-engine"); | ||
import { Chess } from "chess.js"; | ||
|
||
export const maybeMakeComputerMove = internalAction( | ||
async (ctx, { id }: { id: Id<'games'> }) => { | ||
const { runQuery, runMutation } = ctx | ||
async (ctx, { id }: { id: Id<"games"> }) => { | ||
const { runQuery, runMutation } = ctx; | ||
const state = await runQuery(api.games.internalGetPgnForComputerMove, { | ||
id, | ||
}) | ||
}); | ||
if (state === null) { | ||
return | ||
return; | ||
} | ||
const [pgn, strategy] = state | ||
const gameState = new Chess() | ||
gameState.loadPgn(pgn) | ||
const moveNumber = gameState.history().length | ||
const game = new jsChessEngine.Game(gameState.fen()) | ||
let level = 1 | ||
if (strategy === 'hard') { | ||
level = 2 | ||
} else if (strategy === 'tricky') { | ||
const [pgn, strategy] = state; | ||
const gameState = new Chess(); | ||
gameState.loadPgn(pgn); | ||
const moveNumber = gameState.history().length; | ||
const game = new jsChessEngine.Game(gameState.fen()); | ||
let level = 1; | ||
if (strategy === "hard") { | ||
level = 2; | ||
} else if (strategy === "tricky") { | ||
if (moveNumber > 6) { | ||
level = 2 | ||
level = 2; | ||
if (moveNumber % 3 === 0) { | ||
level = 3 | ||
level = 3; | ||
} | ||
} | ||
} | ||
const aiMove = game.aiMove(level) | ||
const aiMove = game.aiMove(level); | ||
// aiMove has format {moveFrom: moveTo} | ||
let moveFrom = Object.keys(aiMove)[0] | ||
let moveTo = aiMove[moveFrom] | ||
console.log(`move at level ${level}: ${moveFrom}->${moveTo}`) | ||
let moveFrom = Object.keys(aiMove)[0]; | ||
let moveTo = aiMove[moveFrom]; | ||
console.log(`move at level ${level}: ${moveFrom}->${moveTo}`); | ||
await runMutation(internal.games.internalMakeComputerMove, { | ||
id, | ||
moveFrom: moveFrom.toLowerCase(), | ||
moveTo: moveTo.toLowerCase(), | ||
}) | ||
}); | ||
} | ||
) | ||
); |
Oops, something went wrong.