Skip to content

Commit

Permalink
Merge branch 'release/v1.2.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
kobalab committed Feb 16, 2024
2 parents 8390c2d + 0f92cb3 commit 83276a3
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 8 deletions.
4 changes: 4 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## v1.2.0 / 2024-02-16

- 時間切れの処理を追加

### v1.1.2 / 2024-02-15

- 不正なパラメータでサーバーが異常終了するバグを修正
Expand Down
36 changes: 35 additions & 1 deletion lib/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,25 @@

const Majiang = require('@kobalab/majiang-core');

function get_timer(type, limit, allowed = 0, wait) {
if (type == 'jieju') return;
if (type.match(/^(kaiju|hule|pingju)$/)) return wait ? [ wait ] : null;
else return [ limit, allowed ];
}

module.exports = class Game extends Majiang.Game {

constructor(socks, callback, rule, title) {
constructor(socks, callback, rule, title, timer) {

super(socks, callback, rule, title);

this._model.title = this._model.title.replace(/\n/, ': ネット対戦\n');
this._model.player = socks.map(s=>s ? s.request.user.name : '(NOP)');
this._uid = socks.map(s=>s ? s.request.user.uid : null);
this._seq = 0;
this._timer = timer;
this._time_allowed = [];
this._time_limit = [];
socks.forEach(s=>this.connect(s));
}

Expand Down Expand Up @@ -74,6 +83,15 @@ module.exports = class Game extends Majiang.Game {
for (let l = 0; l < 4; l++) {
let id = this._model.player_id[l];
msg[l].seq = this._seq;
if (this._timer) {
msg[l].timer = get_timer(type, this._timer[0],
this._time_allowed[id],
this._timer[2]);
this._time_limit[id] = msg[l].timer
? Date.now() + msg[l].timer.reduce((x, y) => x + y) * 1000
+ 200
: null;
}
if (this._players[id])
this._players[id].emit('GAME', msg[l]);
else this._reply[id] = {};
Expand All @@ -87,6 +105,15 @@ module.exports = class Game extends Majiang.Game {

reply(id, reply) {
if (reply.seq != this._seq) return;
if (this._time_limit[id]) {
let allowed = (this._time_limit[id] - Date.now()) / 1000;
if (allowed < 0) this._reply[id] = {};
if (! this._status.match(/^(kaiju|hule|pingju)$/)) {
this._time_allowed[id]
= Math.ceil(Math.min(Math.max(allowed, 0),
this._time_allowed[id]));
}
}
this._reply[id] = reply;
if (this._status == 'jieju') {
if (this._players[id]) this._players[id].emit('END', this._paipu);
Expand All @@ -104,4 +131,11 @@ module.exports = class Game extends Majiang.Game {
}
this.notify_players('say', msg);
}

qipai(shan) {
if (this._timer)
this._time_allowed = [ this._timer[1], this._timer[1],
this._timer[1], this._timer[1] ];
super.qipai(shan);
}
}
11 changes: 7 additions & 4 deletions lib/room.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ function connect(sock) {
USER[user.uid].sock = sock;
let room_no = USER[user.uid].room_no;
send_room_info(room_no);
sock.on('START', (room_no, rule)=> start(sock, room_no, rule));
sock.on('START', (room_no, rule, timer)=>
start(sock, room_no, rule, timer));
if (ROOM[room_no].game) ROOM[room_no].game.connect(sock);
}
sock.on('disconnect', (reason)=> disconnect(sock));
Expand Down Expand Up @@ -173,7 +174,8 @@ function room(sock, room_no, uid) {
debug_log('ROOM:', Object.keys(ROOM).map(
room_no => (ROOM[room_no].game ? '* ' : '') + room_no));
send_room_info(room_no);
sock.on('START', (room_no, rule)=> start(sock, room_no, rule));
sock.on('START', (room_no, rule, timer)=>
start(sock, room_no, rule, timer));
}
status_log();
}
Expand All @@ -195,7 +197,7 @@ function send_room_info(room_no) {
uid => (USER[uid].sock ? '+ ' : '- ') + USER[uid].user.name));
}

function start(sock, room_no, rule) {
function start(sock, room_no, rule, timer) {

let user = get_user(sock);

Expand All @@ -212,7 +214,8 @@ function start(sock, room_no, rule) {
room_no => (ROOM[room_no].game ? '* ' : '') + room_no));
status_log();
};
ROOM[room_no].game = new Game(get_socks(room_no), callback, rule);
ROOM[room_no].game
= new Game(get_socks(room_no), callback, rule, null, timer);
ROOM[room_no].game.speed = 2;
ROOM[room_no].game.kaiju();
debug_log('>> start:', room_no);
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@kobalab/majiang-server",
"version": "1.1.2",
"version": "1.2.0",
"description": "麻雀サーバー",
"publishConfig": {
"access": "public"
Expand Down

0 comments on commit 83276a3

Please sign in to comment.