[network] added NetworkFrame
This commit is contained in:
@@ -105,7 +105,7 @@ exports['client'] = gulp.series(
|
|||||||
|
|
||||||
const testFlash = function() {
|
const testFlash = function() {
|
||||||
const argv = yargs.argv;
|
const argv = yargs.argv;
|
||||||
return build('flash')()
|
return build('flash', {proto_debug: true})()
|
||||||
.pipe(new FlashPlayer().run(argv.dev))
|
.pipe(new FlashPlayer().run(argv.dev))
|
||||||
.pipe(debug());
|
.pipe(debug());
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -8,6 +8,12 @@ const prepare = require('./prepare');
|
|||||||
const debug = require('../tasks/debug');
|
const debug = require('../tasks/debug');
|
||||||
|
|
||||||
|
|
||||||
|
const generate = () => function generate() {
|
||||||
|
return new Haxe().haxelib([
|
||||||
|
'run', 'protohx', 'generate', 'protohx.json'
|
||||||
|
]);
|
||||||
|
};
|
||||||
|
|
||||||
const build = () => function build() {
|
const build = () => function build() {
|
||||||
const argv = yargs.argv;
|
const argv = yargs.argv;
|
||||||
return gulp.src('.')
|
return gulp.src('.')
|
||||||
@@ -18,6 +24,7 @@ const build = () => function build() {
|
|||||||
'protohx',
|
'protohx',
|
||||||
'orm',
|
'orm',
|
||||||
'haxework:git',
|
'haxework:git',
|
||||||
|
'haxe-crypto'
|
||||||
],
|
],
|
||||||
cp: [
|
cp: [
|
||||||
'src/common/haxe',
|
'src/common/haxe',
|
||||||
@@ -27,6 +34,9 @@ const build = () => function build() {
|
|||||||
main: 'ru.m.tankz.server.Server',
|
main: 'ru.m.tankz.server.Server',
|
||||||
outputFile: 'tankz.n',
|
outputFile: 'tankz.n',
|
||||||
debug: argv.dev,
|
debug: argv.dev,
|
||||||
|
values: {
|
||||||
|
proto_debug: true
|
||||||
|
}
|
||||||
}))
|
}))
|
||||||
.pipe(gulp.dest('target'));
|
.pipe(gulp.dest('target'));
|
||||||
};
|
};
|
||||||
@@ -39,5 +49,5 @@ const test = (build) => function test() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
exports['server'] = gulp.series(prepare(Haxe.ID), build());
|
exports['server'] = gulp.series(prepare(Haxe.ID), generate(), build());
|
||||||
exports['server:test'] = gulp.series(prepare(Haxe.ID, FlashPlayer.ID), test(build()));
|
exports['server:test'] = gulp.series(prepare(Haxe.ID, FlashPlayer.ID), generate(), test(build()));
|
||||||
|
|||||||
@@ -38,6 +38,7 @@
|
|||||||
"yaml": "1.3.0",
|
"yaml": "1.3.0",
|
||||||
"haxework": "git@bitbucket.org:shmyga/haxework.git",
|
"haxework": "git@bitbucket.org:shmyga/haxework.git",
|
||||||
"orm": "2.1.0",
|
"orm": "2.1.0",
|
||||||
"yield": "1.1.2"
|
"yield": "1.1.2",
|
||||||
|
"haxe-crypto": "0.0.7"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package ru.m.tankz;
|
package ru.m.tankz;
|
||||||
|
|
||||||
|
import ru.m.tankz.storage.UserStorage;
|
||||||
import openfl.Assets;
|
import openfl.Assets;
|
||||||
import ru.m.tankz.sound.SoundManager;
|
import ru.m.tankz.sound.SoundManager;
|
||||||
import flash.events.KeyboardEvent;
|
import flash.events.KeyboardEvent;
|
||||||
@@ -70,14 +71,16 @@ class Client implements IConnectionHandler {
|
|||||||
Provider.set(IConnection, new ru.m.connect.flash.FlashConnection('localhost', 5001));
|
Provider.set(IConnection, new ru.m.connect.flash.FlashConnection('localhost', 5001));
|
||||||
#elseif html5
|
#elseif html5
|
||||||
Provider.set(IConnection, new ru.m.connect.js.JsConnection('localhost', 5001));
|
Provider.set(IConnection, new ru.m.connect.js.JsConnection('localhost', 5001));
|
||||||
|
#else
|
||||||
|
Provider.set(IConnection, new ru.m.connect.fake.FakeConnection());
|
||||||
#end
|
#end
|
||||||
|
|
||||||
//Provider.get(IConnection).handler.addListener(this);
|
Provider.get(IConnection).handler.addListener(this);
|
||||||
|
|
||||||
view = new ClientView();
|
view = new ClientView();
|
||||||
Provider.set(IFrameSwitcher, view.switcher);
|
Provider.set(IFrameSwitcher, view.switcher);
|
||||||
Root.bind(view);
|
Root.bind(view);
|
||||||
view.content.stage.stageFocusRect = false;
|
view.content.stage.stageFocusRect = false;
|
||||||
//view.logout.onPress = this;
|
|
||||||
|
|
||||||
view.content.stage.addEventListener(KeyboardEvent.KEY_UP, function(event:KeyboardEvent):Void {
|
view.content.stage.addEventListener(KeyboardEvent.KEY_UP, function(event:KeyboardEvent):Void {
|
||||||
if (event.keyCode == Keyboard.ESCAPE) {
|
if (event.keyCode == Keyboard.ESCAPE) {
|
||||||
@@ -88,6 +91,7 @@ class Client implements IConnectionHandler {
|
|||||||
Provider.setFactory(IConfigBundle, ConfigBundle);
|
Provider.setFactory(IConfigBundle, ConfigBundle);
|
||||||
Provider.setFactory(ILevelBundle, LevelBundle);
|
Provider.setFactory(ILevelBundle, LevelBundle);
|
||||||
Provider.setFactory(SaveStorage, SaveStorage);
|
Provider.setFactory(SaveStorage, SaveStorage);
|
||||||
|
Provider.setFactory(UserStorage, UserStorage);
|
||||||
Provider.setFactory(SoundManager, SoundManager);
|
Provider.setFactory(SoundManager, SoundManager);
|
||||||
Provider.setFactory(Game, ClassicGame, ClassicGame.TYPE);
|
Provider.setFactory(Game, ClassicGame, ClassicGame.TYPE);
|
||||||
Provider.setFactory(Game, DotaGame, DotaGame.TYPE);
|
Provider.setFactory(Game, DotaGame, DotaGame.TYPE);
|
||||||
@@ -110,11 +114,11 @@ class Client implements IConnectionHandler {
|
|||||||
public function onConnected():Void {}
|
public function onConnected():Void {}
|
||||||
|
|
||||||
public function onDisconnected():Void {
|
public function onDisconnected():Void {
|
||||||
//view.switcher.change(AuthFrame.ID);
|
view.switcher.change(StartFrame.ID);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function onError(error:Dynamic):Void {
|
public function onError(error:Dynamic):Void {
|
||||||
L.e(TAG, '', error);
|
L.e(TAG, '', error);
|
||||||
//view.switcher.change(AuthFrame.ID);
|
view.switcher.change(StartFrame.ID);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,6 +18,8 @@ views:
|
|||||||
$type: ru.m.tankz.frame.LevelFrame
|
$type: ru.m.tankz.frame.LevelFrame
|
||||||
- id: game
|
- id: game
|
||||||
$type: ru.m.tankz.frame.GameFrame
|
$type: ru.m.tankz.frame.GameFrame
|
||||||
|
- id: network
|
||||||
|
$type: ru.m.tankz.frame.NetworkFrame
|
||||||
- $type: haxework.gui.LabelView
|
- $type: haxework.gui.LabelView
|
||||||
$style: label
|
$style: label
|
||||||
inLayout: false
|
inLayout: false
|
||||||
|
|||||||
88
src/client/haxe/ru/m/tankz/frame/NetworkFrame.hx
Normal file
88
src/client/haxe/ru/m/tankz/frame/NetworkFrame.hx
Normal file
@@ -0,0 +1,88 @@
|
|||||||
|
package ru.m.tankz.frame;
|
||||||
|
|
||||||
|
import haxework.gui.ButtonView;
|
||||||
|
import haxework.gui.InputView;
|
||||||
|
import haxework.gui.LabelView;
|
||||||
|
import haxework.gui.VGroupView;
|
||||||
|
import haxework.gui.ViewBuilder;
|
||||||
|
import haxework.provider.Provider;
|
||||||
|
import protohx.Message;
|
||||||
|
import ru.m.connect.IConnection;
|
||||||
|
import ru.m.tankz.proto.pack.LoginRequest;
|
||||||
|
import ru.m.tankz.proto.pack.LoginResponse;
|
||||||
|
import ru.m.tankz.storage.UserStorage;
|
||||||
|
|
||||||
|
|
||||||
|
interface NetworkFrameLayout {
|
||||||
|
var stateLabel(default, null):LabelView;
|
||||||
|
var nameInput(default, null):InputView;
|
||||||
|
var loginButton(default, null):ButtonView;
|
||||||
|
}
|
||||||
|
|
||||||
|
@:template("ru/m/tankz/frame/NetworkFrame.yaml", "ru/m/tankz/Style.yaml")
|
||||||
|
class NetworkFrame extends VGroupView implements ViewBuilder implements NetworkFrameLayout implements IPacketHandler implements IConnectionHandler {
|
||||||
|
public static var ID(default, never):String = "network";
|
||||||
|
|
||||||
|
private var state(null, set):String;
|
||||||
|
|
||||||
|
private function set_state(value:String):String {
|
||||||
|
stateLabel.text = value;
|
||||||
|
loginButton.disabled = value != 'online';
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function init():Void {
|
||||||
|
loginButton.onPress = this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function onShow():Void {
|
||||||
|
var user:User = Provider.get(UserStorage).read();
|
||||||
|
nameInput.text = user.name;
|
||||||
|
var connection:IConnection = Provider.get(IConnection);
|
||||||
|
connection.packetHandler.addListener(this);
|
||||||
|
connection.handler.addListener(this);
|
||||||
|
state = connection.connected ? 'online' : 'offline';
|
||||||
|
if (!connection.connected) {
|
||||||
|
connection.connect();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function onHide():Void {
|
||||||
|
Provider.get(IConnection).packetHandler.removeListener(this);
|
||||||
|
Provider.get(IConnection).handler.removeListener(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function onPress(view:ButtonView):Void {
|
||||||
|
var user:User = Provider.get(UserStorage).read();
|
||||||
|
user.name = nameInput.text;
|
||||||
|
Provider.get(IConnection).send(
|
||||||
|
new LoginRequest()
|
||||||
|
.setUuid(user.uuid)
|
||||||
|
.setName(user.name)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function onLoginResponse(packet:LoginResponse):Void {
|
||||||
|
var user:User = {
|
||||||
|
uuid: packet.user.uuid,
|
||||||
|
name: packet.user.name,
|
||||||
|
};
|
||||||
|
Provider.get(UserStorage).write(user);
|
||||||
|
//Provider.set(User, user);
|
||||||
|
state = 'logined';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function onPacket(packet:Message):Void {}
|
||||||
|
|
||||||
|
public function onConnected():Void {
|
||||||
|
state = 'online';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function onDisconnected():Void {
|
||||||
|
state = 'offline';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function onError(error:Dynamic):Void {
|
||||||
|
state = 'offline';
|
||||||
|
}
|
||||||
|
}
|
||||||
23
src/client/haxe/ru/m/tankz/frame/NetworkFrame.yaml
Normal file
23
src/client/haxe/ru/m/tankz/frame/NetworkFrame.yaml
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
---
|
||||||
|
pWidth: 100
|
||||||
|
pHeight: 100
|
||||||
|
views:
|
||||||
|
- id: stateLabel
|
||||||
|
$type: haxework.gui.LabelView
|
||||||
|
$style: label
|
||||||
|
text: offline
|
||||||
|
width: 200
|
||||||
|
height: 50
|
||||||
|
- id: nameInput
|
||||||
|
$type: haxework.gui.InputView
|
||||||
|
$style: label
|
||||||
|
width: 200
|
||||||
|
height: 50
|
||||||
|
text: User
|
||||||
|
skin:
|
||||||
|
$type: haxework.gui.skin.ColorSkin
|
||||||
|
color: '#444444'
|
||||||
|
- id: loginButton
|
||||||
|
$type: haxework.gui.ButtonView
|
||||||
|
$style: button
|
||||||
|
text: Login
|
||||||
@@ -19,12 +19,13 @@ interface StartFrameLayout {
|
|||||||
var dota_1p(default, null):ButtonView;
|
var dota_1p(default, null):ButtonView;
|
||||||
var dota_2p_coop(default, null):ButtonView;
|
var dota_2p_coop(default, null):ButtonView;
|
||||||
var dota_2p_vs(default, null):ButtonView;
|
var dota_2p_vs(default, null):ButtonView;
|
||||||
|
var network(default, null):ButtonView;
|
||||||
}
|
}
|
||||||
|
|
||||||
@:template("ru/m/tankz/frame/StartFrame.yaml", "ru/m/tankz/Style.yaml")
|
@:template("ru/m/tankz/frame/StartFrame.yaml", "ru/m/tankz/Style.yaml")
|
||||||
class StartFrame extends VGroupView implements ViewBuilder implements StartFrameLayout {
|
class StartFrame extends VGroupView implements ViewBuilder implements StartFrameLayout {
|
||||||
|
|
||||||
public static inline var ID = "start";
|
public static var ID(default, never):String = "start";
|
||||||
|
|
||||||
public function init():Void {
|
public function init():Void {
|
||||||
classic_1p.onPress = this;
|
classic_1p.onPress = this;
|
||||||
@@ -33,6 +34,7 @@ class StartFrame extends VGroupView implements ViewBuilder implements StartFrame
|
|||||||
dota_1p.onPress = this;
|
dota_1p.onPress = this;
|
||||||
dota_2p_coop.onPress = this;
|
dota_2p_coop.onPress = this;
|
||||||
dota_2p_vs.onPress = this;
|
dota_2p_vs.onPress = this;
|
||||||
|
network.onPress = this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function onShow():Void {
|
public function onShow():Void {
|
||||||
@@ -59,6 +61,8 @@ class StartFrame extends VGroupView implements ViewBuilder implements StartFrame
|
|||||||
startGame(DotaGame.TYPE, DotaGame.PLAYER2_COOP);
|
startGame(DotaGame.TYPE, DotaGame.PLAYER2_COOP);
|
||||||
case 'dota_2p_vs':
|
case 'dota_2p_vs':
|
||||||
startGame(DotaGame.TYPE, DotaGame.PLAYER2_VS);
|
startGame(DotaGame.TYPE, DotaGame.PLAYER2_VS);
|
||||||
|
case 'network':
|
||||||
|
Provider.get(IFrameSwitcher).change(NetworkFrame.ID);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,43 +2,66 @@
|
|||||||
pWidth: 100
|
pWidth: 100
|
||||||
pHeight: 100
|
pHeight: 100
|
||||||
views:
|
views:
|
||||||
|
# logo
|
||||||
- $type: haxework.gui.ImageView
|
- $type: haxework.gui.ImageView
|
||||||
image: "@asset:image:resources/image/ui/logo.png"
|
image: "@asset:image:resources/image/ui/logo.png"
|
||||||
contentSize: true
|
contentSize: true
|
||||||
bottomMargin: 15
|
bottomMargin: 15
|
||||||
|
- $type: haxework.gui.HGroupView
|
||||||
|
contentSize: true
|
||||||
|
views:
|
||||||
|
- $type: haxework.gui.VGroupView
|
||||||
|
contentSize: true
|
||||||
|
views:
|
||||||
|
# classic
|
||||||
|
- $type: haxework.gui.LabelView
|
||||||
|
$style: label
|
||||||
|
fontSize: 20
|
||||||
|
topMargin: 15
|
||||||
|
contentSize: true
|
||||||
|
text: Classic
|
||||||
|
- id: classic_1p
|
||||||
|
$type: haxework.gui.ButtonView
|
||||||
|
text: 1 Player
|
||||||
|
$style: button
|
||||||
|
- id: classic_2p
|
||||||
|
$type: haxework.gui.ButtonView
|
||||||
|
text: 2 Player
|
||||||
|
$style: button
|
||||||
|
- id: classic_load
|
||||||
|
$type: haxework.gui.ButtonView
|
||||||
|
text: Load
|
||||||
|
$style: button
|
||||||
|
- $type: haxework.gui.VGroupView
|
||||||
|
contentSize: true
|
||||||
|
views:
|
||||||
|
# dota
|
||||||
|
- $type: haxework.gui.LabelView
|
||||||
|
$style: label
|
||||||
|
fontSize: 20
|
||||||
|
topMargin: 15
|
||||||
|
contentSize: true
|
||||||
|
text: DotA
|
||||||
|
- id: dota_1p
|
||||||
|
$type: haxework.gui.ButtonView
|
||||||
|
text: 1 Player
|
||||||
|
$style: button
|
||||||
|
- id: dota_2p_coop
|
||||||
|
$type: haxework.gui.ButtonView
|
||||||
|
text: 2 COOP
|
||||||
|
$style: button
|
||||||
|
- id: dota_2p_vs
|
||||||
|
$type: haxework.gui.ButtonView
|
||||||
|
text: 2 VS
|
||||||
|
$style: button
|
||||||
|
# network
|
||||||
- $type: haxework.gui.LabelView
|
- $type: haxework.gui.LabelView
|
||||||
$style: label
|
$style: label
|
||||||
fontSize: 20
|
fontSize: 20
|
||||||
topMargin: 15
|
topMargin: 15
|
||||||
contentSize: true
|
contentSize: true
|
||||||
text: Classic
|
text: Network
|
||||||
- id: classic_1p
|
- id: network
|
||||||
$type: haxework.gui.ButtonView
|
$type: haxework.gui.ButtonView
|
||||||
text: 1 Player
|
text: Network
|
||||||
$style: button
|
|
||||||
- id: classic_2p
|
|
||||||
$type: haxework.gui.ButtonView
|
|
||||||
text: 2 Player
|
|
||||||
$style: button
|
|
||||||
- id: classic_load
|
|
||||||
$type: haxework.gui.ButtonView
|
|
||||||
text: Load
|
|
||||||
$style: button
|
|
||||||
- $type: haxework.gui.LabelView
|
|
||||||
$style: label
|
|
||||||
fontSize: 20
|
|
||||||
topMargin: 15
|
|
||||||
contentSize: true
|
|
||||||
text: DotA
|
|
||||||
- id: dota_1p
|
|
||||||
$type: haxework.gui.ButtonView
|
|
||||||
text: 1 Player
|
|
||||||
$style: button
|
|
||||||
- id: dota_2p_coop
|
|
||||||
$type: haxework.gui.ButtonView
|
|
||||||
text: 2 COOP
|
|
||||||
$style: button
|
|
||||||
- id: dota_2p_vs
|
|
||||||
$type: haxework.gui.ButtonView
|
|
||||||
text: 2 VS
|
|
||||||
$style: button
|
$style: button
|
||||||
27
src/client/haxe/ru/m/tankz/storage/UserStorage.hx
Normal file
27
src/client/haxe/ru/m/tankz/storage/UserStorage.hx
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
package ru.m.tankz.storage;
|
||||||
|
|
||||||
|
import flash.net.SharedObject;
|
||||||
|
|
||||||
|
|
||||||
|
class UserStorage {
|
||||||
|
|
||||||
|
private static var TAG(default, never):String = 'UserStorage';
|
||||||
|
|
||||||
|
private var so:SharedObject;
|
||||||
|
|
||||||
|
public function new() {
|
||||||
|
so = SharedObject.getLocal('user');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function read():Null<User> {
|
||||||
|
var user:User = Reflect.getProperty(so.data, 'user');
|
||||||
|
L.d(TAG, 'read: ${user}');
|
||||||
|
return user != null ? user : {name: 'User', uuid: null};
|
||||||
|
}
|
||||||
|
|
||||||
|
public function write(user:User):Void {
|
||||||
|
L.d(TAG, 'write: ${user}');
|
||||||
|
so.setProperty('user', user);
|
||||||
|
so.flush();
|
||||||
|
}
|
||||||
|
}
|
||||||
6
src/common/haxe/ru/m/User.hx
Normal file
6
src/common/haxe/ru/m/User.hx
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
package ru.m;
|
||||||
|
|
||||||
|
typedef User = {
|
||||||
|
var uuid:String;
|
||||||
|
var name:String;
|
||||||
|
}
|
||||||
7
src/common/haxe/ru/m/connect/fake/FakeConnection.hx
Normal file
7
src/common/haxe/ru/m/connect/fake/FakeConnection.hx
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
package ru.m.connect.fake;
|
||||||
|
|
||||||
|
|
||||||
|
class FakeConnection extends BaseConnection {
|
||||||
|
override public function connect():Void {}
|
||||||
|
override public function disconnect():Void {}
|
||||||
|
}
|
||||||
@@ -2,19 +2,7 @@ package ru.m.tankz;
|
|||||||
|
|
||||||
import protohx.Message;
|
import protohx.Message;
|
||||||
import ru.m.connect.IConnection;
|
import ru.m.connect.IConnection;
|
||||||
import ru.m.tankz.proto.pack.CreateGameRequest;
|
import ru.m.tankz.proto.pack.*;
|
||||||
import ru.m.tankz.proto.pack.CreateGameResponse;
|
|
||||||
import ru.m.tankz.proto.pack.ErrorResponse;
|
|
||||||
import ru.m.tankz.proto.pack.GameActionRequest;
|
|
||||||
import ru.m.tankz.proto.pack.GameListRequest;
|
|
||||||
import ru.m.tankz.proto.pack.GameListResponse;
|
|
||||||
import ru.m.tankz.proto.pack.GameUpdateResponse;
|
|
||||||
import ru.m.tankz.proto.pack.JoinGameRequest;
|
|
||||||
import ru.m.tankz.proto.pack.JoinGameResponse;
|
|
||||||
import ru.m.tankz.proto.pack.LeaveGameRequest;
|
|
||||||
import ru.m.tankz.proto.pack.LeaveGameResponse;
|
|
||||||
import ru.m.tankz.proto.pack.StartGameRequest;
|
|
||||||
import ru.m.tankz.proto.pack.StartGameResponse;
|
|
||||||
import Type;
|
import Type;
|
||||||
|
|
||||||
|
|
||||||
@@ -24,9 +12,10 @@ class PacketBuilder implements IPacketBuilder {
|
|||||||
0x00 => [
|
0x00 => [
|
||||||
0x0001 => ErrorResponse
|
0x0001 => ErrorResponse
|
||||||
],
|
],
|
||||||
/*0x01 => [
|
0x01 => [
|
||||||
|
0x0001 => LoginRequest,
|
||||||
],*/
|
0x0002 => LoginResponse,
|
||||||
|
],
|
||||||
0x02 => [
|
0x02 => [
|
||||||
0x0001 => GameListRequest,
|
0x0001 => GameListRequest,
|
||||||
0x0002 => GameListResponse,
|
0x0002 => GameListResponse,
|
||||||
|
|||||||
@@ -3,15 +3,11 @@ syntax = "proto3";
|
|||||||
package ru.m.tankz.proto.core;
|
package ru.m.tankz.proto.core;
|
||||||
|
|
||||||
|
|
||||||
message Player {
|
message User {
|
||||||
int32 id = 1;
|
string uuid = 1;
|
||||||
string name = 2;
|
string name = 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
enum GameType {
|
|
||||||
CLASSIC = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
enum GameState {
|
enum GameState {
|
||||||
READY = 0;
|
READY = 0;
|
||||||
STARTED = 1;
|
STARTED = 1;
|
||||||
@@ -20,8 +16,8 @@ enum GameState {
|
|||||||
|
|
||||||
message Game {
|
message Game {
|
||||||
int32 id = 1;
|
int32 id = 1;
|
||||||
GameType type = 2;
|
string type = 2;
|
||||||
Player creator = 3;
|
User creator = 3;
|
||||||
repeated Player players = 4;
|
repeated User players = 4;
|
||||||
GameState state = 5;
|
GameState state = 5;
|
||||||
}
|
}
|
||||||
@@ -11,6 +11,17 @@ message ErrorResponse {
|
|||||||
string message = 2;
|
string message = 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Login
|
||||||
|
message LoginRequest {
|
||||||
|
string uuid = 1;
|
||||||
|
string name = 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
message LoginResponse {
|
||||||
|
ru.m.tankz.proto.core.User user = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
message GameListRequest {}
|
message GameListRequest {}
|
||||||
|
|
||||||
message GameListResponse {
|
message GameListResponse {
|
||||||
@@ -18,7 +29,7 @@ message GameListResponse {
|
|||||||
}
|
}
|
||||||
|
|
||||||
message CreateGameRequest {
|
message CreateGameRequest {
|
||||||
ru.m.tankz.proto.core.GameType type = 1;
|
string type = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
message CreateGameResponse {
|
message CreateGameResponse {
|
||||||
|
|||||||
@@ -11,40 +11,40 @@ import neko.net.ThreadServer;
|
|||||||
|
|
||||||
class Server extends ThreadServer<Session, Bytes> {
|
class Server extends ThreadServer<Session, Bytes> {
|
||||||
|
|
||||||
private static inline var TAG = "Server";
|
private static inline var TAG = "Server";
|
||||||
|
|
||||||
public function new() {
|
public function new() {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
||||||
override public function clientConnected(s:Socket):Session {
|
override public function clientConnected(s:Socket):Session {
|
||||||
var session = new Session(s);
|
var session = new Session(s);
|
||||||
L.d(TAG, "Client connected");
|
L.d(TAG, "Client connected");
|
||||||
return session;
|
return session;
|
||||||
}
|
}
|
||||||
|
|
||||||
override public function clientDisconnected(session:Session) {
|
override public function clientDisconnected(session:Session) {
|
||||||
L.d(TAG, "Client disconnected");
|
L.d(TAG, "Client disconnected");
|
||||||
session.onDisconnected();
|
session.onDisconnected();
|
||||||
}
|
}
|
||||||
|
|
||||||
override public function readClientMessage(session:Session, buf:Bytes, pos:Int, len:Int) {
|
override public function readClientMessage(session:Session, buf:Bytes, pos:Int, len:Int) {
|
||||||
return {msg: buf.sub(pos, len), bytes: len};
|
return {msg: buf.sub(pos, len), bytes: len};
|
||||||
}
|
}
|
||||||
|
|
||||||
override public function clientMessage(session:Session, bytes:Bytes) {
|
override public function clientMessage(session:Session, bytes:Bytes) {
|
||||||
session.pushData(bytes);
|
session.pushData(bytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function main() {
|
public static function main() {
|
||||||
L.push(new TraceLogger());
|
L.push(new TraceLogger());
|
||||||
#if debug
|
#if debug
|
||||||
L.push(new SocketLogger());
|
L.push(new SocketLogger());
|
||||||
#end
|
#end
|
||||||
L.d(TAG, "Running");
|
L.d(TAG, "Running");
|
||||||
L.i(TAG, "Build: " + CompilationOption.get("build"));
|
L.i(TAG, "Build: " + CompilationOption.get("build"));
|
||||||
Provider.set(IPacketBuilder, new PacketBuilder());
|
Provider.set(IPacketBuilder, new PacketBuilder());
|
||||||
var wserver = new Server();
|
var wserver = new Server();
|
||||||
wserver.run("localhost", 5001);
|
wserver.run("localhost", 5001);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,142 +1,126 @@
|
|||||||
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.proto.GameActionRequest;
|
|
||||||
import ru.m.tankz.proto.StartGameRequest;
|
|
||||||
import ru.m.tankz.proto.Person;
|
|
||||||
import ru.m.tankz.proto.JoinGameRequest;
|
|
||||||
import ru.m.tankz.proto.CreateGameRequest;
|
|
||||||
import ru.m.connect.neko.NekoWebConnection;
|
|
||||||
import haxe.io.Bytes;
|
import haxe.io.Bytes;
|
||||||
import ru.m.tankz.proto.PersonSelectResponse;
|
|
||||||
import ru.m.tankz.proto.PersonSelectRequest;
|
|
||||||
import ru.m.tankz.proto.Account;
|
|
||||||
import ru.m.connect.neko.NekoConnection;
|
|
||||||
import ru.m.tankz.proto.ErrorResponse;
|
|
||||||
import ru.m.tankz.server.db.DbProvider;
|
|
||||||
import ru.m.tankz.proto.LoginResponse;
|
|
||||||
import ru.m.tankz.proto.LoginRequest;
|
|
||||||
import ru.m.tankz.proto.GamesResponse;
|
|
||||||
import protohx.Message;
|
import protohx.Message;
|
||||||
import ru.m.connect.IConnection;
|
import ru.m.connect.IConnection;
|
||||||
|
import ru.m.connect.neko.NekoConnection;
|
||||||
|
import ru.m.connect.neko.NekoWebConnection;
|
||||||
|
import ru.m.tankz.proto.core.User;
|
||||||
|
import ru.m.tankz.proto.pack.LoginRequest;
|
||||||
|
import ru.m.tankz.proto.pack.LoginResponse;
|
||||||
import sys.net.Socket;
|
import sys.net.Socket;
|
||||||
|
import Type;
|
||||||
|
|
||||||
|
|
||||||
class Session implements IConnectionHandler implements IPacketHandler {
|
class Session implements IConnectionHandler implements IPacketHandler {
|
||||||
|
|
||||||
public static var sessions:Map<Int, Session> = new Map<Int, Session>();
|
public static var sessions:Map<Int, Session> = new Map<Int, Session>();
|
||||||
|
|
||||||
public var account(default, null):Account;
|
public var user(default, null):User;
|
||||||
public var person(default, null):Person;
|
public var gameId(default, null):Int = -1;
|
||||||
public var gameId(default, null):Int = -1;
|
|
||||||
|
|
||||||
private var connection(default, null):IConnection;
|
private var connection(default, null):IConnection;
|
||||||
private var socket:Socket;
|
private var socket:Socket;
|
||||||
|
|
||||||
public function new(socket:Socket) {
|
public function new(socket:Socket) {
|
||||||
this.socket = socket;
|
this.socket = socket;
|
||||||
}
|
|
||||||
|
|
||||||
public function send(packet:Message):Void {
|
|
||||||
connection.send(packet);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function pushData(bytes:Bytes):Void {
|
|
||||||
if (connection != null) {
|
|
||||||
connection.pushData(bytes);
|
|
||||||
} else {
|
|
||||||
var str:String = bytes.getString(0, bytes.length);
|
|
||||||
if (StringTools.startsWith(str, "GET")) {
|
|
||||||
connection = new NekoWebConnection(socket);
|
|
||||||
} else {
|
|
||||||
connection = new NekoConnection(socket);
|
|
||||||
}
|
|
||||||
connection.handler.addListener(this);
|
|
||||||
connection.packetHandler.addListener(this);
|
|
||||||
connection.pushData(bytes);
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
public function send(packet:Message):Void {
|
||||||
|
connection.send(packet);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function pushData(bytes:Bytes):Void {
|
||||||
|
if (connection != null) {
|
||||||
|
connection.pushData(bytes);
|
||||||
|
} else {
|
||||||
|
var str:String = bytes.getString(0, bytes.length);
|
||||||
|
if (StringTools.startsWith(str, "GET")) {
|
||||||
|
connection = new NekoWebConnection(socket);
|
||||||
|
} else {
|
||||||
|
connection = new NekoConnection(socket);
|
||||||
|
}
|
||||||
|
connection.handler.addListener(this);
|
||||||
|
connection.packetHandler.addListener(this);
|
||||||
|
connection.pushData(bytes);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
* Connection handlers
|
* Connection handlers
|
||||||
**/
|
**/
|
||||||
public function onConnected():Void {
|
public function onConnected():Void {
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public function onDisconnected():Void {
|
|
||||||
if (person != null) {
|
|
||||||
var game = GameManager.byPersonId.get(person.id);
|
|
||||||
if (game != null) game.leave(person);
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public function onError(error:Dynamic):Void {
|
public function onDisconnected():Void {
|
||||||
connection.send(
|
/*if (person != null) {
|
||||||
new ErrorResponse()
|
var game = GameManager.byPersonId.get(person.id);
|
||||||
.setCode(0)
|
if (game != null) game.leave(person);
|
||||||
.setMessage(Std.string(error))
|
}*/
|
||||||
);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
public function onError(error:Dynamic):Void {
|
||||||
|
/*connection.send(
|
||||||
|
new ErrorResponse()
|
||||||
|
.setCode(0)
|
||||||
|
.setMessage(Std.string(error))
|
||||||
|
);*/
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
* Packets handlers
|
* Packets handlers
|
||||||
**/
|
**/
|
||||||
public function onLoginRequest(packet:LoginRequest):Void {
|
public function onLoginRequest(packet:LoginRequest):Void {
|
||||||
var db = new DbProvider();
|
connection.send(new LoginResponse().setUser(
|
||||||
account = db.getAccount(packet.login, packet.password);
|
new User()
|
||||||
if (account != null) {
|
.setUuid(packet.uuid != null ? packet.uuid : 'xxx')
|
||||||
connection.send(new LoginResponse().setAccount(account));
|
.setName(packet.name)
|
||||||
} else {
|
));
|
||||||
connection.send(new ErrorResponse().setCode(404).setMessage("Account not found"));
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public function onPersonSelectRequest(packet:PersonSelectRequest):Void {
|
/*public function onPersonSelectRequest(packet:PersonSelectRequest):Void {
|
||||||
var db = new DbProvider();
|
var db = new DbProvider();
|
||||||
var person = db.getPerson(packet.personId);
|
var person = db.getPerson(packet.personId);
|
||||||
if (person != null) {
|
if (person != null) {
|
||||||
this.person = person;
|
this.person = person;
|
||||||
sessions.set(person.id, this);
|
sessions.set(person.id, this);
|
||||||
connection.send(new PersonSelectResponse().setPerson(person));
|
connection.send(new PersonSelectResponse().setPerson(person));
|
||||||
} else {
|
} else {
|
||||||
connection.send(new ErrorResponse().setCode(404).setMessage("Person not found"));
|
connection.send(new ErrorResponse().setCode(404).setMessage("Person not found"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public function onGamesSubscribeRequest(packet:GamesSubscribeRequest):Void {
|
public function onGamesSubscribeRequest(packet:GamesSubscribeRequest):Void {
|
||||||
GameManager.subscribers.set(person.id, true);
|
GameManager.subscribers.set(person.id, true);
|
||||||
connection.send(new GamesResponse().setGames(GameManager.getReadyGames()));
|
connection.send(new GamesResponse().setGames(GameManager.getReadyGames()));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function onGamesUnSubscribeRequest(packet:GamesUnSubscribeRequest):Void {
|
public function onGamesUnSubscribeRequest(packet:GamesUnSubscribeRequest):Void {
|
||||||
GameManager.subscribers.remove(person.id);
|
GameManager.subscribers.remove(person.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function onCreateGameRequest(packet:CreateGameRequest):Void {
|
public function onCreateGameRequest(packet:CreateGameRequest):Void {
|
||||||
new GameManager(person);
|
new GameManager(person);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function onJoinGameRequest(packet:JoinGameRequest):Void {
|
public function onJoinGameRequest(packet:JoinGameRequest):Void {
|
||||||
GameManager.byGameId.get(packet.gameId).join(person);
|
GameManager.byGameId.get(packet.gameId).join(person);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function onLeaveGameRequest(packet:LeaveGameRequest):Void {
|
public function onLeaveGameRequest(packet:LeaveGameRequest):Void {
|
||||||
GameManager.byPersonId.get(person.id).leave(person);
|
GameManager.byPersonId.get(person.id).leave(person);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function onStartGameRequest(packet:StartGameRequest):Void {
|
public function onStartGameRequest(packet:StartGameRequest):Void {
|
||||||
GameManager.byPersonId.get(person.id).start();
|
GameManager.byPersonId.get(person.id).start();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function onGameActionRequest(packet:GameActionRequest):Void {
|
public function onGameActionRequest(packet:GameActionRequest):Void {
|
||||||
GameManager.byPersonId.get(person.id).action(person, packet);
|
GameManager.byPersonId.get(person.id).action(person, packet);
|
||||||
}
|
}*/
|
||||||
|
|
||||||
public function onPacket(packet:Message):Void {
|
public function onPacket(packet:Message):Void {
|
||||||
trace("Unknown packet: " + Type.getClassName(Type.getClass(packet)).split(".").pop());
|
trace("Unknown packet: " + Type.getClassName(Type.getClass(packet)).split(".").pop());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user