Torquescript本身不带与数据库操作的功能,要想实现需要修改引擎内部代码,本人从garagegame的论坛上下载了torque与mysql数据库操作的源码,它提供了一个
MySQL 类,能方便地与mysql数据库操作:
脚本如下:
$MYSQL
::
MYSQL_DB;
function
MySQL()
{
%mysql
=
new MySQL
();
%mysql
.
host
=
"192.168.0.77"
;
// default: "localhost"
%mysql
.
port
=
3306
;
// default: 3306
%mysql
.
user
=
"root"
;
// No default
%mysql
.
pwd
=
"root"
;
// default: ""
%mysql
.
flag_compress
=
false
;
// default: false
%mysql
.
flag_ssl
=
false
;
// default: false
%mysql
.
db
=
"menagerie"
;
// No default
%mysql
.
ValidateSettings();
// optional
%mysql
.
Connect
();
%mysql
.
Query (
"SELECT * FROM tblUser"
);
%result
=
%mysql
.
StoreResult();
for
(
%i
=
0
;
%i
<
%mysql
.
NumRows (
%result
);
%i
++
)
{
%mysql
.
FetchRow (
%result
);
%userid
=
%mysql
.
GetRowCell (
%result
,
"userid"
);
%username
=
%mysql
.
GetRowCell (
%result
,
"username"
);
%pwd
=
%mysql
.
GetRowCell(
%result
,
"password"
);
echo
(
%userid
,
" --- "
,
%username
);
}
%mysql
.
FreeResult (
%result
);
%sqlInsert
=
"Insert into tbluser values('liang','lsl','world')"
;
%mysql
.
Query(
%sqlInsert
);
%result
=
%mysql
.
StoreResult();
%mysql
.
Close();
}
function
OpenMySQLDB()
{
$MYSQL
::
MYSQL_DB
=
new MySQL
();
$MYSQL
::
MYSQL_DB
.
host
=
"192.168.0.77"
;
// default: "localhost"
$MYSQL
::
MYSQL_DB
.
port
=
3306
;
// default: 3306
$MYSQL
::
MYSQL_DB
.
user
=
"root"
;
// No default
$MYSQL
::
MYSQL_DB
.
pwd
=
"root"
;
// default: ""
$MYSQL
::
MYSQL_DB
.
flag_compress
=
false
;
// default: false
$MYSQL
::
MYSQL_DB
.
flag_ssl
=
false
;
// default: false
$MYSQL
::
MYSQL_DB
.
db
=
"menagerie"
;
// No default
$MYSQL
::
MYSQL_DB
.
ValidateSettings();
// optional
$MYSQL
::
MYSQL_DB
.
Connect
();
}
function
CloseMySQLDB()
{
$MYSQL
::
MYSQL_DB
.
Close();
}
function
OperateMySQLDB(
%sql
,
%flag
)
{
$MYSQL
::
MYSQL_DB
.
Query (
%sql
);
%result
=
$MYSQL
::
MYSQL_DB
.
StoreResult();
if
(
%flag
)
{
for
(
%i
=
0
;
%i
<
%mysql
.
NumRows (
%result
);
%i
++
)
{
$MYSQL
::
MYSQL_DB
.
FetchRow (
%result
);
%userid
=
$MYSQL
::
MYSQL_DB
.
GetRowCell (
%result
,
"userid"
);
%username
=
$MYSQL
::
MYSQL_DB
.
GetRowCell (
%result
,
"username"
);
%pwd
=
$MYSQL
::
MYSQL_DB
.
GetRowCell(
%result
,
"password"
);
}
}
$MYSQL
::
MYSQL_DB
.
FreeResult (
%result
);
}
与数据库连接的代码被放在
common
模块中,在
server
端,建立一个
TCPObject
对象,用于监听客户端的消息,在接收到客户端的连接后创建
TCPObject
对象返回给客户端,以此实现通信,客户端会按照一定的协议发送消息到服务器端,服务器根据发来的消息实现与数据库的各种操作。
function
initChatServer()
{
new TCPObject
(chatConnection);
chatConnection
.
listen(
20010
);
echo
(
"Listening for new connections on port 20010"
);
$PollTime
=
60
;
// time, in minutes, between checks to see if the server
echo
(
"Sechedule set"
);
}
function
chatConnection
::
onConnectRequest(
%this
,
%address
,
%id
)
{
%client
=
new TCPObject
(chatClient,
%id
);
echo
(
"conn_hand"
@
%client
);
%i
=
findOpenClientSlot();
$clients
[
%i
,
"clientID"
]
=
%client
;
$clients
[
%i
,
"clientNickname"
]
=
"Guest"
;
$clients
[
%i
,
"connected"
]
=
true
;
OpenMySQLDB();
}
function
chatClient
::
processLine(
%this
,
%line
,
%id
)
{
%posSep
=
strpos
(
%line
,
":"
);
if
(
%posSep
==
0
)
return
;
%len
=
strlen
(
%line
);
%command
=
getSubStr
(
%line
,
0
,
%posSep
);
//?
%argument
=
getSubStr
(
%line
,
%posSep
+
1
,
%len
-
%posSep
+
1
);
//?¨°
%nickname
=
getClientNickname(
%id
);
%i
=
findClientRecord(
%id
);
// This is the string form of the switch/case command
switch
$ (
%command
)
{
case
"create_account"
:
%pos1
=
strpos
(
%line
,
","
);
%pos2
=
strpos
(
%line
,
","
,
%pos1
+
1
);
if
(
%pos1
==
0
)
return
;
%user
=
getsubstr
(
%line
,
%possep
+
1
,
%pos1
-
%posSep
-
1
);
%pass
=
getsubstr
(
%line
,
%pos1
+
1
,
%pos2
-
%pos1
-
1
);
%name
=
getsubstr
(
%line
,
%pos2
+
1
,
%len
-
%pos2
+
1
);
%result
=
create_account(
%user
,
%pass
,
%name
);
%id
.
send(
"send:create_ok/n"
);
}
}
在client端,代码如下:
function
chatConnect(
%server
,
%port
,
%nick
)
{
%address
=
%server
@
":"
@
%port
;
// create a new TCPObject for our network code
new TCPObject
(chatConnection);
// attempt to connect to the server
chatConnection
.
connect
(
%address
);
// flag us as not connected at this point
$connected
=
false
;
// save our nickname
$nickname
=
%nick
;
}
function
chatLogin()
{
if
(
$nickname
$=
""
)
{
%id
=
getRealTime
()
/
1000
/
60
;
$nickname
=
"Guest"
@
%id
;
}
// send the login command to the server
chatConnection
.
send(
"login:"
@
$nickname
@
"/n"
);
}
function
client_login(
%log_id
,
%password
)
//freeman
{
chatconnection
.
send(
"login_server:"
@
%log_id
@
","
@
%password
@
"/n"
);
}