ZMud调用COM的尝试(二)-房间数据采集
本帖最后由 jsleo 于 2013-9-24 11:19 AM 编辑开始尝试采集房间数据,遇到一个问题,就是zmud一直被诟病的乱码问题。
当采集到的数据存储到变量里面的时候,是肯定会出现乱码的。但是经过尝试,发现如果不通过变量中转,而是直接发送给com组件,则没有乱码。
采集的zmud侧代码很简单,也没有反捣乱,就是个尝试。用look后的房间名称触发,因为后面带“ -”,提示符">"作为结束触发,之间的所有内容直接发送给com组件。
zmud代码#CLASS {caiji}
#ALIAS com {#var ccc %comcreate( "zmud.Class1")}
#VAR desc {<OLE object: zmud.Class1>}
#TRIGGER {^(%x)%s~-} {#T+ cjdata;desc = ""}
#CLASS 0
#CLASS {caiji|cjdata}
#TRIGGER {^~>} {desc=@ccc.send("over");#T- cjdata}
#TRIGGER {^(*)$} {desc=@ccc.send("%1")}
#CLASS 0com代码using System;
using System.Collections.Generic;
using System.IO;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using com.a2.DB;
namespace zmud
{
public interface MyCom_Interface
{
string send(string s);
string gowhere(string s);
}
public interface MyCom_Events
{
}
public class Class1 : MyCom_Interface
{
string stat = "name";
string roomname = "";
string roomdesc = "";
string exits = "";
string[] exit;
IDatabase conn = DatabaseFactory.Creator("mssql", "Password=xxxx;User ID=sa;Initial Catalog=xkx;Data Source=(local)");
string from = "";
string result = "";
public string send(string s)
{
s = s.Replace("\t", "").Trim();
if (s == "over")
{
//屏蔽季节和天气
if (roomdesc.IndexOf("「") > 0)
{
roomdesc = roomdesc.Substring(0, roomdesc.IndexOf("「")) + roomdesc.Substring(roomdesc.IndexOf("。", roomdesc.IndexOf("「")) + 1);
}
//起始房间,是否存在多个
if (from == "start")
{
string sql = string.Format("select * from room where name='{0}' and describe='{1}' and exits='{2}'", roomname, roomdesc, exits);
DataTable dt = conn.GetDataTable(sql);
if (dt.Rows.Count > 1)
return "该房间存在多个,不能开始";
if (dt.Rows.Count == 1)
{
return "目前房间是" + dt.Rows["name"] + ",开始行走";
}
if (dt.Rows.Count == 0)
{
sql = string.Format("insert into room (name,describe,exits) values ('{0}','{1}','{2}')", roomname, roomdesc, exits);
conn.Execute(sql);
int roomid = int.Parse(conn.GetDataTable(string.Format("select id from room where name='{0}' and describe='{1}' and exits='{2}'", roomname, roomdesc, exits)).Rows.ToString());
for (int i = 0; i < exit.Length; i++)
{
sql = string.Format("insert into exit (exitname,atroomid) values ('{0}',{1})", exit, roomid);
conn.Execute(sql);
}
}
}
StreamWriter sw = new StreamWriter(@"d:\xkx.txt", true);
sw.WriteLine("name:" + roomname);
sw.WriteLine("desc:" + roomdesc);
sw.WriteLine("exits:" + exits);
sw.WriteLine("exitnum:" + exit.Length);
sw.WriteLine("--------------");
sw.Close();
//变量复位
stat = "name";
roomname = "";
roomdesc = "";
exits = "";
return result;
}
if (stat == "name")
{
if (s.IndexOf(" -") > 0)
{
roomname = s.Substring(0, s.IndexOf(" -"));
stat = "desc";
}
}
else
{
if (stat == "desc")
{
if (s.IndexOf("这里唯一的出口") < 0 && s.IndexOf("这里明显的出口是") < 0)
{
roomdesc += s;
}
else
{
stat = "exit";
}
}
if (stat == "exit")
{
if (s.IndexOf("这里唯一的出口") == 0)
{
exits = s.Substring(s.IndexOf(" "), s.IndexOf("。") - s.IndexOf(" ")).Trim();
exit = new string[] { exits };
}
if (s.IndexOf("这里明显的出口是") == 0)
{
exits = s.Substring(s.IndexOf(" "), s.IndexOf("和") - s.IndexOf(" ")).Trim().Replace("、", ",") + "," + s.Substring(s.IndexOf("和") + 1, s.IndexOf("。") - s.IndexOf("和")).Trim();
exit = exits.Split(',');
}
stat = "over";
}
}
return "";
}
public string gowhere(string s)
{
from = s;
return "";
}
private void print(string s)
{
StreamWriter sw = new StreamWriter(@"d:\xkx.txt", true);
sw.WriteLine(s);
sw.Close();
}
}
}
CREATE TABLE .(
IDENTITY(1,1) NOT NULL,
(50) NOT NULL,
(500) NOT NULL,
(100) NOT NULL,
(200) NULL
) ON
CREATE TABLE .(
IDENTITY(1,1) NOT NULL,
(20) NOT NULL,
NOT NULL,
NULL
) ON 快来看上帝 膜拜~~~~~~~~~~ 看上去好牛的样子啊~~眼馋ing...
不过感觉没有正则,对字符串的判断似乎有些吃力。。。不知道是不是这样 回复 4# hijacker
是。不过字符串处理不多,我懒得去看正则了。
才刚写了个开头,越写越多了
页:
[1]