This commit is contained in:
2015-08-13 17:18:21 +03:00
parent f2cbdbf341
commit 39ab8d30e6
10 changed files with 117 additions and 64 deletions

View File

@@ -9,13 +9,13 @@
<haxelib name="openfl"/> <haxelib name="openfl"/>
<haxelib name="protohx"/> <haxelib name="protohx"/>
<haxelib name="haxework" version="git"/> <haxelib name="haxework" version="git"/>
<window width="800" height="600" if="desktop"/> <window width="760" height="600"/>
<window width="800" height="600" if="flash"/>
<window width="0" height="0" if="html5"/>
<haxeflag name="-D" value="swf-gpu"/> <haxeflag name="-D" value="swf-gpu"/>
<haxeflag name="-D" value="native-trace"/> <haxeflag name="-D" value="native-trace"/>
<haxeflag name="-D" value="proto_debug"/> <!--<haxeflag name="-D" value="proto_debug"/>-->
<haxeflag name="-dce" value="no"/> <haxeflag name="-dce" value="no"/>
<haxeflag name="-debug"/> <haxeflag name="-debug"/>
<haxeflag name="--macro" value="Meta.set('0.0.0')"/> <haxeflag name="--macro" value="Meta.set('0.0.0')"/>
<template path="src/client/webapp/index_template.html" rename="index.html"/>
</project> </project>

View File

@@ -6,4 +6,4 @@
-cp src/server/haxe -cp src/server/haxe
-cp src-gen/haxe -cp src-gen/haxe
-neko target/server.n -neko target/server.n
-D proto_debug # -D proto_debug

View File

@@ -1,5 +1,7 @@
package ru.m.tankz.view.frames; package ru.m.tankz.view.frames;
import ru.m.tankz.proto.GamesUnSubscribeRequest;
import ru.m.tankz.proto.GamesSubscribeRequest;
import haxework.gui.ViewBuilder; import haxework.gui.ViewBuilder;
import ru.m.tankz.proto.JoinGameResponse; import ru.m.tankz.proto.JoinGameResponse;
import ru.m.tankz.proto.JoinGameRequest; import ru.m.tankz.proto.JoinGameRequest;
@@ -9,14 +11,12 @@ import haxework.gui.frame.IFrameSwitcher;
import haxework.gui.ButtonView; import haxework.gui.ButtonView;
import haxework.gui.LabelView; import haxework.gui.LabelView;
import ru.m.tankz.proto.GamesResponse; import ru.m.tankz.proto.GamesResponse;
import ru.m.tankz.proto.GamesRequest;
import ru.m.tankz.proto.Game; import ru.m.tankz.proto.Game;
import haxework.gui.list.ListView; import haxework.gui.list.ListView;
import protohx.Message; import protohx.Message;
import ru.m.core.connect.IConnection; import ru.m.core.connect.IConnection;
import ru.m.tankz.data.GameData; import ru.m.tankz.data.GameData;
import haxework.provider.Provider; import haxework.provider.Provider;
import haxework.gui.list.VListView;
import haxework.gui.VGroupView; import haxework.gui.VGroupView;
@:template("layout/frames/game_list.json", "layout/styles.json") @:template("layout/frames/game_list.json", "layout/styles.json")
@@ -35,10 +35,11 @@ class GameListFrame extends VGroupView implements ViewBuilder implements IPacket
public function onShow() { public function onShow() {
findViewById("name", LabelView).text = Provider.get(GameData).person.name; findViewById("name", LabelView).text = Provider.get(GameData).person.name;
Provider.get(IConnection).packetHandler.addListener(this); Provider.get(IConnection).packetHandler.addListener(this);
Provider.get(IConnection).send(new GamesRequest()); Provider.get(IConnection).send(new GamesSubscribeRequest());
} }
public function onHide() { public function onHide() {
Provider.get(IConnection).send(new GamesUnSubscribeRequest());
Provider.get(IConnection).packetHandler.removeListener(this); Provider.get(IConnection).packetHandler.removeListener(this);
} }

View File

@@ -1,19 +1,15 @@
package ru.m.tankz.view.frames; package ru.m.tankz.view.frames;
import ru.m.tankz.proto.LeaveGameRequest;
import ru.m.tankz.proto.LeaveGameResponse; import ru.m.tankz.proto.LeaveGameResponse;
import ru.m.tankz.proto.JoinGameResponse; import ru.m.tankz.proto.JoinGameResponse;
import haxework.gui.ViewBuilder; import haxework.gui.ViewBuilder;
import ru.m.tankz.proto.ExitGameResponse;
import haxework.gui.frame.IFrameSwitcher; import haxework.gui.frame.IFrameSwitcher;
import ru.m.tankz.proto.GamesResponse;
import ru.m.tankz.proto.ExitGameRequest;
import ru.m.tankz.proto.StartGameResponse; import ru.m.tankz.proto.StartGameResponse;
import ru.m.tankz.proto.StartGameRequest; import ru.m.tankz.proto.StartGameRequest;
import ru.m.tankz.data.GameData; import ru.m.tankz.data.GameData;
import protohx.Message; import protohx.Message;
import haxework.gui.ButtonView; import haxework.gui.ButtonView;
import haxework.gui.list.VListView;
import ru.m.tankz.proto.Person;
import haxework.provider.Provider; import haxework.provider.Provider;
import haxework.gui.VGroupView; import haxework.gui.VGroupView;
import ru.m.core.connect.IConnection; import ru.m.core.connect.IConnection;
@@ -53,12 +49,12 @@ class GameReadyFrame extends VGroupView implements ViewBuilder implements IPacke
public function onLeaveGameResponse(packet:LeaveGameResponse):Void { public function onLeaveGameResponse(packet:LeaveGameResponse):Void {
Provider.get(GameData).game = packet.game; Provider.get(GameData).game = packet.game;
list.data = Provider.get(GameData).game.persons; list.data = Provider.get(GameData).game.persons;
} var person = Provider.get(GameData).person;
if (!Lambda.exists(packet.game.persons, function(person) return person.id == person.id)) {
public function onExitGameResponse(packet:ExitGameResponse):Void {
Provider.get(GameData).game = null; Provider.get(GameData).game = null;
Provider.get(IFrameSwitcher).change(GameListFrame.ID); Provider.get(IFrameSwitcher).change(GameListFrame.ID);
} }
}
public function onPacket(packet:Message):Void {} public function onPacket(packet:Message):Void {}
@@ -67,7 +63,7 @@ class GameReadyFrame extends VGroupView implements ViewBuilder implements IPacke
case "start": case "start":
Provider.get(IConnection).send(new StartGameRequest()); Provider.get(IConnection).send(new StartGameRequest());
case "exit": case "exit":
Provider.get(IConnection).send(new ExitGameRequest()); Provider.get(IConnection).send(new LeaveGameRequest());
} }
} }
} }

View File

@@ -0,0 +1,51 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>::APP_TITLE::</title>
<meta id="viewport" name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<meta name="apple-mobile-web-app-capable" content="yes">
::if linkedLibraries::::foreach (linkedLibraries)::
<script type="text/javascript" src="::__current__::"></script>::end::::end::
<script type="text/javascript" src="./::APP_FILE::.js"></script>
<script>
window.addEventListener ("touchmove", function (event) { event.preventDefault (); }, false);
if (typeof window.devicePixelRatio != 'undefined' && window.devicePixelRatio > 2) {
var meta = document.getElementById ("viewport");
meta.setAttribute ('content', 'width=device-width, initial-scale=' + (2 / window.devicePixelRatio) + ', user-scalable=no');
}
</script>
<style>
html,body { margin: 0; padding: 0; height: 100%; overflow: hidden; }
#openfl-content { background: #000000; width: ::WIN_WIDTH::px; height: ::WIN_HEIGHT::px; margin: 0 auto; }
::foreach assets::::if (type == "font")::
@font-face {
font-family: '::fontName::';
src: url('::targetPath::.eot');
src: url('::targetPath::.eot?#iefix') format('embedded-opentype'),
url('::targetPath::.svg#my-font-family') format('svg'),
url('::targetPath::.woff') format('woff'),
url('::targetPath::.ttf') format('truetype');
font-weight: normal;
font-style: normal;
}::end::::end::
</style>
</head>
<body>
<div id="openfl-content"></div>
<script type="text/javascript">
lime.embed ("openfl-content", ::WIN_WIDTH::, ::WIN_HEIGHT::, "::WIN_FLASHBACKGROUND::");
</script>
</body>
</html>

View File

@@ -1,5 +1,7 @@
package ru.m.tankz; package ru.m.tankz;
import ru.m.tankz.proto.LeaveGameRequest;
import ru.m.tankz.proto.LeaveGameResponse;
import ru.m.tankz.proto.GameUpdateResponse; import ru.m.tankz.proto.GameUpdateResponse;
import ru.m.tankz.proto.GameActionRequest; import ru.m.tankz.proto.GameActionRequest;
import ru.m.core.connect.IConnection; import ru.m.core.connect.IConnection;
@@ -9,7 +11,8 @@ import ru.m.tankz.proto.LoginResponse;
import ru.m.tankz.proto.PersonSelectRequest; import ru.m.tankz.proto.PersonSelectRequest;
import ru.m.tankz.proto.PersonSelectResponse; import ru.m.tankz.proto.PersonSelectResponse;
import ru.m.tankz.proto.ErrorResponse; import ru.m.tankz.proto.ErrorResponse;
import ru.m.tankz.proto.GamesRequest; import ru.m.tankz.proto.GamesSubscribeRequest;
import ru.m.tankz.proto.GamesUnSubscribeRequest;
import ru.m.tankz.proto.GamesResponse; import ru.m.tankz.proto.GamesResponse;
import ru.m.tankz.proto.CreateGameRequest; import ru.m.tankz.proto.CreateGameRequest;
import ru.m.tankz.proto.CreateGameResponse; import ru.m.tankz.proto.CreateGameResponse;
@@ -17,8 +20,6 @@ import ru.m.tankz.proto.JoinGameRequest;
import ru.m.tankz.proto.JoinGameResponse; import ru.m.tankz.proto.JoinGameResponse;
import ru.m.tankz.proto.StartGameRequest; import ru.m.tankz.proto.StartGameRequest;
import ru.m.tankz.proto.StartGameResponse; import ru.m.tankz.proto.StartGameResponse;
import ru.m.tankz.proto.ExitGameRequest;
import ru.m.tankz.proto.ExitGameResponse;
class PacketBuilder implements IPacketBuilder { class PacketBuilder implements IPacketBuilder {
@@ -33,7 +34,7 @@ class PacketBuilder implements IPacketBuilder {
0x0004 => PersonSelectResponse 0x0004 => PersonSelectResponse
], ],
0x02 => [ 0x02 => [
0x0001 => GamesRequest, 0x0001 => GamesSubscribeRequest,
0x0002 => GamesResponse, 0x0002 => GamesResponse,
0x0003 => CreateGameRequest, 0x0003 => CreateGameRequest,
0x0004 => CreateGameResponse, 0x0004 => CreateGameResponse,
@@ -41,8 +42,9 @@ class PacketBuilder implements IPacketBuilder {
0x0006 => JoinGameResponse, 0x0006 => JoinGameResponse,
0x0007 => StartGameRequest, 0x0007 => StartGameRequest,
0x0008 => StartGameResponse, 0x0008 => StartGameResponse,
0x0009 => ExitGameRequest, 0x0009 => LeaveGameRequest,
0x000a => ExitGameResponse 0x000a => LeaveGameResponse,
0x000b => GamesUnSubscribeRequest
], ],
0x03 => [ 0x03 => [
0x0001 => GameActionRequest, 0x0001 => GameActionRequest,

View File

@@ -11,7 +11,7 @@ class Tank extends MobileEntity {
public var bulletsCount:Int = 0; public var bulletsCount:Int = 0;
public function new(personId:Int, id:Int, x:Float, y:Float, direction:Directiondo apt-g) { public function new(personId:Int, id:Int, x:Float, y:Float, direction:Direction) {
super(id, x, y, 4, direction); super(id, x, y, 4, direction);
this.personId = personId; this.personId = personId;
width = 34; width = 34;

View File

@@ -47,7 +47,11 @@ message Game {
required GameState state = 4; required GameState state = 4;
} }
message GamesRequest { message GamesSubscribeRequest {
}
message GamesUnSubscribeRequest {
} }
@@ -72,7 +76,7 @@ message JoinGameResponse {
} }
message LeaveGameRequest { message LeaveGameRequest {
required int32 game_id = 1;
} }
message LeaveGameResponse { message LeaveGameResponse {
@@ -87,14 +91,6 @@ message StartGameResponse {
required Game game = 1; required Game game = 1;
} }
message ExitGameRequest {
}
message ExitGameResponse {
}
/** /**
Game Game
*/ */

View File

@@ -1,8 +1,8 @@
package ru.m.tankz.server.game; package ru.m.tankz.server.game;
import ru.m.tankz.proto.GamesResponse;
import ru.m.tankz.proto.GameActionType; import ru.m.tankz.proto.GameActionType;
import ru.m.tankz.proto.GameActionRequest; import ru.m.tankz.proto.GameActionRequest;
import ru.m.tankz.proto.ExitGameResponse;
import ru.m.tankz.proto.GameObjectType; import ru.m.tankz.proto.GameObjectType;
import ru.m.tankz.proto.GameChangeType; import ru.m.tankz.proto.GameChangeType;
import ru.m.tankz.proto.GameUpdateResponse; import ru.m.tankz.proto.GameUpdateResponse;
@@ -59,6 +59,7 @@ class GameManager {
public static var byGameId:Map<Int, GameManager> = new Map<Int, GameManager>(); public static var byGameId:Map<Int, GameManager> = new Map<Int, GameManager>();
public static var byPersonId:Map<Int, GameManager> = new Map<Int, GameManager>(); public static var byPersonId:Map<Int, GameManager> = new Map<Int, GameManager>();
public static var subscribers:Map<Int, Bool> = new Map<Int, Bool>();
private static var idCounter:Int = 0; private static var idCounter:Int = 0;
@@ -79,6 +80,19 @@ class GameManager {
byGameId.set(game.id, this); byGameId.set(game.id, this);
byPersonId.set(person.id, this); byPersonId.set(person.id, this);
broadcast(new CreateGameResponse().setGame(game)); broadcast(new CreateGameResponse().setGame(game));
broadcastGames();
}
public static function getReadyGames():Array<Game> {
return Lambda.array(Lambda.filter(Lambda.map(GameManager.byGameId, function(gm) return gm.game), function(game) return game.state == GameState.READY));
}
public function broadcastGames() {
var packet = new GamesResponse().setGames(getReadyGames());
for (personId in subscribers.keys()) {
var session = Session.sessions.get(personId);
session.send(packet);
}
} }
public function broadcast(packet:Message) { public function broadcast(packet:Message) {
@@ -97,10 +111,12 @@ class GameManager {
public function leave(person:Person) { public function leave(person:Person) {
game.setPersons(game.persons.filter(function(p) return p.id != person.id)); game.setPersons(game.persons.filter(function(p) return p.id != person.id));
byPersonId.remove(person.id); byPersonId.remove(person.id);
var packet = new LeaveGameResponse().setGame(game);
Session.sessions.get(person.id).send(packet);
if (game.persons.length == 0/* || person.id == game.creator.id*/) { if (game.persons.length == 0/* || person.id == game.creator.id*/) {
stop(); stop();
} else { } else {
broadcast(new LeaveGameResponse().setGame(game)); broadcast(packet);
} }
} }
@@ -117,13 +133,15 @@ class GameManager {
public function stop() { public function stop() {
game.setState(GameState.ENDED); game.setState(GameState.ENDED);
game.setPersons([]);
byGameId.remove(game.id); byGameId.remove(game.id);
for (p in game.persons) byPersonId.remove(p.id); for (p in game.persons) byPersonId.remove(p.id);
if (timer != null) { if (timer != null) {
timer.stop(); timer.stop();
timer = null; timer = null;
} }
broadcast(new ExitGameResponse()); broadcast(new LeaveGameResponse().setGame(game));
broadcastGames();
} }
public function action(person:Person, action:GameActionRequest) { public function action(person:Person, action:GameActionRequest) {

View File

@@ -1,27 +1,13 @@
package ru.m.tankz.server.session; package ru.m.tankz.server.session;
import ru.m.tankz.proto.GamesUnSubscribeRequest;
import ru.m.tankz.proto.GamesSubscribeRequest;
import ru.m.tankz.proto.LeaveGameRequest;
import ru.m.tankz.server.game.GameManager; import ru.m.tankz.server.game.GameManager;
import ru.m.tankz.proto.GameObjectType;
import ru.m.tankz.proto.GameChangeType;
import ru.m.tankz.proto.GameChange;
import ru.m.tankz.config.TankzConfig.DEFAULT;
import ru.m.tankz.proto.GameUpdateResponse;
import haxe.Timer;
import ru.m.tankz.core.Direction;
import ru.m.tankz.engine.Engine;
import ru.m.tankz.engine.IEngine;
import ru.m.tankz.proto.GameActionType;
import ru.m.tankz.proto.GameActionRequest; import ru.m.tankz.proto.GameActionRequest;
import ru.m.tankz.proto.ExitGameResponse;
import ru.m.tankz.proto.ExitGameRequest;
import ru.m.tankz.proto.StartGameRequest; import ru.m.tankz.proto.StartGameRequest;
import ru.m.tankz.proto.StartGameResponse;
import ru.m.tankz.proto.GameState;
import ru.m.tankz.proto.Person; import ru.m.tankz.proto.Person;
import ru.m.tankz.proto.JoinGameResponse;
import ru.m.tankz.proto.JoinGameRequest; import ru.m.tankz.proto.JoinGameRequest;
import ru.m.tankz.proto.Game;
import ru.m.tankz.proto.CreateGameResponse;
import ru.m.tankz.proto.CreateGameRequest; import ru.m.tankz.proto.CreateGameRequest;
import ru.m.core.connect.neko.NekoWebConnection; import ru.m.core.connect.neko.NekoWebConnection;
import haxe.io.Bytes; import haxe.io.Bytes;
@@ -33,7 +19,6 @@ import ru.m.tankz.proto.ErrorResponse;
import ru.m.tankz.server.db.DbProvider; import ru.m.tankz.server.db.DbProvider;
import ru.m.tankz.proto.LoginResponse; import ru.m.tankz.proto.LoginResponse;
import ru.m.tankz.proto.LoginRequest; import ru.m.tankz.proto.LoginRequest;
import ru.m.tankz.proto.GamesRequest;
import ru.m.tankz.proto.GamesResponse; import ru.m.tankz.proto.GamesResponse;
import protohx.Message; import protohx.Message;
import ru.m.core.connect.IConnection; import ru.m.core.connect.IConnection;
@@ -122,9 +107,13 @@ class Session implements IConnectionHandler implements IPacketHandler {
} }
} }
public function onGamesRequest(packet:GamesRequest):Void { public function onGamesSubscribeRequest(packet:GamesSubscribeRequest):Void {
var games = Lambda.array(Lambda.map(GameManager.byGameId, function(gm) return gm.game)); GameManager.subscribers.set(person.id, true);
connection.send(new GamesResponse().setGames(games)); connection.send(new GamesResponse().setGames(GameManager.getReadyGames()));
}
public function onGamesUnSubscribeRequest(packet:GamesUnSubscribeRequest):Void {
GameManager.subscribers.remove(person.id);
} }
public function onCreateGameRequest(packet:CreateGameRequest):Void { public function onCreateGameRequest(packet:CreateGameRequest):Void {
@@ -135,12 +124,12 @@ class Session implements IConnectionHandler implements IPacketHandler {
GameManager.byGameId.get(packet.gameId).join(person); GameManager.byGameId.get(packet.gameId).join(person);
} }
public function onStartGameRequest(packet:StartGameRequest):Void { public function onLeaveGameRequest(packet:LeaveGameRequest):Void {
GameManager.byPersonId.get(person.id).start(); GameManager.byPersonId.get(person.id).leave(person);
} }
public function onExitGameRequest(packet:ExitGameRequest):Void { public function onStartGameRequest(packet:StartGameRequest):Void {
GameManager.byPersonId.get(person.id).leave(person); GameManager.byPersonId.get(person.id).start();
} }
public function onGameActionRequest(packet:GameActionRequest):Void { public function onGameActionRequest(packet:GameActionRequest):Void {