Skip to content
This repository has been archived by the owner on Jan 15, 2024. It is now read-only.

Commit

Permalink
Merge next
Browse files Browse the repository at this point in the history
  • Loading branch information
dynilath committed Dec 22, 2018
1 parent 1a4879c commit c13c495
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 41 deletions.
35 changes: 21 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,37 +191,44 @@ QQ:1701687847
> DiceBot:``` * 反pal魅魔 斗殴(65) 掷骰: d100b2 = [(10) + 9 + (9)] [0] = 90```
使用```p[数值]```来产生惩罚骰。
使用```p[数值]```来产生惩罚骰。
> dynilath:```.cp2闪避(50)```
> DiceBot:``` * 反pal魅魔 闪避(50) 掷骰: d100p2 = [(1) + 7 + (7)] [0] = 70```
可以自动计算奖惩相抵。
可以自动计算奖惩相抵。
> dynilath:```.cp5b5p2b3奖罚抵消```
> DiceBot:``` * 反pal魅魔 奖罚抵消 掷骰: d100p5b5p2b3 = d100b1 = [0 + (1)] [7] = 7```
## 我是一个wod玩家
骰子机器人提供了wod定制的骰子。使用指令为```.w[骰子数量]t[难度]```
骰子机器人提供了wod定制的骰子。使用指令为```.wn[骰子数量]``````.wo[骰子数量]```
分别对应nWoD和oWod。

> dynilath:```.w4t8```
nWoD规则下,默认困难度为8,出10会加骰。
> dynilath:```.wn4```
> DiceBot:``` * 反pal魅魔 掷骰: WoD 4t8 = [(2) + (6) + 8 + (5)] = 1```
> DiceBot:``` * dynilath 掷骰: nWoD 4 = [10 + (3) + (7) + (1) + (3)] = 1```
出现结果1时会扣除一个成功。
> dynilath:```.w4t6```
oWoD规则下,默认困难度为10,出1会扣除一个成功。
> dynilath:```.wo4```
> DiceBot:``` * 反pal魅魔 掷骰: WoD 4t6 = [(2) + (4) + (1) + 8] = 0```
> DiceBot:``` * dynilath 掷骰: oWoD 4 = [7 + 8 + (2) + (1)] = 1```
当然,不会扣成负数。
> dynilath:```.w4t8```
当然,不会扣成负数。
> dynilath:```.wo4```
> DiceBot:``` * 反pal魅魔 掷骰: WoD 4t8 = [(1) + (4) + (1) + 10] = 0```
> DiceBot:``` * dynilath 掷骰: oWoD 4 = [(1) + (4) + (1) + 10] = 0```
可以加上```b[数值]```以在骰出该数值以上的结果时,获得奖励骰。
> dynilath:```.w4t6b8```
无论是nWoD还是oWod,都可以加上```d[数值]```以更改困难度。
> dynilath:```.wo4d8```
> DiceBot:``` * 反pal魅魔 掷骰: WoD 4t6b8 = [(3) + (4) + 8 + 8 + 7 + (4)] = 3```
> DiceBot:``` * dynilath 掷骰: oWoD 4d8 = [(5) + (5) + (2) + 9] = 1```
无论是nWoD还是oWod,都可以加上```b[数值]```以设置奖励骰阈值。这里为了避免错误输入产生过长的结果,当阈值小于6时,取10。
> dynilath:```.wo4b6```
> DiceBot:``` * dynilath 掷骰: oWoD 4b6 = [(1) + (2) + 7 + 6 + (5) + (1)] = 0```
## 手动骰子
手动骰子会产生一些会保存在数据库的骰子数据。
Expand Down
47 changes: 36 additions & 11 deletions src/dicebot/dice_roller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,14 @@ namespace dicebot::roll{
return ost.str();
}

roll_status dice_roll::finish_wod(unsigned int const i_d) noexcept{
roll_status dice_roll::finish_wod(unsigned int const i_d, bool const failing) noexcept{
int penalty = 0;
for(uint16_t i = 0;i<this->results.size();i++){
if(this->results[i].first >= i_d){
this->results[i].second = true;
this->summary ++;
}
else if(this->results[i].first == 1){
else if(failing && this->results[i].first == 1){
penalty ++ ;
}
}
Expand Down Expand Up @@ -352,7 +352,7 @@ namespace dicebot::roll{
}
}

roll_status roll_wod(dice_roll & dice, int const i_val, int const i_d, int const i_bonus) noexcept{
roll_status roll_wod(dice_roll & dice, int const i_val, int const i_d, int const i_bonus, bool failing) noexcept{
dice.clear();

_RANDOMIZE(10,1);
Expand All @@ -366,33 +366,58 @@ namespace dicebot::roll{
if(dice.results.size() > MAX_DICE_NUM) break;
dice.add_ignored_result(single_result);
}
return dice.finish_wod(i_d);
return dice.finish_wod(i_d,failing);
}
else{
return dice.dice_exceed();
}
}

//4t5b10
roll_status roll_wod(dice_roll & dice, std::string const & str_dice_command) noexcept{
roll_status roll_nwod(dice_roll & dice, std::string const & str_dice_command) noexcept{
dice.clear();
try {
std::string source(str_dice_command);
std::regex regex_pb("^(\\d+)[tT](\\d+)(?:[bB](\\d+))?");
std::regex regex_pb("^(\\d+)(?:[dD](\\d+))?(?:[bB](\\d+))?");
std::smatch smatch_coc;
std::regex_search(source,smatch_coc,regex_pb);
if(smatch_coc.begin() == smatch_coc.end()) return dice.general_err();

uint16_t i_dice = std::stoi(smatch_coc[1].str());
uint16_t i_diff = std::stoi(smatch_coc[2].str());
uint16_t i_bonus = 11;
uint16_t i_diff = 8;
if(smatch_coc[2].matched) i_diff = std::stoi(smatch_coc[2].str());
uint16_t i_bonus = 10;
if(smatch_coc[3].matched) i_bonus = std::stoi(smatch_coc[3].str());
if(i_bonus < 6) i_bonus = 10;
return roll_wod(dice, i_dice, i_diff, i_bonus, false);
}
catch (const std::invalid_argument& ia) {
#ifdef _DEBUG
logger::log("roll_nwod", ia.what());
#endif
return dice.general_err();
}
}

return roll_wod(dice, i_dice, i_diff, i_bonus);
roll_status roll_owod(dice_roll & dice, std::string const & str_dice_command) noexcept{
dice.clear();
try {
std::string source(str_dice_command);
std::regex regex_pb("^(\\d+)(?:[dD](\\d+))?(?:[bB](\\d+))?");
std::smatch smatch_coc;
std::regex_search(source,smatch_coc,regex_pb);
if(smatch_coc.begin() == smatch_coc.end()) return dice.general_err();

uint16_t i_dice = std::stoi(smatch_coc[1].str());
uint16_t i_diff = 6;
if(smatch_coc[2].matched) i_diff = std::stoi(smatch_coc[2].str());
uint16_t i_bonus = 11;
if(smatch_coc[3].matched) i_bonus = std::stoi(smatch_coc[3].str());
if(i_bonus < 6) i_bonus = 10;
return roll_wod(dice, i_dice, i_diff, i_bonus, true);
}
catch (const std::invalid_argument& ia) {
#ifdef _DEBUG
logger::log("dice_roller", ia.what());
logger::log("roll_owod", ia.what());
#endif
return dice.general_err();
}
Expand Down
7 changes: 4 additions & 3 deletions src/dicebot/dice_roller.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace dicebot::roll{
size_t clear();
roll_status finish_roll() noexcept;
roll_status finish_coc() noexcept;
roll_status finish_wod(unsigned int const i_d) noexcept;
roll_status finish_wod(unsigned int const i_d, bool const failing) noexcept;
roll_status dice_exceed() noexcept;
roll_status general_err() noexcept;
explicit operator bool() const noexcept;
Expand All @@ -44,8 +44,9 @@ namespace dicebot::roll{
roll_status roll_coc(dice_roll & dice, int const i_bp) noexcept;
roll_status roll_coc(dice_roll & dice, std::string const & str_dice_command) noexcept;

roll_status roll_wod(dice_roll & dice, int const i_val, int const i_d, int const i_bonus) noexcept;
roll_status roll_wod(dice_roll & dice, std::string const & str_dice_command) noexcept;
roll_status roll_wod(dice_roll & dice, int const i_val, int const i_d, int const i_bonus, bool failing) noexcept;
roll_status roll_nwod(dice_roll & dice, std::string const & str_dice_command) noexcept;
roll_status roll_owod(dice_roll & dice, std::string const & str_dice_command) noexcept;


void random_initialize();
Expand Down
70 changes: 58 additions & 12 deletions src/dicebot/protocol_wod_dice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,22 @@ namespace dicebot::protocol{
protocol_wod_dice::protocol_wod_dice(){
this->identifier = new std::string("w");
this->regex_identifier = new std::string("[wW]");
this->regex_filter_full_dice = new std::regex("^ *(\\d+)[tT](\\d+)(?:[bB](\\d+))?");
this->regex_filter_full_dice = new std::regex("^(\\d+)(?:[dD](\\d+))?(?:[bB](\\d+))? *");
this->regex_detail_command = new std::regex("^([nN]|[oO]) *");

this->method_map = new std::map<std::string, WOD_DICE_CALL_TYPE_R>();
this->method_map->insert(std::pair<std::string, WOD_DICE_CALL_TYPE_R>("n", &protocol_wod_dice::nwod));
this->method_map->insert(std::pair<std::string, WOD_DICE_CALL_TYPE_R>("o", &protocol_wod_dice::owod));
}


protocol_wod_dice::~protocol_wod_dice(){
delete this->identifier;
delete this->regex_identifier;
delete this->regex_filter_full_dice;
delete this->regex_detail_command;

delete this->method_map;
}

std::string protocol_wod_dice::resolve_request(
Expand All @@ -23,31 +31,69 @@ namespace dicebot::protocol{
const int64_t user_qq_id,
bool isfromGroup){

std::string str_nickname;
(nickname_manager::instance)->get_nickname(group_id, user_qq_id, str_nickname, isfromGroup);

std::smatch match_list_command_identifier_match;
std::regex_search(message, match_list_command_identifier_match, *this->regex_detail_command);
if (match_list_command_identifier_match.begin() == match_list_command_identifier_match.end())
return std::string();

std::string str_match = match_list_command_identifier_match[1];
std::transform(str_match.begin(),str_match.end(),str_match.begin(),tolower);

std::map<std::string, WOD_DICE_CALL_TYPE_R>::iterator iter = this->method_map->find(str_match);
if (iter != method_map->end()) {
WOD_DICE_CALL_TYPE(dice_call) = (*iter).second;
return (this->*dice_call)(match_list_command_identifier_match.suffix().str(), str_nickname);
}

return std::string();
}

std::string protocol_wod_dice::nwod(std::string message, std::string nick_name){
std::smatch command_match;
std::ostringstream ostr(std::ostringstream::ate);
std::regex_search(message, command_match, *this->regex_filter_full_dice);
if (command_match.begin() != command_match.end()) {
std::string str_roll_msg = command_match.suffix().str();
std::string str_roll_source = command_match.str();
dicebot::remove_space_and_tab(str_roll_source);

//roll::dice_roller diceRoll(str_roll_source, roll::roll_mode::COC_PB);
roll::dice_roll dr;
roll::roll_wod(dr,str_roll_source);
roll::dice_roll dr;
roll::roll_nwod(dr,str_roll_source);
if (dr) {
std::ostringstream ostr(std::ostringstream::ate);
ostr << u8" * " << nick_name ;
if(str_roll_msg.size() > 0) ostr << u8" " << str_roll_msg ;
ostr << u8" 掷骰: nWoD " << str_roll_source ;
std::string detail = dr.detail();
if(detail.size()>0) ostr << u8" = " << detail;
ostr << u8" = " << dr.summary;
}
}
return ostr.str();
}

std::string str_nickname;
(nickname_manager::instance)->get_nickname(group_id, user_qq_id, str_nickname, isfromGroup);
std::string protocol_wod_dice::owod(std::string message, std::string nick_name){
std::smatch command_match;
std::ostringstream ostr(std::ostringstream::ate);
std::regex_search(message, command_match, *this->regex_filter_full_dice);
if (command_match.begin() != command_match.end()) {
std::string str_roll_msg = command_match.suffix().str();
std::string str_roll_source = command_match.str();
dicebot::remove_space_and_tab(str_roll_source);

ostr << u8" * " << str_nickname ;
roll::dice_roll dr;
roll::roll_owod(dr,str_roll_source);
if (dr) {
ostr << u8" * " << nick_name ;
if(str_roll_msg.size() > 0) ostr << u8" " << str_roll_msg ;
ostr << u8" 掷骰: WoD " << str_roll_source ;
ostr << u8" 掷骰: oWoD " << str_roll_source ;
std::string detail = dr.detail();
if(detail.size()>0) ostr << u8" = " << detail;
ostr << u8" = " << dr.summary;
return ostr.str();
}
}
return std::string();
}
return ostr.str();
}
}
8 changes: 8 additions & 0 deletions src/dicebot/protocol_wod_dice.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@
#include "./protocol_base.h"

namespace dicebot::protocol{

#define WOD_DICE_CALL_TYPE_R std::string(protocol_wod_dice::* )(std::string, std::string)
#define WOD_DICE_CALL_TYPE(_name) std::string(protocol_wod_dice::* _name )(std::string, std::string)

class protocol_wod_dice : public protocol_base
{
private:
std::regex * regex_detail_command;
std::map<std::string, WOD_DICE_CALL_TYPE_R> * method_map;
public:
protocol_wod_dice();
~protocol_wod_dice();
Expand All @@ -16,5 +21,8 @@ namespace dicebot::protocol{
const int64_t group_id,
const int64_t user_qq_id,
bool isfromGroup);

std::string nwod(std::string message, std::string nick_name);
std::string owod(std::string message, std::string nick_name);
};
}
2 changes: 1 addition & 1 deletion src/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ ASSERT_LE(dr.summary,_max);}}
sample_sum,
max_val,
min_val,
dicebot::roll::roll_wod(dr,"5T8"));
dicebot::roll::roll_owod(dr,"5T8"));

std::vector<int> compare;
compare.assign(max_val - min_val+1,0);
Expand Down

0 comments on commit c13c495

Please sign in to comment.