All typescript errors are gone but game does not yet work.

This commit is contained in:
Alexander Bass 2022-06-24 10:34:25 -04:00
parent c2d02655cd
commit 46d99053b3
20 changed files with 74 additions and 98 deletions

4
dist/game.js vendored
View file

@ -5,12 +5,10 @@ export class Game {
constructor(id, options) { constructor(id, options) {
this.id = id; this.id = id;
this.players = []; this.players = [];
this.oldWorld = [];
this.world = new World(options); this.world = new World(options);
} }
addPlayer(id, name, color) { addPlayer(id, name, color) {
if (this.getAllNames().includes(name)) // if (this.getAllNames().includes(name)) return false
return false;
const player = new Player(id, name, color); const player = new Player(id, name, color);
this.players.push(player); this.players.push(player);
return player; return player;

19
dist/roomManager.js vendored
View file

@ -1,15 +1,11 @@
import { Game } from "./game.js"; import { Game } from "./game.js";
import * as util from "./util.js"; import * as util from "./util.js";
import { IllegalAction } from "./server/illegalAction.js";
class RoomManager { class RoomManager {
constructor() { constructor() {
this.games = []; this.games = [];
} }
getGameByID(id) { getGameByID(id) {
const game = this.games.filter(obj => obj.id == id)[0]; const game = this.games.filter(obj => obj.id == id)[0];
if (game === undefined) {
return false;
}
return game; return game;
} }
addGame(id, options) { addGame(id, options) {
@ -36,26 +32,23 @@ class RoomManager {
} }
return ids; return ids;
} }
playerJoinGame(roomCode, playerName, playerID) { playerJoinGame(roomCode, playerName, playerColor, playerID) {
// See if the client is already in a game // See if the client is already in a game
if (this.getAllPlayerIDs().includes(playerID)) { if (this.getAllPlayerIDs().includes(playerID)) {
return new IllegalAction(id, 22);
} }
if (!this.getAllGameIDs().includes(roomCode)) { if (!this.getAllGameIDs().includes(roomCode)) {
return new IllegalAction(id, 30);
} }
const game = this.getGameByID(roomCode); 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 // See if player name is taken already
if (player === false) { // if (player === false) {
this.removeGame(game); // this.removeGameByID(game.id)
return new IllegalAction(id, 20); // }
}
return [game, player]; return [game, player];
} }
playerCreateGame(options, playerName, playerColor, playerID) { playerCreateGame(options, playerName, playerColor, playerID) {
if (this.getAllPlayerIDs().includes(playerID)) { if (this.getAllPlayerIDs().includes(playerID)) {
return new IllegalAction(id, 22); // return new IllegalAction(playerID, 22)
} }
console.log(options); console.log(options);
const id = util.createCode(); const id = util.createCode();

View file

@ -26,7 +26,7 @@ function connected(client) {
client.sendMeta(); client.sendMeta();
}); });
client.on('joinGame', function (data) { 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.join(info[0].id);
client.game = info[0]; client.game = info[0];
client.player = info[1]; client.player = info[1];

View file

@ -5,5 +5,4 @@ var app = express();
const __dirname = path.resolve(path.dirname('')); const __dirname = path.resolve(path.dirname(''));
app.use(express.static(__dirname + '/www/')); app.use(express.static(__dirname + '/www/'));
export var webServer = app.listen(port, function () { export var webServer = app.listen(port, function () {
var port = webServer.address().port;
}); });

View file

@ -1,14 +1,5 @@
import * as util from "../util.js"; import * as util from "../util.js";
const searchLoc = 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, 1], [0, 1], [1, 1],
[-1, 0], [1, 0], [-1, 0], [1, 0],
[-1, -1], [0, -1], [1, -1] [-1, -1], [0, -1], [1, -1]

11
dist/world/world.js vendored
View file

@ -1,7 +1,6 @@
import * as obfuscater from "./obfuscator.js"; import * as obfuscater from "./obfuscator.js";
import * as generator from "./generator.js"; import * as generator from "./generator.js";
import * as marker from "./marker.js"; import * as marker from "./marker.js";
import * as log from "../log.js";
import * as flagger from "./flagger.js"; import * as flagger from "./flagger.js";
import * as placer from "./placer.js"; import * as placer from "./placer.js";
export class World { export class World {
@ -25,27 +24,27 @@ export class World {
this.data = flagger.flag(x, y, this.data, player); this.data = flagger.flag(x, y, this.data, player);
} }
if (mode === 0) { if (mode === 0) {
this.data = placer.place(x, y, this.data, player); this.data = placer.place(x, y, this.data);
} }
} }
else { else {
this.generate(x, y).mark(); 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) { generate(x, y) {
if (this.isGenerated) if (this.isGenerated)
return log.log("Already Generated"); return console.log("Already Generated");
return generator.generate(x, y, this); return generator.generate(x, y, this);
} }
mark() { mark() {
if (this.isMarked) if (this.isMarked)
return log.log("Already Marked!"); return console.log("Already Marked!");
return marker.mark(this); return marker.mark(this);
} }
obfuscate() { obfuscate() {
if (this.isObfuscated) if (this.isObfuscated)
return log.log("already done"); return console.log("already done");
const tempWorld = JSON.parse(JSON.stringify(this)); const tempWorld = JSON.parse(JSON.stringify(this));
; ;
return obfuscater.obfuscate(tempWorld); return obfuscater.obfuscate(tempWorld);

View file

@ -1,38 +1,39 @@
import {Player} from "./player.js" import {Player} from "./player.js"
import * as log from "./log.js"
import {roomManager} from "./roomManager.js" import {roomManager} from "./roomManager.js"
import { World} from "./world/world.js" import { World} from "./world/world.js"
export class Game { export class Game {
constructor(id, options) { id: string;
players: Player[];
world: World;
constructor(id:string, options:any) {
this.id = id; this.id = id;
this.players = []; this.players = [];
this.oldWorld = []
this.world = new World(options) this.world = new World(options)
} }
addPlayer(id, name, color) { addPlayer(id:string, name:string, color:string):Player {
if (this.getAllNames().includes(name)) return false // if (this.getAllNames().includes(name)) return false
const player = new Player(id, name, color); const player = new Player(id, name, color);
this.players.push(player); this.players.push(player);
return player return player
} }
removePlayerByID(id) { removePlayerByID(id:string) {
this.players = this.players.filter(obj => obj.id != id); this.players = this.players.filter(obj => obj.id != id);
if (this.players.length < 1) { if (this.players.length < 1) {
roomManager.removeGameByID(this.id); roomManager.removeGameByID(this.id);
} }
} }
getAllIDs() { getAllIDs():string[] {
let ids = [] let ids = []
for (let i = 0; i < this.players.length; i++) { for (let i = 0; i < this.players.length; i++) {
ids.push(this.players[i].id) ids.push(this.players[i].id)
} }
return ids return ids
} }
getAllNames() { getAllNames():string[] {
let names = [] let names = []
for (let i = 0; i < this.players.length; i++) { for (let i = 0; i < this.players.length; i++) {
names.push(this.players[i].name) names.push(this.players[i].name)

View file

@ -1,5 +1,8 @@
export class Player { export class Player {
constructor(id, name, color) { id: string;
name: string;
color: string;
constructor(id:string, name:string, color:string) {
this.id = id; this.id = id;
this.name = name; this.name = name;
this.color = color; this.color = color;

View file

@ -2,23 +2,21 @@ import {Game} from "./game.js"
import * as util from "./util.js" import * as util from "./util.js"
import {IllegalAction} from "./server/illegalAction.js" import {IllegalAction} from "./server/illegalAction.js"
class RoomManager { class RoomManager {
games:Game[];
constructor(){ constructor(){
this.games = []; this.games = [];
} }
getGameByID(id) { getGameByID(id:string) {
const game = this.games.filter(obj => obj.id == id)[0] const game = this.games.filter(obj => obj.id == id)[0]
if (game === undefined) {
return false
}
return game return game
} }
addGame(id, options) { addGame(id:string, options:any) {
const game = new Game(id, options); const game = new Game(id, options);
this.games.push(game); this.games.push(game);
return game return game
} }
removeGameByID(id) { removeGameByID(id:string) {
this.games = this.games.filter(obj => obj.id != id); this.games = this.games.filter(obj => obj.id != id);
} }
@ -41,27 +39,24 @@ class RoomManager {
return ids 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 // See if the client is already in a game
if (this.getAllPlayerIDs().includes(playerID)) { if (this.getAllPlayerIDs().includes(playerID)) {
return new IllegalAction(id, 22)
} }
if (!this.getAllGameIDs().includes(roomCode)) { if (!this.getAllGameIDs().includes(roomCode)) {
return new IllegalAction(id, 30)
} }
const game = this.getGameByID(roomCode); 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 // See if player name is taken already
if (player === false) { // if (player === false) {
this.removeGame(game) // this.removeGameByID(game.id)
return new IllegalAction(id, 20) // }
}
return [game, player] return [game, player]
} }
playerCreateGame(options, playerName, playerColor, playerID) { playerCreateGame(options:any, playerName:string, playerColor:string, playerID:string):any[] {
if (this.getAllPlayerIDs().includes(playerID)) { if (this.getAllPlayerIDs().includes(playerID)) {
return new IllegalAction(id, 22) // return new IllegalAction(playerID, 22)
} }
console.log(options) console.log(options)
const id = util.createCode() const id = util.createCode()

View file

@ -1,6 +1,8 @@
import {io} from "./io.js" import {io} from "./io.js"
export class IllegalAction { export class IllegalAction {
constructor(id, action){ action: number;
id: string;
constructor(id:string, action:number){
this.action = action this.action = action
this.id = id this.id = id
this.sendIllegalAction(); this.sendIllegalAction();

View file

@ -1,14 +1,13 @@
import {IllegalAction} from "./illegalAction.js" import {IllegalAction} from "./illegalAction.js"
import * as log from "../log.js"
import {roomManager} from "../roomManager.js" import {roomManager} from "../roomManager.js"
import {io} from "./io.js" import {io} from "./io.js"
io.on('connection', function(client){ io.on('connection', function(client:any){
connected(client) connected(client)
}); });
function connected(client) { function connected(client:any) {
client.sync = function() { client.sync = function() {
const tempWorld = client.game.world.obfuscate(); const tempWorld = client.game.world.obfuscate();
io.to(client.game.id).emit('sync',{world: tempWorld}) io.to(client.game.id).emit('sync',{world: tempWorld})
@ -32,8 +31,8 @@ function connected(client) {
client.sendMeta(); client.sendMeta();
}) })
client.on('joinGame', function(data){ client.on('joinGame', function(data:any){
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.join(info[0].id)
client.game = info[0]; client.game = info[0];
client.player = info[1] client.player = info[1]
@ -42,7 +41,7 @@ function connected(client) {
client.sendMeta() 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) const info = roomManager.playerCreateGame(data.options, data.name, data.color, client.id)
client.join(info[0].id) client.join(info[0].id)
client.game = info[0]; client.game = info[0];
@ -52,11 +51,11 @@ function connected(client) {
client.sendMeta() client.sendMeta()
}) })
client.on('leaveGame', function(data){ client.on('leaveGame', function(data:any){
client.game.removePlayerByID(client.id) 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) 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.game.world.click(data.tilePosition[0],data.tilePosition[1], data.mode, client.player)
client.sync() client.sync()

View file

@ -6,5 +6,4 @@ var app = express()
const __dirname = path.resolve(path.dirname('')); const __dirname = path.resolve(path.dirname(''));
app.use(express.static(__dirname + '/www/')); app.use(express.static(__dirname + '/www/'));
export var webServer = app.listen(port, function () { export var webServer = app.listen(port, function () {
var port = webServer.address().port;
}); });

View file

@ -1,12 +1,12 @@
import crypto from "crypto"; 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); 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)); return Math.max(min, Math.min(number, max));
} }
export function createCode() { export function createCode():string {
const randomString = crypto.randomBytes(3).toString("hex").toUpperCase() const randomString = crypto.randomBytes(3).toString("hex").toUpperCase()
return randomString return randomString
} }

View file

@ -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] let tile = data[x][y]
if (!tile.mask || tile.color === 0) return data if (!tile.mask || tile.color === 0) return data

View file

@ -1,20 +1,12 @@
import * as util from "../util.js" import * as util from "../util.js"
const searchLoc = 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,1], [0,1], [1,1],
[-1,0], [1,0], [-1,0], [1,0],
[-1,-1],[0,-1],[1,-1] [-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; var minesPlanted = 0;
while (minesPlanted < world.mines || !(minesPlanted <= world.width*world.height-15)) { while (minesPlanted < world.mines || !(minesPlanted <= world.width*world.height-15)) {
const x = util.randomNumber(0,world.width) const x = util.randomNumber(0,world.width)

View file

@ -5,7 +5,7 @@ const searchLoc =
[-1,-1],[0,-1],[1,-1] [-1,-1],[0,-1],[1,-1]
] ]
// Place Numbers // Place Numbers
export function mark(world) { export function mark(world:any):any {
for(let x = 0; x < world.width; x++){ for(let x = 0; x < world.width; x++){
for(let y = 0; y < world.height; y++){ for(let y = 0; y < world.height; y++){

View file

@ -1,5 +1,5 @@
export function obfuscate(world) { export function obfuscate(world:any):any {
let tempWorld = world; let tempWorld = world;
for(let x = 0; x < tempWorld.width; x++){ for(let x = 0; x < tempWorld.width; x++){
for(let y = 0; y < tempWorld.height; y++){ for(let y = 0; y < tempWorld.height; y++){

View file

@ -1,6 +1,5 @@
import * as log from "../log.js"
import * as revealer from "./revealer.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]; let tile = data[x][y];
if (tile.mask === true) { if (tile.mask === true) {
if (tile.mask && tile.flag === 0) { if (tile.mask && tile.flag === 0) {

View file

@ -6,11 +6,11 @@ const searchLoc =
[-1,-1],[0,-1],[1,-1] [-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) { if (data[x][y].type !== 5) {
var toSearch = []; var toSearch:any[] = [];
var searchedLocations = []; var searchedLocations:any[] = [];
toSearch.push([x, y]) toSearch.push([x, y])
if (data[x][y].type === 1) { if (data[x][y].type === 1) {

View file

@ -1,12 +1,18 @@
import * as obfuscater from "./obfuscator.js"; import * as obfuscater from "./obfuscator.js";
import * as generator from "./generator.js" import * as generator from "./generator.js"
import * as marker from "./marker.js" import * as marker from "./marker.js"
import * as log from "../log.js"
import * as flagger from "./flagger.js" import * as flagger from "./flagger.js"
import * as placer from "./placer.js" import * as placer from "./placer.js"
export class World { 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.mines = options.mines
this.width = options.width*1 this.width = options.width*1
this.height = options.height*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 (this.isGenerated) {
if (mode === 2) { if (mode === 2) {
this.data = flagger.flag(x, y, this.data, player) this.data = flagger.flag(x, y, this.data, player)
} }
if (mode === 0) { if (mode === 0) {
this.data = placer.place(x, y, this.data, player) this.data = placer.place(x, y, this.data)
} }
} else { } else {
this.generate(x, y).mark(); 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) { generate(x:number, y:number) {
if (this.isGenerated) return log.log("Already Generated"); if (this.isGenerated) return console.log("Already Generated");
return generator.generate(x, y, this); return generator.generate(x, y, this);
} }
mark() { mark() {
if (this.isMarked) return log.log("Already Marked!"); if (this.isMarked) return console.log("Already Marked!");
return marker.mark(this); return marker.mark(this);
} }
obfuscate() { obfuscate() {
if (this.isObfuscated) return log.log("already done") if (this.isObfuscated) return console.log("already done")
const tempWorld = JSON.parse(JSON.stringify(this));; const tempWorld = JSON.parse(JSON.stringify(this));;
return obfuscater.obfuscate(tempWorld); return obfuscater.obfuscate(tempWorld);
} }