From 46d99053b3e79b6ff5ce07afe5d2f9ba0f34ff3f Mon Sep 17 00:00:00 2001 From: Alexander Bass Date: Fri, 24 Jun 2022 10:34:25 -0400 Subject: [PATCH] All typescript errors are gone but game does not yet work. --- dist/game.js | 4 +--- dist/roomManager.js | 19 ++++++------------- dist/server/netcode.js | 2 +- dist/server/server.js | 1 - dist/world/generator.js | 11 +---------- dist/world/world.js | 11 +++++------ src/game.ts | 17 +++++++++-------- src/player.ts | 5 ++++- src/roomManager.ts | 27 +++++++++++---------------- src/server/illegalAction.ts | 4 +++- src/server/netcode.ts | 15 +++++++-------- src/server/server.ts | 1 - src/util.ts | 6 +++--- src/world/flagger.ts | 2 +- src/world/generator.ts | 10 +--------- src/world/marker.ts | 2 +- src/world/obfuscator.ts | 2 +- src/world/placer.ts | 3 +-- src/world/revealer.ts | 6 +++--- src/world/world.ts | 24 +++++++++++++++--------- 20 files changed, 74 insertions(+), 98 deletions(-) diff --git a/dist/game.js b/dist/game.js index 1c62fcf..8c8c2dd 100644 --- a/dist/game.js +++ b/dist/game.js @@ -5,12 +5,10 @@ export class Game { constructor(id, options) { this.id = id; this.players = []; - this.oldWorld = []; this.world = new World(options); } addPlayer(id, name, color) { - if (this.getAllNames().includes(name)) - return false; + // if (this.getAllNames().includes(name)) return false const player = new Player(id, name, color); this.players.push(player); return player; diff --git a/dist/roomManager.js b/dist/roomManager.js index 3ada5a3..636c862 100644 --- a/dist/roomManager.js +++ b/dist/roomManager.js @@ -1,15 +1,11 @@ import { Game } from "./game.js"; import * as util from "./util.js"; -import { IllegalAction } from "./server/illegalAction.js"; class RoomManager { constructor() { this.games = []; } getGameByID(id) { const game = this.games.filter(obj => obj.id == id)[0]; - if (game === undefined) { - return false; - } return game; } addGame(id, options) { @@ -36,26 +32,23 @@ class RoomManager { } return ids; } - playerJoinGame(roomCode, playerName, playerID) { + playerJoinGame(roomCode, playerName, playerColor, playerID) { // See if the client is already in a game if (this.getAllPlayerIDs().includes(playerID)) { - return new IllegalAction(id, 22); } if (!this.getAllGameIDs().includes(roomCode)) { - return new IllegalAction(id, 30); } const game = this.getGameByID(roomCode); - const player = game.addPlayer(playerID, playerName); + const player = game.addPlayer(playerID, playerName, playerColor); // See if player name is taken already - if (player === false) { - this.removeGame(game); - return new IllegalAction(id, 20); - } + // if (player === false) { + // this.removeGameByID(game.id) + // } return [game, player]; } playerCreateGame(options, playerName, playerColor, playerID) { if (this.getAllPlayerIDs().includes(playerID)) { - return new IllegalAction(id, 22); + // return new IllegalAction(playerID, 22) } console.log(options); const id = util.createCode(); diff --git a/dist/server/netcode.js b/dist/server/netcode.js index cee1009..712b8d9 100644 --- a/dist/server/netcode.js +++ b/dist/server/netcode.js @@ -26,7 +26,7 @@ function connected(client) { client.sendMeta(); }); client.on('joinGame', function (data) { - const info = roomManager.playerJoinGame(data.room, data.name, client.id); + const info = roomManager.playerJoinGame(data.room, data.name, data.color, client.id); client.join(info[0].id); client.game = info[0]; client.player = info[1]; diff --git a/dist/server/server.js b/dist/server/server.js index 2d3a67d..0339261 100644 --- a/dist/server/server.js +++ b/dist/server/server.js @@ -5,5 +5,4 @@ var app = express(); const __dirname = path.resolve(path.dirname('')); app.use(express.static(__dirname + '/www/')); export var webServer = app.listen(port, function () { - var port = webServer.address().port; }); diff --git a/dist/world/generator.js b/dist/world/generator.js index 10022c3..78526a8 100644 --- a/dist/world/generator.js +++ b/dist/world/generator.js @@ -1,14 +1,5 @@ import * as util from "../util.js"; -const searchLoc = -// [ -// [0, 2], -// [-1, 1], [0, 1], [1, 1], -// [-2,0], [-1, 0],/*Start*/[1, 0], [2,0], -// [-1,-1], [0,-1], [1,-1], -// [0,-2] -// -// ] -[ +const searchLoc = [ [-1, 1], [0, 1], [1, 1], [-1, 0], [1, 0], [-1, -1], [0, -1], [1, -1] diff --git a/dist/world/world.js b/dist/world/world.js index 53a74d7..162cf8b 100644 --- a/dist/world/world.js +++ b/dist/world/world.js @@ -1,7 +1,6 @@ import * as obfuscater from "./obfuscator.js"; import * as generator from "./generator.js"; import * as marker from "./marker.js"; -import * as log from "../log.js"; import * as flagger from "./flagger.js"; import * as placer from "./placer.js"; export class World { @@ -25,27 +24,27 @@ export class World { this.data = flagger.flag(x, y, this.data, player); } if (mode === 0) { - this.data = placer.place(x, y, this.data, player); + this.data = placer.place(x, y, this.data); } } else { this.generate(x, y).mark(); - this.data = placer.place(x, y, this.data, player); + this.data = placer.place(x, y, this.data); } } generate(x, y) { if (this.isGenerated) - return log.log("Already Generated"); + return console.log("Already Generated"); return generator.generate(x, y, this); } mark() { if (this.isMarked) - return log.log("Already Marked!"); + return console.log("Already Marked!"); return marker.mark(this); } obfuscate() { if (this.isObfuscated) - return log.log("already done"); + return console.log("already done"); const tempWorld = JSON.parse(JSON.stringify(this)); ; return obfuscater.obfuscate(tempWorld); diff --git a/src/game.ts b/src/game.ts index a9b64f9..1853d61 100644 --- a/src/game.ts +++ b/src/game.ts @@ -1,38 +1,39 @@ import {Player} from "./player.js" -import * as log from "./log.js" import {roomManager} from "./roomManager.js" import { World} from "./world/world.js" export class Game { - constructor(id, options) { + id: string; + players: Player[]; + world: World; + constructor(id:string, options:any) { this.id = id; this.players = []; - this.oldWorld = [] this.world = new World(options) } - addPlayer(id, name, color) { - if (this.getAllNames().includes(name)) return false + addPlayer(id:string, name:string, color:string):Player { + // if (this.getAllNames().includes(name)) return false const player = new Player(id, name, color); this.players.push(player); return player } - removePlayerByID(id) { + removePlayerByID(id:string) { this.players = this.players.filter(obj => obj.id != id); if (this.players.length < 1) { roomManager.removeGameByID(this.id); } } - getAllIDs() { + getAllIDs():string[] { let ids = [] for (let i = 0; i < this.players.length; i++) { ids.push(this.players[i].id) } return ids } - getAllNames() { + getAllNames():string[] { let names = [] for (let i = 0; i < this.players.length; i++) { names.push(this.players[i].name) diff --git a/src/player.ts b/src/player.ts index 2720897..106bb0c 100644 --- a/src/player.ts +++ b/src/player.ts @@ -1,5 +1,8 @@ export class Player { - constructor(id, name, color) { + id: string; + name: string; + color: string; + constructor(id:string, name:string, color:string) { this.id = id; this.name = name; this.color = color; diff --git a/src/roomManager.ts b/src/roomManager.ts index cf1cb2c..65ae215 100644 --- a/src/roomManager.ts +++ b/src/roomManager.ts @@ -2,23 +2,21 @@ import {Game} from "./game.js" import * as util from "./util.js" import {IllegalAction} from "./server/illegalAction.js" class RoomManager { + games:Game[]; constructor(){ this.games = []; } - getGameByID(id) { + getGameByID(id:string) { const game = this.games.filter(obj => obj.id == id)[0] - if (game === undefined) { - return false - } return game } - addGame(id, options) { + addGame(id:string, options:any) { const game = new Game(id, options); this.games.push(game); return game } - removeGameByID(id) { + removeGameByID(id:string) { this.games = this.games.filter(obj => obj.id != id); } @@ -41,27 +39,24 @@ class RoomManager { return ids } - playerJoinGame(roomCode, playerName, playerID) { + playerJoinGame(roomCode:string, playerName:string, playerColor:string, playerID:string):any[] { // See if the client is already in a game if (this.getAllPlayerIDs().includes(playerID)) { - return new IllegalAction(id, 22) } if (!this.getAllGameIDs().includes(roomCode)) { - return new IllegalAction(id, 30) } const game = this.getGameByID(roomCode); - const player = game.addPlayer(playerID, playerName); + const player = game.addPlayer(playerID, playerName, playerColor); // See if player name is taken already - if (player === false) { - this.removeGame(game) - return new IllegalAction(id, 20) - } + // if (player === false) { + // this.removeGameByID(game.id) + // } return [game, player] } - playerCreateGame(options, playerName, playerColor, playerID) { + playerCreateGame(options:any, playerName:string, playerColor:string, playerID:string):any[] { if (this.getAllPlayerIDs().includes(playerID)) { - return new IllegalAction(id, 22) + // return new IllegalAction(playerID, 22) } console.log(options) const id = util.createCode() diff --git a/src/server/illegalAction.ts b/src/server/illegalAction.ts index d0876ae..a46073e 100644 --- a/src/server/illegalAction.ts +++ b/src/server/illegalAction.ts @@ -1,6 +1,8 @@ import {io} from "./io.js" export class IllegalAction { - constructor(id, action){ + action: number; + id: string; + constructor(id:string, action:number){ this.action = action this.id = id this.sendIllegalAction(); diff --git a/src/server/netcode.ts b/src/server/netcode.ts index d726c9d..e63e1fb 100644 --- a/src/server/netcode.ts +++ b/src/server/netcode.ts @@ -1,14 +1,13 @@ import {IllegalAction} from "./illegalAction.js" -import * as log from "../log.js" import {roomManager} from "../roomManager.js" import {io} from "./io.js" -io.on('connection', function(client){ +io.on('connection', function(client:any){ connected(client) }); -function connected(client) { +function connected(client:any) { client.sync = function() { const tempWorld = client.game.world.obfuscate(); io.to(client.game.id).emit('sync',{world: tempWorld}) @@ -32,8 +31,8 @@ function connected(client) { client.sendMeta(); }) - client.on('joinGame', function(data){ - const info = roomManager.playerJoinGame(data.room, data.name, client.id) + client.on('joinGame', function(data:any){ + const info = roomManager.playerJoinGame(data.room, data.name, data.color, client.id) client.join(info[0].id) client.game = info[0]; client.player = info[1] @@ -42,7 +41,7 @@ function connected(client) { client.sendMeta() }) - client.on('createGame', function(data){ + client.on('createGame', function(data:any){ const info = roomManager.playerCreateGame(data.options, data.name, data.color, client.id) client.join(info[0].id) client.game = info[0]; @@ -52,11 +51,11 @@ function connected(client) { client.sendMeta() }) - client.on('leaveGame', function(data){ + client.on('leaveGame', function(data:any){ client.game.removePlayerByID(client.id) }) - client.on('clickCanvas', function(data){ + client.on('clickCanvas', function(data:any){ if (!roomManager.getAllPlayerIDs().includes(client.id)) return new IllegalAction(client.id, 1) client.game.world.click(data.tilePosition[0],data.tilePosition[1], data.mode, client.player) client.sync() diff --git a/src/server/server.ts b/src/server/server.ts index 8f8e610..d7487a8 100644 --- a/src/server/server.ts +++ b/src/server/server.ts @@ -6,5 +6,4 @@ var app = express() const __dirname = path.resolve(path.dirname('')); app.use(express.static(__dirname + '/www/')); export var webServer = app.listen(port, function () { - var port = webServer.address().port; }); diff --git a/src/util.ts b/src/util.ts index c49079d..248433a 100644 --- a/src/util.ts +++ b/src/util.ts @@ -1,12 +1,12 @@ import crypto from "crypto"; -export function randomNumber(min, max) { +export function randomNumber(min:number, max:number):number { return Math.floor(Math.random() * (max - min) + min); } -export function clamp(number, min, max) { +export function clamp(number:number, min:number, max:number):number { return Math.max(min, Math.min(number, max)); } -export function createCode() { +export function createCode():string { const randomString = crypto.randomBytes(3).toString("hex").toUpperCase() return randomString } diff --git a/src/world/flagger.ts b/src/world/flagger.ts index 7fd95f1..269e7bb 100644 --- a/src/world/flagger.ts +++ b/src/world/flagger.ts @@ -1,5 +1,5 @@ -export function flag(x, y, data, player) { +export function flag(x:number, y:number, data:any, player:any):any { let tile = data[x][y] if (!tile.mask || tile.color === 0) return data diff --git a/src/world/generator.ts b/src/world/generator.ts index 7af0462..f9ddb11 100644 --- a/src/world/generator.ts +++ b/src/world/generator.ts @@ -1,20 +1,12 @@ import * as util from "../util.js" const searchLoc = -// [ -// [0, 2], -// [-1, 1], [0, 1], [1, 1], -// [-2,0], [-1, 0],/*Start*/[1, 0], [2,0], -// [-1,-1], [0,-1], [1,-1], -// [0,-2] -// -// ] [ [-1,1], [0,1], [1,1], [-1,0], [1,0], [-1,-1],[0,-1],[1,-1] ] -export function generate(avoidX, avoidY, world){ +export function generate(avoidX:number, avoidY:number, world:any):any { var minesPlanted = 0; while (minesPlanted < world.mines || !(minesPlanted <= world.width*world.height-15)) { const x = util.randomNumber(0,world.width) diff --git a/src/world/marker.ts b/src/world/marker.ts index 7bff182..821f892 100644 --- a/src/world/marker.ts +++ b/src/world/marker.ts @@ -5,7 +5,7 @@ const searchLoc = [-1,-1],[0,-1],[1,-1] ] // Place Numbers -export function mark(world) { +export function mark(world:any):any { for(let x = 0; x < world.width; x++){ for(let y = 0; y < world.height; y++){ diff --git a/src/world/obfuscator.ts b/src/world/obfuscator.ts index d1cb45f..4921082 100644 --- a/src/world/obfuscator.ts +++ b/src/world/obfuscator.ts @@ -1,5 +1,5 @@ -export function obfuscate(world) { +export function obfuscate(world:any):any { let tempWorld = world; for(let x = 0; x < tempWorld.width; x++){ for(let y = 0; y < tempWorld.height; y++){ diff --git a/src/world/placer.ts b/src/world/placer.ts index 59bc1ed..7f08726 100644 --- a/src/world/placer.ts +++ b/src/world/placer.ts @@ -1,6 +1,5 @@ -import * as log from "../log.js" import * as revealer from "./revealer.js" -export function place(x, y, data) { +export function place(x:number, y:number, data:any):any { let tile = data[x][y]; if (tile.mask === true) { if (tile.mask && tile.flag === 0) { diff --git a/src/world/revealer.ts b/src/world/revealer.ts index 91442f3..5b53995 100644 --- a/src/world/revealer.ts +++ b/src/world/revealer.ts @@ -6,11 +6,11 @@ const searchLoc = [-1,-1],[0,-1],[1,-1] ] -export function reveal(x, y, data) { +export function reveal(x:number, y:number, data:any):any { if (data[x][y].type !== 5) { - var toSearch = []; - var searchedLocations = []; + var toSearch:any[] = []; + var searchedLocations:any[] = []; toSearch.push([x, y]) if (data[x][y].type === 1) { diff --git a/src/world/world.ts b/src/world/world.ts index 5e7ab26..34fca17 100644 --- a/src/world/world.ts +++ b/src/world/world.ts @@ -1,12 +1,18 @@ import * as obfuscater from "./obfuscator.js"; import * as generator from "./generator.js" import * as marker from "./marker.js" -import * as log from "../log.js" import * as flagger from "./flagger.js" import * as placer from "./placer.js" export class World { - constructor(options) { + mines: number; + width: number; + height: number; + isGenerated: boolean; + isObfuscated: boolean; + isMarked: boolean; + data: any; + constructor(options:any) { this.mines = options.mines this.width = options.width*1 this.height = options.height*1 @@ -21,34 +27,34 @@ export class World { } } - click (x, y, mode, player) { + click (x:number, y:number, mode:number, player:any) { if (this.isGenerated) { if (mode === 2) { this.data = flagger.flag(x, y, this.data, player) } if (mode === 0) { - this.data = placer.place(x, y, this.data, player) + this.data = placer.place(x, y, this.data) } } else { this.generate(x, y).mark(); - this.data = placer.place(x, y, this.data, player) + this.data = placer.place(x, y, this.data) } } - generate(x, y) { - if (this.isGenerated) return log.log("Already Generated"); + generate(x:number, y:number) { + if (this.isGenerated) return console.log("Already Generated"); return generator.generate(x, y, this); } mark() { - if (this.isMarked) return log.log("Already Marked!"); + if (this.isMarked) return console.log("Already Marked!"); return marker.mark(this); } obfuscate() { - if (this.isObfuscated) return log.log("already done") + if (this.isObfuscated) return console.log("already done") const tempWorld = JSON.parse(JSON.stringify(this));; return obfuscater.obfuscate(tempWorld); }