Skip to content

Commit

Permalink
chore: 增加应答时对内存数据的修改,使 Promise.then() 中拿到的数据为最新的 (#176)
Browse files Browse the repository at this point in the history
* chore: 增加应答时对内存数据的修改,使 Promise.then() 中拿到的数据为最新的

* v0.18.0-alpha.5
  • Loading branch information
oneRain authored Feb 27, 2019
1 parent d600933 commit 5939707
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 40 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@leancloud/play",
"version": "0.18.0-alpha.4",
"version": "0.18.0-alpha.5",
"protocolVersion": 0,
"description": "LeanCloud Play SDK",
"main": "dist/play-node.js",
Expand Down
24 changes: 12 additions & 12 deletions src/GameConnection.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,34 +82,34 @@ export default class GameConnection extends Connection {
await super.send(msg, undefined, false);
}

async setRoomOpened(opened) {
setRoomOpened(opened) {
const msg = {
cmd: 'conv',
op: 'open',
toggle: opened,
};
await super.send(msg, undefined, false);
return super.send(msg, undefined, false);
}

async setRoomVisible(visible) {
setRoomVisible(visible) {
const msg = {
cmd: 'conv',
op: 'visible',
toggle: visible,
};
await super.send(msg, undefined, false);
return super.send(msg, undefined, false);
}

async setMaster(newMasterId) {
setMaster(newMasterId) {
const msg = {
cmd: 'conv',
op: 'update-master-client',
masterActorId: newMasterId,
};
await super.send(msg, undefined, false);
return super.send(msg, undefined, false);
}

async kickPlayer(actorId, code, msg) {
kickPlayer(actorId, code, msg) {
const req = {
cmd: 'conv',
op: 'kick',
Expand All @@ -118,7 +118,7 @@ export default class GameConnection extends Connection {
appCode: code,
appMsg: msg,
};
await super.send(req, undefined, false);
return super.send(req, undefined, false);
}

async sendEvent(eventId, eventData, options) {
Expand All @@ -132,7 +132,7 @@ export default class GameConnection extends Connection {
await super.send(msg, false);
}

async setRoomCustomProperties(properties, expectedValues) {
setRoomCustomProperties(properties, expectedValues) {
const msg = {
cmd: 'conv',
op: 'update',
Expand All @@ -141,10 +141,10 @@ export default class GameConnection extends Connection {
if (expectedValues) {
msg.expectAttr = expectedValues;
}
await super.send(msg, undefined, false);
return super.send(msg, undefined, false);
}

async setPlayerCustomProperties(actorId, properties, expectedValues) {
setPlayerCustomProperties(actorId, properties, expectedValues) {
const msg = {
cmd: 'conv',
op: 'update-player-prop',
Expand All @@ -154,7 +154,7 @@ export default class GameConnection extends Connection {
if (expectedValues) {
msg.expectAttr = expectedValues;
}
await super.send(msg, undefined, false);
return super.send(msg, undefined, false);
}

_getPingDuration() {
Expand Down
56 changes: 43 additions & 13 deletions src/PlayFSM.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,11 +237,13 @@ const PlayFSM = machina.Fsm.extend({
});
});
this._gameConn.on(ROOM_OPEN_CHANGED_EVENT, open => {
this._play._room._opened = open;
this._play.emit(Event.ROOM_OPEN_CHANGED, {
opened: open,
});
});
this._gameConn.on(ROOM_VISIBLE_CHANGED_EVENT, visible => {
this._play._room._visible = visible;
this._play.emit(Event.ROOM_VISIBLE_CHANGED, {
visible,
});
Expand Down Expand Up @@ -332,38 +334,66 @@ const PlayFSM = machina.Fsm.extend({
},

setRoomOpened(opened) {
return this._gameConn.setRoomOpened(opened);
return this._gameConn.setRoomOpened(opened).then(
tap(res => {
const { toggle } = res;
this._play._room._opened = toggle;
})
);
},

setRoomVisible(visible) {
return this._gameConn.setRoomVisible(visible);
return this._gameConn.setRoomVisible(visible).then(
tap(res => {
const { toggle } = res;
this._play._room._visible = toggle;
})
);
},

setMaster(newMasterId) {
return this._gameConn.setMaster(newMasterId);
return this._gameConn.setMaster(newMasterId).then(
tap(res => {
const { masterActorId } = res;
this._play._room._masterActorId = masterActorId;
})
);
},

kickPlayer(actorId, code, msg) {
return this._gameConn.kickPlayer(actorId, code, msg);
return this._gameConn.kickPlayer(actorId, code, msg).then(
tap(res => {
const { targetActorId } = res;
this._play._room._removePlayer(targetActorId);
})
);
},

sendEvent(eventId, eventData, options) {
return this._gameConn.sendEvent(eventId, eventData, options);
},

setRoomCustomProperties(properties, expectedValues) {
return this._gameConn.setRoomCustomProperties(
properties,
expectedValues
);
return this._gameConn
.setRoomCustomProperties(properties, expectedValues)
.then(
tap(res => {
const { attr } = res;
this._play._room._mergeProperties(attr);
})
);
},

setPlayerCustomProperties(actorId, properties, expectedValues) {
return this._gameConn.setPlayerCustomProperties(
actorId,
properties,
expectedValues
);
return this._gameConn
.setPlayerCustomProperties(actorId, properties, expectedValues)
.then(
tap(res => {
const { actorId: aId, attr } = res;
const player = this._play._room.getPlayer(aId);
player._mergeProperties(attr);
})
);
},

close() {
Expand Down
22 changes: 15 additions & 7 deletions test/ChangeProperties.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import d from 'debug';
import Event from '../src/Event';

// import CreateRoomFlag from '../src/CreateRoomFlag';
import { newPlay } from './Utils';
import { newPlay, newQCloudPlay } from './Utils';

const { expect } = require('chai');

Expand All @@ -12,8 +12,8 @@ describe('test change properties', () => {
it('test change room properties', () =>
new Promise(async resolve => {
const roomName = 'tcp0_r';
const p0 = newPlay('tcp0_0');
const p1 = newPlay('tcp0_1');
const p0 = newQCloudPlay('tcp0_0');
const p1 = newQCloudPlay('tcp0_1');
let f0 = false;
let f1 = false;
await p0.connect();
Expand Down Expand Up @@ -46,7 +46,11 @@ describe('test change properties', () => {
title: 'room311',
gold: 1000,
};
p1.room.setCustomProperties(props);
await p1.room.setCustomProperties(props);
debug(`current room title: ${props.title}`);
expect(props.title).to.be.equal('room311');
debug(`current room gold: ${props.gold}`);
expect(props.gold).to.be.equal(1000);
}));

it('test change room properties with cas', () =>
Expand Down Expand Up @@ -109,8 +113,8 @@ describe('test change properties', () => {
it('test change player properties', () =>
new Promise(async resolve => {
const roomName = 'tcp2_r';
const p0 = newPlay('tcp2_0');
const p1 = newPlay('tcp2_1');
const p0 = newQCloudPlay('tcp2_0');
const p1 = newQCloudPlay('tcp2_1');
let f0 = false;
let f1 = false;

Expand Down Expand Up @@ -170,7 +174,11 @@ describe('test change properties', () => {
props.poker = poker;
const arr = [true, 111, poker];
props.arr = arr;
p1.player.setCustomProperties(props);
await p1.player.setCustomProperties(props);
debug(`current nickname: ${props.nickname}`);
expect(props.nickname).to.be.equal('Li Lei');
debug(`current gold: ${props.gold}`);
expect(props.gold).to.be.equal(1000);
}));

it('test change player properties with cas', () =>
Expand Down
13 changes: 9 additions & 4 deletions test/CreateRoom.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { newPlay } from './Utils';
import { newPlay, newQCloudPlay } from './Utils';
import { error } from '../src/Logger';
import Event from '../src/Event';

const { expect } = require('chai');
const debug = require('debug')('Test:CreateRoom');

describe('test create room', () => {
it('test null name room', async () => {
Expand Down Expand Up @@ -109,20 +110,24 @@ describe('test create room', () => {

it('test room open and visible', async () =>
new Promise(async resolve => {
const p = newPlay('cr6');
const p = newQCloudPlay('cr6');
await p.connect();
await p.createRoom();
p.on(Event.ROOM_OPEN_CHANGED, data => {
const { opened } = data;
expect(opened).to.be.equal(false);
expect(p.room.opened).to.be.equal(false);
});
p.on(Event.ROOM_VISIBLE_CHANGED, async data => {
const { visible } = data;
expect(visible).to.be.equal(false);
expect(p.room.visible).to.be.equal(false);
await p.close();
resolve();
});
p.setRoomOpened(false);
p.setRoomVisible(false);
await p.setRoomOpened(false);
debug(`current room opened: ${p.room.opened}`);
await p.setRoomVisible(false);
debug(`current room visible: ${p.room.visible}`);
}));
});
2 changes: 2 additions & 0 deletions test/Kick.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ describe('test kick', () => {
}
});

expect(p0.room.playerList.length).to.be.equal(2);
await p0.kickPlayer(p1.player.actorId);
expect(p0.room.playerList.length).to.be.equal(1);
f0 = true;
if (f0 && f1) {
debug('f0 close');
Expand Down
10 changes: 7 additions & 3 deletions test/Master.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Event from '../src/Event';
import CreateRoomFlag from '../src/CreateRoomFlag';
import { newPlay } from './Utils';
import { newPlay, newQCloudPlay } from './Utils';

const { expect } = require('chai');
const debug = require('debug')('Test:Master');
Expand All @@ -9,8 +9,8 @@ describe('test master', () => {
it('test set new master', async () =>
new Promise(async resolve => {
const roomName = 'tm0_r';
const p0 = newPlay('tm0_0');
const p1 = newPlay('tm0_1');
const p0 = newQCloudPlay('tm0_0');
const p1 = newQCloudPlay('tm0_1');
let f0 = false;
let f1 = false;

Expand Down Expand Up @@ -38,7 +38,11 @@ describe('test master', () => {
resolve();
}
});
expect(p0.room.masterId).to.be.equal(p0.player.actorId);
debug(`before master id: ${p0.room.masterId}`);
await p0.setMaster(p1.player.actorId);
expect(p0.room.masterId).to.be.equal(p1.player.actorId);
debug(`after master id: ${p0.room.masterId}`);
}));

it('test master leave', () =>
Expand Down

0 comments on commit 5939707

Please sign in to comment.