Browse Source

Fixed game engine conditionals, and made it possible to restart sessions and delete them

iam 1 year ago
parent
commit
773ca1e0ba

+ 0 - 75
games/game.txt

@@ -1,75 +0,0 @@
1
-;setup;
2
-    info: fork in the road
3
-    var: moo = muu
4
-    info: the current storyline is $storyline and your health is $health
5
-    info: moo is $int($calc(1+1+1+0.5+int(10.7)))
6
-    
7
-    option: aaaaaaaaa
8
-        info: moo
9
-    endoption
10
-    
11
-    if: (item(moo >= 10)&item(moo >= 1000))|(life(> 1000)|gameclock(> 10))
12
-        option: item
13
-            var: moo = true
14
-            info: You gain 500 health!
15
-            health: +500|You died
16
-            info: you find 500 moos
17
-            item: moo+500
18
-            goto: ;setup;
19
-        option: hasMOOs
20
-            info: this was just a test
21
-            goto: ;setup;
22
-        endoption
23
-    endif
24
-    
25
-    if: var(moo=true)
26
-        option: Moo is true!
27
-            goto: ;setup;
28
-        endoption
29
-    endif
30
-    
31
-    if: life(> 1000)
32
-        option: highHealth!
33
-            info: wow high health
34
-            goto: ;setup;
35
-        endoption
36
-    endif
37
-    
38
-    if: gameclock(> 10)
39
-        option: you have lived over 10 moves
40
-            info: wow high gameclock
41
-            goto: ;setup;
42
-        endoption
43
-    endif
44
-    
45
-    option: get free moos
46
-        goto: ;moos;
47
-    option: lose health
48
-        info: you lose 10 health
49
-        health: -10|You died
50
-        goto: ;setup;
51
-    option: end game
52
-        endgame: moo
53
-    endoption
54
-end
55
-
56
-;setup.1;
57
-    option: back
58
-        goto: ;setup;
59
-    endoption
60
-end
61
-
62
-;moos;
63
-    info: You found a moo farm and were given 500 moos
64
-    
65
-    item: moo+500
66
-    info: moo + 500!
67
-    
68
-    option: give back moos
69
-        info: Why... idk but you give them all back
70
-        item: moo-500
71
-        goto: ;setup;
72
-    option: back
73
-        goto: ;setup;
74
-    endoption
75
-end

+ 0 - 56
games/student.txt

@@ -1,56 +0,0 @@
1
-;1;
2
-    info: It's 8am. Your alarm starts going off.
3
-    option: Get up
4
-        var: early = true
5
-        info: You get out of bed
6
-    endoption
7
-    option: Sleep through it
8
-    endoption
9
-end
10
-
11
-;checkSchedule;
12
-    info: <schedule>
13
-    option: back
14
-        goto: $var(back)
15
-    endoption
16
-end
17
-
18
-;1.1;
19
-    info: .
20
-    option: Look around
21
-        info: You're in your dorm
22
-        goto: ;1.1;
23
-    endoption
24
-    
25
-    option: Check schedule
26
-        var: back = ;1.1;
27
-        goto: ;checkSchedule;
28
-    endoption
29
-    
30
-    if: var(early = true)
31
-        option: Go to gym
32
-            goto: ;gym.early;
33
-        endoption
34
-    endif
35
-
36
-    if: var(early = false)
37
-        option: Go to gym
38
-            goto: ;gym.late;
39
-        endoption
40
-    endif
41
-end
42
-
43
-;gym.early;
44
-    info: You get to the gym. It's pretty early, and not many people are here yet
45
-    option: Go back to dorm
46
-        var: early = false
47
-        goto: ;1.1;
48
-    endoption
49
-end
50
-
51
-;gym.late;
52
-    info: You get to the gym. It's later in the day, and the gym is completely full
53
-    option: Go to free weights
54
-    option: Wait for a machine
55
-    option: 
56
-end

+ 33 - 28
src/gameController.cpp

@@ -105,7 +105,14 @@ bool GameController::getOptions(std::vector<std::string> &toReturn)
105 105
 std::string GameController::doCommandBlock(std::vector<Command*> &commands)
106 106
 {
107 107
     std::string returnString = "";
108
-    for(auto place : commands)
108
+    
109
+    std::vector<Command*> checkedCommands;
110
+    
111
+    for(auto command : commands)
112
+        if(checkCondition(command->condition))
113
+            checkedCommands.push_back(command);
114
+        
115
+    for(auto place : checkedCommands)
109 116
     {
110 117
         returnString = doCommand(place);
111 118
         if(returnString != "")
@@ -194,19 +201,19 @@ std::string GameController::doHealth(std::string command)
194 201
     {
195 202
         health += stoi(amount);
196 203
         std::cout << "+" << stoi(amount) << " health. (" << health << " now) " << sessionKey << std::endl;
197
-        std::cout << " " << sessionKey << std::endl;
204
+        std::cout << "  " << sessionKey << std::endl;
198 205
     }
199 206
     else if(type == '-')
200 207
     {
201 208
         health -= stoi(amount);
202 209
         std::cout << "-" << stoi(amount) << " health. (" << health << " now) " << sessionKey << std::endl;
203
-        std::cout << " " << sessionKey << std::endl;
210
+        std::cout << "  " << sessionKey << std::endl;
204 211
     }
205 212
     else if(type == '=')
206 213
     {
207 214
         health = stoi(amount);
208 215
         std::cout << "Health set to " << health << " " << sessionKey << std::endl;
209
-        std::cout << " " << sessionKey << std::endl;
216
+        std::cout << "  " << sessionKey << std::endl;
210 217
     }
211 218
     
212 219
     if(health <= 0)
@@ -326,30 +333,28 @@ std::string GameController::doMacros(std::string macroString)
326 333
 
327 334
 std::string GameController::doCommand(Command* command)
328 335
 {
336
+    
329 337
     std::string macroCommand;
330 338
     
331
-    if(checkCondition(command->condition))
332
-    {
333
-        macroCommand = doMacros(command->command);
339
+    macroCommand = doMacros(command->command);
334 340
         
335
-        if(command->commandType == "item")
336
-            addItem(macroCommand);
337
-        else if(command->commandType == "endgame")
338
-            return macroCommand;
339
-        else if(command->commandType == "goto")
340
-            return macroCommand;
341
-        else if(command->commandType == "rand")
342
-            return doRand(macroCommand);
343
-        else if(command->commandType == "health")
344
-            return doHealth(macroCommand);
345
-        else if(command->commandType == "include")
346
-            parseFile(macroCommand);
347
-        else if(command->commandType == "info")
348
-            std::cout << macroCommand << " " << sessionKey << std::endl;
349
-        else if(command->commandType == "var")
350
-            addVariable(macroCommand);
351
-    }
352
-    
341
+    if(command->commandType == "item")
342
+        addItem(macroCommand);
343
+    else if(command->commandType == "endgame")
344
+        return macroCommand;
345
+    else if(command->commandType == "goto")
346
+        return macroCommand;
347
+    else if(command->commandType == "rand")
348
+        return doRand(macroCommand);
349
+    else if(command->commandType == "health")
350
+        return doHealth(macroCommand);
351
+    else if(command->commandType == "include")
352
+        parseFile(macroCommand);
353
+    else if(command->commandType == "info")
354
+        std::cout << macroCommand << " " << sessionKey << std::endl;
355
+    else if(command->commandType == "var")
356
+        addVariable(macroCommand);
357
+
353 358
     return "";
354 359
 }
355 360
 
@@ -392,14 +397,14 @@ bool GameController::addItem(std::string itemString)
392 397
                 {
393 398
                     itemsAmount.at(place) += intAmount;
394 399
                     std::cout << itemName << " + " << amount << " (" << itemsAmount.at(place) << " now) " << sessionKey << std::endl;
395
-                    std::cout << " " << sessionKey << std::endl;
400
+                    std::cout << "  " << sessionKey << std::endl;
396 401
                 }
397 402
                 break;
398 403
                 case '-':
399 404
                 {
400 405
                     itemsAmount.at(place) -= intAmount;
401 406
                     std::cout << itemName << " - " << amount << " (" << itemsAmount.at(place) << " now) " << sessionKey << std::endl;
402
-                    std::cout << " " << sessionKey << std::endl;
407
+                    std::cout << "  " << sessionKey << std::endl;
403 408
                 }
404 409
                 break;
405 410
                 case '/':
@@ -421,7 +426,7 @@ bool GameController::addItem(std::string itemString)
421 426
     if(not foundPlace)
422 427
     {
423 428
         std::cout << itemName << " + " << amount << " (" << amount << " now) " << sessionKey << std::endl;
424
-        std::cout << " " << sessionKey << std::endl;
429
+        std::cout << "  " << sessionKey << std::endl;
425 430
         
426 431
         items.push_back(itemName);
427 432
         itemsAmount.push_back(intAmount);

+ 2 - 9
webserver/html/playGame.html

@@ -14,9 +14,8 @@
14 14
         <div class="container gameText">
15 15
             <div class="pre-scrollable mainGame">
16 16
                 <div id="log" class=""></div>
17
-                <div id="restart"></div>
18 17
             </div>
19
-            <div class="textarea"><span id="lts">&lt;&lt;</span><input id="textbox" type="text" autofocus></div>
18
+            <div class="textarea"><div class="input-group-btn"><span id="lts">&lt;&lt;</span><input id="textbox" type="text" autofocus/><button onclick="restartGame()" class="btn btn-warning reload">Restart</button></div></div>
20 19
         </div>
21 20
     </body>
22 21
     
@@ -28,8 +27,7 @@
28 27
         function restartGame()
29 28
         {
30 29
             $('#log').html('');
31
-            $("#restart").html('');
32
-            socket.emit('restarto', document.cookie);
30
+            socket.emit('restart', document.cookie);
33 31
             $('#textbox').focus();
34 32
         }
35 33
         
@@ -46,16 +44,11 @@
46 44
             }
47 45
         });
48 46
         
49
-        socket.on('restarti', function(data) {
50
-            $("#restart").html('<br><br><button type="button" class="btn btn-default" onclick="restartGame()">Restart</button><br><br>');
51
-        });
52
-        
53 47
         socket.on('newID', function(data) {
54 48
             document.cookie = "currentGame=" + data + "; expires=Thu, 18 Dec 2038 12:00:00 UTC; path=/";
55 49
         });
56 50
         
57 51
         socket.on('in', function(data) {
58
-            console.log(data);
59 52
             $("#log").append(data);
60 53
             $(".mainGame").stop();
61 54
             $(".mainGame").animate({ scrollTop: $('.mainGame').prop("scrollHeight")}, 1000);

+ 9 - 2
webserver/html/resumeGame.html

@@ -15,9 +15,15 @@
15 15
                 {
16 16
                     document.cookie = "currentGame=" + key + "; expires=Thu, 18 Dec 2038 12:00:00 UTC; path=/";
17 17
                     document.cookie = "currentGamePath=" + path + "; expires=Thu, 18 Dec 2038 12:00:00 UTC; path=/";
18
-                    console.log('/playGame' + path);
19 18
                     window.location = '/playGame' + path;
20 19
                 }
20
+                function deleteGame(gameID, card)
21
+                {
22
+                    $(card).parent().parent().parent().css('visibility','hidden');
23
+                    $.post("/deleteGame", {gameID: gameID});
24
+                    document.cookie = "currentGame=; expires=Thu, 18 Dec 2038 12:00:00 UTC; path=/";
25
+                    document.cookie = "currentGamePath=; expires=Thu, 18 Dec 2038 12:00:00 UTC; path=/";
26
+                }
21 27
             </script>
22 28
             
23 29
             <% for (x in sessions) { %>
@@ -31,7 +37,8 @@
31 37
                                 <div class="card-block">
32 38
                                     <h4 class="card-title"><%= foundGame[0]['Name'] %></h4>
33 39
                                     <p class="card-text"><%= foundGame[0]['Description'] %></p>
34
-                                    <a onclick="getGame('<%= sessions[x]['path'] %>' , '<%= sessions[x]['key'] %> ');" class="btn btn-primary">Resume</a>
40
+                                    <a onclick="getGame('<%= sessions[x]['path'] %>' , '<%= sessions[x]['key'] %>');" class="btn btn-primary">Resume</a>
41
+                                    <a onclick="deleteGame('<%= sessions[x]['key'] %>', this);" class="btn btn-danger">Delete</a>
35 42
                                 </div>
36 43
                             </div>
37 44
                         </div>

+ 6 - 0
webserver/public/stylesheets/game.css

@@ -10,6 +10,12 @@
10 10
     word-wrap: break-word;       /* Internet Explorer 5.5+ */
11 11
 }
12 12
 
13
+.reload {
14
+    padding-bottom: 4px !important;
15
+    padding-top: 3px !important;
16
+    margin-bottom: 11px !important;
17
+}
18
+
13 19
 #game {
14 20
     font-family: "Courier New", Courier, monospace;
15 21
 }

+ 5 - 14
webserver/routes/findGames.js

@@ -14,6 +14,10 @@ function findGames(callback_)
14 14
     });
15 15
 }
16 16
 
17
+router.post('/deleteGame', function(req, res) {
18
+    getGames.dropGame(req.cookies['sessionID'], req.body.gameID);
19
+});
20
+
17 21
 router.get('/findgame', function(req, res) {
18 22
     if("sessionID" in req.cookies) {
19 23
         findGames(function(games) {
@@ -28,20 +32,7 @@ router.get('/findgame', function(req, res) {
28 32
         });
29 33
     } else {
30 34
         getGames.createUser(res, function() {
31
-            
32
-            //same as above
33
-            
34
-            findGames(function(games) {
35
-                ejs.renderFile(__dirname + baseFilePath + 'findGame.html', {active:"Find", games:games}, function(err, result) {
36
-                    if (!err) {
37
-                        res.end(result);
38
-                    } else {
39
-                        res.end(err.toString());
40
-                        console.log(err);
41
-                    }
42
-                });
43
-            });
44
-            
35
+            res.redirect(req.originalUrl);
45 36
         });
46 37
     }
47 38
 });

+ 22 - 17
webserver/routes/getGames.js

@@ -44,22 +44,6 @@ function createUser(res, callback_)
44 44
     });
45 45
 }
46 46
 
47
-function dropGame(userID, gameID)
48
-{
49
-    MongoClient.connect(url, function (err, db) {
50
-        if (err) {
51
-            console.log('Unable to connect to the mongoDB server. Error:', err);
52
-            callback_('error');
53
-        } else {
54
-            var collection = db.collection("Users");
55
-            collection.update({'UserID': userID},{$pull: { 'GameKeys': {key:gameID} }}, function(err) {
56
-                //console.log("NEW " + gameKey + " " + gameInfo['File'] + " " + gameInfo['Start'] +'\n');
57
-                db.close();
58
-            });
59
-        }
60
-    });
61
-}
62
-
63 47
 function getUser(key, callback_)
64 48
 {
65 49
     MongoClient.connect(url, function (err, db) {
@@ -110,10 +94,31 @@ module.exports = function(mainGameServer)
110 94
             }
111 95
         });
112 96
     }
97
+    
98
+    function dropGame(userID, gameID)
99
+    {
100
+        //Does not delete scrollback for game and socket**
101
+        //Need to do that
102
+        //(Later)
103
+        
104
+        MongoClient.connect(url, function (err, db) {
105
+            if (err) {
106
+                console.log('Unable to connect to the mongoDB server. Error:', err);
107
+                callback_('error');
108
+            } else {
109
+                var collection = db.collection("Users");
110
+                collection.update({'UserID': userID},{$pull: { 'GameKeys': {key:gameID} }}, function(err) {
111
+                    mainGameServer.stdin.write("DELETE " + gameID + '\n');
112
+                    db.close();
113
+                });
114
+            }
115
+        });
116
+    }
117
+    
113 118
     module.exports.createGame = createGame;
119
+    module.exports.dropGame = dropGame;
114 120
 }
115 121
 
116
-module.exports.dropGame = dropGame;
117 122
 module.exports.dropDatabase = dropDatabase;
118 123
 module.exports.createUser = createUser;
119 124
 module.exports.getUser = getUser;

+ 20 - 5
webserver/routes/manageSocket.js

@@ -40,7 +40,7 @@ module.exports = function(io, mainGameServer)
40 40
                 {
41 41
                     if(sendBuffer[x][sendBuffer[x].length-1] == "DEATH") {
42 42
                         sockets[x]['Socket'].emit('in', sendBuffer[x].slice(0,sendBuffer[x].length-1).join("<br>"));
43
-                        delete scrollback[sockets[x]['Socket']['currentGame']]
43
+                        delete scrollback[x]
44 44
                         delete sockets[x]
45 45
                     } else {
46 46
                         sockets[x]['Socket'].emit('in', sendBuffer[x].join("<br>"));
@@ -59,6 +59,8 @@ module.exports = function(io, mainGameServer)
59 59
         data = data.toString();
60 60
         datum = data.split('\n');
61 61
         
62
+        //on death
63
+        
62 64
         for(x in datum)
63 65
         {
64 66
             w = datum[x].split(' ');
@@ -67,7 +69,6 @@ module.exports = function(io, mainGameServer)
67 69
             if(w[0] == "DEATH")
68 70
             {
69 71
                 getGames.dropGame(sockets[gameKey]['sessionID'], gameKey);
70
-                sockets[gameKey]['Socket'].emit('restarti');
71 72
             }
72 73
             
73 74
             toReturn = w.slice(0,w.length-1).join(" ");
@@ -118,12 +119,12 @@ module.exports = function(io, mainGameServer)
118 119
                 }
119 120
             });
120 121
             
121
-            sockets[cookieObject['currentGame']] = {'Socket':socket, 'sessionID':cookieObject['sessionID'], 'currentGame':cookieObject['currentGame']};
122
+            sockets[cookieObject['currentGame']] = {'Socket':socket, 'sessionID':cookieObject['sessionID']};
122 123
         }
123 124
         
124 125
         console.log("Connected");
125 126
         
126
-        socket.on('restarto', function(cookies) {
127
+        socket.on('restart', function(cookies) {
127 128
             console.log(cookies);
128 129
             sections = cookies.split('; ');
129 130
             cookieObject = {}
@@ -145,7 +146,20 @@ module.exports = function(io, mainGameServer)
145 146
                         if(subFound.length > 0) {
146 147
                             getGames.createGame(cookieObject['sessionID'], subFound[0], function(newKey) {
147 148
                                 socket.emit("newID", newKey);
148
-                                sockets[newKey] = {'Socket':socket, 'sessionID':cookieObject['sessionID'], 'currentGame':cookieObject['currentGame']};
149
+                                
150
+                                //on restart
151
+                                
152
+                                if (cookieObject['currentGame'] in scrollback)
153
+                                {
154
+                                    delete scrollback[cookieObject['currentGame']];
155
+                                    delete sockets[cookieObject['currentGame']];
156
+                                    getGames.dropGame(cookieObject['sessionID'], cookieObject['currentGame']);
157
+                                }
158
+                                
159
+                                mainGameServer.stdin.write("DELETE " + cookieObject['currentGame'] + '\n');
160
+                                scrollback[cookieObject['currentGame']] = [];
161
+                                
162
+                                sockets[newKey] = {'Socket':socket, 'sessionID':cookieObject['sessionID']};
149 163
                             });
150 164
                         }
151 165
                     });
@@ -175,6 +189,7 @@ module.exports = function(io, mainGameServer)
175 189
                 if(data == "ok")
176 190
                 {
177 191
                     toSend = msg + " " + cookieObject['currentGame'] + "|\n";
192
+                    console.log(toSend);
178 193
                     mainGameServer.stdin.write(toSend);
179 194
                     
180 195
                     scrollback[cookieObject['currentGame']].push("<b>>&nbsp;&nbsp;" + msg + "</b>");