Browse Source

version v0.0.9

frtk 5 years ago
parent
commit
b5064543e4

+ 6
- 6
client/index.html View File

@@ -7,12 +7,12 @@
7 7
   <title>nodePong, a multiplayer online pong based on nodejs and socket.io</title>
8 8
   <link rel="stylesheet" type="text/css" media="all" href="./css/main.css">
9 9
   <script type="text/javascript" src="http://127.0.0.1:8042/socket.io/socket.io.js"></script>
10
-  <script type="text/javascript" src="./src/jquery-2.1.3.min.js"></script>
11
-  <script type="text/javascript" src="./js/npg_client.js"></script>
12
-  <script type="text/javascript" src="./js/socket/socketio_handler.js"></script>
13
-  <script type="text/javascript" src="./js/keyboard/event_handler.js"></script>
14
-  <script type="text/javascript" src="./js/core/page_base.js"></script>
15
-  <script type="text/javascript" src="./js/pages/login_page.js"></script>
10
+  <script type="text/javascript" src="./jquery/jquery-2.1.3.min.js"></script>
11
+  <script type="text/javascript" src="./lib/npg_client.js"></script>
12
+  <script type="text/javascript" src="./lib/socket/socketio_handler.js"></script>
13
+  <script type="text/javascript" src="./lib/keyboard/event_handler.js"></script>
14
+  <script type="text/javascript" src="./lib/core/page_base.js"></script>
15
+  <script type="text/javascript" src="./lib/pages/login_page.js"></script>
16 16
 </head>
17 17
 
18 18
 <body>  

+ 4
- 0
client/jquery/jquery-2.1.3.min.js
File diff suppressed because it is too large
View File


+ 13
- 0
client/lib/config/NPGClientConfig.js View File

@@ -0,0 +1,13 @@
1
+/**
2
+ * @file NPGClientConfig.js
3
+ */
4
+
5
+var NPGClient.Config = {
6
+
7
+       
8
+
9
+
10
+
11
+
12
+
13
+}

+ 114
- 0
client/lib/core/page_base.js View File

@@ -0,0 +1,114 @@
1
+/**
2
+ * @file AppPage.js
3
+ * @author frtk@tetalab
4
+ */
5
+
6
+NPGClient.AppPage = function(n) { 
7
+    
8
+  Object.defineProperty(this, 'id', { value: NPGClient.pageCount++ });
9
+   
10
+  this.name = n || '';
11
+  // draw loop paramaters
12
+  this.loopIntervalId = 0;
13
+  this.loopDelay = 40; 
14
+  // canvas 
15
+  this.canvas_id = "";
16
+  this.canvas_bcol = "";
17
+  this.canvas = {};
18
+  this.ctx = {};
19
+
20
+  // ui elements   
21
+  this.uiElems = [];
22
+
23
+};
24
+
25
+NPGClient.AppPage.prototype = {
26
+
27
+  constructor: NPGClient.AppPage,
28
+
29
+  //
30
+  setLoopDelay: function(d) {
31
+    this.loopDelay = d;
32
+  },
33
+
34
+  //
35
+  startDrawLoop: function() {
36
+    var self = this;
37
+    self.loopIntervalId = setInterval(function() { 
38
+      self.run(); 
39
+    }, self.loopDelay);
40
+  },
41
+
42
+  //
43
+  stopDrawLoop: function() {
44
+    var self = this;
45
+    clearInterval(self.loopIntervalId); 
46
+    self.loopIntervalId = 0;            
47
+  },
48
+
49
+  //
50
+  run: function() {
51
+    var self = this;
52
+    self.drawUI();
53
+  },
54
+
55
+  //
56
+  configUI: function(id, w, h, c) {
57
+    var self = this;
58
+    self.canvas_id = id;
59
+    self.canvas = document.getElementById(id); 
60
+    self.ctx = self.canvas.getContext("2d");
61
+    self.canvas.width = w;
62
+    self.canvas.height = h;
63
+    self.canvas_bcol = c;
64
+  },
65
+
66
+  //
67
+  addUIObject: function(c) {
68
+    var self = this;
69
+    self.uiElems.push(c);
70
+  },
71
+
72
+  //
73
+  getUIElemByName: function(name) {
74
+    var self = this;
75
+    if (self.uiElems.length > 0) {
76
+      for (var i = 0; i < self.uiElems.length; i++)
77
+ 	if (self.uiElems[i].name == name) return self.uiElems[i];
78
+    }
79
+    return undefined;
80
+  },
81
+
82
+  //
83
+  drawUI: function() {
84
+    var self = this;
85
+    self.clear();
86
+    self.drawUIBackground();
87
+    self.drawUIObjects();	
88
+  },
89
+
90
+  //
91
+  drawUIBackground: function() {
92
+    var self = this;
93
+    self.ctx.fillStyle = self.canvas_bcol;
94
+    self.ctx.fillRect(0, 0, self.canvas.width, self.canvas.height);
95
+  },
96
+  
97
+  //
98
+  drawUIObjects: function() {
99
+    var self = this;
100
+    if (self.uiElems.length > 0) {
101
+      for (var i = 0; i < self.uiElems.length; i++)
102
+	self.uiElems[i].draw(self.ctx);
103
+    }
104
+  },
105
+
106
+  //
107
+  clear: function() {
108
+    var self = this;
109
+    self.ctx.clearRect(0, 0, self.canvas.width, self.canvas.height); 
110
+  },
111
+
112
+    
113
+};
114
+

+ 51
- 0
client/lib/keyboard/event_handler.js View File

@@ -0,0 +1,51 @@
1
+/**
2
+ * @file event_handling.js
3
+ * @autour frtk@tetalab
4
+ */
5
+
6
+NPGClient.evtHandler = {
7
+
8
+  keysList: {
9
+    F1 : 112,
10
+    SPACE : 32,
11
+    ESC : 27,
12
+    ARROW_UP : 38,
13
+    ARROW_DOWN : 40,
14
+    ENTER : 13,
15
+    DELETE : 46,
16
+    BACKSPACE : 8,
17
+  },  
18
+    
19
+  keyState: {},
20
+  
21
+  //
22
+  init: function() {
23
+    var self = this;
24
+    //
25
+    self.keyState = {};
26
+    for (k in self.keyList) {
27
+      self.keyState[self.keysList[k]] = false;
28
+    }               
29
+    //
30
+    document.addEventListener('keydown',function(e) {
31
+      self.keyState[e.keyCode || e.which] = true;
32
+      self.onKeyDown(e);
33
+    }, true);
34
+    //
35
+    document.addEventListener('keyup',function(e){
36
+      self.keyState[e.keyCode || e.which] = false;
37
+    }, true);                       
38
+  },
39
+
40
+  // 
41
+  loginValidKey: function(k) {
42
+    return (key >= 48 && key <= 90);
43
+  },
44
+   
45
+  //
46
+  onKeyDown : function(evt) {
47
+    console.log(evt.keyCode); 
48
+  },
49
+
50
+
51
+}

+ 8
- 0
client/lib/npg_client.js View File

@@ -0,0 +1,8 @@
1
+/**
2
+ * @file NPGClient.js
3
+ * @author frtk
4
+ */
5
+
6
+var NPGClient = { 'version': '' };
7
+
8
+

+ 13
- 0
client/lib/pages/login_page.js View File

@@ -0,0 +1,13 @@
1
+/**
2
+ * @file login_page.js
3
+ * @author frtk@tetalab
4
+ */
5
+
6
+NPGClient.LoginPage = {
7
+
8
+    pageIdx: 0,
9
+    page: new NPGClient.AppPage('login'),
10
+    
11
+    
12
+}; 
13
+

+ 77
- 0
client/lib/socket/socketio_handler.js View File

@@ -0,0 +1,77 @@
1
+/**
2
+ * @file SocketIO.js
3
+ * @author frtk
4
+ */
5
+
6
+NPGClient.SocketIO = { 
7
+
8
+  // Connection to server  
9
+  conn_nAttempts: 0,
10
+  conn_IntervalID: 0,
11
+  socket: {},
12
+  isConnected: false,
13
+
14
+  // start connection loop
15
+  initConnectLoop:  function() { 
16
+    var self = this;
17
+      console.log();  
18
+      self.startConnectLoop();
19
+  },
20
+  // start connection loop
21
+  startConnectLoop:  function() { 
22
+    var self = this;
23
+    self.conn_IntervalID = setInterval(function() { self.connect(); }, 1500); 
24
+  },
25
+  // stop connection loop
26
+  stopConnectLoop:  function() { 
27
+    var self =  this;
28
+    clearInterval(self.conn_IntervalID);
29
+    self.conn_IntervalID = 0;
30
+    self.conn_nAttempts = 0;
31
+  },
32
+  // connect socket
33
+  connect: function() {
34
+    var self = this;  
35
+    // check for connection
36
+    if (self.isConnected) {
37
+      return false;  
38
+    }
39
+    //
40
+    if (typeof io !== 'object') {
41
+      //console.log('[NPGClient.SocketIO] io object is unknown');
42
+    }
43
+    if ((typeof self.socket.socket === 'undefined')) {      
44
+      console.log('[NPGClient.SocketIO] attempting server connection...');
45
+      self.socket = io.connect();  
46
+      self.defineSocketMsgs();
47
+    } else if (!self.socket.socket.connected && !self.socket.socket.connecting) {
48
+      console.log('[NPGClient.SocketIO] attempting server connection...');
49
+      self.socket.socket.connect();   
50
+    }
51
+  },
52
+  // Define Socket Messages
53
+  defineSocketMsgs: function() {
54
+    var self = this;  
55
+    if (typeof self.socket === 'undefined') {
56
+      return false;
57
+    }
58
+    console.log('[NPGClient.SocketIO] deploying socket object features.');
59
+    // 'connecting' 
60
+    self.socket.on('connecting', function () {
61
+      console.log('[NPGClient.SocketIO] connecting ...');
62
+    });
63
+    // 'connect'
64
+    self.socket.on('connect', function() {
65
+      console.log('[NPGClient.SocketIO] connected to server');
66
+      self.stopConnectLoop();
67
+      self.isConnected = true;
68
+    });
69
+    // 'disconnect'
70
+    self.socket.on('disconnect', function() {
71
+      console.log('[NPGClient.SocketIO] > Server: socket disconnected.');
72
+      self.startConnectLoop();
73
+      self.isConnected = false;
74
+    });     
75
+  }
76
+  
77
+};

+ 50
- 0
npg_app.js View File

@@ -0,0 +1,50 @@
1
+/**
2
+ * @file nodePong.js
3
+ * @author frtk
4
+ */
5
+
6
+
7
+/*
8
+ * nodejs modules
9
+ */
10
+//--- 
11
+var fs = require('fs');
12
+var express = require('express');
13
+var http = require('http');
14
+//--- server app libs & params
15
+//var servP = require('./server/server_config.js');  
16
+var npg = require('./server/server_core.js');
17
+//--- nodePong config
18
+var cfg = require('./server/server_config.js');
19
+
20
+
21
+//
22
+npg.NPGServer.init(cfg.Config);
23
+
24
+
25
+/*
26
+ * starting HTTP and socket.io services
27
+ */
28
+//--- HTTP server
29
+npg.NPGServer.log("$ # starting http service on port " + npg.NPGServer.port);
30
+var app = express();
31
+var httpserv = http.createServer(app);
32
+httpserv.listen(npg.NPGServer.port);
33
+//--- allow access to static files from "/client" directory
34
+app.use(express.static(__dirname + '/client/'));
35
+
36
+//--- socket.io
37
+npg.NPGServer.log('$ # registering socket.io service on port ' + npg.NPGServer.port);  
38
+var io = require('socket.io').listen(httpserv, { log: true } );
39
+//-- setup server socket handling features
40
+npg.NPGServer.socketHandling(io);
41
+//
42
+npg.NPGServer.log('$ #####');
43
+
44
+
45
+
46
+/*
47
+ * running nodePong server
48
+ */
49
+//---
50
+

+ 8
- 5
package.json View File

@@ -1,11 +1,10 @@
1 1
 {
2
-  "name": "nodePong.js",
3
-  "version": "0.0.1",
2
+  "name": "npg_app.js",
3
+  "version": "0.0.9",
4 4
   "description": "Multiplayer online Pong Game using nodejs and socket.io",
5 5
   "directories": {
6
-    "doc": "docs",
7
-    "example": "examples",
8
-    "test": "test"
6
+    "server": "server",
7
+    "client": "client",
9 8
   },
10 9
   "repository": {
11 10
     "type": "git",
@@ -20,5 +19,9 @@
20 19
   "author": "frtk@tetalab",
21 20
   "license": "MIT",
22 21
   "dependencies": {
22
+    "express": "",
23
+    "fs": "",
24
+    "http": "",
25
+    "socket.io": ""
23 26
   }
24 27
 }

+ 141
- 0
server/npg_server.js View File

@@ -0,0 +1,141 @@
1
+/**
2
+ * @file npg_server.js
3
+ * @author frtk                                                                                 
4
+ */
5
+
6
+
7
+
8
+/**
9
+ *
10
+ * nodePong Server Object
11
+ *
12
+ */
13
+var NPGServer = {
14
+  
15
+  /*
16
+   * Data 
17
+   */
18
+  // app
19
+  version: '',
20
+  host: '',
21
+  port: 0,
22
+
23
+  // Users and Games
24
+  users: [],
25
+  games: [],
26
+
27
+
28
+  /*
29
+   *
30
+   */
31
+  //--- init()
32
+  init: function(o) {
33
+    var self = this;
34
+    self.version = o.VERSION || '';  
35
+    self.host = o.HTTP.host || ''; 
36
+    self.port = o.HTTP.port || 8042;  
37
+    self.log('$ ##### nodePong - v'+self.version);  
38
+  },
39
+
40
+
41
+  //--- setVersion(s)
42
+  setVersion: function(s) {
43
+    this.version = s;
44
+  },
45
+
46
+
47
+  /*
48
+   * Server Messages
49
+   */ 
50
+  //--- 
51
+  log: function(msg) {
52
+    var self = this;
53
+    return console.log(self.newDateToString() + msg);
54
+  },
55
+  //--- dateToString
56
+  dateToString: function(date) {
57
+    var self = this;
58
+    return '[' + self.addZero(date.getHours(), 'h') + ':' 
59
+               + self.addZero(date.getMinutes(), 'm') + ':' 
60
+               + self.addZero(date.getSeconds(), 's') + ':' 
61
+               + self.addZero(date.getMilliseconds(), 'ms') + ']';
62
+  },
63
+  //--- addZero
64
+  addZero: function(value, type) {
65
+    switch(type) {
66
+      case 'h':
67
+      case 'm':
68
+      case 's':
69
+        if (value < 10) value = '0' + value;
70
+          break;
71
+      case 'ms':
72
+        if (value < 10) value = '00' + value;
73
+        else if (value >= 10 && value < 100) value = '0' + value;
74
+        break;
75
+      default:
76
+        break;                  
77
+    };
78
+    return value;
79
+  },
80
+  //--- return new date string
81
+  newDateToString: function() {
82
+    var self = this;  
83
+    return self.dateToString(new Date());
84
+  },
85
+
86
+
87
+   
88
+  /*
89
+   * Socket.io handling
90
+   */ 
91
+  //--- 
92
+  socketHandling: function(io) {
93
+    var self = this;
94
+    io.sockets.on('connection', function (socket) {
95
+      //
96
+      self.log('$ User connected : id=' + socket.id);
97
+
98
+      // 'disconnect'
99
+      socket.on('disconnect', function () {                                                                                 self.log('$ User disconnected : id=' + socket.id);                                                         
100
+      });
101
+
102
+      //  
103
+    });
104
+  },
105
+
106
+
107
+};
108
+
109
+
110
+
111
+/**
112
+ *
113
+ * nodePong User Object
114
+ *
115
+ */
116
+var User = function(name) {
117
+};
118
+
119
+
120
+
121
+/**
122
+ *
123
+ * nodePong Game Object
124
+ *
125
+ */
126
+var Game = function() {
127
+};
128
+
129
+
130
+
131
+
132
+/**
133
+ *
134
+ * EXPORT
135
+ *
136
+ */
137
+if (typeof exports !== "undefined") {
138
+  exports.NPGServer = NPGServer;
139
+  exports.User = User;
140
+  exports.Game = Game;
141
+}

+ 23
- 0
server/npg_server_config.js View File

@@ -0,0 +1,23 @@
1
+/**
2
+ * @file npg_server_config.js
3
+ * @author frtk
4
+ */
5
+
6
+var Config = {
7
+  VERSION : '0.0.8',
8
+  HTTP: {
9
+    host: '127.0.0.1',
10
+    port: 8042
11
+  },
12
+}
13
+
14
+
15
+
16
+/**
17
+ *
18
+ * EXPORTS
19
+ *
20
+ */
21
+if (typeof exports !== "undefined") {
22
+  exports.Config = Config;
23
+}

+ 14
- 0
version.md View File

@@ -1,4 +1,18 @@
1 1
 
2
+### **v0.0.9:**
3
+-------------------------- focus: renaming files/directories
4
+---- App
5
+- nodePong.js -> npg_app.js
6
+- package.json: updated "directories" & "dependencies" parameters
7
+---- Client
8
+- client/src -> client/jquery
9
+- client/js -> client/lib
10
+- updated index.html
11
+---- Server
12
+- server_core.js -> npg_server.js
13
+- server_config.js -> npg_server_config.js
14
+
15
+
2 16
 ### **v0.0.8:**
3 17
 --- focus on client side
4 18
 - Renamed files (lower case + _ )

Loading…
Cancel
Save