Navigation Menu

Skip to content

Commit

Permalink
Move divine to this project
Browse files Browse the repository at this point in the history
  • Loading branch information
Menci committed Feb 10, 2020
1 parent 8ace4db commit 1ad83d4
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 14 deletions.
49 changes: 49 additions & 0 deletions divine.json
@@ -0,0 +1,49 @@
{
"items": [
{
"title": "刷题",
"detail": [
"一遍过样例",
null
]
},
{
"title": "装弱",
"detail": [
"我好菜啊",
"你太强了"
]
},
{
"title": {
"boy": "搞基",
"girl": "搞姬"
},
"detail": [
"爱上学习",
"会被掰弯"
]
},
{
"title": "直播写代码",
"detail": [
"月入百万",
"CE, RE and T,身败名裂"
]
},
{
"title": "学数论",
"detail": [
"思维敏捷",
"咋看都不会"
]
},
{
"title": "参加模拟赛",
"detail": [
"AK 虐场",
"爆零"
]
}
]
}
74 changes: 74 additions & 0 deletions libs/divine.js
@@ -0,0 +1,74 @@
let Random = require('random-js');

let crc32 = require('buffer-crc32');
let hash = x => crc32.unsigned(Buffer(x));

let config = require('../divine.json');

/*
name: Used for the random seed
sex: 1 for boys, -1 for girls, 0 for others
*/
module.exports = (name, sex) => {
if (sex == 1) sex = 'boy';
else if (sex == -1) sex = 'girl';

let res = {
fortune: undefined,
good: [],
bad: []
};

let date = new Date();
let h = hash(name + hash(date.getFullYear().toString() + date.getMonth().toString() + date.getDate().toString()));
let random = new Random(Random.engines.mt19937().seed(h));

let x = random.integer(1, 100);
if (x <= 25) res.fortune = '大吉';
else if (x <= 50) res.fortune = '大凶';
else if (x <= 60) res.fortune = '中平';
else if (x <= 70) res.fortune = '小吉';
else if (x <= 80) res.fortune = '小凶';
else if (x <= 90) res.fortune = '吉';
else res.fortune = '凶';

let items = Array.from(config.items);
function generate(type) {
while (1) {
let id = random.integer(0, items.length - 1);

if (!items[id]) continue;

let x = Object.assign({}, items[id]);
if (typeof x.title === 'object') {
if (x.title[sex]) x.title = x.title[sex];
else continue;
}

if (!x.detail[type]) continue;

x.detail = x.detail[type];

if (typeof x.detail === 'object') {
if (x.detail[sex]) x.detail = x.detail[sex];
else continue;
}

items[id] = null;

return x;
}
}

if (res.fortune != '大凶') {
res.good[0] = generate(0);
res.good[1] = generate(0);
}

if (res.fortune != '大吉') {
res.bad[0] = generate(1);
res.bad[1] = generate(1);
}

return res;
};
2 changes: 1 addition & 1 deletion modules/index.js
Expand Up @@ -2,7 +2,7 @@ let User = syzoj.model('user');
let Article = syzoj.model('article');
let Contest = syzoj.model('contest');
let Problem = syzoj.model('problem');
let Divine = require('syzoj-divine');
let Divine = syzoj.lib('divine');
let TimeAgo = require('javascript-time-ago');
let zh = require('../libs/timeago');
TimeAgo.locale(zh);
Expand Down
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -30,6 +30,7 @@
"async-lock": "^1.2.0",
"bluebird": "^3.5.4",
"body-parser": "^1.15.2",
"buffer-crc32": "^0.2.13",
"cheerio": "^1.0.0-rc.1",
"command-line-args": "^5.1.0",
"cookie-parser": "^1.4.4",
Expand Down Expand Up @@ -57,6 +58,7 @@
"nodemailer": "^4.7.0",
"object-assign-deep": "^0.4.0",
"object-hash": "^1.3.1",
"random-js": "^2.1.0",
"randomstring": "^1.1.5",
"redis": "^2.8.0",
"redis-lru": "^0.6.0",
Expand All @@ -68,7 +70,6 @@
"session-file-store": "^1.0.0",
"socket.io": "^2.2.0",
"stream-to-string": "^1.2.0",
"syzoj-divine": "^1.0.2",
"syzoj-renderer": "^1.0.5",
"tempfile": "^2.0.0",
"tmp-promise": "^1.0.3",
Expand Down
16 changes: 4 additions & 12 deletions yarn.lock
Expand Up @@ -2817,10 +2817,10 @@ random-bytes@~1.0.0:
resolved "https://registry.yarnpkg.com/random-bytes/-/random-bytes-1.0.0.tgz#4f68a1dc0ae58bd3fb95848c30324db75d64360b"
integrity sha1-T2ih3Arli9P7lYSMMDJNt11kNgs=

random-js@^1.0.8:
version "1.0.8"
resolved "https://registry.yarnpkg.com/random-js/-/random-js-1.0.8.tgz#968fd689a6f25d6c0aac766283de2f688c9c190a"
integrity sha1-lo/WiabyXWwKrHZig94vaIycGQo=
random-js@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/random-js/-/random-js-2.1.0.tgz#0c03238b0a4f701f7a4c7303036ade3083d8ee14"
integrity sha512-CRUyWmnzmZBA7RZSVGq0xMqmgCyPPxbiKNLFA5ud7KenojVX2s7Gv+V7eB52beKTPGxWRnVZ7D/tCIgYJJ8vNQ==

randomstring@^1.1.5:
version "1.1.5"
Expand Down Expand Up @@ -3373,14 +3373,6 @@ symbol-tree@^3.2.2:
resolved "https://registry.yarnpkg.com/symbol-tree/-/symbol-tree-3.2.2.tgz#ae27db38f660a7ae2e1c3b7d1bc290819b8519e6"
integrity sha1-rifbOPZgp64uHDt9G8KQgZuFGeY=

syzoj-divine@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/syzoj-divine/-/syzoj-divine-1.0.2.tgz#e8053e2b5b19915b55d4b9198c923bfc187778bc"
integrity sha1-6AU+K1sZkVtV1LkZjJI7/Bh3eLw=
dependencies:
buffer-crc32 "^0.2.13"
random-js "^1.0.8"

syzoj-renderer@^1.0.5:
version "1.0.5"
resolved "https://registry.yarnpkg.com/syzoj-renderer/-/syzoj-renderer-1.0.5.tgz#89a0c7a2aaaf938ae4771bfc14eb8bfbc6c11a28"
Expand Down

0 comments on commit 1ad83d4

Please sign in to comment.