-
Notifications
You must be signed in to change notification settings - Fork 378
/
Copy pathtranslate_sqlite_mission_and_quest.js
192 lines (167 loc) · 6.07 KB
/
translate_sqlite_mission_and_quest.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
const process = require('process');
const fs = require('fs');
const singleton = Symbol();
const singletonEnforcer = Symbol();
const sequelize = require('sequelize');
const baseAbsPath = __dirname + '/';
const cmdArgs = process.argv.slice(2);
if (3 != cmdArgs.length) {
console.error("Please specify exactly 3 parameters as the path of the target SQLite file, where the second param must be 'zh_cn', and the third param must be 'en_us', without quotes");
process.exit(1);
}
const fromLang = cmdArgs[1];
const toLang = cmdArgs[2];
if ("zh_cn" != fromLang || "en_us" != toLang) {
console.error(fromLang, " -> ", toLang, " is currently not supported.");
process.exit(1);
}
const sqliteFilePath = cmdArgs[0];
class SQLiteManager {
static get instance() {
if (!this[singleton]) {
this[singleton] = new SQLiteManager(singletonEnforcer);
}
return this[singleton];
}
constructor(enforcer) {
if (enforcer != singletonEnforcer) {
throw "Cannot construct singleton";
}
this.initConnection = this.initConnection.bind(this);
this.testConnectionAsync = this.testConnectionAsync.bind(this);
}
initConnection(sqlitePath) {
// Refernece https://sequelize.readthedocs.io/en/v3/docs/getting-started/.
this.dbRef = new sequelize('preconfigured', 'null', 'null', {
logging: false,
dialect: 'sqlite',
storage: sqlitePath
});
}
testConnectionAsync() {
const instance = this;
return instance.dbRef.authenticate();
}
}
SQLiteManager.instance.initConnection(sqliteFilePath);
global.window = {}; // A trick to circumvent the "ReferenceError: ... is not defined" in Nodejs for variable named "window".
const zhCnScript = require('./frontend/assets/resources/i18n/zh');
const enUsScript = require('./frontend/assets/resources/i18n/en');
const regexList = [
/累计接待([0-9]+)个客人/,
/累计上菜([0-9]+)次/,
/累计生产([0-9]+)个([\S]+)/,
/建造([0-9]+)间([\S]+)/,
/([\S]+)升级到lv([0-9]+)/,
/解锁([\S]+)/,
/累计收入([0-9]+)金币/
];
function buildableNameZhCnToEnUs(zhName) {
for (let k in window.i18n.languages['zh'].BuildingInfo.DisplayName) {
const candidateName = window.i18n.languages['zh'].BuildingInfo.DisplayName[k];
if (candidateName == zhName) {
enName = window.i18n.languages['en'].BuildingInfo.DisplayName[k];
return enName;
}
}
console.log("Can't find English version of ", zhName);
return null;
}
function ingredientNameZhCnToEnUs(zhName) {
for (let k in window.i18n.languages['zh'].Ingredient.DisplayName) {
const candidateName = window.i18n.languages['zh'].Ingredient.DisplayName[k];
if (candidateName == zhName) {
const enName = window.i18n.languages['en'].Ingredient.DisplayName[k];
return enName;
}
}
console.log("Can't find English version of ", zhName);
return null;
}
function translateTextFromZhCnToEnUs(textStr) {
let matchedGroups = null;
let enName = null;
for (let i in regexList) {
matchedGroups = (textStr).match(regexList[i]);
if (null != matchedGroups && 0 < matchedGroups.length) {
// console.log(textStr + " matches " + regexList[i] + "\nat i == " + i);
let toRet = null;
const intIndice = parseInt(i);
switch (intIndice) {
case 0:
toRet = "Serve " + matchedGroups[1] + " guests.";
return toRet;
case 1:
toRet = "Serve " + matchedGroups[1] + " dishes.";
return toRet;
case 2:
enName = ingredientNameZhCnToEnUs(matchedGroups[2]);
if (null == enName) {
return null;
} else {
toRet = "Produce " + matchedGroups[1] + " \"" + enName + "\".";
return toRet;
}
return toRet;
case 3:
enName = buildableNameZhCnToEnUs(matchedGroups[2]);
if (null == enName) {
return null;
} else {
toRet = "Build " + matchedGroups[1] + " \"" + enName + "\".";
return toRet;
}
case 4:
enName = buildableNameZhCnToEnUs(matchedGroups[1]);
if (null == enName) {
return null;
} else {
toRet = "Upgrade \"" + enName + "\" to level " + matchedGroups[2] + ".";
return toRet;
}
return toRet;
case 5:
enName = ingredientNameZhCnToEnUs(matchedGroups[1]);
if (null == enName) {
return null;
} else {
toRet = "Learn to serve \"" + enName + "\".";
return toRet;
}
case 6:
toRet = "Earn " + matchedGroups[1] + " gold.";
return toRet;
default:
return null;
}
}
}
return null;
}
SQLiteManager.instance.dbRef.query('SELECT * FROM mission', {type: sequelize.QueryTypes.SELECT}).then((missionList) => {
for (let k in missionList) {
const mission = missionList[k];
const translatedDescription = translateTextFromZhCnToEnUs(mission.description);
if (null == translatedDescription) {
continue;
}
console.log(mission.id, mission.description, " => ", translatedDescription);
SQLiteManager.instance.dbRef.query('UPDATE mission SET description=? WHERE id=?', {replacements: [translatedDescription, mission.id], type: sequelize.QueryTypes.UPDATE})
.then(([results, metadata]) => {
});
SQLiteManager.instance.dbRef.query('SELECT * FROM quest WHERE mission_id = ?', {replacements: [mission.id], type: sequelize.QueryTypes.SELECT})
.then((questListOfThisMission) => {
for (let kk in questListOfThisMission) {
const quest = questListOfThisMission[kk];
const translatedContent = translateTextFromZhCnToEnUs(quest.content);
if (null == translatedContent) {
continue;
}
console.log(quest.id, quest.content, " => ", translatedContent);
SQLiteManager.instance.dbRef.query('UPDATE quest SET content=? WHERE id=?', {replacements: [translatedContent, quest.id], type: sequelize.QueryTypes.UPDATE})
.then(([results2, metadata2]) => {
});
}
});
}
});