add seed mechanism

This commit is contained in:
Simon Vieille 2022-02-02 17:00:24 +01:00
parent 1641fb6f30
commit 20609ef414
3 changed files with 83439 additions and 113968 deletions

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

View file

@ -39,7 +39,7 @@ const token = authClient.accessToken
const client = new MatrixClient(url, token, storage, cryptoProvider) const client = new MatrixClient(url, token, storage, cryptoProvider)
const games = [] const games = []
const randomWord = uniqueRandomArray(words); const randomSeeds = uniqueRandomArray(words.seeds);
const sendMessage = async (room, text) => { const sendMessage = async (room, text) => {
client.sendMessage(room, { client.sendMessage(room, {
@ -97,11 +97,29 @@ const renderMatrix = (game, options) => {
return markdown return markdown
} }
const startGame = async (meta) => { const startGame = async (meta, seed) => {
const word = randomWord() let data
games[meta.room] = new Game(word, allWords)
await sendMessage(meta.room, 'Le mot commence par la lettre `' + word.substr(0, 1) + '`') if (seed) {
const index = words.seeds.indexOf(seed)
if (index === -1) {
return await sendMessage(meta.room, 'Désolé mais cette seed n\'existe pas.')
}
data = words.datas[index]
} else {
seed = randomSeeds()
data = words.datas[words.seeds.indexOf(seed)]
}
games[meta.room] = new Game(data.word, allWords)
const word = data.word
const firstLetter = word.substr(0, 1)
const size = word.length
await sendMessage(meta.room, `Le mot commence par la lettre \`${firstLetter}\` et contient ${size} lettres. Vous pouvez partager cette grille avec sa seed : \`${seed}\``)
await sendMessage(meta.room, renderMatrix(games[meta.room])) await sendMessage(meta.room, renderMatrix(games[meta.room]))
console.log(`${meta.sender} on room ${meta.room} starts the game (${word})`) console.log(`${meta.sender} on room ${meta.room} starts the game (${word})`)
@ -123,9 +141,9 @@ commander
}) })
commander commander
.command('start') .command('start [seed]')
.description('`start` : démarrer une partie') .description('`start` ou `start seed` : démarrer une partie avec éventuellement la seed d\'une grille')
.action(async (meta) => { .action(async (meta, seed) => {
if (games.hasOwnProperty(meta.room)) { if (games.hasOwnProperty(meta.room)) {
const currentGame = games[meta.room] const currentGame = games[meta.room]
@ -137,17 +155,19 @@ commander
]) { ]) {
await sendMessage(meta.room, item) await sendMessage(meta.room, item)
} }
return
} }
} }
startGame(meta) startGame(meta, seed)
}) })
commander commander
.command('restart') .command('restart [seed]')
.description('`restart` : démarrer une nouvelle partie') .description('`restart` ou `restart seed` : démarrer une nouvelle partie avec éventuellement la seed d\'une grille')
.action(async (meta) => { .action(async (meta, seed) => {
startGame(meta) startGame(meta, seed)
}) })
commander commander