Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ support custom online game names, add menu system, config persistence #10

Merged
merged 1 commit into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,5 @@ dist
.tern-port

.DS_Store

netrisse.config
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ a network version of tetris for the console/terminal
## roadmap

* [x] single player mode
* [ ] two-player networked mode
* [x] two-player networked mode
* [ ] three/four player networked mode

## inspiration
Expand Down
1 change: 1 addition & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ const ultraMegaConfig = require('eslint-config-ultra-mega');

module.exports = [
...ultraMegaConfig,
{ languageOptions: { globals: { screen: 'off' } } },
];
53 changes: 28 additions & 25 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 15 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,18 @@
"name": "netrisse",
"description": "a network version of tetris for the console/terminal",
"version": "2.0.0",
"author": "Chris de Almeida",
"main": "./src/netrisse.js",
"bin": {
"netrisse": "./src/netrisse.js"
},
"scripts": {
"circles": "madge --extensions js --circular .",
"lint": "eslint .",
"lint:fix": "eslint --fix .",
"netrisse": "node ./src/netrisse.js",
"test": "npm run lint && npm run circles && node ./test/algorithm-test.js"
},
"dependencies": {
"mersenne-twister": "^1.1.0",
"netrisse-lib": "^2.0.0",
Expand All @@ -14,35 +22,29 @@
"uuid": "^9.0.0",
"ws": "^8.9.0"
},
"scripts": {
"circles": "madge --extensions js --circular .",
"lint": "eslint .",
"lint:fix": "eslint --fix .",
"test": "npm run lint && npm run circles && node ./test/algorithm-test.js"
"devDependencies": {
"eslint": "^9.4.0",
"eslint-config-ultra-mega": "^1.2.0",
"madge": "^7.0.0"
},
"repository": {
"type": "git",
"url": "git+https://github.com/ctcpip/netrisse.git"
},
"author": "Chris de Almeida",
"license": "GPL-3.0-or-later",
"bugs": {
"url": "https://github.com/ctcpip/netrisse/issues"
},
"homepage": "https://github.com/ctcpip/netrisse#readme",
"devDependencies": {
"eslint": "^9.3.0",
"eslint-config-ultra-mega": "^1.2.0",
"madge": "^7.0.0"
},
"pkg": {
"assets": "node_modules/terminal-kit/**/*"
},
"license": "GPL-3.0-or-later",
"engines": {
"node": ">=18"
},
"keywords": [
"netris",
"netrisse"
"netrisse",
"tetris"
]
}
4 changes: 2 additions & 2 deletions src/board.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module.exports = class Board {
linesCleared = 0;
gameOver = false;

constructor(top, right, bottom, left, screen, game, seed, playerID) {
constructor({ top, right, bottom, left }, screen, game, seed, playerID) {
this.top = top;
this.right = right;
this.bottom = bottom;
Expand Down Expand Up @@ -451,7 +451,7 @@ module.exports = class Board {
resetAutoMoveTimer() {
if (!this.replay && this.isMainBoard) {
this.stopAutoMoveTimer();
this.currentTimeout = setTimeout(this.moveShapeAutomatically.bind(this), this.game.interval);
this.currentTimeout = setTimeout(this.moveShapeAutomatically.bind(this), this.game.speed);
}
}

Expand Down
9 changes: 6 additions & 3 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@ module.exports = class NetrisseClient {
connect(seed) {
this.ws = new WS(`ws://${this.server}`);

this.ws.on('error', err => {
throw new Error(err); // do something here
});
const { promise, resolve, reject } = Promise.withResolvers();

this.ws.on('error', reject);

this.ws.on('open', () => {
this.sendMessage(messageTypeEnum.CONNECT, { seed });
resolve();
});

return promise;
}

disconnect() {
Expand Down
76 changes: 76 additions & 0 deletions src/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
const fs = require('node:fs');

class Config {
// can't use private properties with proxies 😿
_customServer;
_gameName;
_playerName = 'The Unknown Netrisser';
_speed = 0.5 * 1000;

get customServer() {
return this._customServer;
}

set customServer(customServer) {
this._customServer = customServer;
}

get gameName() {
return this._gameName;
}

set gameName(gameName) {
if (!gameName) {
throw new Error('game name is required');
}
this._gameName = gameName;
}

get playerName() {
return this._playerName;
}

set playerName(playerName) {
if (playerName) {
this._playerName = playerName;
}
}

get speed() {
return this._speed;
}

set speed(speed) {
this._speed = speed;
}

save() {
try {
fs.writeFileSync(configFile, JSON.stringify(this));
}
catch (error) { // eslint-disable-line no-unused-vars
// debug(error)
}
}
};

const configFile = 'netrisse.config';
const config = new Config();

try {
const savedConfig = JSON.parse(fs.readFileSync(configFile));
Object.assign(config, savedConfig);
}
catch (error) { // eslint-disable-line no-unused-vars
// debug(error)
}

const handler = {
set(target, property, value) {
target[property] = value;
target.save();
return true;
},
};

module.exports = new Proxy(config, handler);
4 changes: 2 additions & 2 deletions src/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ module.exports = class Game {
boards = [];
started = false;

constructor(interval, algorithm, client, currentPlayerID) {
this.interval = interval;
constructor(speed, algorithm, client, currentPlayerID) {
this.speed = speed;
this.algorithm = algorithm;
this.client = client;
this.currentPlayerID = currentPlayerID;
Expand Down
36 changes: 36 additions & 0 deletions src/intro.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const config = require('./config');
const start = require('./start');
const quit = require('./quit');
const multiplayer = require('./multiplayer');

module.exports = function intro(screen, seed) {
const mainBoardPosition = { top: 2, right: 21, bottom: 23, left: 0 };

let cursorY = 3;

screen.clear();
screen.d(2, cursorY, '✨ Welcome to Netrisse! 🎉', { color: 'green' });
screen.d(5, cursorY += 3, '1) Single player 🧍', { color: 'amber' });
screen.d(5, cursorY += 2, '2) Multiplayer 🧑‍🤝‍🧑', { color: 'amber' });
screen.d(5, cursorY += 2, '3) Options 🔧', { color: 'amber' });
screen.d(5, cursorY += 2, 'Q) Quit 🚪', { color: 'brightred' });
screen.render();

const keyHandler = name => {
switch (name) {
case '1':
screen.term.removeListener('key', keyHandler);
start(seed, screen, mainBoardPosition, config.speed, null);
break;
case '2':
screen.term.removeListener('key', keyHandler);
multiplayer(screen, seed, intro, mainBoardPosition);
break;
case 'CTRL_C': case 'ESCAPE': case 'Q': case 'q':
quit(screen, mainBoardPosition);
break;
}
};

screen.term.on('key', keyHandler);
};
Loading
Loading