Skip to content

Commit

Permalink
Merge pull request #11 from 54145a/main
Browse files Browse the repository at this point in the history
  • Loading branch information
helloyork authored Jan 10, 2024
2 parents 8945001 + f2cf2d4 commit cbb5743
Show file tree
Hide file tree
Showing 7 changed files with 383 additions and 1,734 deletions.
14 changes: 14 additions & 0 deletions src/数据处理工具/145powerTag/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* !Info {Project} -来自145a
* 145powerTag 1.1
*/
world.querySelectorAll(".powerTag").forEach(entity=>{
entity.tags().forEach(tag=>{
if(tag[0]="?"){
const entry = tag.slice(1).split("=");
entity[entry[0]] = eval(entry[1]);
entity.removeTag(tag);
}
});
});
//请勿删除最后一行
70 changes: 70 additions & 0 deletions src/数据处理工具/145terrain/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/**
* !Info {Module} -来自145a
* 145terrain 1.0
*/
class Terrain {
constructor() {
this.data = [];
}
/**
* @param {GameVector3} spot
*/
at(spot){
if(!this.data[spot.x]||!this.data[spot.x][spot.y])return 0;
return this.data[spot.x][spot.y][spot.z];
}
/**
* @param {GameBounds3} bounds
*/
read(bounds) {
for (let x = 0; x < bounds.hi.x - bounds.lo.x; x++) {
this.data[x] = [];
for (let y = 0; y < bounds.hi.y - bounds.lo.y; y++) {
this.data[x][y] = [];
for (let z = 0; z < bounds.hi.z - bounds.lo.z; z++) {
this.data[x][y][z] = voxels.getVoxelId(x + bounds.lo.x, y + bounds.lo.y, z + bounds.lo.z);
}
}
}
}
/**
* @param {GameBounds3} bounds
*/
write(bounds) {
for (let x = 0; x < bounds.hi.x - bounds.lo.x; x++) {
for (let y = 0; y < bounds.hi.y - bounds.lo.y; y++) {
for (let z = 0; z < bounds.hi.z - bounds.lo.z; z++) {
voxels.setVoxelId(x + bounds.lo.x, y + bounds.lo.y, z + bounds.lo.z, this.data[x][y][z]);
}
}
}
}
}
/**
* @param {GameBounds3} bounds
*/
voxels.clear = function (bounds) {
for (let x = 0; x < bounds.hi.x - bounds.lo.x; x++) {
for (let y = 0; y < bounds.hi.y - bounds.lo.y; y++) {
for (let z = 0; z < bounds.hi.z - bounds.lo.z; z++) {
voxels.setVoxel(x + bounds.lo.x, y + bounds.lo.y, z + bounds.lo.z, 0);
}
}
}
}
/**
* @param {GameBounds3} bounds
*/
voxels.read = function (bounds) {
let terrain = new Terrain();
terrain.read(bounds);
return terrain;
}
/**
* @param {GameBounds3} bounds
* @param {Terrain} terrain
*/
voxels.write = function (bounds, terrain) {
terrain.write(bounds);
}
//请勿删除最后一行
125 changes: 125 additions & 0 deletions src/数据库工具/145storage/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/**
* !Info {Module} -来自145a
* 145Storage 3.2
*/
/**
* 缓冲时间,防止http报错429
* @type {number}
*/
storage.REQUEST_MIN_INTERVAL = 64;
/**
* 初始化云属性
* @param {object} host
* @param {string} key
* @param {any} defaultValue
* @param {function(JSONValue)} updater
*/
storage.setupCloudVariable = async function (host, key, defaultValue, updater) {
const cacheKey = `_${key}_cache`;
const isWaitingKey = `_${key}_isWaiting`;
const scheduleUpdateKey = `_${key}_scheduleUpdate`;
host[cacheKey] = defaultValue;
host[isWaitingKey] = false;
host[scheduleUpdateKey] = async function () {
if (host[isWaitingKey]) return;
host[isWaitingKey] = true;
await sleep(storage.REQUEST_MIN_INTERVAL);
host[isWaitingKey] = false;
updater(host[cacheKey]);
}
if (typeof host[cacheKey] === "object") {
host[key] = new Proxy(host[cacheKey], {
set(target, property, value, receiver) {
Reflect.set(target, property, value, receiver);
host[scheduleUpdateKey]();
return true;
}
});
} else {
Object.defineProperty(host, key, {
get() {
return host[cacheKey];
},
set(value) {
host[cacheKey] = value;
host[scheduleUpdateKey]();
return true;
}
});
}
}
/**
* @typedef {function(a,b):number} Sorter
* @type {Object<Sorter>}
*/
Array.SorterType = {
DESC: (a, b) => b - a,
ASC: (a, b) => a - b
}
/**
* 初始化一个145DataStorage
* @param {String} name
* @param {JSONValue} defaultUserData 用户默认属性和数据类型
* @returns {GameDataStorage}
*/
storage.get145DataStorage = function (name, defaultUserData) {
let host = storage.getDataStorage(name);
Object.assign(defaultUserData, {
name: "游客",//此项不可删除
joinTimes: 0,//此项不可删除
});
/**
* 为用户初始化
* @param {GamePlayerEntity} user
*/
host.setupUser = async function (user) {
if (!user || !user.isPlayer) throw `setupUser的参数不太对劲`;
if (!user.player.userId) return;
let dataStorage = this;
const data = await dataStorage.get(user.player.userId);
if (!data) {
await dataStorage.set(user.player.userId, defaultUserData);
return await dataStorage.setupUser(user);
}
user._isPreparingUpdate_ = false;
user._storageUpdateCache_ = {};
Object.keys(defaultUserData).forEach(key => {
if(typeof data.value[key] !== typeof defaultUserData[key]){
data.value[key] = defaultUserData[key];
}
user._storageUpdateCache_[key] = data.value[key];
storage.setupCloudVariable(user, key, data.value[key], async (value) => {
user._storageUpdateCache_[key] = value;
if (user._isPreparingUpdate_) return;
user._isPreparingUpdate_ = true;
await sleep(storage.REQUEST_MIN_INTERVAL);
dataStorage.set(user.player.userId, user._storageUpdateCache_);
user._isPreparingUpdate_ = false;
});
});
user.name = user.player.name;
user.joinTime += 1;
}
/**
* 获取排行榜
* {@link Array.SorterType}
* @param {string} type
* @param {string} displayType
* @param {number} num
* @param {Sorter} sorter
*/
host.getRank = async function (type, displayType, num = 10, sorter) {
let data = (await this.list({
cursor: 0,
pageSize: Number.MAX_SAFE_INTEGER - 1
})).getCurrentPage().map(v => ({
key: v.key,
value: v.value
})).filter(v=>!["null","undefined"].includes(typeof v.value[type]));
data.sort((a, b) => sorter(a.value[type], b.value[type]));
let result = `用户名 ${displayType}\n` + data.slice(0, num).map((v) => `${v.value.name} ${v.value[type]}`).join("\n");
return result;
}
return host;
}
//请勿删除最后一行
119 changes: 0 additions & 119 deletions src/模板代码/145Storage/index.js

This file was deleted.

Loading

0 comments on commit cbb5743

Please sign in to comment.