Coder Social home page Coder Social logo

mykeels / whot-server Goto Github PK

View Code? Open in Web Editor NEW
6.0 3.0 5.0 27 KB

A web sockets server for whot games using https://github.com/mykeels/whot

License: MIT License

JavaScript 88.70% Shell 7.31% Dockerfile 3.99%
whot nodejs http-server game card-game

whot-server's Introduction

Whot Server

An HTTP server serving a REST API for hosting Whot! games.

Setup and Usage

npm install
npm start

Routes

The following routes are available:

Category Verb Url Description
Default GET/POST ~/ Welcomes you to the API
Games GET ~/games Lists games
Games POST ~/games Creates a new Game
Game WebSockets ~/game/:id Participate in a Game

Socket Signals

Event Originator Parameters Description
player:play Client index, iNeed Player selects a card by index to play
market:pick Client -- Pleyer opts to pick from market
player:hand Server hand Shows Player's hand (cards)
game:start Server pile Start Game
turn:switch Server -- Tell Player it's his turn
pile:top Server pile Show card at the top of the pile
error:player-out-of-turn Server -- player is out of turn
error:invalid-card-index Server -- card index played is our of bounds
error:card-not-match-pile Server -- card played doesn't match card at the top of the pile
error:could-not-play-card Server -- Hopefully, this never happens, but the card could not be played for some reason

Docker

This project has been setup to use docker to create a development environment, so prepare to be dazzled. The readme assumes docker version >= 1.9.1 installed on your system.

The project contains bash scripts to simplify the interaction with docker and enable dynamic code changes. These can be found in

<project_root>/bin

To start up disposable containers use:

bin/start_disposable.sh

The command will attempt to start up containers based on a specific image. If the image cannot be found, it will be downloaded automatically. If the project's image cannot be found, it will be built from the Dockerfile automatically.

When all is complete, you will be taken directly to the shell of the container with the application started for you.

At this point the app will be accessible with base url:

http://localhost:32801

Thus your adventure begins...

Test Mock Script

Copy and paste this in your browser console:

(() => {
  const makeRandomPlay = (ws) => {
    if (ws.canPlay()) {
      const compatibles = ws.hand.filter(card => card.matches(ws.pile))
      const compatibleCardIndex = ws.hand.indexOf(compatibles[Math.floor(Math.random() * compatibles.length)])
      let iNeed = null
      if (ws.hand[compatibleCardIndex].shape === 'Whot') {
        const eligibleCards = ws.hand.filter(card => card.shape != 'Whot')
        iNeed = (eligibleCards[Math.floor(Math.random() * eligibleCards.length)] || {}).shape || 'Circle'
      }
      ws.send(JSON.stringify({ message: 'player:play', index: compatibleCardIndex, card: ws.hand[compatibleCardIndex], iNeed }))
    }
    else {
      ws.send(JSON.stringify({ message: 'market:pick' }))
    }
  }

  const setupCard = (c) => {
    c.matches = (card = new Card()) => {
      return (c.shape === 'Whot') ||
        (card.shape === 'Whot' && c.iNeed && (c.iNeed === card.shape)) || (card.shape === c.shape) || (card.value === c.value)
    }
    return c
  }

  var wss = [1, 2, 3, 4].map(function () {
    const ws = new WebSocket('ws://localhost:8800/game/1')
    ws.onmessage = (msg) => {
      try {
        const data = JSON.parse(msg.data)
        switch(data.message) {
        case 'player:hand':
          ws.hand = data.hand.map(setupCard)
          break;
        case 'game:start':
          ws.pile = setupCard(data.pile)
          break;
        case 'turn:switch':
          makeRandomPlay(ws)
          break;
        case 'player:play':
        case 'pile:top':
          ws.pile = data.card
          break;
        }
      }
      catch (ex) {
        console.error(ex)
      }
    }
    ws.onerror = (err) => console.error(err)
    ws.onopen = () => {
      console.log('WebSocket connection opened')
      ws.send(JSON.stringify({ message: 'hello' }))
    }
    ws.canPlay = () => (ws.hand.findIndex(card => card.matches(ws.pile)) >= 0)
        return ws
    })

    return wss
})()

whot-server's People

Contributors

mykeels avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

whot-server's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.