use orm
This commit is contained in:
6
gen.sh
6
gen.sh
@@ -1,2 +1,6 @@
|
||||
#!/bin/bash
|
||||
haxelib run protohx generate protohx.json
|
||||
haxelib run protohx generate protohx.json
|
||||
haxelib run orm mysql://shmyga:xkbp8jh9z2@localhost:3306/armageddon \
|
||||
-s src-gen/haxe \
|
||||
-c ru.m.tankz.db \
|
||||
-a ru.m.tankz.db
|
||||
@@ -17,5 +17,4 @@
|
||||
<haxeflag name="-dce" value="no"/>
|
||||
<haxeflag name="-debug"/>
|
||||
<haxeflag name="--macro" value="Meta.set('0.0.0')"/>
|
||||
<haxeflag name="--macro" value="include('ru.m.tankz')"/>
|
||||
</project>
|
||||
@@ -1,5 +1,6 @@
|
||||
-main ru.m.tankz.server.Server
|
||||
-lib protohx
|
||||
-lib orm
|
||||
-lib haxework
|
||||
-cp src/common/haxe
|
||||
-cp src/server/haxe
|
||||
|
||||
@@ -29,7 +29,7 @@ class JsConnection extends BaseConnection {
|
||||
}
|
||||
|
||||
private function buildSocket(host:String, port:Int):WebSocket {
|
||||
return untyped __js__("self.socket = new WebSocket('ws://'+host+':'+port); ");
|
||||
return untyped __js__("new WebSocket('ws://'+host+':'+port);");
|
||||
}
|
||||
|
||||
override public function connect():Void {
|
||||
|
||||
@@ -16,22 +16,22 @@ class Server extends ThreadServer<Session, Bytes> {
|
||||
super();
|
||||
}
|
||||
|
||||
override function clientConnected(s:Socket):Session {
|
||||
override public function clientConnected(s:Socket):Session {
|
||||
var session = new Session(s);
|
||||
L.d(TAG, "Client connected");
|
||||
return session;
|
||||
}
|
||||
|
||||
override function clientDisconnected(session:Session) {
|
||||
override public function clientDisconnected(session:Session) {
|
||||
L.d(TAG, "Client disconnected");
|
||||
session.onDisconnected();
|
||||
}
|
||||
|
||||
override 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};
|
||||
}
|
||||
|
||||
override function clientMessage(session:Session, bytes:Bytes) {
|
||||
override public function clientMessage(session:Session, bytes:Bytes) {
|
||||
session.pushData(bytes);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
package ru.m.tankz.server.db;
|
||||
|
||||
import ru.m.tankz.db.Orm;
|
||||
import ru.m.tankz.proto.Person;
|
||||
import ru.m.tankz.proto.Account;
|
||||
import sys.db.Mysql;
|
||||
import sys.db.Connection;
|
||||
|
||||
typedef DbPerson = {
|
||||
@:optional var a_id:Int;
|
||||
@@ -13,37 +12,33 @@ typedef DbPerson = {
|
||||
|
||||
class Db {
|
||||
|
||||
private var db:Connection;
|
||||
private var db:orm.Db;
|
||||
private var orm:Orm;
|
||||
|
||||
public function new() {
|
||||
db = Mysql.connect({
|
||||
host: "localhost",
|
||||
port: 3306,
|
||||
user: "shmyga",
|
||||
pass: "xkbp8jh9z2",
|
||||
socket: null,
|
||||
database: "armageddon"
|
||||
});
|
||||
db = new orm.Db("mysql://shmyga:xkbp8jh9z2@localhost:3306/armageddon");
|
||||
orm = new Orm(db);
|
||||
}
|
||||
|
||||
public function getAccount(login:String, password:String):Null<Account> {
|
||||
var rset = db.request("SELECT a.id AS a_id, p.id AS p_id, p.name AS p_name FROM account AS a LEFT OUTER JOIN person AS p ON(a.id=p.account_id) WHERE a.login='" + login + "' AND a.password='" + password + "'");
|
||||
if (!rset.hasNext()) return null;
|
||||
var account = new Account().setLogin(login);
|
||||
while (rset.hasNext()) {
|
||||
var data:DbPerson = rset.next();
|
||||
account.setId(data.a_id);
|
||||
if (data.p_name != null) {
|
||||
account.addPersons(new Person().setId(data.p_id).setName(data.p_name));
|
||||
var account = null;
|
||||
var accountModel = orm.account.getByLogin(login);
|
||||
if (accountModel != null && accountModel.password == password) {
|
||||
account = new Account().setLogin(accountModel.login);
|
||||
var personsModels = orm.person.getByAccount_id(accountModel.id);
|
||||
for (personModel in personsModels) {
|
||||
account.addPersons(new Person().setId(personModel.id).setName(personModel.name));
|
||||
}
|
||||
}
|
||||
return account;
|
||||
}
|
||||
|
||||
public function getPerson(account_id:Int, person_id:Int):Null<Person> {
|
||||
var rset = db.request("SELECT id AS p_id, name AS p_name FROM person WHERE id=" + person_id + " AND account_id=" + account_id);
|
||||
if (!rset.hasNext()) return null;
|
||||
var data:DbPerson = rset.next();
|
||||
return new Person().setId(data.p_id).setName(data.p_name);
|
||||
public function getPerson(person_id:Int):Null<Person> {
|
||||
var person = null;
|
||||
var personModel = orm.person.get(person_id);
|
||||
if (personModel != null) {
|
||||
person = new Person().setId(personModel.id).setName(personModel.name);
|
||||
}
|
||||
return person;
|
||||
}
|
||||
}
|
||||
@@ -151,7 +151,7 @@ class Session implements IConnectionHandler implements IPacketHandler {
|
||||
|
||||
public function onPersonSelectRequest(packet:PersonSelectRequest):Void {
|
||||
var db = new Db();
|
||||
var person = db.getPerson(account.id, packet.personId);
|
||||
var person = db.getPerson(packet.personId);
|
||||
if (person != null) {
|
||||
this.person = person;
|
||||
connection.send(new PersonSelectResponse().setPerson(person));
|
||||
|
||||
Reference in New Issue
Block a user