-
This commit is contained in:
@@ -1,9 +1,14 @@
|
||||
package ru.m.armageddon.proto;
|
||||
|
||||
message User {
|
||||
message Person {
|
||||
required int32 id = 1;
|
||||
required string name = 2;
|
||||
}
|
||||
|
||||
message Account {
|
||||
required int32 id = 1;
|
||||
required string login = 2;
|
||||
required string nickname = 3;
|
||||
repeated Person persons = 3;
|
||||
}
|
||||
|
||||
message LoginRequest {
|
||||
@@ -12,7 +17,7 @@ message LoginRequest {
|
||||
}
|
||||
|
||||
message LoginResponse {
|
||||
required User user = 1;
|
||||
required Account account = 1;
|
||||
}
|
||||
|
||||
message ErrorResponse {
|
||||
|
||||
@@ -40,11 +40,20 @@
|
||||
]
|
||||
},
|
||||
|
||||
{
|
||||
"id":"error",
|
||||
"type":"haxework.gui.LabelView",
|
||||
"pWidth":100,
|
||||
"height":30,
|
||||
"fontColor":"0xff0000",
|
||||
"text":""
|
||||
},
|
||||
|
||||
{
|
||||
"type":"haxework.gui.SpriteView",
|
||||
"pWidth":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';
|
||||
|
||||
|
||||
DROP TABLE IF EXISTS armageddon.users;
|
||||
CREATE TABLE IF NOT EXISTS armageddon.users (
|
||||
DROP TABLE IF EXISTS armageddon.account;
|
||||
CREATE TABLE IF NOT EXISTS armageddon.account (
|
||||
id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
|
||||
login VARCHAR(255) UNIQUE,
|
||||
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;
|
||||
|
||||
import ru.m.armageddon.proto.ErrorResponse;
|
||||
import ru.m.armageddon.proto.Account;
|
||||
import haxework.gui.LabelView;
|
||||
import haxework.gui.ButtonView;
|
||||
import haxework.gui.Root;
|
||||
@@ -8,8 +10,6 @@ import flash.display.Sprite;
|
||||
import haxework.gui.IGroupView;
|
||||
import haxe.Json;
|
||||
import openfl.Assets;
|
||||
import haxe.Timer;
|
||||
import ru.m.armageddon.proto.User;
|
||||
import ru.m.armageddon.proto.LoginResponse;
|
||||
import protohx.Message;
|
||||
import haxe.crypto.Md5;
|
||||
@@ -30,11 +30,12 @@ class Client implements IConnectionHandler {
|
||||
|
||||
|
||||
private var connection:IConnection;
|
||||
private var user:User;
|
||||
private var account:Account;
|
||||
|
||||
private var loginButton:ButtonView;
|
||||
private var connectionStateLabel:LabelView;
|
||||
private var nicknameLabel:LabelView;
|
||||
private var errorLabel:LabelView;
|
||||
|
||||
public function new() {
|
||||
var bytes = Assets.getBytes("res/layout/main.json");
|
||||
@@ -44,19 +45,21 @@ class Client implements IConnectionHandler {
|
||||
loginButton = v.findViewById("panel:login");
|
||||
connectionStateLabel = v.findViewById("panel:connection_state");
|
||||
nicknameLabel = v.findViewById("panel:nickname");
|
||||
errorLabel = v.findViewById("error");
|
||||
|
||||
connection = new FlashConnection("localhost", 5000, this);
|
||||
}
|
||||
|
||||
private function refreshUI():Void {
|
||||
connectionStateLabel.text = connection.connected ? "Connected" : "Disconnected";
|
||||
nicknameLabel.text = user == null ? "" : user.nickname;
|
||||
loginButton.disabled = connection.connected && user != null;
|
||||
nicknameLabel.text = account == null ? "" : account.login;
|
||||
loginButton.disabled = connection.connected && account != null;
|
||||
}
|
||||
|
||||
public function onPress(view:ButtonView):Void {
|
||||
switch (view.id) {
|
||||
case "login":
|
||||
errorLabel.text = "";
|
||||
connection.connect();
|
||||
}
|
||||
}
|
||||
@@ -71,17 +74,23 @@ class Client implements IConnectionHandler {
|
||||
}
|
||||
|
||||
public function onDisconnected():Void {
|
||||
this.user = null;
|
||||
account = null;
|
||||
refreshUI();
|
||||
}
|
||||
|
||||
public function onError(error:Dynamic):Void {
|
||||
this.user = null;
|
||||
account = null;
|
||||
errorLabel.text = Std.string(error);
|
||||
refreshUI();
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
|
||||
@@ -28,6 +28,7 @@ class NekoConnection extends BaseConnection {
|
||||
try {
|
||||
receive(packet);
|
||||
} catch (error:Dynamic) {
|
||||
trace(error);
|
||||
handler.onError(error);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,12 +14,12 @@ class Server extends ThreadServer<Session, Bytes> {
|
||||
|
||||
override function clientConnected(s:Socket):Session {
|
||||
var session = new Session(s);
|
||||
Lib.println("client: " + session.user + " / " + s.peer());
|
||||
Lib.println("client: " + s.peer());
|
||||
return 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) {
|
||||
|
||||
@@ -1,8 +1,15 @@
|
||||
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.Connection;
|
||||
|
||||
typedef DbPerson = {
|
||||
var p_id:Int;
|
||||
var p_name:String;
|
||||
}
|
||||
|
||||
class Db {
|
||||
|
||||
private var db:Connection;
|
||||
@@ -18,10 +25,14 @@ class Db {
|
||||
});
|
||||
}
|
||||
|
||||
public function getUser(login:String, password:String) {
|
||||
var rset = db.request("SELECT * FROM Users WHERE login='" + login + "' AND password='" + password + "'");
|
||||
public function getAccount(login:String, password:String):Null<Account> {
|
||||
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;
|
||||
var user = rset.next();
|
||||
return user;
|
||||
var account = new Account().setLogin(login);
|
||||
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;
|
||||
|
||||
import ru.m.armageddon.proto.Account;
|
||||
import ru.m.armageddon.core.connect.neko.NekoConnection;
|
||||
import ru.m.armageddon.proto.ErrorResponse;
|
||||
import ru.m.armageddon.server.db.Db;
|
||||
import ru.m.armageddon.proto.User;
|
||||
import ru.m.armageddon.proto.LoginResponse;
|
||||
import ru.m.armageddon.proto.LoginRequest;
|
||||
import protohx.Message;
|
||||
@@ -12,7 +12,7 @@ import sys.net.Socket;
|
||||
|
||||
class Session implements IConnectionHandler {
|
||||
|
||||
public var user(default, null):User;
|
||||
public var account(default, null):Account;
|
||||
public var connection(default, null):IConnection;
|
||||
|
||||
public function new(socket:Socket) {
|
||||
@@ -37,20 +37,11 @@ class Session implements IConnectionHandler {
|
||||
|
||||
public function onLoginRequest(packet:LoginRequest):Void {
|
||||
var db = new Db();
|
||||
var userData = db.getUser(packet.login, packet.password);
|
||||
if (userData != null) {
|
||||
var user = new User();
|
||||
user.login = userData.login;
|
||||
user.nickname = userData.login;
|
||||
this.user = user;
|
||||
var response = new LoginResponse();
|
||||
response.user = user;
|
||||
connection.send(response);
|
||||
account = db.getAccount(packet.login, packet.password);
|
||||
if (account != null) {
|
||||
connection.send(new LoginResponse().setAccount(account));
|
||||
} else {
|
||||
var response = new ErrorResponse();
|
||||
response.code = 403;
|
||||
response.message = "User not found";
|
||||
connection.send(response);
|
||||
connection.send(new ErrorResponse().setCode(404).setMessage("Account not found"));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user