-
Notifications
You must be signed in to change notification settings - Fork 46
/
state-machine.js
142 lines (122 loc) · 3.72 KB
/
state-machine.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
var moves = require('./move-types.js'),
Q = require('q');
var redis,
rtg;
/* For using RedisToGo on Heroku. If you're not using RedisToGo or Heroku,
* feel free to remove this part and just use
* redis = require("redis").createClient();
*/
if(process.env.REDISTOGO_URL) {
rtg = require("url").parse(process.env.REDISTOGO_URL);
redis = require("redis").createClient(rtg.port, rtg.hostname);
redis.auth(rtg.auth.split(":")[1]);
} else {
//then we're running locally
redis = require("redis").createClient();
}
/* Turn Redis Methods Into Promise-returning Ones */
QRedis = {};
QRedis.sadd = Q.nbind(redis.sadd, redis);
QRedis.hmset = Q.nbind(redis.hmset, redis);
QRedis.hgetall = Q.nbind(redis.hgetall, redis);
QRedis.exists = Q.nbind(redis.exists, redis);
QRedis.del = Q.nbind(redis.del, redis);
QRedis.set = Q.nbind(redis.set, redis);
QRedis.get = Q.nbind(redis.get, redis);
QRedis.decrby = Q.nbind(redis.decrby, redis);
QRedis.smembers = Q.nbind(redis.smembers, redis);
module.exports = {};
module.exports.newBattle = function(playerName, channel) {
return QRedis.exists("currentBattle")
.then(function(exists){
if(!exists) {
return QRedis.hmset("currentBattle", {
"playerName": playerName,
"channel": channel
})
} else {
throw new Error("Battle exists");
}
})
}
module.exports.getBattle = function() {
return QRedis.hgetall("currentBattle");
}
module.exports.endBattle = function() {
return QRedis.del([
"currentBattle",
"user:allowedMoves",
"npc:allowedMoves",
"npc:hp",
"user:hp",
"user:pkmnTypes",
"npc:pkmnTypes"
])
}
module.exports.addMove = function(data) {
return QRedis.sadd("user:allowedMoves", data.name.toLowerCase())
.then(function(addReturned){
return QRedis.hmset("move:"+data.name.toLowerCase(),{
"power": data.power,
"type": moves.getMoveType(data.name.toLowerCase())
})
});
}
module.exports.addMoveNPC = function(data) {
return QRedis.sadd("npc:allowedMoves", data.name.toLowerCase())
.then(function(addReturned){
return QRedis.hmset("move:"+data.name.toLowerCase(),{
"power": data.power,
"type": moves.getMoveType(data.name.toLowerCase())
})
});
}
module.exports.setUserPkmnTypes = function(typesArray) {
//TODO: use apply (or Q's version of it)
if(typesArray[1]) {
return QRedis.sadd("user:pkmnTypes", typesArray[0], typesArray[1]);
} else {
return QRedis.sadd("user:pkmnTypes", typesArray[0]);
}
}
module.exports.setNpcPkmnTypes = function(typesArray) {
//TODO: use apply (or Q's version of it)
if(typesArray[1]) {
return QRedis.sadd("npc:pkmnTypes", typesArray[0], typesArray[1]);
} else {
return QRedis.sadd("npc:pkmnTypes", typesArray[0]);
}
}
module.exports.getUserPkmnTypes = function() {
return QRedis.smembers("user:pkmnTypes");
}
module.exports.getNpcPkmnTypes = function() {
return QRedis.smembers("npc:pkmnTypes");
}
module.exports.getUserAllowedMoves = function() {
return QRedis.smembers("user:allowedMoves");
}
module.exports.getNpcAllowedMoves = function() {
return QRedis.smembers("npc:allowedMoves");
}
module.exports.getSingleMove = function(moveName) {
return QRedis.hgetall("move:"+moveName.toLowerCase());
}
module.exports.setNpcHP = function(hp) {
return QRedis.set("npc:hp", hp);
}
module.exports.getNpcHP = function() {
return QRedis.get("npc:hp");
}
module.exports.setUserHP = function(hp) {
return QRedis.set("user:hp", hp);
}
module.exports.getUserHP = function() {
return QRedis.get("user:hp");
}
module.exports.doDamageToUser = function(damage) {
return QRedis.decrby("user:hp", damage);
}
module.exports.doDamageToNpc = function(damage) {
return QRedis.decrby("npc:hp", damage);
}