Skip to content

Commit

Permalink
Merge branch 'release/v0.1.3'
Browse files Browse the repository at this point in the history
  • Loading branch information
kobalab committed Dec 8, 2021
2 parents 737681c + 516fa33 commit 692740a
Show file tree
Hide file tree
Showing 12 changed files with 121 additions and 25 deletions.
14 changes: 14 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
### v0.1.3 / 2021-12-09

- Majiang.Shoupai
- インスタンスメソッド fromString() を追加
- Majiang.Game
- 対局終了時に呼び出す callback の引数に牌譜を渡すよう変更
- Majiang.Player との待ち合わせ時間を変更
- ロン和了の際にロン牌を手牌に加えているバグを修正
- 和了・流局時に開かれた手牌が卓情報に反映されないバグを修正
- Majiang.Board
- パラメータなしでインスタンスが生成できるよう修正
- Majiang.Player
- コンストラクタで空の卓情報を生成するよう修正

### v0.1.2 / 2021-11-17

- Majiang.Player
Expand Down
11 changes: 11 additions & 0 deletions lib/board.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ class Shan {
module.exports = class Board {

constructor(kaiju) {
if (kaiju) this.kaiju(kaiju);
}

kaiju(kaiju) {

this.title = kaiju.title;
this.player = kaiju.player;
Expand Down Expand Up @@ -102,6 +106,9 @@ module.exports = class Board {
}

hule(hule) {
let shoupai = this.shoupai[hule.l];
shoupai.fromString(hule.shoupai);
if (hule.baojia != null) shoupai.dapai(shoupai.get_dapai().pop());
if (this._fenpei) {
this.changbang = 0;
this.lizhibang = 0;
Expand All @@ -115,5 +122,9 @@ module.exports = class Board {

pingju(pingju) {
if (! pingju.name.match(/^/)) this.lizhi();
for (let l = 0; l < 4; l++) {
if (pingju.shoupai[l])
this.shoupai[l].fromString(pingju.shoupai[l]);
}
}
}
18 changes: 9 additions & 9 deletions lib/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ module.exports = class Game {
else if (this._status == 'gangzimo') this.reply_zimo();
else if (this._status == 'hule') this.reply_hule();
else if (this._status == 'pingju') this.reply_pingju();
else this._callback();
else this._callback(this._paipu);
}

do_sync() {
Expand All @@ -154,7 +154,7 @@ module.exports = class Game {
else break;
}

this._callback();
this._callback(this._paipu);

return this;
}
Expand Down Expand Up @@ -188,7 +188,7 @@ module.exports = class Game {
}
}));
}
this.call_players('kaiju', msg);
this.call_players('kaiju', msg, 0);

if (this._view) this._view.kaiju();
}
Expand Down Expand Up @@ -250,7 +250,7 @@ module.exports = class Game {
if (i != l) msg[l].qipai.shoupai[i] = '';
}
}
this.call_players('qipai', msg);
this.call_players('qipai', msg, 0);

if (this._view) this._view.redraw();
}
Expand Down Expand Up @@ -444,7 +444,7 @@ module.exports = class Game {
? this._gang[0] + this._gang.substr(-1)
: this._dapai.substr(0,2)
) + '_+=-'[(4 + model.lunban - menfeng) % 4];
let shoupai = model.shoupai[menfeng];
let shoupai = model.shoupai[menfeng].clone();
let fubaopai = shoupai.lizhi ? model.shan.fubaopai : null;

let param = {
Expand Down Expand Up @@ -499,7 +499,7 @@ module.exports = class Game {
for (let l = 0; l < 4; l++) {
msg[l] = JSON.parse(JSON.stringify(paipu));
}
this.call_players('hule', msg);
this.call_players('hule', msg, this._wait);

if (this._view) this._view.update(paipu);
}
Expand Down Expand Up @@ -580,7 +580,7 @@ module.exports = class Game {
for (let l = 0; l < 4; l++) {
msg[l] = JSON.parse(JSON.stringify(paipu));
}
this.call_players('pingju', msg);
this.call_players('pingju', msg, this._wait);

if (this._view) this._view.update(paipu);
}
Expand Down Expand Up @@ -670,7 +670,7 @@ module.exports = class Game {
for (let l = 0; l < 4; l++) {
msg[l] = JSON.parse(JSON.stringify(paipu));
}
this.call_players('jieju', msg);
this.call_players('jieju', msg, this._wait);

if (this._view) this._view.summary(this._paipu);
}
Expand Down Expand Up @@ -713,7 +713,7 @@ module.exports = class Game {
if (this.get_dapai().find(p => p == dapai)) {
if (reply.dapai.substr(-1) == '*' && this.allow_lizhi(dapai)) {
if (this._view) this._view.say('lizhi', model.lunban);
return this.delay(()=>this.dapai(reply.dapai), 0);
return this.delay(()=>this.dapai(reply.dapai));
}
return this.delay(()=>this.dapai(dapai), 0);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* @kobalab/majiang-core v0.1.2
* @kobalab/majiang-core v0.1.3
*
* Copyright(C) 2021 Satoshi Kobayashi
* Released under the MIT license
Expand Down
10 changes: 7 additions & 3 deletions lib/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ const Majiang = {

module.exports = class Player {

constructor() {
this._model = new Majiang.Board();
}

action(msg, callback) {

this._callback = callback;
Expand Down Expand Up @@ -41,9 +45,9 @@ module.exports = class Player {
}

kaiju(kaiju) {
this._id = kaiju.id;
this._rule = kaiju.rule;
this._model = new Majiang.Board(kaiju);
this._id = kaiju.id;
this._rule = kaiju.rule;
this._model.kaiju(kaiju);

if (this._callback) this.action_kaiju(kaiju);
}
Expand Down
16 changes: 16 additions & 0 deletions lib/shoupai.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,22 @@ module.exports = class Shoupai {
return shoupai;
}

fromString(paistr) {
const shoupai = Shoupai.fromString(paistr);
this._bingpai = {
_: shoupai._bingpai._,
m: shoupai._bingpai.m.concat(),
p: shoupai._bingpai.p.concat(),
s: shoupai._bingpai.s.concat(),
z: shoupai._bingpai.z.concat(),
};
this._fulou = shoupai._fulou.concat();
this._zimo = shoupai._zimo;
this._lizhi = shoupai._lizhi;

return this;
}

decrease(s, n) {
let bingpai = this._bingpai[s];
if (bingpai[n] == 0 || n == 5 && bingpai[0] == bingpai[5]) {
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-core",
"version": "0.1.2",
"version": "0.1.3",
"description": "麻雀基本ライブラリ",
"publishConfig": {
"access": "public"
Expand Down
16 changes: 13 additions & 3 deletions test/board.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ suite('Majiang.Board', ()=>{
assert.deepEqual(board.player, ['私','下家','対面','上家']));
test('起家が設定されること', ()=>
assert.equal(board.qijia, 1));
test('パラメータなしでもインスタンスが生成できること', ()=>
assert.ok(new Majiang.Board()));
});

suite('manfeng(id)', ()=>{
Expand Down Expand Up @@ -195,7 +197,10 @@ suite('Majiang.Board', ()=>{
suite('hule(hule)', ()=>{
const board = init_board();
board.zimo({ l: 0, p: 'm1' });
board.hule({ fubaopai: ['s9'] });
board.hule({ l: 0, shoupai: 'm123p456s789z1122z2*', fubaopai: ['s9'] });
test('和了者の手牌が設定されること', ()=>{
assert.equal(board.shoupai[0], 'm123p456s789z1122z2*')
});
test('裏ドラを参照できること', ()=>
assert.equal(board.shan.fubaopai[0], 's9'));
test('ダブロンの際に持ち点の移動が反映されていること', ()=>{
Expand All @@ -219,19 +224,24 @@ suite('Majiang.Board', ()=>{
});

suite('pingju(pingju)', ()=>{
test('倒牌した手牌が設定されること', ()=>{
const board = init_board();
board.pingju({ name: '', shoupai: ['','m123p456s789z1122','','']});
assert.equal(board.shoupai[1], 'm123p456s789z1122');
});
test('リーチ宣言後の流局でリーチが成立すること', ()=>{
const board = init_board();
board.zimo({ l: 0, p: 'm1' });
board.dapai({ l: 0, p: 'm1_*' });
board.pingju({ name: '荒牌平局' });
board.pingju({ name: '荒牌平局', shoupai: [] });
assert.equal(board.defen[board.player_id[0]], 9000);
assert.equal(board.lizhibang, 5);
});
test('三家和の場合はリーチが成立しないこと', ()=>{
const board = init_board();
board.zimo({ l: 0, p: 'm1' });
board.dapai({ l: 0, p: 'm1_*' });
board.pingju({ name: '三家和' });
board.pingju({ name: '三家和', shoupai: [] });
assert.equal(board.defen[board.player_id[0]], 10000);
assert.equal(board.lizhibang, 4);
});
Expand Down
6 changes: 5 additions & 1 deletion test/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -1848,9 +1848,13 @@ suite('Majiang.Game', ()=>{

suite('_callback()', ()=>{
test('終局時にコンストラクタで指定したコールバックが呼ばれること', (done)=>{
const callback = (paipu)=>{
assert.deepEqual(paipu, game._paipu);
done();
}
const game = init_game({rule:Majiang.rule({'場数':0}),
shoupai:['m123p456s789z1122','','',''],
zimo:['z2'],callback:done});
zimo:['z2'],callback:callback});
game.zimo();
game.hule();
game.next();
Expand Down
32 changes: 27 additions & 5 deletions test/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ suite('Majiang.Player', ()=>{

test('クラスが存在すること', ()=> assert.ok(Majiang.Player));

test('インスタンスが生成できること', ()=> assert.ok(new Majiang.Player()));
suite('constructor()', ()=>{
const player = new Majiang.Player();
test('インスタンスが生成できること', ()=> assert.ok(player));
test('初期値が設定されること', ()=> assert.ok(player._model));
});

suite('kaiju(kaiju)', ()=>{
test('初期値が設定されること', ()=>{
Expand Down Expand Up @@ -175,15 +179,18 @@ suite('Majiang.Player', ()=>{
suite('hule(hule)', ()=>{
test('卓情報が更新されること', ()=>{
const player = init_player();
player.hule({ fubaopai: ['s1'] });
player.hule({ l: 1, shoupai: 'm123p456s789z1122z1*',
fubaopai: ['s1'] });
assert.equal(player._model.shoupai[1], 'm123p456s789z1122z1*')
assert.equal(player.shan.fubaopai[0], 's1');
});
});
suite('pingju(pingju)', ()=>{
test('卓情報が更新されること', ()=>{
const player = init_player();
player.dapai({ l: 1, p: 'm3*' });
player.pingju({ name: '' });
player.pingju({ name:'', shoupai:['','','','m123p456s789z1122*'] });
assert.equal(player._model.shoupai[3], 'm123p456s789z1122*');
assert.equal(player._model.lizhibang, 1);
});
});
Expand Down Expand Up @@ -403,10 +410,25 @@ suite('Majiang.Player', ()=>{
player.action({ kaigang: { baopai: 'm1' } }, error);
});
test('和了 (hule)', (done)=>{
player.action({ hule: {} }, done);
const hule = { hule: {
l: 2,
shoupai: 'p7p7,z111-,z222=,z333+,z444-',
baojia: 3,
fubaopai: null,
damanguan: 2,
defen: 64000,
hupai: [ { name: '大四喜', fanshu: '**', baojia: 0 } ],
fenpai: [ -32000, 0, 64000, -32000 ]
} };
player.action(hule, done);
});
test('流局 (pingju)', (done)=>{
player.action({ pingju: { name: '' } }, done);
const pingju = { pingju: {
name: '荒牌平局',
shoupai: [ '', 'p2234406z333555', '', 'p11223346777z77' ],
fenpai: [ -1500, 1500, -1500, 1500 ]
} };
player.action(pingju, done);
});
test('終局 (jieju)', (done)=>{
player.action({ jieju: {} }, done);
Expand Down
15 changes: 15 additions & 0 deletions test/shoupai.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,21 @@ suite('Majiang.Shoupai', ()=>{
});
});

suite('fromString(paistr)', ()=>{
test('牌姿から手牌を更新できること', ()=>
assert.equal('' + new Shoupai().fromString('m123p456s789z1122z2'),
'm123p456s789z1122z2'));
test('副露あり', ()=>
assert.equal('' + new Shoupai().fromString('m123p456s789z2,z111='),
'm123p456s789z2,z111='));
test('リーチ後', ()=>
assert.equal('' + new Shoupai().fromString('m123p456s789z1122*'),
'm123p456s789z1122*'));
test('伏せ牌あり', ()=>
assert.equal('' + new Shoupai().fromString('__________,z111='),
'__________,z111='));
});

suite('zumo(p)', ()=>{
test('萬子をツモれること', ()=>
assert.equal('' + Shoupai('m123p456s789z4567').zimo('m1'),
Expand Down

0 comments on commit 692740a

Please sign in to comment.