29
My Project Documentation Release 1 Alexandre Z July 19, 2016

My Project Documentation · My Project Documentation, Release 1 5.4.6Asyncio and generation behavior of the bot In game framework, there is a bot. Bot to move in concentric circles,

  • Upload
    others

  • View
    36

  • Download
    0

Embed Size (px)

Citation preview

Page 1: My Project Documentation · My Project Documentation, Release 1 5.4.6Asyncio and generation behavior of the bot In game framework, there is a bot. Bot to move in concentric circles,

My Project DocumentationRelease 1

Alexandre Z

July 19, 2016

Page 2: My Project Documentation · My Project Documentation, Release 1 5.4.6Asyncio and generation behavior of the bot In game framework, there is a bot. Bot to move in concentric circles,
Page 3: My Project Documentation · My Project Documentation, Release 1 5.4.6Asyncio and generation behavior of the bot In game framework, there is a bot. Bot to move in concentric circles,

Contents

1 Framework Installation 3

2 Getting Started 5

3 Source code 7

4 Dependencies 9

5 License 115.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115.2 Low-level . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155.3 Game framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155.4 Low-level game framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16

6 Indices and tables 25

i

Page 4: My Project Documentation · My Project Documentation, Release 1 5.4.6Asyncio and generation behavior of the bot In game framework, there is a bot. Bot to move in concentric circles,

ii

Page 5: My Project Documentation · My Project Documentation, Release 1 5.4.6Asyncio and generation behavior of the bot In game framework, there is a bot. Bot to move in concentric circles,

My Project Documentation, Release 1

This asynchronous framework with a modular structure like Django. But with mongodb, jinja2, websocket out of thebox, and more than a simple barrier to entry.

Built on the basis of asyncio and aiohttp. In the framework, have batteries. The prototype of the game framework as acomponent.

Contents 1

Page 6: My Project Documentation · My Project Documentation, Release 1 5.4.6Asyncio and generation behavior of the bot In game framework, there is a bot. Bot to move in concentric circles,

My Project Documentation, Release 1

2 Contents

Page 7: My Project Documentation · My Project Documentation, Release 1 5.4.6Asyncio and generation behavior of the bot In game framework, there is a bot. Bot to move in concentric circles,

CHAPTER 1

Framework Installation

$ pip install tao1

3

Page 8: My Project Documentation · My Project Documentation, Release 1 5.4.6Asyncio and generation behavior of the bot In game framework, there is a bot. Bot to move in concentric circles,

My Project Documentation, Release 1

4 Chapter 1. Framework Installation

Page 9: My Project Documentation · My Project Documentation, Release 1 5.4.6Asyncio and generation behavior of the bot In game framework, there is a bot. Bot to move in concentric circles,

CHAPTER 2

Getting Started

Create a project anywhere:

$ utils.py -p name

Create an application in the folder of the project apps:

$ utils.py -a name

Run server:

$ python3 index.py

5

Page 10: My Project Documentation · My Project Documentation, Release 1 5.4.6Asyncio and generation behavior of the bot In game framework, there is a bot. Bot to move in concentric circles,

My Project Documentation, Release 1

6 Chapter 2. Getting Started

Page 11: My Project Documentation · My Project Documentation, Release 1 5.4.6Asyncio and generation behavior of the bot In game framework, there is a bot. Bot to move in concentric circles,

CHAPTER 3

Source code

The project is hosted on GitHub

Please feel free to file an issue on the bug tracker if you have found a bug or have some suggestion in order to improvethe library.

7

Page 12: My Project Documentation · My Project Documentation, Release 1 5.4.6Asyncio and generation behavior of the bot In game framework, there is a bot. Bot to move in concentric circles,

My Project Documentation, Release 1

8 Chapter 3. Source code

Page 13: My Project Documentation · My Project Documentation, Release 1 5.4.6Asyncio and generation behavior of the bot In game framework, there is a bot. Bot to move in concentric circles,

CHAPTER 4

Dependencies

Python 3.5+ and aiohttp

9

Page 14: My Project Documentation · My Project Documentation, Release 1 5.4.6Asyncio and generation behavior of the bot In game framework, there is a bot. Bot to move in concentric circles,

My Project Documentation, Release 1

10 Chapter 4. Dependencies

Page 15: My Project Documentation · My Project Documentation, Release 1 5.4.6Asyncio and generation behavior of the bot In game framework, there is a bot. Bot to move in concentric circles,

CHAPTER 5

License

It’s MIT licensed and freely available.

Feel free to improve this package and send a pull request to GitHub.

Contents:

5.1 Introduction

This asynchronous framework with a modular structure like Django. But with mongodb, jinja2, websocket out of thebox, and more than a simple barrier to entry.

5.1.1 Framework Installation

$ pip install tao1

5.1.2 Getting Started

Create a project anywhere:

$ utils.py -p name

Create an application in the folder of the project apps:

$ utils.py -a name

Run server:

$ python3 index.py

5.1.3 Source code

The project is hosted on GitHub

Please feel free to file an issue on the bug tracker if you have found a bug or have some suggestion in order to improvethe library.

11

Page 16: My Project Documentation · My Project Documentation, Release 1 5.4.6Asyncio and generation behavior of the bot In game framework, there is a bot. Bot to move in concentric circles,

My Project Documentation, Release 1

5.1.4 Dependencies

Python 3.5+ and aiohttp

Installation Python 3.5 for ubuntu:

sudo add-apt-repository ppa:fkrull/deadsnakessudo apt-get updatesudo apt-get install python3.5 python3.5-dev

5.1.5 Deploy

When you develop enough to run the file python3 index.py. For production to run index.py, is better to usethe supervisor and nginx. Settings supervisor in /etc:

[program:name]command=python3 index.pydirectory=/path/to/your/projectuser=nobodyautorestart=trueredirect_stderr=true

Settings nginx in /etc:

server {server_name aio.dev;location / {

proxy_pass http://127.0.0.1:8080;}

location /ws {proxy_pass http://127.0.0.1:8080;proxy_http_version 1.1;proxy_set_header Upgrade $http_upgrade;proxy_set_header Connection "upgrade";

}}

5.1.6 Structure

Project structure:

• In the locale folder there are the translation files.

• In the static folder are static files, scripts .js, styles .css, images, etc.

• In the templ folder there are the templates.

12 Chapter 5. License

Page 17: My Project Documentation · My Project Documentation, Release 1 5.4.6Asyncio and generation behavior of the bot In game framework, there is a bot. Bot to move in concentric circles,

My Project Documentation, Release 1

• In folder apps contains app.

• index.py this is run servers files.

• settings.py this is settings files.

• gunicorn.conf.py this is configuration files for gunicorn. Not mandatory.

• app.py like index.py but for gunicorn. Not mandatory.

Module structure:

5.1.7 Routes

Example route in file routes.py:

route( 'GET', '/ws', ws, 'ws' )

5.1. Introduction 13

Page 18: My Project Documentation · My Project Documentation, Release 1 5.4.6Asyncio and generation behavior of the bot In game framework, there is a bot. Bot to move in concentric circles,

My Project Documentation, Release 1

5.1.8 Templates

In framework integrated jinja2. Templates are always in the templ folder.

To call the template function templ and pass it the template name. If the template is in some sort of module, the calllooks like this apps.modul_name.templ_name.

If the template is in the root of the project in the templ folder, then simply write his name.

Example:

def page(request):return templ('index', request, {'key':'val'} )

5.1.9 Websockets

The websocket to create games and chat very easy to use.

The first is the need to call route with the template to draw the route and chat with the handler for chat:

route( 'GET', '/ws', ws, 'ws' )route( 'GET', '/wsh', ws_handler, 'ws_handler' )

These routes work you can see an example.

The second is the functions themselves. Function for render chat page

async def ws(request):return templ('apps.app:chat', request, {} )

Function handler chat:

async def ws_handler(request):ws = web.WebSocketResponse()await ws.prepare(request)async for msg in ws:

if msg.tp == aiohttp.MsgType.text:if msg.data == 'close':

await ws.close()else:

ws.send_str(msg.data + '/answer')elif msg.tp == aiohttp.MsgType.error:

print('ws connection closed with exception %s' % ws.exception())print('websocket connection closed')return ws

5.1.10 Database

To write the database query you need to request.db and then as usual.

async def test_db(request):# save docrequest.db.doc.save({"_id":"test", "status":"success"})# find docval = request.db.doc.find_one({"_id":"test"})return templ('apps.app:db_test', request, {'key':val})

14 Chapter 5. License

Page 19: My Project Documentation · My Project Documentation, Release 1 5.4.6Asyncio and generation behavior of the bot In game framework, there is a bot. Bot to move in concentric circles,

My Project Documentation, Release 1

5.1.11 Static files

Static files it is better to entrust nginx but tao1 able return files.

All files must be located in the folder static.

If they are the root of the project then the path will be like this /static/static/file_name.pg. Ifthe files are in a certain module, then the path like this /static/module_name/file_name.jpg.

5.1.12 Caching

Create cache for function 5 second, the first parameter - name

@cache("main_page", expire=5)async def page(request):

return templ('index', request, {'key':'val'} )

5.2 Low-level

pass

5.3 Game framework

5.3.1 About game framework

In this case, the game framework is a battery, the basic framework. The game is a 3D multiplayer shooting. Prototypegame framework have multiplayer mode, the mini-map, rooms, bot, several typical models of tanks. The client side iswritten in the framework WEBGL babylon.js.

Game consists of babylon.js and python 3.5.

5.3.2 Getting Started

Game start is located on the route /pregame.

5.2. Low-level 15

Page 20: My Project Documentation · My Project Documentation, Release 1 5.4.6Asyncio and generation behavior of the bot In game framework, there is a bot. Bot to move in concentric circles,

My Project Documentation, Release 1

5.4 Low-level game framework

5.4.1 Import models and blender

For games need 3D models to represent characters and landscape. For this you can use a free 3D editor blender. Modelyou can draw or import with free resources, for example here or here.

If the model will move around the map, they need to do monolithic. There are two ways. First method. To makeall elements in the model as one object. If there is only one texture. You need to select all model elements, they arehighlighted in orange. And in the object menu select join.

Second method. To associate all the elements of the model according to the principle of parent-child. This option canbe used if we have a lot of textures in the model. To do this, right-click to select alternately the parent and the child,press ctrl+P to select the menu object.

5.4.2 Loading models in the game with babylon.js and the models themselves

Loading models can be done very simply.

var loader = new BABYLON.AssetsManager(scene);mesh = loader.addMeshTask('enemy1', "", "/static/game/t3/", "enemy.babylon");

The most common operation is object cloning. For example, there are trees, so as not to load each tree, you can loadone tree to clone it around the scene with different coordinates:

var palm = loader.addMeshTask('palm', "", "/static/game/g6/", "tree.babylon");palm.onSuccess = function (task) {

var p = task.loadedMeshes[0];p.position = new BABYLON.Vector3(25, -2, 25);var p1 = p.clone('p1');p1.position = new BABYLON.Vector3(10, -2, 20);var p2 = p.clone('p2');p2.position = new BABYLON.Vector3(15, -2, 30);

};

When you load the models it is convenient to use AssetsManager. Need to avoid uploading anything during thegame, for various reasons.

A simplified scheme of loading of objects looks like this:

16 Chapter 5. License

Page 21: My Project Documentation · My Project Documentation, Release 1 5.4.6Asyncio and generation behavior of the bot In game framework, there is a bot. Bot to move in concentric circles,

My Project Documentation, Release 1

5.4. Low-level game framework 17

Page 22: My Project Documentation · My Project Documentation, Release 1 5.4.6Asyncio and generation behavior of the bot In game framework, there is a bot. Bot to move in concentric circles,

My Project Documentation, Release 1

5.4.3 Of movement, the minimap and the sound of babylon.js

To control the character used FreeCamera. This is not quite correct in the case of tanks. But in the next version ofthe game, I want to make a futuristic map like this star wars. And more convenient to use for controlling characterwill be FreeCamera.

An example of the most common cameras for games:

//FollowCameravar camera = new BABYLON.FollowCamera("camera1", new BABYLON.Vector3(0, 2, 0), scene);camera.target = mesh;

//FreeCameravar camera = new BABYLON.FreeCamera("camera1", new BABYLON.Vector3( 0, 2, 0), scene);mesh.parent = camera;

At this time, the game has the sound of a gunshot, the sound of an explosion and the sound of movement technique.

The sound of a gunshot. Occurs when mouse click: .. code-block:: javascript

var gunS = new BABYLON.Sound(“gunshot”, “static/gun.wav”, scene); win-dow.addEventListener(“mousedown”, function (e) {

if (!lock && e.button === 0) gunS.play();

});

The sound of movement technique: .. code-block:: javascript

var ms = new BABYLON.Sound(“mss”, “static/move.mp3”, scene, null, { loop: true, autoplay: false });document.addEventListener(“keydown”, function(e){

switch (e.keyCode) { case 38: case 40: case 83: case 87:

if (!ms.isPlaying) ms.play(); break; }

}); document.addEventListener(“keyup”, function(e){

switch (e.keyCode) {

case 38: case 40: case 83: case 87:

if (ms.isPlaying) ms.pause(); break;

}

});

Any shooters there is always a minimap. The minimap is done with the camera positioned above the scene. Viewfrom this camera is displayed in the right place.

var camera2 = new BABYLON.FreeCamera("minimap", new BABYLON.Vector3(0,170,0), scene);

camera2.viewport = new BABYLON.Viewport(x, y, width, height);

scene.activeCameras.push(camera);scene.activeCameras.push(camera2);

5.4.4 Backend, websocket and sync game

Connection between client and server is done using websockets. Simple scheme looks so:

18 Chapter 5. License

Page 23: My Project Documentation · My Project Documentation, Release 1 5.4.6Asyncio and generation behavior of the bot In game framework, there is a bot. Bot to move in concentric circles,

My Project Documentation, Release 1

5.4. Low-level game framework 19

Page 24: My Project Documentation · My Project Documentation, Release 1 5.4.6Asyncio and generation behavior of the bot In game framework, there is a bot. Bot to move in concentric circles,

My Project Documentation, Release 1

On the server we receive from a clien message, watch the action, and in accordance with the desired function is called,event handler:

async def game_handler(request):. . .async for msg in ws:

if msg.tp == MsgType.text:if msg.data == 'close':

await ws.close()else:

e = json.loads( msg.data )action = e['e']if action in handlers:

handler = handlers[action]handler(ws, e). . .

if action : move We transmit these coordinates to the Player class, it processes them and returns back, and webroadcast them to all other players in the room.

def h_move(me, e):me.player.set_pos(e['x'], e['y'], e['z'])mess = dict(e="move", id=me.player.id, **me.player.pos_as_dict)me.player.room.send_all(mess, except_=(me.player,))

5.4.5 Balancing players by rooms

If in the same room more players than specified in the settings, we create a new room and placed the player there. Theroom number is assigned when a player with page /pregame, comes into game.

def check_room(request):found = Nonefor _id, room in rooms.items():

if len(room.players) < 3:found = _idbreak

else:while not found:

_id = uuid4().hex[:3]if _id not in rooms: found = _id

Scheme of work with rooms:

20 Chapter 5. License

Page 25: My Project Documentation · My Project Documentation, Release 1 5.4.6Asyncio and generation behavior of the bot In game framework, there is a bot. Bot to move in concentric circles,

My Project Documentation, Release 1

5.4.6 Asyncio and generation behavior of the bot

In game framework, there is a bot. Bot to move in concentric circles, approaching the last player appeared.

Scheme of work bot

5.4. Low-level game framework 21

Page 26: My Project Documentation · My Project Documentation, Release 1 5.4.6Asyncio and generation behavior of the bot In game framework, there is a bot. Bot to move in concentric circles,

My Project Documentation, Release 1

22 Chapter 5. License

Page 27: My Project Documentation · My Project Documentation, Release 1 5.4.6Asyncio and generation behavior of the bot In game framework, there is a bot. Bot to move in concentric circles,

My Project Documentation, Release 1

5.4.7 Roadmap game

• Movement at different angles

• Space scene

• A detailed review of all movements on the server

• Check vector of shot on the server

• Artificial intelligence for bots

• Showcase players

5.4. Low-level game framework 23

Page 28: My Project Documentation · My Project Documentation, Release 1 5.4.6Asyncio and generation behavior of the bot In game framework, there is a bot. Bot to move in concentric circles,

My Project Documentation, Release 1

24 Chapter 5. License

Page 29: My Project Documentation · My Project Documentation, Release 1 5.4.6Asyncio and generation behavior of the bot In game framework, there is a bot. Bot to move in concentric circles,

CHAPTER 6

Indices and tables

• genindex

• modindex

• search

25