|
1 | 1 | var mongoose = require('mongoose'); |
2 | 2 |
|
3 | | - |
4 | 3 | var schema = new mongoose.Schema( |
5 | 4 | { |
6 | | - user0: mongoose.Schema.Types.Mixed, |
7 | | - user1: mongoose.Schema.Types.Mixed, |
8 | | - caseArray: [mongoose.Schema.Types.Mixed], |
9 | | - currentPlayer : String |
| 5 | + user0: mongoose.Schema.Types.Mixed, |
| 6 | + user1: mongoose.Schema.Types.Mixed, |
| 7 | + caseArray: [mongoose.Schema.Types.Mixed], |
| 8 | + currentPlayer: Number |
10 | 9 |
|
11 | 10 | } |
12 | 11 | ); |
13 | 12 |
|
14 | | -schema.methods.initGame = function (user0, user1) { |
15 | | - if(user0 && user1){ |
16 | | - this.user0 = user0; |
17 | | - this.user1 = user1; |
18 | | - this.caseArray = []; |
19 | | - var rand = Math.floor(Math.random()*100); |
20 | | - if(rand % 2 == 0){ |
21 | | - this.currentPlayer = this.user0.id; |
22 | | - this.user0.color = 'red'; |
23 | | - this.user1.color = 'yellow'; |
24 | | - } else { |
25 | | - this.currentPlayer = this.user1.id; |
26 | | - this.user0.color = 'yellow'; |
27 | | - this.user1.color = 'red'; |
28 | | - } |
| 13 | +schema.methods.initGame = function(user0, user1) { |
| 14 | + if (user0 && user1) { |
| 15 | + this.user0 = user0; |
| 16 | + this.user1 = user1; |
| 17 | + this.caseArray = []; |
| 18 | + var rand = Math.floor(Math.random() * 100); |
| 19 | + if (rand % 2 === 0) { |
| 20 | + this.currentPlayer = this.user0.id; |
| 21 | + this.user0.color = 'red'; |
| 22 | + this.user1.color = 'yellow'; |
| 23 | + } else { |
| 24 | + this.currentPlayer = this.user1.id; |
| 25 | + this.user0.color = 'yellow'; |
| 26 | + this.user1.color = 'red'; |
29 | 27 | } |
| 28 | + } |
30 | 29 | }; |
31 | 30 |
|
32 | | - |
33 | | - |
34 | | - |
35 | | -schema.methods.gameAction = function (userId, caseName) { |
36 | | - if(userId == this.currentPlayer){ |
37 | | - if(!this.caseArray){ |
38 | | - this.caseArray = []; |
39 | | - } |
40 | | - if(this.caseArray.filter(function (item) { |
41 | | - return item.caseName == caseName |
42 | | - }).length != 0){ |
43 | | - console.error('try to overwrite'); |
44 | | - } else{ |
45 | | - this.caseArray.push({ |
46 | | - caseName : caseName, |
47 | | - userId : userId, |
48 | | - }) |
49 | | - if(this.currentPlayer){ |
50 | | - if(this.currentPlayer == this.user0.id){ |
51 | | - this.currentPlayer = this.user1.id |
52 | | - } else if(this.currentPlayer == this.user1.id){ |
53 | | - this.currentPlayer = this.user0.id |
54 | | - } else{ |
55 | | - console.error('the value of current player is incorrect'); |
56 | | - } |
57 | | - } else{ |
58 | | - console.error('currentPlayer must be init') |
59 | | - } |
| 31 | +schema.methods.gameAction = function(userId, caseName) { |
| 32 | + if (userId === parseInt(this.currentPlayer)) { |
| 33 | + if (!this.caseArray) { |
| 34 | + this.caseArray = []; |
| 35 | + } |
| 36 | + if (this.caseArray.filter(function(item) { |
| 37 | + return item.caseName === caseName; |
| 38 | + }).length !== 0) { |
| 39 | + console.error('try to overwrite'); |
| 40 | + } else { |
| 41 | + this.caseArray.push({ |
| 42 | + caseName: caseName, |
| 43 | + userId: userId |
| 44 | + }); |
| 45 | + if (this.currentPlayer) { |
| 46 | + if (parseInt(this.currentPlayer) === this.user0.id) { |
| 47 | + this.currentPlayer = this.user1.id; |
| 48 | + } else if (parseInt(this.currentPlayer) === this.user1.id) { |
| 49 | + this.currentPlayer = this.user0.id; |
| 50 | + } else { |
| 51 | + console.error('the value of current player is incorrect'); |
60 | 52 | } |
61 | | - } else{ |
62 | | - console.error('wrong current player') |
| 53 | + } else { |
| 54 | + console.error('currentPlayer must be init'); |
| 55 | + } |
63 | 56 | } |
64 | | - |
65 | | - |
| 57 | + } else { |
| 58 | + console.error('wrong current player'); |
| 59 | + } |
66 | 60 |
|
67 | 61 | }; |
68 | 62 |
|
69 | | - |
70 | 63 | schema.set('versionKey', false); |
71 | 64 | module.exports = mongoose.model('Game', schema); |
72 | 65 |
|
0 commit comments