Skip to content

Commit

Permalink
Add simple bots (#11)
Browse files Browse the repository at this point in the history
* Add bot moves logic

* Fix claim selection

* Fix tests

* Add fill with bots to API

* Add fill with bots button

* Add usernames to API

* Fix tests for names

* Finish implementing usernames

* Add reconnecting after user disconnects
  • Loading branch information
neelsomani authored Apr 26, 2020
1 parent a921695 commit 002705a
Show file tree
Hide file tree
Showing 19 changed files with 420 additions and 133 deletions.
32 changes: 11 additions & 21 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,21 @@
import json
import os
from threading import Event, Thread

from flask import Flask, request
from flask_sockets import Sockets
import gevent

from backend import RoomManager
import util

app = Flask(__name__, static_folder='build/', static_url_path='/')
app.debug = 'DEBUG' in os.environ

sockets = Sockets(app)
room_manager = RoomManager()


def call_repeatedly(interval, func, *args):
stopped = Event()

def loop():
while not stopped.wait(interval):
func(*args)
Thread(target=loop).start()
return stopped.set


call_repeatedly(RoomManager.DELETE_ROOMS_AFTER_MIN * 60,
room_manager.delete_unused_rooms)
room_manager = RoomManager(app.logger)
util.schedule(RoomManager.DELETE_ROOMS_AFTER_MIN * 60,
room_manager.delete_unused_rooms,
repeat=True)


@app.route('/')
Expand Down Expand Up @@ -63,21 +52,22 @@ def submit(ws):
continue

app.logger.info('Handling message: {}'.format(msg))
room_manager.handle_message(user_msg, app.logger)
room_manager.handle_message(user_msg)


@sockets.route('/receive')
def receive(ws):
""" Register the WebSocket to send messages to the client. """
game_uuid, player_uuid, n_players = (
game_uuid, player_uuid, n_players, username = (
request.args.get('game_uuid'),
request.args.get('player_uuid'),
request.args.get('n_players')
request.args.get('n_players'),
request.args.get('username')
)
room_manager.join_game(client=ws,
logger=app.logger,
player_uuid=player_uuid,
game_uuid=game_uuid,
n_players=n_players)
n_players=n_players,
username=username)
while not ws.closed:
gevent.sleep(0.1)
Loading

0 comments on commit 002705a

Please sign in to comment.