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) {
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;

19
dist/roomManager.js vendored
View file

@ -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();

View file

@ -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];

View file

@ -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;
});

View file

@ -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]

11
dist/world/world.js vendored
View file

@ -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);

View file

@ -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)

View file

@ -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;

View file

@ -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()

View file

@ -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();

View file

@ -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()

View file

@ -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;
});

View file

@ -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
}

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

View file

@ -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)

View file

@ -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++){

View file

@ -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++){

View file

@ -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) {

View file

@ -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) {

View file

@ -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);
}