Skip to content

Commit

Permalink
feat(AnysTable): get override
Browse files Browse the repository at this point in the history
  • Loading branch information
mystringEmpty committed Feb 2, 2024
1 parent 64760af commit 22773ca
Show file tree
Hide file tree
Showing 11 changed files with 218 additions and 91 deletions.
2 changes: 1 addition & 1 deletion Dice/CharacterCard.h
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ class CharaCard: public AnysTable

[[nodiscard]] string show(bool isWhole);

bool has(const string& key)const;
bool has(const string& key)const override;
//can get attr by card or temp
//bool available(const string& key) const;

Expand Down
18 changes: 14 additions & 4 deletions Dice/DiceAttrVar.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,19 @@ class AttrVar {
AttrVar(const toml::node&);
AttrVar(const YAML::Node&);
AttrVar(const AnysTable& vars) :type(Type::Table), table(vars) {}
AttrVar(const AttrObject& vars) :type(Type::Table), table(vars) {}
AttrVar(const AttrObject& obj){
if (obj) {
type = Type::Table;
new(&table) AttrObject(obj);
}
}
template<class C>
AttrVar(const ptr<C>& p) : type(Type::Table), table(std::static_pointer_cast<AnysTable>(p)) {}
AttrVar(const ptr<C>& p) {
if (p) {
type = Type::Table;
new(&table) AttrObject(std::static_pointer_cast<AnysTable>(p));
}
}
explicit AttrVar(const AttrVars& vars) :type(Type::Table), table(std::make_shared<AnysTable>(vars)) {}
void des() {
if (type == Type::Text)text.~string();
Expand Down Expand Up @@ -244,7 +254,7 @@ class AnysTable: public std::enable_shared_from_this<AnysTable> {
bool operator<(const AnysTable other)const;
//bool operator<(const AnysTable& other)const { return dict < other.dict; }
bool empty()const;
bool has(const string& key)const;
virtual bool has(const string& key)const;
void set(const string& key, const AttrVar& val);
void set(const string& key);
void set(int i, const AttrVar& val);
Expand All @@ -255,7 +265,7 @@ class AnysTable: public std::enable_shared_from_this<AnysTable> {
size_t size()const { return dict.size(); }
size_t length()const { return list ? list->size() : dict.size(); }
AttrVar index(const string& key)const;
AttrVar get(const string& key, const AttrVar& val = {})const;
virtual AttrVar get(const string& key, const AttrVar& val = {})const;
string get_str(const string& key)const;
string get_str(const string& key, const string& val)const;
string print(const string& key)const;
Expand Down
47 changes: 34 additions & 13 deletions Dice/DiceEvent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
using namespace std;
static bool is_digit(char c) { return c >= '0' && c <= '9'; }

AttrVar idx_at(const AttrObject& eve) {
static AttrVar idx_at(const AttrObject& eve) {
if (eve->has("at"))return eve->at("at");
if (!eve->has("uid"))return {};
return eve->at("at") = eve->has("gid")
? AttrVar("[CQ:at,qq=" + eve->get_str("uid") + "]")
: idx_nick(eve);
}
AttrVar idx_gAuth(const AttrObject& eve) {
static AttrVar idx_gAuth(const AttrObject& eve) {
if (!eve->has("uid")|| !eve->has("gid"))return {};
if (int auth{ DD::getGroupAuth(eve->get_ll("gid"),eve->get_ll("uid"),0) })
return eve->at("grpAuth") = auth;
Expand All @@ -37,24 +37,45 @@ AttrGetters MsgIndexs{
{"pc", idx_pc},
{"at", idx_at},
{"@", idx_at},
{"char", [](const AttrObject& obj) {
return obj->has("uid") ?
obj->at("char") = AttrVar(getPlayer(obj->get_ll("uid"))[obj->get_ll("gid")])
: AttrVar();
}},
{"gender", [](const AttrObject& vars) {
return vars->has("uid") ? vars->at("gender") = getUserItem(vars->get_ll("uid"),"gender") : AttrVar();
}},
{"grpAuth", idx_gAuth},
{"fromUser", [](const AttrObject& vars) {
return vars->has("uid") ? vars->at("fromUser") = vars->get_str("uid") : "";
}},
{"fromQQ", [](const AttrObject& vars) {
return vars->has("uid") ? vars->at("fromQQ") = vars->get_str("uid") : "";
}},
{"user", [](const AttrObject& obj) {
return obj->has("uid") ?
obj->at("user") = getUser(obj->get_ll("uid")).shared_from_this()
: AttrVar();
}},
{"char", [](const AttrObject& obj) {
return obj->has("uid") ?
obj->at("char") = getPlayer(obj->get_ll("uid"))[obj->get_ll("gid")]
: AttrVar();
}},
{"gender", [](const AttrObject& vars) {
return vars->has("uid") ? vars->at("gender") = getUserItem(vars->get_ll("uid"),"gender") : AttrVar();
}},
{"fromGroup", [](const AttrObject& vars) {
return vars->has("gid") ? vars->at("fromGroup") = vars->get_str("gid") : "";
}},
{"grp", [](const AttrObject& obj) {
return obj->has("gid") ?
obj->at("grp") = chat(obj->get_ll("gid")).shared_from_this()
: AttrVar();
}},
{"group", [](const AttrObject& obj) {
return obj->has("gid") ?
obj->at("group") = chat(obj->get_ll("gid")).shared_from_this()
: AttrVar();
}},
{"grpAuth", idx_gAuth},
{"game", [](const AttrObject& obj) {
if (AttrVar game{sessions.get_if(*obj)}) {
return obj->at("game") = game;
}
return AttrVar();
}},
};

DiceEvent::DiceEvent(const AttrVars& var, const chatInfo& ct)
Expand Down Expand Up @@ -1601,7 +1622,7 @@ int DiceEvent::BasicOrder()
replyMsg("strGameItemSet");
}
else {
set("set_val", print(thisGame->getAttr(strItem)));
set("set_val", print(thisGame->get(strItem)));
replyMsg("strGameItemShow");
}
}
Expand Down Expand Up @@ -1809,7 +1830,7 @@ int DiceEvent::InnerOrder() {
if (action == "show") {
if ((thisGame || (thisGame = sessions.get_if(fromChat)))
&& thisGame->has("rr_rc")) {
set("rule", thisGame->getAttr("rr_rc"));
set("rule", thisGame->get("rr_rc"));
}
else if (isPrivate()) {
if (User& user{ getUser(fromChat.uid) }; user.is("rc房规"))
Expand Down
15 changes: 2 additions & 13 deletions Dice/DiceJS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ JSValue js_newAttr(JSContext* ctx, const AttrVar& var) {
}
return ary;
}
else return JS_NewObject(ctx);
break;
case AttrVar::Type::ID:
return JS_NewInt64(ctx, (int64_t)var.id);
Expand Down Expand Up @@ -732,19 +733,7 @@ int js_dice_context_get_own(JSContext* ctx, JSPropertyDescriptor* desc, JSValueC
JS2OBJ(this_val);
if (desc) {
string key{ js_AtomtoGBK(ctx, prop) };
if (key == "user" && obj->has("uid")) {
desc->value = js_newDiceContext(ctx, getUser(obj->get_ll("uid")).shared_from_this());
}
else if ((key == "grp" || key == "group") && obj->has("gid")) {
desc->value = js_newDiceContext(ctx, chat(obj->get_ll("gid")).shared_from_this());
}
else if (key == "game") {
if (auto game = sessions.get_if(*obj)) {
desc->value = js_newGameTable(ctx, game);
}
else return FALSE;
}
else if (auto val{ getContextItem(obj, key) }) {
if (auto val{ getContextItem(obj, key) }) {
desc->value = js_newAttr(ctx, val);
}
else return FALSE;
Expand Down
33 changes: 3 additions & 30 deletions Dice/DiceLua.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1160,21 +1160,7 @@ int Context_index(lua_State* L) {
return 1;
}
LUA2OBJ(1);
if (key == "user" && obj->has("uid")) {
lua_push_Context(L, getUser(obj->get_ll("uid")).shared_from_this());
return 1;
}
else if((key == "grp" || key == "group") && obj->has("gid")) {
lua_push_Context(L, chat(obj->get_ll("gid")).shared_from_this());
return 1;
}
else if (key == "game") {
if (auto game = sessions.get_if(*obj)) {
lua_push_GameTable(L, game);
return 1;
}
}
else if (auto val{ getContextItem(obj, key) }) {
if (auto val{ getContextItem(obj, key) }) {
lua_push_attr(L, val);
return 1;
}
Expand Down Expand Up @@ -1258,25 +1244,12 @@ int GameTable_index(lua_State* L) {
lua_pushcfunction(L, GameTable_message);
return 1;
}
else if (key == "gms") {
lua_push_Set(L, game->get_gm());
return 1;
}
else if (key == "pls") {
lua_push_Set(L, game->get_pl());
return 1;
}
else if (key == "obs") {
lua_push_Set(L, game->get_ob());
return 1;
}
lua_push_attr(L, game->getAttr(key));
lua_push_attr(L, game->get(key));
return 1;
}
int GameTable_newindex(lua_State* L) {
LUA2GAME(1);
string key{ lua_to_gbstring(L, 2) };
if (lua_gettop(L) < 3) {
if (string key{ lua_to_gbstring(L, 2) }; lua_gettop(L) < 3) {
game->reset(key);
}
else if (AttrVar val{ lua_to_attr(L, 3) }; val.is_null()) {
Expand Down
8 changes: 3 additions & 5 deletions Dice/DiceMsgSend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,13 @@ using namespace std;

chatInfo::chatInfo(long long u, long long g, long long c) :uid(u), gid(g), chid(c) {
if (gid) {
if (chid)type = msgtype::Channel;
else type = msgtype::Group;
type = chid ? msgtype::Channel : msgtype::Group;
}
else {
if (chid)type = msgtype::ChannelPrivate;
else type = msgtype::Private;
type = chid ? msgtype::ChannelPrivate : msgtype::Private;
}
}
chatInfo::chatInfo(const AnysTable& obj):uid(obj.get_ll("uid")), chid(obj.get_ll("chid")) {
chatInfo::chatInfo(const AnysTable& obj):uid(obj.get_ll("uid")), gid(obj.get_ll("gid")), chid(obj.get_ll("chid")) {
type = (gid = obj.get_ll("gid"))
? chid ? msgtype::ChannelPrivate : msgtype::Private
: chid ? msgtype::Channel : msgtype::Group;
Expand Down
12 changes: 1 addition & 11 deletions Dice/DicePython.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ PyObject* py_build_attr(const AttrVar& var) {
}
return ary;
}
else return PyDict_New();
break;
case AttrVar::Type::ID:
return PyLong_FromLongLong(var.id);
Expand Down Expand Up @@ -466,17 +467,6 @@ PyObject* PyContext_getattro(PyObject* self, PyObject* attr) {
PY2TAB(self);
string key{ py_to_gbstring(attr) };
//console.log("PyContext_getattro:" + key, 0);
if (key == "user" && obj->has("uid")) {
return py_newContext(getUser(obj->get_ll("uid")).shared_from_this());
}
else if ((key == "grp" || key == "group") && obj->has("gid")) {
return py_newContext(chat(obj->get_ll("gid")).shared_from_this());
}
else if (key == "game") {
if (auto game = sessions.get_if(*obj)) {
return py_newGame(game);
}
}
return py_build_attr(getContextItem(obj, key));
}
int PyContext_setattro(PyObject* self, PyObject* attr, PyObject* val) {
Expand Down
37 changes: 30 additions & 7 deletions Dice/DiceSession.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* 会话管理
* 抽象于聊天窗口的单位
* Copyright (C) 2019-2023 String.Empty
* Copyright (C) 2019-2024 String.Empty
*/
#include <shared_mutex>
#include "filesystem.hpp"
Expand All @@ -22,6 +22,31 @@ unordered_set<chatInfo>LogList;

const std::filesystem::path LogInfo::dirLog{ std::filesystem::path("user") / "log" };

bool DiceSession::has(const string& key)const {
static std::unordered_set<string> items{ "name", "gms", "pls", "obs" };
return (dict.count(key) && !dict.at(key).is_null())
|| items.count(key);
}
AttrVar DiceSession::get(const string& item, const AttrVar& val)const {
if (!item.empty()) {
if (auto it{ dict.find(item) }; it != dict.end()) {
return it->second;
}
if (item == "name") {
return name;
}
else if (item == "gms") {
return get_gm();
}
else if (item == "pls") {
return get_pl();
}
else if (item == "obs") {
return get_ob();
}
}
return val;
}
size_t DiceSession::roll(size_t face) {
if (roulette.count(face)) {
auto res = roulette[face].roll();
Expand Down Expand Up @@ -65,12 +90,6 @@ string DiceSession::show()const {
}
return li.show("\n");
}
bool DiceSession::hasAttr(const string& key) {
return has(key);
}
AttrVar DiceSession::getAttr(const string& key) {
return get(key);
}

bool DiceSession::table_del(const string& tab, const string& item) {
if (!has(tab) || !get_obj(tab)->has(item))return false;
Expand Down Expand Up @@ -773,6 +792,10 @@ shared_ptr<Session> DiceSessionManager::get(chatInfo ct) {
else return SessionByChat[ct] = SessionByName[name];
}
}
shared_ptr<Session> DiceSessionManager::get_if(chatInfo ct)const {
ct = ct.locate();
return ct && SessionByChat.count(ct) ? SessionByChat.at(ct) : shared_ptr<Session>();
}
int DiceSessionManager::load() {
if (auto fileGM{ DiceDir / "user" / "GameTable.toml" };std::filesystem::exists(fileGM)) {
if (ifstream ifs{ fileGM }) {
Expand Down
34 changes: 28 additions & 6 deletions Dice/DiceSession.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,27 @@
/*
* _______ ________ ________ ________ __
* | __ \ |__ __| | _____| | _____| | |
* | | | | | | | | | |_____ | |
* | | | | | | | | | _____| |__|
* | |__| | __| |__ | |_____ | |_____ __
* |_______/ |________| |________| |________| |__|
*
* Dice! QQ Dice Robot for TRPG
* 游戏会话
* Copyright (C) 2018-2021 w4123溯洄
* Copyright (C) 2019-2024 String.Empty
*
* This program is free software: you can redistribute it and/or modify it under the terms
* of the GNU Affero General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License along with this
* program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <unordered_set>
#include "filesystem.hpp"
Expand Down Expand Up @@ -101,6 +125,7 @@ class DiceSession: public AnysTable{
dict_ci<DeckInfo> decks;
void save() const;
public:
MetaType getType()const override { return MetaType::Game; }
//native filename
const string name;
fifo_set<chatInfo> areas;
Expand Down Expand Up @@ -136,8 +161,8 @@ class DiceSession: public AnysTable{
return *this;
}
string show()const;
bool hasAttr(const string& key);
AttrVar getAttr(const string& key);
bool has(const string& key)const override;
AttrVar get(const string& key, const AttrVar& val = {})const override;
void set(const string& key, const AttrVar& val){
if (!key.empty()) {
if (val.is_null())dict.erase(key);
Expand Down Expand Up @@ -245,10 +270,7 @@ class DiceSessionManager {
void clear() { SessionByName.clear(); SessionByChat.clear(); linker = {}; }
shared_ptr<Session> get(chatInfo);
shared_ptr<Session> newGame(const string& name, const chatInfo& ct);
shared_ptr<Session> get_if(const chatInfo& ct)const {
auto chat{ ct.locate() };
return chat && SessionByChat.count(chat) ? SessionByChat.at(chat) : shared_ptr<Session>();
}
shared_ptr<Session> get_if(chatInfo ct)const;
shared_ptr<Session> getByName(const string& name)const {
return SessionByName.count(name) ? SessionByName.at(name) : ptr<Session>();
}
Expand Down
Loading

0 comments on commit 22773ca

Please sign in to comment.