PageRenderTime 31ms CodeModel.GetById 2ms app.highlight 25ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/game.js

https://bitbucket.org/nehar/ti
JavaScript | 284 lines | 237 code | 36 blank | 11 comment | 52 complexity | a7c8656da135b102c7348786b3b4c840 MD5 | raw file
  1/*jshint node:true */
  2module.exports = {
  3
  4    game: function game(map, socket, io) {
  5        "use strict";
  6        socket.newtroops = 10;
  7        //przygotowanie danych mapy
  8
  9        map[socket.room] = {na1: {},na2: {},na3: {},na4: {},na5: {},na6: {},na7: {},na8: {},na9: {},na10: {},sa1: {},sa2: {},sa3: {},sa4: {},af1: {},af2: {},af3: {},af4: {},af5: {},eu1: {},eu2: {},eu3: {},eu4: {},eu5: {},eu6: {},as1: {},as2: {},as3: {},as4: {},as5: {},as6: {},as7: {},as8: {},as9: {},as10: {},as11: {},aus1: {},aus2: {},aus3: {}};
 10        mapIni(map[socket.room]);
 11
 12        //inicjalizajca mapy u klienta
 13        socket.emit('startGame');
 14        //inicjalizacja danych u klienta po pod??czeniu drugiej osoby
 15        if (io.sockets.manager.rooms['/' + socket.room].length === 2) {
 16            map[socket.room] = mapRandom(map[socket.room], io, socket);
 17
 18
 19            for (var i in io.sockets.manager.rooms['/' + socket.room]) {
 20
 21
 22                if (io.sockets.manager.rooms['/' + socket.room][i] === socket.id) {
 23                    io.sockets.sockets[io.sockets.manager.rooms['/' + socket.room][i]].emit('startMap', map[socket.room], io.sockets.manager.rooms['/' + socket.room][(i + 1) % 2], socket.newtroops);
 24                    io.sockets.sockets[socket.oponent].oponent = socket.id;
 25                }
 26                else {
 27                    socket.oponent = io.sockets.manager.rooms['/' + socket.room][i];
 28                    io.sockets.sockets[io.sockets.manager.rooms['/' + socket.room][i]].emit('startMap', map[socket.room], io.sockets.manager.rooms['/' + socket.room][(i + 1) % 2], socket.newtroops);
 29                }
 30
 31            }
 32        }
 33    },
 34
 35    socket: function socket(map, socket, io) {
 36        "use strict";
 37
 38        //dodawanie jednostki do pola
 39        socket.on('addTroop', function (area) {
 40            console.log(socket.id + '          ' + map[socket.room][area].owner + '   ' + area);
 41            if (socket.id === map[socket.room][area].owner && socket.newtroops !== 0) {
 42
 43                map[socket.room][area].army.soldier[0] = map[socket.room][area].army.soldier[0] + 1;
 44                socket.newtroops = socket.newtroops - 1;
 45                io.sockets.to(socket.room).emit('updateTroops', area, map[socket.room][area].army.soldier[0]);
 46                socket.emit('troopsLeft', socket.newtroops);
 47                if (socket.newtroops === 0 && io.sockets.sockets[socket.oponent].newtroops === 0) {
 48                    socket.stopinterval = 0;
 49                    io.sockets.to(socket.room).emit('endTroops');
 50                    io.sockets.to(socket.room).emit('updateChat', 'SERVER', 'Start Game');
 51                }
 52            }
 53        });
 54
 55
 56		//generowanie ilo?ci bonusowych wojsk
 57        socket.on('bonus', function () {
 58            var newtroops = setInterval(bonusTroops, 7000);
 59
 60            function bonusTroops() {
 61                if (socket.stopinterval === 1) {
 62                    clearInterval(newtroops);
 63                    return 0;
 64                }
 65
 66                var key, bonus = 0,
 67                    na = 0,
 68                    sa = 0,
 69                    eu = 0,
 70                    af = 0,
 71                    aus = 0,
 72                    as = 0;
 73                for (key in map[socket.room]) {
 74                    if (map[socket.room][key].owner === socket.id) {
 75
 76                        if (key.match(/na/)) {
 77                            na = na + 1;
 78                        }
 79                        else if (key.match(/sa/)) {
 80                            sa = sa + 1;
 81                        }
 82                        else if (key.match(/eu/)) {
 83                            eu = eu + 1;
 84                        }
 85                        else if (key.match(/af/)) {
 86                            af = af + 1;
 87                        }
 88                        else if (key.match(/aus/)) {
 89                            aus = aus + 1;
 90                        }
 91                        else if (key.match(/as/)) {
 92                            as = as + 1;
 93                        }
 94                    }
 95                }
 96                if (na === 10) {
 97                    bonus = bonus + 6;
 98                }
 99                if (sa === 4) {
100                    bonus = bonus + 2;
101                }
102                if (eu === 6) {
103                    bonus = bonus + 4;
104                }
105                if (af === 5) {
106                    bonus = bonus + 3;
107                }
108                if (as === 11) {
109                    bonus = bonus + 7;
110                }
111                if (aus === 3) {
112                    bonus = bonus + 1;
113                }
114
115                socket.newtroops = socket.newtroops + bonus + 1;
116                socket.emit('bonusTroops', socket.newtroops);
117
118            }
119        });
120
121        //atakowanie
122        socket.on('attack', function (from, to, number) {
123            number = parseInt(number, 10);
124            if (!isNaN(number) && socket.id === map[socket.room][from].owner && socket.oponent === map[socket.room][to].owner) {
125                if (map[socket.room][from].army.soldier[0] < number) {
126                    number = map[socket.room][from].army.soldier[0];
127                }
128
129                var attack = setInterval(function () {
130                    attackMove();
131                }, 3000);
132            }
133
134            function attackMove() {
135                if (number === 0) {
136                    clearInterval(attack);
137
138
139                }
140                else if (map[socket.room][to].army.soldier[0] === 0) {
141                    clearInterval(attack);
142                    map[socket.room][to].owner = socket.id;
143                    map[socket.room][to].army.soldier[0] = number;
144                    map[socket.room][from].army.soldier[0] = map[socket.room][from].army.soldier[0] - number;
145                    io.sockets.to(socket.room).emit('updateOwner', to, map[socket.room][to].owner);
146                    io.sockets.to(socket.room).emit('updateTroops', from, map[socket.room][from].army.soldier[0]);
147                    io.sockets.to(socket.room).emit('updateTroops', to, map[socket.room][to].army.soldier[0]);
148                    if (winCheck(map, socket) === 1) {
149                        io.sockets.to(socket.room).emit('winGame', socket.username);
150                        socket.emit('backToLobby');
151
152                    }
153
154                }
155                else {
156                    if (Math.floor((Math.random() * 1000) + 1) < 600) {
157                        map[socket.room][from].army.soldier[0] = map[socket.room][from].army.soldier[0] - 1;
158                        number = number - 1;
159                        io.sockets.to(socket.room).emit('updateTroops', from, map[socket.room][from].army.soldier[0]);
160                    }
161                    else {
162                        map[socket.room][to].army.soldier[0] = map[socket.room][to].army.soldier[0] - 1;
163                        io.sockets.to(socket.room).emit('updateTroops', to, map[socket.room][to].army.soldier[0]);
164                    }
165                }
166            }
167
168        });
169
170        //przemieszczanie jednostek
171        socket.on('move', function (from, to, number) {
172            number = parseInt(number, 10);
173
174            if (!isNaN(number) && socket.id === map[socket.room][from].owner && socket.id === map[socket.room][to].owner) {
175                if (map[socket.room][from].army.soldier[0] < number) {
176                    number = map[socket.room][from].army.soldier[0];
177                }
178                map[socket.room][from].army.soldier[0] = map[socket.room][from].army.soldier[0] - number;
179                io.sockets.to(socket.room).emit('updateTroops', from, map[socket.room][from].army.soldier[0]);
180                socket.emit('moveTimer');
181
182                setTimeout(fn, 3000);
183
184            }
185
186            function fn() {
187                if (map[socket.room][to].owner === socket.id) {
188                    map[socket.room][to].army.soldier[0] = map[socket.room][to].army.soldier[0] + parseInt(number, 10);
189                    io.sockets.to(socket.room).emit('updateTroops', to, map[socket.room][to].army.soldier[0]);
190                }
191                else if (map[socket.room][from].owner === socket.id) {
192                    map[socket.room][from].army.soldier[0] = map[socket.room][from].army.soldier[0] + parseInt(number, 10);
193                    io.sockets.to(socket.room).emit('updateTroops', from, map[socket.room][from].army.soldier[0]);
194                }
195            }
196        });
197
198    }
199};
200//sprawdzenie czy wygra?
201function winCheck(map, socket) {
202    "use strict";
203    for (var key in map[socket.room]) {
204        if (map[socket.room][key].owner !== socket.id) {
205            return 0;
206        }
207
208    }
209    return 1;
210}
211
212//podstawowe dane mapy
213function mapIni(map) {
214    "use strict";
215    for (var key in map) {
216        map[key] = {
217            owner: null,
218            army: {
219                soldier: [0, 0]
220            },
221            buildings: {
222                wall: 0
223            },
224            connections: []
225        };
226    }
227    map.na1.connections = ["na2", "na4", "as9"];
228    map.na2.connections = ["na1", "na3", "na4", "na5"];
229    map.na3.connections = ["na2", "na5", "na7", "na6"];
230    map.na4.connections = ["na1", "na2", "na5", "na8"];
231    map.na5.connections = ["na4", "na2", "na3", "na7", "na8", "na10"];
232    map.na6.connections = ["na3", "na7", "eu1", "eu3"];
233    map.na7.connections = ["na3", "na6", "na5", "na10"];
234    map.na8.connections = ["na5", "na4", "na10", "na9"];
235    map.na9.connections = ["na8", "na10", "sa1"];
236    map.na10.connections = ["na8", "na5", "na7", "na9"];
237    map.sa1.connections = ["na9", "sa2", "sa4"];
238    map.sa2.connections = ["sa1", "sa3", "sa4"];
239    map.sa3.connections = ["sa2", "sa4"];
240    map.sa4.connections = ["sa1", "sa2", "sa3", "af1"];
241    map.af1.connections = ["sa4", "eu2", "af2", "af5", "af3"];
242    map.af2.connections = ["af1", "af5", "as1"];
243    map.af3.connections = ["af1", "af5", "af4"];
244    map.af4.connections = ["af3", "af5"];
245    map.af5.connections = ["af1", "af2", "af3", "af4"];
246    map.eu1.connections = ["eu2", "eu3", "eu4", "na6"];
247    map.eu2.connections = ["eu1", "eu4", "eu6", "af1"];
248    map.eu3.connections = ["eu1", "eu4", "eu5", "na6"];
249    map.eu4.connections = ["eu1", "eu2", "eu3", "eu6", "eu5"];
250    map.eu5.connections = ["eu3", "eu4", "eu6", "as3", "as2", "as1"];
251    map.eu6.connections = ["eu2", "eu4", "eu5", "as1"];
252    map.as1.connections = ["eu6", "eu5", "as2", "as10", "af2"];
253    map.as2.connections = ["as1", "as10", "as4", "as3", "eu5"];
254    map.as3.connections = ["as2", "as5", "as4", "eu5"];
255    map.as4.connections = ["as2", "as3", "as5", "as8", "as10", "as11"];
256    map.as5.connections = ["as3", "as4", "as6", "as7", "as8"];
257    map.as6.connections = ["as5", "as7", "as9"];
258    map.as7.connections = ["as5", "as6", "as8", "as9"];
259    map.as8.connections = ["as5", "as7", "as9", "as4"];
260    map.as9.connections = ["as6", "as7", "as8"];
261    map.as10.connections = ["as1", "as2", "as4", "as11"];
262    map.as11.connections = ["as10", "as4", "aus1"];
263    map.aus1.connections = ["aus2", "aus3", "as11"];
264    map.aus2.connections = ["aus1", "aus3"];
265    map.aus3.connections = ["aus1", "aus2"];
266}
267
268//losowanie p�l
269
270function mapRandom(map, io, socket) {
271    "use strict";
272    var random = ['na1', 'na2', 'na3', 'na4', 'na5', 'na6', 'na7', 'na8', 'na9', 'na10', 'sa1', 'sa2', 'sa3', 'sa4', 'af1', 'af2', 'af3', 'af4', 'af5', 'eu1', 'eu2', 'eu3', 'eu4', 'eu5', 'eu6', 'as1', 'as2', 'as3', 'as4', 'as5', 'as6', 'as7', 'as8', 'as9', 'as10', 'as11', 'aus1', 'aus2', 'aus3'];
273    for (var i = 0; i < 39; i = i + 1) {
274        var rand = Math.floor(Math.random() * random.length);
275        if (i % 2 === 0) {
276            map[random[rand]].owner = io.sockets.sockets[io.sockets.manager.rooms['/' + socket.room][0]].id;
277        }
278        else {
279            map[random[rand]].owner = io.sockets.sockets[io.sockets.manager.rooms['/' + socket.room][1]].id;
280        }
281        random.splice(rand, 1);
282    }
283    return map;
284}