用户:
伴雪纷飞查看:2 回复:3 评论:2 创建时间:2023-01-08T16:03:58
console.clear()
//circle.js来源于
/喵mirrors/douglascrockford/json-js/-/blob/master/cycle.js
if (typeof JSON.decycle !== "function") {
JSON.decycle = function decycle(object, replacer) {
"use strict";
var objects = new WeakMap(); // object to path mappings
return (function derez(value, path) {
var old_path;
var nu;
if (replacer !== undefined) {
value = replacer(value);
}
if (
typeof value === "object"
&& value !== null
&& !(value instanceof Boolean)
&& !(value instanceof Date)
&& !(value instanceof Number)
&& !(value instanceof RegExp)
&& !(value instanceof String)
) {
old_path = objects.get(value);
if (old_path !== undefined) {
return {$ref: old_path};
}
objects.set(value, path);
if (Array.isArray(value)) {
nu = [];
value.forEach(function (element, i) {
nu[i] = derez(element, path + "[" + i + "]");
});
} else {
nu = {};
Object.keys(value).forEach(function (name) {
nu[name] = derez(
value[name],
path + "[" + JSON.stringify(name) + "]"
);
});
}
return nu;
}
return value;
}(object, "$"));
};
}
if (typeof JSON.retrocycle !== "function") {
JSON.retrocycle = function retrocycle($) {
"use strict";
var px = /^\$(?:\[(?:\d+|"(?:[^\\"\u0000-\u001f]|\\(?:[\\"\/bfnrt]|u[0-9a-zA-Z]{4}))*")\])*$/;
(function rez(value) {
if (value && typeof value === "object") {
if (Array.isArray(value)) {
value.forEach(function (element, i) {
if (typeof element === "object" && element !== null) {
var path = element.$ref;
if (typeof path === "string" && px.test(path)) {
value[i] = eval(path);
} else {
rez(element);
}
}
});
} else {
Object.keys(value).forEach(function (name) {
var item = value[name];
if (typeof item === "object" && item !== null) {
var path = item.$ref;
if (typeof path === "string" && px.test(path)) {
value[name] = eval(path);
} else {
rez(item);
}
}
});
}
}
}($));
return $;
};
}
//以下代码为本人编写
db.sql`CREATE TABLE IF NOT EXISTS playerstate (
state LONGTEXT NOT NULL,
userKey CHAR(16) NOT NULL
)`;//创建表
async function save(entity) {
if (!entity.player.userKey)return;
if (!(await (db.sql`SELECT * FROM playerstate WHERE userKey=${entity.player.userKey} limit 1`))[0]){
await db.sql`
INSERT INTO playerstate (state,userKey) --给表添加两个值
VALUES(${JSON.stringify(JSON.decycle(entity))},${entity.player.userKey})--将玩家实体对象转换为字符串
--ON CONFLICT(userKey) --当存在时
--DO UPDATE
--SET state=excluded.state--使用固定名称的“表”
`
}
else{
await db.sql`
UPDATE playerstate SET state=${JSON.stringify(JSON.decycle(entity))} WHERE userKey=${entity.player.userKey}
`
}
allPlayerList()
}
async function load(entity){
const info = (await (db.sql`SELECT * FROM playerstate WHERE userKey=${entity.player.userKey} limit 1`))[0]
if(!info)return null
Object.assign({}, entity, JSON.retrocycle(info))
return entity
}
async function allPlayerList(){//调用这个函数
const dict = await db.sql`SELECT * FROM playerstate`
for (const p of dict) {
console.log(JSON.stringify(p))
}
return JSON.stringify(dict)
return dict.map(d => JSON.parse(d.state))
}
world.onPlayerJoin(({entity})=>{load(entity)})
world.onPlayerLeave(({entity})=>{save(entity)})
world.onPress(({ button, entity }) => {button==Box3ButtonType.ACTION1?save(entity):0})
原本我要写一个万能sql代码a
能记录下玩家的全部状态( )
这样就再也不担心sql问题了
但失败的问题在于似乎无法直接修改entity对象( 悲
上午还白问问了一个循环调用的问题( )(恼
代码放出来了
改改 说不定还有什么妙用