add shape modes any and flat

This commit is contained in:
Zutatensuppe 2021-06-03 09:07:57 +02:00
parent b88321bb1b
commit 2d83fd441f
9 changed files with 98 additions and 30 deletions

View file

@ -1,5 +1,5 @@
import GameCommon from './../common/GameCommon'
import { Change, Game, Input, ScoreMode, Timestamp } from './../common/Types'
import { Change, Game, Input, ScoreMode, ShapeMode, Timestamp } from './../common/Types'
import Util, { logger } from './../common/Util'
import { Rng } from './../common/Rng'
import GameLog from './GameLog'
@ -14,17 +14,19 @@ async function createGameObject(
targetTiles: number,
image: PuzzleCreationImageInfo,
ts: Timestamp,
scoreMode: ScoreMode
scoreMode: ScoreMode,
shapeMode: ShapeMode
): Promise<Game> {
const seed = Util.hash(gameId + ' ' + ts)
const rng = new Rng(seed)
return {
id: gameId,
rng: { type: 'Rng', obj: rng },
puzzle: await createPuzzle(rng, targetTiles, image, ts),
puzzle: await createPuzzle(rng, targetTiles, image, ts, shapeMode),
players: [],
evtInfos: {},
scoreMode,
shapeMode,
}
}
@ -33,18 +35,29 @@ async function createGame(
targetTiles: number,
image: PuzzleCreationImageInfo,
ts: Timestamp,
scoreMode: ScoreMode
scoreMode: ScoreMode,
shapeMode: ShapeMode
): Promise<void> {
const gameObject = await createGameObject(
gameId,
targetTiles,
image,
ts,
scoreMode
scoreMode,
shapeMode
)
GameLog.create(gameId)
GameLog.log(gameId, Protocol.LOG_HEADER, 1, targetTiles, image, ts, scoreMode)
GameLog.log(
gameId,
Protocol.LOG_HEADER,
1,
targetTiles,
image,
ts,
scoreMode,
shapeMode
)
GameCommon.setGame(gameObject.id, gameObject)
GameStorage.setDirty(gameId)

View file

@ -1,7 +1,7 @@
import Util from './../common/Util'
import { Rng } from './../common/Rng'
import Images from './Images'
import { EncodedPiece, EncodedPieceShape, PieceShape, Puzzle } from '../common/Types'
import { EncodedPiece, EncodedPieceShape, PieceShape, Puzzle, ShapeMode } from '../common/Types'
import { Dim, Point } from '../common/Geometry'
export interface PuzzleCreationImageInfo {
@ -28,7 +28,8 @@ async function createPuzzle(
rng: Rng,
targetTiles: number,
image: PuzzleCreationImageInfo,
ts: number
ts: number,
shapeMode: ShapeMode
): Promise<Puzzle> {
const imagePath = image.file
const imageUrl = image.url
@ -44,7 +45,7 @@ async function createPuzzle(
for (let i = 0; i < rawPieces.length; i++) {
rawPieces[i] = { idx: i }
}
const shapes = determinePuzzleTileShapes(rng, info)
const shapes = determinePuzzleTileShapes(rng, info, shapeMode)
let positions: Point[] = new Array(info.tiles)
for (const piece of rawPieces) {
@ -162,13 +163,24 @@ async function createPuzzle(
},
}
}
function determineTabs (shapeMode: ShapeMode): number[] {
switch(shapeMode) {
case ShapeMode.ANY:
return [-1, 0, 1]
case ShapeMode.FLAT:
return [0]
case ShapeMode.NORMAL:
default:
return [-1, 1]
}
}
function determinePuzzleTileShapes(
rng: Rng,
info: PuzzleCreationInfo
info: PuzzleCreationInfo,
shapeMode: ShapeMode
): Array<EncodedPieceShape> {
const tabs = [-1, 1]
const tabs: number[] = determineTabs(shapeMode)
const shapes: Array<PieceShape> = new Array(info.tiles)
for (let i = 0; i < info.tiles; i++) {
const coord = Util.coordByPieceIdx(info, i)

View file

@ -19,7 +19,7 @@ import {
UPLOAD_DIR,
} from './Dirs'
import GameCommon from '../common/GameCommon'
import { ServerEvent, Game as GameType, GameSettings, ScoreMode } from '../common/Types'
import { ServerEvent, Game as GameType, GameSettings, ScoreMode, ShapeMode } from '../common/Types'
import GameStorage from './GameStorage'
import Db from './Db'
@ -89,7 +89,8 @@ app.get('/api/replay-data', async (req, res): Promise<void> => {
log[0][2],
log[0][3],
log[0][4],
log[0][5] || ScoreMode.FINAL
log[0][5] || ScoreMode.FINAL,
log[0][6] || ShapeMode.NORMAL
)
}
res.send({ log, game: game ? Util.encodeGame(game) : null })
@ -201,7 +202,8 @@ app.post('/api/newgame', express.json(), async (req, res): Promise<void> => {
gameSettings.tiles,
gameSettings.image,
ts,
gameSettings.scoreMode
gameSettings.scoreMode,
gameSettings.shapeMode
)
}
res.send({ id: gameId })