type hints galore!

This commit is contained in:
Zutatensuppe 2021-05-29 15:36:03 +02:00
parent 7b1f270587
commit 46f3fc7480
17 changed files with 700 additions and 667 deletions

View file

@ -1,18 +1,18 @@
import GameCommon, { ScoreMode } from './../common/GameCommon'
import GameCommon, { Game, ScoreMode, Timestamp } from './../common/GameCommon'
import Util from './../common/Util'
import { Rng } from './../common/Rng'
import GameLog from './GameLog'
import { createPuzzle } from './Puzzle'
import { createPuzzle, PuzzleCreationImageInfo } from './Puzzle'
import Protocol from './../common/Protocol'
import GameStorage from './GameStorage'
async function createGameObject(
gameId: string,
targetTiles: number,
image: { file: string, url: string },
image: PuzzleCreationImageInfo,
ts: number,
scoreMode: ScoreMode
) {
): Promise<Game> {
const seed = Util.hash(gameId + ' ' + ts)
const rng = new Rng(seed)
return {
@ -28,11 +28,17 @@ async function createGameObject(
async function createGame(
gameId: string,
targetTiles: number,
image: { file: string, url: string },
image: PuzzleCreationImageInfo,
ts: number,
scoreMode: ScoreMode
): Promise<void> {
const gameObject = await createGameObject(gameId, targetTiles, image, ts, scoreMode)
const gameObject = await createGameObject(
gameId,
targetTiles,
image,
ts,
scoreMode
)
GameLog.create(gameId)
GameLog.log(gameId, Protocol.LOG_HEADER, 1, targetTiles, image, ts, scoreMode)
@ -41,7 +47,7 @@ async function createGame(
GameStorage.setDirty(gameId)
}
function addPlayer(gameId: string, playerId: string, ts: number): void {
function addPlayer(gameId: string, playerId: string, ts: Timestamp): void {
const idx = GameCommon.getPlayerIndexById(gameId, playerId)
const diff = ts - GameCommon.getStartTs(gameId)
if (idx === -1) {
@ -74,14 +80,4 @@ export default {
createGame,
addPlayer,
handleInput,
getAllGames: GameCommon.getAllGames,
getActivePlayers: GameCommon.getActivePlayers,
getFinishedTileCount: GameCommon.getFinishedTileCount,
getImageUrl: GameCommon.getImageUrl,
getTileCount: GameCommon.getTileCount,
exists: GameCommon.exists,
playerExists: GameCommon.playerExists,
get: GameCommon.get,
getStartTs: GameCommon.getStartTs,
getFinishTs: GameCommon.getFinishTs,
}

View file

@ -8,19 +8,19 @@ const log = logger('GameLog.js')
const filename = (gameId: string) => `${DATA_DIR}/log_${gameId}.log`
const create = (gameId: string) => {
const create = (gameId: string): void => {
const file = filename(gameId)
if (!fs.existsSync(file)) {
fs.appendFileSync(file, '')
}
}
const exists = (gameId: string) => {
const exists = (gameId: string): boolean => {
const file = filename(gameId)
return fs.existsSync(file)
}
const _log = (gameId: string, ...args: Array<any>) => {
const _log = (gameId: string, ...args: Array<any>): void => {
const file = filename(gameId)
if (!fs.existsSync(file)) {
return

View file

@ -41,7 +41,7 @@ function loadGame(gameId: string): void {
}
if (typeof game.puzzle.data.finished === 'undefined') {
const unfinished = game.puzzle.tiles
.map(Util.decodeTile)
.map(Util.decodePiece)
.find((t: Piece) => t.owner !== -1)
game.puzzle.data.finished = unfinished ? 0 : Time.timestamp()
}

View file

@ -2,7 +2,12 @@ import Util from './../common/Util'
import { Rng } from './../common/Rng'
import Images from './Images'
import { EncodedPiece, EncodedPieceShape, PieceShape, Puzzle } from '../common/GameCommon'
import { Point } from '../common/Geometry'
import { Dim, Point } from '../common/Geometry'
export interface PuzzleCreationImageInfo {
file: string
url: string
}
interface PuzzleCreationInfo {
width: number
@ -22,7 +27,7 @@ const TILE_SIZE = 64
async function createPuzzle(
rng: Rng,
targetTiles: number,
image: { file: string, url: string },
image: PuzzleCreationImageInfo,
ts: number
): Promise<Puzzle> {
const imagePath = image.file
@ -33,11 +38,7 @@ async function createPuzzle(
if (!dim.w || !dim.h) {
throw `[ 2021-05-16 invalid dimension for path ${imagePath} ]`
}
const info: PuzzleCreationInfo = determinePuzzleInfo(
dim.w,
dim.h,
targetTiles
)
const info: PuzzleCreationInfo = determinePuzzleInfo(dim, targetTiles)
let tiles = new Array(info.tiles)
for (let i = 0; i < tiles.length; i++) {
@ -98,7 +99,7 @@ async function createPuzzle(
positions = rng.shuffle(positions)
const pieces: Array<EncodedPiece> = tiles.map(tile => {
return Util.encodeTile({
return Util.encodePiece({
idx: tile.idx, // index of tile in the array
group: 0, // if grouped with other tiles
z: 0, // z index of the tile
@ -181,9 +182,12 @@ function determinePuzzleTileShapes(
return shapes.map(Util.encodeShape)
}
const determineTilesXY = (w: number, h: number, targetTiles: number) => {
const w_ = w < h ? (w * h) : (w * w)
const h_ = w < h ? (h * h) : (w * h)
const determineTilesXY = (
dim: Dim,
targetTiles: number
): { tilesX: number, tilesY: number } => {
const w_ = dim.w < dim.h ? (dim.w * dim.h) : (dim.w * dim.w)
const h_ = dim.w < dim.h ? (dim.h * dim.h) : (dim.w * dim.h)
let size = 0
let tiles = 0
do {
@ -198,11 +202,10 @@ const determineTilesXY = (w: number, h: number, targetTiles: number) => {
}
const determinePuzzleInfo = (
w: number,
h: number,
dim: Dim,
targetTiles: number
): PuzzleCreationInfo => {
const {tilesX, tilesY} = determineTilesXY(w, h, targetTiles)
const {tilesX, tilesY} = determineTilesXY(dim, targetTiles)
const tiles = tilesX * tilesY
const tileSize = TILE_SIZE
const width = tilesX * tileSize

View file

@ -19,7 +19,7 @@ import {
PUBLIC_DIR,
UPLOAD_DIR,
} from './Dirs'
import { GameSettings, ScoreMode } from '../common/GameCommon'
import GameCommon, { Game as GameType, GameSettings, ScoreMode } from '../common/GameCommon'
import GameStorage from './GameStorage'
import Db from './Db'
@ -81,7 +81,7 @@ app.get('/api/replay-data', async (req, res) => {
return
}
const log = await GameLog.get(gameId, offset, size)
let game = null
let game: GameType|null = null
if (offset === 0) {
// also need the game
game = await Game.createGameObject(
@ -107,15 +107,15 @@ app.get('/api/newgame-data', (req, res) => {
app.get('/api/index-data', (req, res) => {
const ts = Time.timestamp()
const games = [
...Game.getAllGames().map((game: any) => ({
...GameCommon.getAllGames().map((game: any) => ({
id: game.id,
hasReplay: GameLog.exists(game.id),
started: Game.getStartTs(game.id),
finished: Game.getFinishTs(game.id),
tilesFinished: Game.getFinishedTileCount(game.id),
tilesTotal: Game.getTileCount(game.id),
players: Game.getActivePlayers(game.id, ts).length,
imageUrl: Game.getImageUrl(game.id),
started: GameCommon.getStartTs(game.id),
finished: GameCommon.getFinishTs(game.id),
tilesFinished: GameCommon.getFinishedPiecesCount(game.id),
tilesTotal: GameCommon.getPieceCount(game.id),
players: GameCommon.getActivePlayers(game.id, ts).length,
imageUrl: GameCommon.getImageUrl(game.id),
})),
]
@ -193,7 +193,7 @@ app.post('/newgame', bodyParser.json(), async (req, res) => {
const gameSettings = req.body as GameSettings
log.log(gameSettings)
const gameId = Util.uniqId()
if (!Game.exists(gameId)) {
if (!GameCommon.exists(gameId)) {
const ts = Time.timestamp()
await Game.createGame(
gameId,
@ -238,13 +238,13 @@ wss.on('message', async ({socket, data} : { socket: WebSocket, data: any }) => {
const msgType = msg[0]
switch (msgType) {
case Protocol.EV_CLIENT_INIT: {
if (!Game.exists(gameId)) {
if (!GameCommon.exists(gameId)) {
throw `[game ${gameId} does not exist... ]`
}
const ts = Time.timestamp()
Game.addPlayer(gameId, clientId, ts)
GameSockets.addSocket(gameId, socket)
const game = Game.get(gameId)
const game: GameType = GameCommon.get(gameId)
notify(
[Protocol.EV_SERVER_INIT, Util.encodeGame(game)],
[socket]
@ -252,7 +252,7 @@ wss.on('message', async ({socket, data} : { socket: WebSocket, data: any }) => {
} break
case Protocol.EV_CLIENT_EVENT: {
if (!Game.exists(gameId)) {
if (!GameCommon.exists(gameId)) {
throw `[game ${gameId} does not exist... ]`
}
const clientSeq = msg[1]
@ -260,7 +260,7 @@ wss.on('message', async ({socket, data} : { socket: WebSocket, data: any }) => {
const ts = Time.timestamp()
let sendGame = false
if (!Game.playerExists(gameId, clientId)) {
if (!GameCommon.playerExists(gameId, clientId)) {
Game.addPlayer(gameId, clientId, ts)
sendGame = true
}
@ -269,7 +269,7 @@ wss.on('message', async ({socket, data} : { socket: WebSocket, data: any }) => {
sendGame = true
}
if (sendGame) {
const game = Game.get(gameId)
const game: GameType = GameCommon.get(gameId)
notify(
[Protocol.EV_SERVER_INIT, Util.encodeGame(game)],
[socket]