-
Notifications
You must be signed in to change notification settings - Fork 1
/
v-ui-handler.js
167 lines (144 loc) · 4.88 KB
/
v-ui-handler.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
class UiHandler
{
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Setup
constructor(container_player_cards, container_alerts, container_stats, players_local, players_remote)
{
// Ui elements
this.container_player_cards_ = container_player_cards;
this.player_cards_ = [];
this.container_stats_ = container_stats;
this.container_alerts_ = container_alerts;
// Information
this.players_local_ = players_local;
this.players_remote_ = players_remote;
// Stats
this.win_count_ = document.createElement("p");
this.units_traveled_ = document.createElement("p");
this.win_count_.className = "truncate";
this.units_traveled_.className = "truncate";
this.container_stats_.appendChild(this.win_count_);
this.container_stats_.appendChild(this.units_traveled_);
// Generate player cards
this.generatePlayerCards();
}
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Methods
generatePlayerCards()
{
for (let index = 0; index < 4; index++)
{
// Create nodes
let node_player_card = document.createElement('div');
let node_id = document.createElement('div');
let node_connection = document.createElement('div');
let node_name = document.createElement('div');
// Class nodes
node_player_card.className = 'playercard';
node_id.className = 'id truncate';
node_connection.className = 'connection truncate';
node_name.className = 'name truncate';
// Fill nodes
node_id.appendChild(document.createTextNode('Player ' + (index + 1) + ' *'));
node_connection.appendChild(document.createTextNode('Disconnected'));
node_name.appendChild(document.createTextNode('---'));
// Append nodes
node_player_card.appendChild(node_id);
node_player_card.appendChild(node_connection);
node_player_card.appendChild(node_name);
// Store and display card
this.player_cards_.push(node_player_card);
this.container_player_cards_.appendChild(node_player_card);
}
}
resetPlayerCards()
{
let index = 1;
this.player_cards_.forEach(function(player_card)
{
// Fill nodes
player_card.children[0].textContent = 'Player ' + index + ' *';
player_card.children[1].textContent = 'Disconnected';
player_card.children[2].textContent = '---';
player_card.style.borderColor = '#555';
index++;
});
}
updatePlayerCards()
{
// Update local player
this.updatePlayerCard(this.players_local_[0].id_);
// Update remote players
let event_target = this;
this.players_remote_.forEach(function(player_remote)
{
event_target.updatePlayerCard(player_remote.id_);
}
);
}
updatePlayerCard(player_id)
{
if(this.players_local_[0].id_ == player_id)
{
let player_card = this.player_cards_[player_id - 1];
player_card.children[1].textContent = 'Local';
player_card.style.borderColor = this.players_local_[0].color_;
}
else
{
let event_target = this;
this.players_remote_.forEach(function(player_remote)
{
if(player_remote.id_ == player_id)
{
let player_card = event_target.player_cards_[player_id - 1];
player_card.children[1].textContent = 'Connected';
player_card.style.borderColor = player_remote.color_;
}
}
);
}
}
updateStats(win_count, units_traveled)
{
this.win_count_.textContent = "Win count: " + win_count;
this.units_traveled_.textContent = "Traveled: " + Math.round(units_traveled) + "km";
}
updateWallUsed(playerID)
{
this.player_cards_[playerID - 1].children[0].textContent = 'Player ' + playerID;
}
generateAlert(text_title, text_content, self_close_after=10)
{
// Create nodes
let node_alert = document.createElement('div');
let node_title = document.createElement('span');
let node_text = document.createElement('span');
// Class nodes
node_alert.className = 'alert';
node_title.className = 'title';
node_text.className = 'text';
// Fill nodes
node_alert.appendChild(node_title);
node_alert.appendChild(node_text);
node_title.appendChild(document.createTextNode(text_title));
node_text.appendChild(document.createTextNode(text_content));
// Add node to container
this.container_alerts_.appendChild(node_alert);
// Close alert
if(self_close_after)
{
setInterval(
function()
{
node_alert.remove();
},
self_close_after * 1000
);
}
}
resetAlerts()
{
this.container_alerts_.innerHTML = "";
}
}