Skip to content

Commit 46bb358

Browse files
author
ray.ni
committed
add all files
1 parent d5c00ea commit 46bb358

File tree

4 files changed

+473
-0
lines changed

4 files changed

+473
-0
lines changed

getendgame.js

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
/*
2+
Get the end game database from the forthfreak
3+
It will be used to build a end game table.
4+
*/
5+
var request = require('request'),
6+
cheerio = require('cheerio'),
7+
mongodb = require('mongodb'),
8+
jsdom = require('jsdom'),
9+
fs = require('fs'),
10+
jquerystring = fs.readFileSync("./jquery-1.6.min.js").toString();
11+
12+
13+
var proxy = 'http://3.87.248.6:88';
14+
var site_url = 'http://lpforth.forthfreak.net/';
15+
16+
var getEndGame = function getEndGame(url, callback) {
17+
var reqOptions = {uri: url};
18+
if (proxy !== '') reqOptions['proxy'] = proxy;
19+
20+
var self = this;
21+
request(reqOptions, function(err, response, body){
22+
//Just a basic error check
23+
if(err && response.statusCode !== 200){
24+
console.log('Request error.');
25+
return;
26+
}
27+
28+
var $ = cheerio.load(body),
29+
$body = $('body'),
30+
$items = $body.find('tr');
31+
$items.each(function(i, item) {
32+
var eg = {},
33+
tds = $(item).find('td');
34+
if (i === 0) {
35+
// skip the header
36+
return;
37+
}
38+
eg.originname = $(tds[0]).find('a').text();
39+
eg.name = convertName(eg.originname);
40+
eg.order = $(tds[2]).text();
41+
eg.redwin = $(tds[3]).text();
42+
eg.redlongestwin = $(tds[4]).text();
43+
eg.blackdraw = $(tds[5]).text();
44+
eg.blacklongestwin = $(tds[6]).text();
45+
eg.date = $(tds[8]).text();
46+
47+
if (callback) {
48+
callback(eg);
49+
}
50+
});
51+
});
52+
};
53+
54+
/*
55+
notation used in naming the end game of the web site
56+
k > r > c > n > p > b > e
57+
K, k -- (k)ing
58+
B, b -- (b)ishop, assistant, guard
59+
E, e -- (e)lephant, minister
60+
R, r -- (r)ook, chariot, car
61+
N, n -- k(n)ight, horse
62+
C, c -- (c)annon, gun
63+
P, p -- (p)awn
64+
65+
we use following notation:
66+
Red Black
67+
K, k -- (k)ing
68+
A, a -- (a)ssistant, guard
69+
B, b -- elephant, minister
70+
R, r -- (r)ook, chariot, car
71+
N, n -- k(n)ight, horse
72+
C, c -- (c)annon, gun
73+
P, p -- (p)awn
74+
75+
we use uppercase for red pieces
76+
name 'kreekpb' will transfer to 'KRBBkpa', we always have K/k, so we will drop them in the name
77+
so the name become 'RBBpa'
78+
*/
79+
function convertName(name) {
80+
var i, newname = [], red = true;
81+
if (name.length <= 1) return;
82+
for (i = 1; i < name.length; i = i + 1) {
83+
if (red) {
84+
if (name[i] === 'b') {
85+
newname.push('A');
86+
} else if (name[i] === 'e') {
87+
newname.push('B');
88+
} else if (name[i] !== 'k') {
89+
newname.push(name[i].toUpperCase());
90+
} else {
91+
red = false;
92+
}
93+
} else {
94+
if (name[i] === 'b') {
95+
newname.push('a');
96+
} else if (name[i] === 'e') {
97+
newname.push('b');
98+
} else if (name[i] !== 'k') {
99+
newname.push(name[i]);
100+
} else {
101+
}
102+
}
103+
}
104+
return newname.join('');
105+
}
106+
107+
function EndGamePersister(options) {
108+
this.host = options == null || options.host == null? "127.0.0.1": options.host;
109+
this.port = options == null || options.port == null? 27017: options.port;
110+
this.database = options == null || options.database == null? 'test': options.database;
111+
this.server = new mongodb.Server(this.host, this.port, {auto_reconnect: true, pool_size: 4,
112+
socketOptions: {keepAlive: 1}});
113+
this.connector = new mongodb.Db(this.database, this.server, {});
114+
115+
var self = this;
116+
117+
this.saveEndGame = function(eg) {
118+
if (self.db) {
119+
return insertEndGame(self.db, eg);
120+
}
121+
self.connector.open(function (error, db) {
122+
if (error) throw error;
123+
console.log('connected to database:' + self.database + '@' + self.host + ':' + self.port);
124+
self.db = db;
125+
insertEndGame(db, eg);
126+
});
127+
};
128+
129+
var insertEndGame = function(db, eg) {
130+
var collGame = new mongodb.Collection(db, 'endgame');
131+
collGame.insert({'name': eg.name, 'originname': eg.originname, 'order': eg.order, 'redwin': eg.redwin,
132+
'redlongestwin': eg.redlongestwin, 'blackdraw': eg.blackdraw, 'blacklongestwin': eg.blacklongestwin, 'date': eg.date});
133+
console.log(eg.name + ' saved!');
134+
};
135+
136+
}
137+
138+
var scrapeEndGames = function (callback) {
139+
getEndGame(site_url + 'available-rook-js.html', callback);
140+
getEndGame(site_url + 'available-cannon-js.html', callback);
141+
getEndGame(site_url + 'available-knight-js.html', callback);
142+
getEndGame(site_url + 'available-pawn-js.html', callback);
143+
getEndGame(site_url + 'available-misc-js.html', callback);
144+
145+
};
146+
147+
scrapeEndGames(new EndGamePersister().saveEndGame);
148+
149+
150+
151+

0 commit comments

Comments
 (0)