Skip to content

Commit

Permalink
- moved folder structure to scaffold libs
Browse files Browse the repository at this point in the history
- moved example in folder
- moved libs to their own folder
- improved exporting for server and client libs
  • Loading branch information
vikkio88 committed Jan 21, 2020
1 parent 6ea5264 commit ab20393
Show file tree
Hide file tree
Showing 65 changed files with 2,492 additions and 361 deletions.
36 changes: 20 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
# ws-gamelib
some tests with websockets games
## Backend
```
npm install
```
# Strummulu
a small fullstack library to serve web-browser games based on websockets communication
# Docs
coming soon...

```
npm run dev
```
## Examples

you can fuind some examples in the `example/` folder.

### Revorbaro
Frontend developed in **React**.

Revorbaro is like Rock, Paper, Scissor but with a gun.

You can **shoot** only if you have a bullet loaded, you will have a bullet loaded if you **reload** and you can **defend**.

- 2 x **shoot** is a draw
- **shoot** can be blocked by **defend**
- if you **shoot** while someone else **reload**, you win.

it is running at [https://revorbaro-ws.surge.sh](https://revorbaro-ws.surge.sh)



## Frontend
```
npm install
```

```
npm start
```
40 changes: 0 additions & 40 deletions backend/index.js

This file was deleted.

File renamed without changes.
2 changes: 2 additions & 0 deletions client/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { default as strummulu } from './strummulu';
export { default as messageHandler } from './messageHandler';
File renamed without changes.
10 changes: 10 additions & 0 deletions client/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "strummulu-client",
"version": "0.1.0",
"dependencies": {
"socket.io-client": "^2.3.0"
},
"scripts": {
"test": ""
}
}
63 changes: 63 additions & 0 deletions client/strummulu.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import openSocket from 'socket.io-client';

const logger = msg => console.log(msg);
const CLIENT_EVENTS = {
CREATE_ROOM: 'createRoom',
JOIN_ROOM: 'joinRoom',
LEAVE_ROOM: 'leaveRoom',
ACTION: 'action'
};

const SERVER_EVENTS = {
MESSAGE: 'message',
ERROR: 'errorMessage',
STATE_UPDATE: 'stateUpdate'
};

const defaultConfig = {
[SERVER_EVENTS.MESSAGE]: logger,
[SERVER_EVENTS.ERROR]: logger,
};

const strummulu = serverUrl => {
return {
EVENTS: SERVER_EVENTS,

init(config = {}) {
this.socket = openSocket(serverUrl);
config = { ...defaultConfig, ...config }
this.bindEvents(config);
},

getId() {
return this.socket ? this.socket.id : null;
},

bindEvents(config) {
for (const message in SERVER_EVENTS) {
const event = SERVER_EVENTS[message];
const handler = config[event] || logger;
this.socket.on(event, handler);
}
},



createRoom() {
this.socket.emit(CLIENT_EVENTS.CREATE_ROOM, { me: this.getId() });
},
joinRoom(roomId) {
this.socket.emit(CLIENT_EVENTS.JOIN_ROOM, { roomId });
},
leaveRoom(roomId) {
this.socket.emit(CLIENT_EVENTS.LEAVE_ROOM, { roomId });
},
sendAction(type, roomId, payload = {}) {
this.socket.emit(CLIENT_EVENTS.ACTION, { type, roomId, payload })
}

}
};


export default strummulu;
24 changes: 24 additions & 0 deletions examples/revorbaroReact/backend/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# production
/build

# misc
.DS_Store
.env
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const GameLogicInterface = require('../game/logic/GameLogicInterface');
const GameLogicInterface = require('strummulu-server/game/logic/GameLogicInterface');

const ACTIONS = {
SHOOT: 'shoot',
DEFEND: 'defend',
Expand Down
13 changes: 13 additions & 0 deletions examples/revorbaroReact/backend/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const { strummuluServerFactory, GAME_TYPES, Server, gameRoomFactory } = require('strummulu-server');
const RevorbaroGameLogic = require('./GameLogic/RevorbaroGameLogic');
const port = process.env.PORT || 5000;

const gameServer = new Server({
roomFactory: (client, data) => gameRoomFactory(GAME_TYPES.TWO_PLAYERS_TURN_BASED, new RevorbaroGameLogic(), client)
});

const server = strummuluServerFactory(gameServer);


server.listen(port);
console.log('game server listening on ', port);
Loading

0 comments on commit ab20393

Please sign in to comment.