-
This commit is contained in:
@@ -1,9 +1,14 @@
|
|||||||
package ru.m.armageddon.proto;
|
package ru.m.armageddon.proto;
|
||||||
|
|
||||||
message User {
|
message Person {
|
||||||
|
required int32 id = 1;
|
||||||
|
required string name = 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
message Account {
|
||||||
required int32 id = 1;
|
required int32 id = 1;
|
||||||
required string login = 2;
|
required string login = 2;
|
||||||
required string nickname = 3;
|
repeated Person persons = 3;
|
||||||
}
|
}
|
||||||
|
|
||||||
message LoginRequest {
|
message LoginRequest {
|
||||||
@@ -12,7 +17,7 @@ message LoginRequest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
message LoginResponse {
|
message LoginResponse {
|
||||||
required User user = 1;
|
required Account account = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
message ErrorResponse {
|
message ErrorResponse {
|
||||||
|
|||||||
@@ -40,11 +40,20 @@
|
|||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
"id":"error",
|
||||||
|
"type":"haxework.gui.LabelView",
|
||||||
|
"pWidth":100,
|
||||||
|
"height":30,
|
||||||
|
"fontColor":"0xff0000",
|
||||||
|
"text":""
|
||||||
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
"type":"haxework.gui.SpriteView",
|
"type":"haxework.gui.SpriteView",
|
||||||
"pWidth":100,
|
"pWidth":100,
|
||||||
"pHeight":100,
|
"pHeight":100,
|
||||||
"skin":{"type":"haxework.gui.skin.ColorSkin", "color":"0x000000"}
|
"skin":{"type":"haxework.gui.skin.ColorSkin", "color":"0x505050"}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
19
sql/init.sql
19
sql/init.sql
@@ -6,10 +6,23 @@ CREATE DATABASE IF NOT EXISTS armageddon;
|
|||||||
GRANT ALL ON armageddon.* TO 'shmyga'@'localhost' IDENTIFIED BY 'xkbp8jh9z2';
|
GRANT ALL ON armageddon.* TO 'shmyga'@'localhost' IDENTIFIED BY 'xkbp8jh9z2';
|
||||||
|
|
||||||
|
|
||||||
DROP TABLE IF EXISTS armageddon.users;
|
DROP TABLE IF EXISTS armageddon.account;
|
||||||
CREATE TABLE IF NOT EXISTS armageddon.users (
|
CREATE TABLE IF NOT EXISTS armageddon.account (
|
||||||
id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
|
id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
|
||||||
login VARCHAR(255) UNIQUE,
|
login VARCHAR(255) UNIQUE,
|
||||||
password VARCHAR(32)
|
password VARCHAR(32)
|
||||||
);
|
);
|
||||||
INSERT INTO armageddon.users (login,password) VALUES('shmyga', 'd48cc4eb42c058869ae90daef9606e43');
|
|
||||||
|
DROP TABLE IF EXISTS armageddon.person;
|
||||||
|
CREATE TABLE IF NOT EXISTS armageddon.account (
|
||||||
|
id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
|
||||||
|
account_id INT UNSIGNED NOT NULL,
|
||||||
|
name VARCHAR(255),
|
||||||
|
|
||||||
|
CONSTRAINT person_2_account FOREIGN KEY (account_id) REFERENCES armageddon.account(id)
|
||||||
|
MATCH SIMPLE ON DELETE CASCADE ON UPDATE NO ACTION
|
||||||
|
);
|
||||||
|
|
||||||
|
INSERT INTO armageddon.account (id,login,password) VALUES(1,'shmyga', 'd48cc4eb42c058869ae90daef9606e43');
|
||||||
|
INSERT INTO armageddon.person (id,account_id,name) VALUES(1,1,'-=Shmyga=-');
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
package ru.m.armageddon.client;
|
package ru.m.armageddon.client;
|
||||||
|
|
||||||
|
import ru.m.armageddon.proto.ErrorResponse;
|
||||||
|
import ru.m.armageddon.proto.Account;
|
||||||
import haxework.gui.LabelView;
|
import haxework.gui.LabelView;
|
||||||
import haxework.gui.ButtonView;
|
import haxework.gui.ButtonView;
|
||||||
import haxework.gui.Root;
|
import haxework.gui.Root;
|
||||||
@@ -8,8 +10,6 @@ import flash.display.Sprite;
|
|||||||
import haxework.gui.IGroupView;
|
import haxework.gui.IGroupView;
|
||||||
import haxe.Json;
|
import haxe.Json;
|
||||||
import openfl.Assets;
|
import openfl.Assets;
|
||||||
import haxe.Timer;
|
|
||||||
import ru.m.armageddon.proto.User;
|
|
||||||
import ru.m.armageddon.proto.LoginResponse;
|
import ru.m.armageddon.proto.LoginResponse;
|
||||||
import protohx.Message;
|
import protohx.Message;
|
||||||
import haxe.crypto.Md5;
|
import haxe.crypto.Md5;
|
||||||
@@ -30,11 +30,12 @@ class Client implements IConnectionHandler {
|
|||||||
|
|
||||||
|
|
||||||
private var connection:IConnection;
|
private var connection:IConnection;
|
||||||
private var user:User;
|
private var account:Account;
|
||||||
|
|
||||||
private var loginButton:ButtonView;
|
private var loginButton:ButtonView;
|
||||||
private var connectionStateLabel:LabelView;
|
private var connectionStateLabel:LabelView;
|
||||||
private var nicknameLabel:LabelView;
|
private var nicknameLabel:LabelView;
|
||||||
|
private var errorLabel:LabelView;
|
||||||
|
|
||||||
public function new() {
|
public function new() {
|
||||||
var bytes = Assets.getBytes("res/layout/main.json");
|
var bytes = Assets.getBytes("res/layout/main.json");
|
||||||
@@ -44,19 +45,21 @@ class Client implements IConnectionHandler {
|
|||||||
loginButton = v.findViewById("panel:login");
|
loginButton = v.findViewById("panel:login");
|
||||||
connectionStateLabel = v.findViewById("panel:connection_state");
|
connectionStateLabel = v.findViewById("panel:connection_state");
|
||||||
nicknameLabel = v.findViewById("panel:nickname");
|
nicknameLabel = v.findViewById("panel:nickname");
|
||||||
|
errorLabel = v.findViewById("error");
|
||||||
|
|
||||||
connection = new FlashConnection("localhost", 5000, this);
|
connection = new FlashConnection("localhost", 5000, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function refreshUI():Void {
|
private function refreshUI():Void {
|
||||||
connectionStateLabel.text = connection.connected ? "Connected" : "Disconnected";
|
connectionStateLabel.text = connection.connected ? "Connected" : "Disconnected";
|
||||||
nicknameLabel.text = user == null ? "" : user.nickname;
|
nicknameLabel.text = account == null ? "" : account.login;
|
||||||
loginButton.disabled = connection.connected && user != null;
|
loginButton.disabled = connection.connected && account != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function onPress(view:ButtonView):Void {
|
public function onPress(view:ButtonView):Void {
|
||||||
switch (view.id) {
|
switch (view.id) {
|
||||||
case "login":
|
case "login":
|
||||||
|
errorLabel.text = "";
|
||||||
connection.connect();
|
connection.connect();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -71,17 +74,23 @@ class Client implements IConnectionHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function onDisconnected():Void {
|
public function onDisconnected():Void {
|
||||||
this.user = null;
|
account = null;
|
||||||
refreshUI();
|
refreshUI();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function onError(error:Dynamic):Void {
|
public function onError(error:Dynamic):Void {
|
||||||
this.user = null;
|
account = null;
|
||||||
|
errorLabel.text = Std.string(error);
|
||||||
refreshUI();
|
refreshUI();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function onLoginResponse(packet:LoginResponse):Void {
|
public function onLoginResponse(packet:LoginResponse):Void {
|
||||||
this.user = packet.user;
|
account = packet.account;
|
||||||
|
refreshUI();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function onErrorResponse(packet:ErrorResponse):Void {
|
||||||
|
errorLabel.text = packet.message;
|
||||||
refreshUI();
|
refreshUI();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ class NekoConnection extends BaseConnection {
|
|||||||
try {
|
try {
|
||||||
receive(packet);
|
receive(packet);
|
||||||
} catch (error:Dynamic) {
|
} catch (error:Dynamic) {
|
||||||
|
trace(error);
|
||||||
handler.onError(error);
|
handler.onError(error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,12 +14,12 @@ class Server extends ThreadServer<Session, Bytes> {
|
|||||||
|
|
||||||
override function clientConnected(s:Socket):Session {
|
override function clientConnected(s:Socket):Session {
|
||||||
var session = new Session(s);
|
var session = new Session(s);
|
||||||
Lib.println("client: " + session.user + " / " + s.peer());
|
Lib.println("client: " + s.peer());
|
||||||
return session;
|
return session;
|
||||||
}
|
}
|
||||||
|
|
||||||
override function clientDisconnected(session:Session) {
|
override function clientDisconnected(session:Session) {
|
||||||
Lib.println("client " + Std.string(session.user) + " disconnected");
|
Lib.println("client disconnected");
|
||||||
}
|
}
|
||||||
|
|
||||||
override function readClientMessage(session:Session, buf:Bytes, pos:Int, len:Int) {
|
override function readClientMessage(session:Session, buf:Bytes, pos:Int, len:Int) {
|
||||||
|
|||||||
@@ -1,8 +1,15 @@
|
|||||||
package ru.m.armageddon.server.db;
|
package ru.m.armageddon.server.db;
|
||||||
|
|
||||||
|
import ru.m.armageddon.proto.Person;
|
||||||
|
import ru.m.armageddon.proto.Account;
|
||||||
import sys.db.Mysql;
|
import sys.db.Mysql;
|
||||||
import sys.db.Connection;
|
import sys.db.Connection;
|
||||||
|
|
||||||
|
typedef DbPerson = {
|
||||||
|
var p_id:Int;
|
||||||
|
var p_name:String;
|
||||||
|
}
|
||||||
|
|
||||||
class Db {
|
class Db {
|
||||||
|
|
||||||
private var db:Connection;
|
private var db:Connection;
|
||||||
@@ -18,10 +25,14 @@ class Db {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getUser(login:String, password:String) {
|
public function getAccount(login:String, password:String):Null<Account> {
|
||||||
var rset = db.request("SELECT * FROM Users WHERE login='" + login + "' AND password='" + password + "'");
|
var rset = db.request("SELECT p.id AS p_id, p.name AS p_name FROM account AS a LEFT JOIN person AS p ON(a.id=p.account_id) WHERE a.login='" + login + "' AND a.password='" + password + "'");
|
||||||
if (!rset.hasNext()) return null;
|
if (!rset.hasNext()) return null;
|
||||||
var user = rset.next();
|
var account = new Account().setLogin(login);
|
||||||
return user;
|
while (rset.hasNext()) {
|
||||||
|
var data:DbPerson = rset.next();
|
||||||
|
account.addPersons(new Person().setId(data.p_id).setName(data.p_name));
|
||||||
|
}
|
||||||
|
return account;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,9 +1,9 @@
|
|||||||
package ru.m.armageddon.server.session;
|
package ru.m.armageddon.server.session;
|
||||||
|
|
||||||
|
import ru.m.armageddon.proto.Account;
|
||||||
import ru.m.armageddon.core.connect.neko.NekoConnection;
|
import ru.m.armageddon.core.connect.neko.NekoConnection;
|
||||||
import ru.m.armageddon.proto.ErrorResponse;
|
import ru.m.armageddon.proto.ErrorResponse;
|
||||||
import ru.m.armageddon.server.db.Db;
|
import ru.m.armageddon.server.db.Db;
|
||||||
import ru.m.armageddon.proto.User;
|
|
||||||
import ru.m.armageddon.proto.LoginResponse;
|
import ru.m.armageddon.proto.LoginResponse;
|
||||||
import ru.m.armageddon.proto.LoginRequest;
|
import ru.m.armageddon.proto.LoginRequest;
|
||||||
import protohx.Message;
|
import protohx.Message;
|
||||||
@@ -12,7 +12,7 @@ import sys.net.Socket;
|
|||||||
|
|
||||||
class Session implements IConnectionHandler {
|
class Session implements IConnectionHandler {
|
||||||
|
|
||||||
public var user(default, null):User;
|
public var account(default, null):Account;
|
||||||
public var connection(default, null):IConnection;
|
public var connection(default, null):IConnection;
|
||||||
|
|
||||||
public function new(socket:Socket) {
|
public function new(socket:Socket) {
|
||||||
@@ -37,20 +37,11 @@ class Session implements IConnectionHandler {
|
|||||||
|
|
||||||
public function onLoginRequest(packet:LoginRequest):Void {
|
public function onLoginRequest(packet:LoginRequest):Void {
|
||||||
var db = new Db();
|
var db = new Db();
|
||||||
var userData = db.getUser(packet.login, packet.password);
|
account = db.getAccount(packet.login, packet.password);
|
||||||
if (userData != null) {
|
if (account != null) {
|
||||||
var user = new User();
|
connection.send(new LoginResponse().setAccount(account));
|
||||||
user.login = userData.login;
|
|
||||||
user.nickname = userData.login;
|
|
||||||
this.user = user;
|
|
||||||
var response = new LoginResponse();
|
|
||||||
response.user = user;
|
|
||||||
connection.send(response);
|
|
||||||
} else {
|
} else {
|
||||||
var response = new ErrorResponse();
|
connection.send(new ErrorResponse().setCode(404).setMessage("Account not found"));
|
||||||
response.code = 403;
|
|
||||||
response.message = "User not found";
|
|
||||||
connection.send(response);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user