Browse Source

Finished resuming game

iam 1 year ago
parent
commit
a44b05b637

+ 3 - 3
src/main.cpp

@@ -82,10 +82,10 @@ std::string getUserInput(std::vector<GameContainer*> *mainGameVector, int *gameI
82 82
             if(*gameIndex >= 0 && chosenint <= 0)
83 83
             {
84 84
                 if(mainGameVector->at(*gameIndex)->game->getItems() == "")
85
-                    std::cout << "You currently have no items\n";
85
+                    std::cout << "You currently have no items " << gameName << std::endl;
86 86
                 else
87
-                    std::cout << "You have: " << mainGameVector->at(*gameIndex)->game->getItems() << std::endl;
88
-                std::cout << "Your health is currently " << mainGameVector->at(*gameIndex)->game->health << std::endl;
87
+                    std::cout << "You have: " << mainGameVector->at(*gameIndex)->game->getItems() << " " << gameName << std::endl;
88
+                std::cout << "Your health is currently " << mainGameVector->at(*gameIndex)->game->health << " " << gameName << std::endl;
89 89
             }
90 90
         }
91 91
         //Server command//

+ 16 - 4
webserver/html/playGame.html

@@ -12,7 +12,10 @@
12 12
     <body>
13 13
         <%- include ./headings/navbar.ejs %>
14 14
         <div class="container gameText">
15
-            <div id="log"></div>
15
+            <div class="pre-scrollable mainGame">
16
+                <div id="log" class=""></div>
17
+                <div id="restart"></div>
18
+            </div>
16 19
             <div class="textarea"><span id="lts">&lt;&lt;</span><input id="textbox" type="text" autofocus></div>
17 20
         </div>
18 21
     </body>
@@ -22,6 +25,14 @@
22 25
     <script>
23 26
         var socket = io();
24 27
         
28
+        function restartGame()
29
+        {
30
+            $('#log').html('');
31
+            $("#restart").html('');
32
+            socket.emit('restarto', document.cookie);
33
+            $('#textbox').focus();
34
+        }
35
+        
25 36
         $('#textbox').bind("enterKey",function(e){
26 37
             socket.emit('out', $("#textbox").val(), document.cookie);
27 38
             $("#log").append("<br><b>>&emsp;&emsp;" + $("#textbox").val() + "</b><br>");
@@ -36,7 +47,7 @@
36 47
         });
37 48
         
38 49
         socket.on('restarti', function(data) {
39
-            socket.emit("restarto", document.cookie);
50
+            $("#restart").html('<br><br><button type="button" class="btn btn-default" onclick="restartGame()">Restart</button><br><br>');
40 51
         });
41 52
         
42 53
         socket.on('newID', function(data) {
@@ -44,9 +55,10 @@
44 55
         });
45 56
         
46 57
         socket.on('in', function(data) {
58
+            console.log(data);
47 59
             $("#log").append(data);
48
-            $("html, body").stop();
49
-            $("html, body").animate({ scrollTop: $(document).height() }, "slow");
60
+            $(".mainGame").stop();
61
+            $(".mainGame").animate({ scrollTop: $(".mainGame").height() }, "slow");
50 62
             return false;
51 63
         });
52 64
     </script>

+ 31 - 1
webserver/html/resumeGame.html

@@ -9,7 +9,37 @@
9 9
         <div class="container">
10 10
             Resume
11 11
             <hr>
12
-            <%=JSON.stringify(sessions)%>
12
+            
13
+            <script>
14
+                function getGame(path, key)
15
+                {
16
+                    document.cookie = "currentGame=" + key + "; expires=Thu, 18 Dec 2038 12:00:00 UTC; path=/";
17
+                    document.cookie = "currentGamePath=" + path + "; expires=Thu, 18 Dec 2038 12:00:00 UTC; path=/";
18
+                    console.log('/playGame' + path);
19
+                    window.location = '/playGame' + path;
20
+                }
21
+            </script>
22
+            
23
+            <% for (x in sessions) { %>
24
+                <% if (x%3 == 0) { %>
25
+                    <div class="row">
26
+                <% } %>
27
+                        <div class="col-sm-4">
28
+                            <div class="card" style="width: 20rem;">
29
+                                <% var foundGame = games['Games'].filter(function(v){ return v["Path"] == sessions[x]['path'];}); %>
30
+                                <img class="card-img-top" src="/thumbnails/<%= foundGame[0]['Thumbnail'] %>" alt="Card image cap" height="180" width="318">
31
+                                <div class="card-block">
32
+                                    <h4 class="card-title"><%= foundGame[0]['Name'] %></h4>
33
+                                    <p class="card-text"><%= foundGame[0]['Description'] %></p>
34
+                                    <a onclick="getGame('<%= sessions[x]['path'] %>' , '<%= sessions[x]['key'] %> ');" class="btn btn-primary">Resume</a>
35
+                                </div>
36
+                            </div>
37
+                        </div>
38
+                <% if (x%3 == 2 || x == sessions.length-1) { %>
39
+                    </div>
40
+                    <hr>
41
+                <% } %>
42
+            <% } %>
13 43
         </div>
14 44
     </body>
15 45
     <%- include ./headings/footer.ejs %>

+ 13 - 4
webserver/public/stylesheets/game.css

@@ -1,3 +1,9 @@
1
+.pre-scrollable {
2
+    max-height: none !important;
3
+    height: 70vh;
4
+    margin-top: 30px;
5
+}
6
+
1 7
 #game {
2 8
     font-family: "Courier New", Courier, monospace;
3 9
 }
@@ -13,14 +19,12 @@
13 19
 {
14 20
     color: #555;
15 21
     max-width: 960px;
16
-    margin-top: 50px;
17 22
 }
18 23
 
19 24
 #log:after
20 25
 {
21 26
     content: ' ';
22 27
     display: block;
23
-    height: 150px;
24 28
 }
25 29
 
26 30
 #log b
@@ -52,17 +56,22 @@
52 56
     margin-left: 5px;
53 57
     width: 95%;
54 58
 }
59
+
55 60
 .line
56 61
 {
57 62
     white-space: pre-line;
58 63
 }
59 64
 @media screen and (min-width: 910px)
60 65
 {
61
-    .textarea,#log
66
+    #log, #restart
62 67
     {
63
-        margin-left: 192px !important;
68
+        margin-left: 100px !important;
64 69
         margin-right: 192px !important;
65 70
     }
71
+    .textarea
72
+    {
73
+        margin-left: 192px !important;
74
+    }
66 75
 }
67 76
 @media screen and (max-width: 500px)
68 77
 {

+ 13 - 9
webserver/routes/findGames.js

@@ -36,16 +36,20 @@ router.get('/findgame', function(req, res) {
36 36
 router.get('/resume', function(req, res) {
37 37
     if("sessionID" in req.cookies) {
38 38
         getGames.getUser(req.cookies['sessionID'], function(data) {
39
-            findGames(function(games) {
40
-                ejs.renderFile(__dirname + baseFilePath + 'resumeGame.html', {active:"Resume", sessions:data, games:games}, function(err, result) {
41
-                    if (!err) {
42
-                        res.end(result);
43
-                    } else {
44
-                        res.end(err.toString());
45
-                        console.log(err);
46
-                    }
39
+            if(data.length > 0) {
40
+                findGames(function(games) {
41
+                    ejs.renderFile(__dirname + baseFilePath + 'resumeGame.html', {active:"Resume", sessions:data[0]['GameKeys'], games:games}, function(err, result) {
42
+                        if (!err) {
43
+                            res.end(result);
44
+                        } else {
45
+                            res.end(err.toString());
46
+                            console.log(err);
47
+                        }
48
+                    });
47 49
                 });
48
-            });
50
+            } else {
51
+                res.end('sessionID does not match any in the database. Clear your cookies.');
52
+            }
49 53
         });
50 54
     } else {
51 55
         getGames.createUser(res, function() {

+ 17 - 2
webserver/routes/getGames.js

@@ -44,6 +44,22 @@ 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
+
47 63
 function getUser(key, callback_)
48 64
 {
49 65
     MongoClient.connect(url, function (err, db) {
@@ -85,8 +101,6 @@ module.exports = function(mainGameServer)
85 101
             } else {
86 102
                 var collection = db.collection("Users");
87 103
                 collection.update({'UserID': key},{$push: { 'GameKeys': {key:gameKey, path:gameInfo['Path']} }}, function(err) {
88
-                    //add game here
89
-                    //File, Start
90 104
                     console.log("NEW " + gameKey + " " + gameInfo['File'] + " " + gameInfo['Start'] +'\n');
91 105
                     mainGameServer.stdin.write("NEW " + gameKey + " " + gameInfo['File'] + " " + gameInfo['Start'] +'\n');
92 106
                     
@@ -99,6 +113,7 @@ module.exports = function(mainGameServer)
99 113
     module.exports.createGame = createGame;
100 114
 }
101 115
 
116
+module.exports.dropGame = dropGame;
102 117
 module.exports.dropDatabase = dropDatabase;
103 118
 module.exports.createUser = createUser;
104 119
 module.exports.getUser = getUser;

+ 77 - 31
webserver/routes/manageSocket.js

@@ -1,6 +1,24 @@
1 1
 var getGames = require('./getGames');
2 2
 var findGames = require('./findGames');
3 3
 
4
+//make sure right user is playing right game
5
+function checkGame(cookieObject, callback_)
6
+{
7
+    getGames.getUser(cookieObject['sessionID'], function(data) {
8
+        if(data.length > 0)
9
+        {
10
+            findingGame = data[0]['GameKeys'].filter(function(v){ return v["key"] == cookieObject['currentGame'];});
11
+            
12
+            if(findingGame.length > 0)
13
+                callback_("ok")
14
+            else
15
+                callback_("Error: game not associated with user"); 
16
+        }
17
+        else
18
+            callback_("Error: sessionID not found");
19
+    });
20
+}
21
+
4 22
 module.exports = function(io, mainGameServer)
5 23
 {
6 24
     var sockets = {};
@@ -8,6 +26,9 @@ module.exports = function(io, mainGameServer)
8 26
     //data sends faster than client connects
9 27
     var sendBuffer = {};
10 28
     
29
+    //store past user text
30
+    var scrollback = {};
31
+    
11 32
     function readBuffer() {
12 33
         for(x in sendBuffer)
13 34
         {
@@ -17,7 +38,14 @@ module.exports = function(io, mainGameServer)
17 38
                 
18 39
                 if(x in sockets)
19 40
                 {
20
-                    sockets[x].emit('in', sendBuffer[x].join("<br>"));
41
+                    if(sendBuffer[x][sendBuffer[x].length-1] == "DEATH") {
42
+                        sockets[x]['Socket'].emit('in', sendBuffer[x].slice(0,sendBuffer[x].length-1).join("<br>"));
43
+                        delete scrollback[sockets[x]['Socket']['currentGame']]
44
+                        delete sockets[x]
45
+                    } else {
46
+                        sockets[x]['Socket'].emit('in', sendBuffer[x].join("<br>"));
47
+                    }
48
+                    
21 49
                     sendBuffer[x] = [];
22 50
                     //console.log(sendBuffer);
23 51
                 }
@@ -38,18 +66,26 @@ module.exports = function(io, mainGameServer)
38 66
             
39 67
             if(w[0] == "DEATH")
40 68
             {
41
-                sockets[gameKey].emit('restarti');
69
+                getGames.dropGame(sockets[gameKey]['sessionID'], gameKey);
70
+                sockets[gameKey]['Socket'].emit('restarti');
42 71
             }
43 72
             
44 73
             toReturn = w.slice(0,w.length-1).join(" ");
45 74
             
46
-            console.log(toReturn)
75
+            //console.log(toReturn)
47 76
             
48 77
             if(!(gameKey in sendBuffer))
49 78
                 sendBuffer[gameKey] = [];
50 79
             
51 80
             if(toReturn != [''])
81
+            {
52 82
                 sendBuffer[gameKey].push(toReturn.split("    ").join("&nbsp;&nbsp;&nbsp;&nbsp;"));
83
+                
84
+                if(!(gameKey in scrollback))
85
+                    scrollback[gameKey] = []
86
+                
87
+                scrollback[gameKey].push(toReturn);
88
+            }
53 89
         }
54 90
     });
55 91
     
@@ -67,7 +103,24 @@ module.exports = function(io, mainGameServer)
67 103
                     cookieObject[isSplit[0]] = isSplit[1]
68 104
             }
69 105
             
70
-            sockets[cookieObject['currentGame']] = socket;
106
+            checkGame(cookieObject, function(data) {
107
+                if(data == "ok")
108
+                {
109
+                    //returning user
110
+                    if(cookieObject['currentGame'] in scrollback)
111
+                    {
112
+                        if(scrollback[cookieObject['currentGame']].length > 0) {
113
+                            sendBuffer[cookieObject['currentGame']] = scrollback[cookieObject['currentGame']]
114
+                        }
115
+                    } else {
116
+                        scrollback[cookieObject['currentGame']] = [];
117
+                    }
118
+                } else {
119
+                    console.log(data);
120
+                }
121
+            });
122
+            
123
+            sockets[cookieObject['currentGame']] = {'Socket':socket, 'sessionID':cookieObject['sessionID'], 'currentGame':cookieObject['currentGame']};
71 124
         }
72 125
         
73 126
         console.log("Connected");
@@ -84,25 +137,24 @@ module.exports = function(io, mainGameServer)
84 137
             }
85 138
             
86 139
             getGames.getUser(cookieObject['sessionID'], function(data) {
87
-                if(data.length > 0)
88
-                {
89
-                    findingGame = data[0]['GameKeys'].filter(function(v){ return v["key"] == cookieObject['currentGame'];});
140
+                if(data.length > 0) {
141
+                    findGames.findGames(function(games) {
142
+                        
143
+                        gamePath = unescape(cookieObject['currentGamePath']);
90 144
                     
91
-                    if(findingGame.length > 0)
92
-                    {
93
-                        findGames.findGames(function(data2) {
94
-                            subFound = data2['Games'].filter(function(v){ return v["Path"] == findingGame[0]['path'];});
145
+                        subFound = games['Games'].filter(function(v){ return v["Path"] == gamePath;});
146
+                        
147
+                        if(subFound.length > 0) {
95 148
                             getGames.createGame(cookieObject['sessionID'], subFound[0], function(newKey) {
96 149
                                 socket.emit("newID", newKey);
97
-                                sockets[newKey] = socket;
150
+                                sockets[newKey] = {'Socket':socket, 'sessionID':cookieObject['sessionID'], 'currentGame':cookieObject['currentGame']};
98 151
                             });
99
-                        });
100
-                    }
101
-                    else
102
-                        console.log("Error: game not associated with user"); 
152
+                        }
153
+                    });
154
+                } else {
155
+                    console.log("Error: sessionID not found"); 
103 156
                 }
104 157
             });
105
-            
106 158
         });
107 159
         
108 160
         socket.on('out', function(msg, cookies){
@@ -121,24 +173,18 @@ module.exports = function(io, mainGameServer)
121 173
             console.log(cookieObject);
122 174
             //console.log(sockets);
123 175
             
124
-            //make sure right user is playing right game
125
-            getGames.getUser(cookieObject['sessionID'], function(data) {
126
-                if(data.length > 0)
176
+            checkGame(cookieObject, function(data) {
177
+                if(data == "ok")
127 178
                 {
128
-                    findingGame = data[0]['GameKeys'].filter(function(v){ return v["key"] == cookieObject['currentGame'];});
129
-                    
130
-                    if(findingGame.length > 0)
131
-                    {
132
-                        toSend = msg + " " + cookieObject['currentGame'] + "|\n";
133
-                        mainGameServer.stdin.write(toSend);
134
-                    }
135
-                    else
136
-                        console.log("Error: game not associated with user"); 
179
+                    toSend = msg + " " + cookieObject['currentGame'] + "|\n";
180
+                    mainGameServer.stdin.write(toSend);
181
+                } else {
182
+                    console.log(data);
137 183
                 }
138
-                else
139
-                    console.log("Error: sessionID not found");
140 184
             });
141 185
             
186
+
187
+            
142 188
         });
143 189
     });
144 190
 }