-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsavegames.js~
222 lines (189 loc) · 5.01 KB
/
savegames.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// num_buildings
// voting_cards
// max_steps
// load games from menu
// init_game .. :P
remoteStorage.defineModule("roadriverhedge",
function(privateClient, publicClient){
privateClient.declareType('savegame', {
'description' : 'a savegame you might want to load later',
'type' : 'object',
'properties' : {
'name' : {
'type' : 'string',
},
'map' : {
'type' : 'object'
},
'troops' : {
'type' : 'object'
},
'players' : {
'type' : 'object'
},
'config' : {
'type' : 'object'
}
}
});
return {
'exports' : {
'save' : function(name, data) {
return privateClient.storeObject('savegame', "savegames/"+name, {
'map' : data['map'],
'troops' : data['troops'],
'players' : data['players'],
'config' : data['config']
});
},
'load' : function(name) {
return privateClient.getObject("savegames/"+name);
},
'list_games' : function() {
return privateClient.getListing("savegames/");
}
}
}
}
)
function init_remoteStorage(){
remoteStorage.claimAccess({'roadriverhedge': 'rw'}).then(function(){
remoteStorage.displayWidget();
});
}
function save_game(name){
function preprocess_map_for_saving(){
for(var i = 0; i < map.length; i++){
if(map[i].state != null && map[i].state != types[types.length-1]){
console.log(map[i]);
map[i].orientation = map[i].div()
.getElementsByClassName("way")[0].
src.match(/_(\d)_(\d)/).slice(1);
}
}
return map;
}
data = {
'map' : preprocess_map_for_saving(),
'players' : players,
'troops' : troops,
'config' : {
'max_x' : max_x,
'max_y' : max_y,
'num_buildings' : num_buildings,
'voting_cards' : voting_cards,
'voting_timeout' : voting_timeout,
'max_steps' : max_steps,
'players_total' : players_total,
'current_player' : current_player,
'interface_mode' : interface_mode,
'phase' : phase
}
}
return remoteStorage.roadriverhedge.save(name, data)
}
function reset_game(data){
console.log(data);
config = data['config'];
//ugly setting config should happen at one place but init_game depends on right interface mode(proably make interface changeable ingame)
interface_mode = config['interface_mode'];
if(phase == 'out of game'){
init_game();
}
max_x = config['max_x'];
max_y = config['max_y'];
phase = config['phase'];
players_total = config['players_total'];
current_player = config['current_player'];
num_buildings = config['num_buildings'];
voting_cards = config['voting_cards'];
voting_timeout = config['voting_timeout'];
max_steps = config['max_steps'];
map = data['map'];
players = data['players'];
troops = data['troops'];
redraw_map();
reinit_players();
reinit_troopers();
}
function load_game(name){
remoteStorage.roadriverhedge.load(name).then(
reset_game
);
}
function reinit_troopers(){
troop_c = 0;
for(var i = 0; i < 4; i++){
t = troops[i];
troops[i] = new Troop(t.type);
// troops[i].x = t.x; done by
// troops[i].y = t.y; move:)
troops[i].move(t.x,t.y);
}
}
function reinit_players(){
pannels = document.getElementsByClassName("players_pannel");
while(pannels.length > 1){
pannels[1].remove();
}
for(var i = 0; i < players_total; i++){
p = players[i]
players[i] = new Player(p.num, p.name, p.building);
if(p.role!="human"){
players[i].ai = new Ai(players[i]);
}
players[i].walls = p.walls;
players[i].rivers = p.rivers;
players[i].streets = p.streets;
players[i].votes = p.votes;
players[i].out_of_game = p.out_of_game;
players[i].redraw();
}
the_player().div().className = "players_pannel active";
}
function redraw_map(){
// TODO drawing hedges in the right shape but this is usable now
var fields = document.getElementsByClassName('field');
while(fields.length > 1){
fields[1].remove();
}
for(var i = 0; i < max_x*max_y; i++){
f = map[i];
map[i] = new Field(Math.floor(i/max_x),i % max_x);
if( f.building ){
map[i].building = f.building;
map[i].type = 'building';
map[i].value = f.value;
if(f.building!="starting_point"){
map[i].add(_building(f.building, f.value));
}
}
if(f.state){
map[i].state = f.state;
if(f.state != types[types.length-1]){
map[i].build(f.state,
Number(f.orientation[0]),
Number(f.orientation[1])
);
}
}
}
}
function savegames_list(){
function create_list_element(name){
function handle_savegame_onclick(e){
document.all.savegame_name.value = e.toElement.innerHTML;
}
var li = document.createElement("li");
li.className = "savegame_name";
li.innerHTML = name;
li.addEventListener("click", handle_savegame_onclick);
document.all.savegames.appendChild(li);
}
remoteStorage.roadriverhedge.list_games().then(function(games_list){
for(var k in games_list){
console.log(" " + k + " : " + games_list[k] );
create_list_element(games_list[k]);
}
});
}