This repository has been archived by the owner on Oct 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FriendsList.as
270 lines (206 loc) · 6.35 KB
/
FriendsList.as
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package {
import flash.display.MovieClip;
import flash.display.*;
import br.com.stimuli.loading.BulkLoader;
import br.com.stimuli.loading.BulkProgressEvent;
import flash.events.*;
import flash.display.Bitmap;
import flash.display.Loader;
import flash.net.*;
import flash.display.DisplayObject;
import flash.external.ExternalInterface;
public class FriendsList extends MovieClip {
public var bulkLoader : BulkLoader;
private var jsonObj:Object;
private var myLoader:Loader;
private var picturesArr:Array;
private var db:DbAccess;
private var currentPage:int=1;
private var pageCount:int=1;
private var friendsCount:int=1;
private const friendsPerPage:int=5;
// for offline play
private var offline:Boolean=true;
public function FriendsList() {
if (offline){
this.visible=false;
this.enabled=false;
return;
}
forwardButton.addEventListener( MouseEvent.CLICK, goForward);
backButton.addEventListener( MouseEvent.CLICK, goBack);
// getting data from javascript
//ExternalInterface.call("getFriendCount");
if (!bulkLoader) loadData();
}
private function loadData() {
setPageCount();
if (!db){
trace("loadData");
db=new DbAccess();
db.addEventListener( GameEvent.GET_FRIENDS_LIST, loadImages);
}
db.getFriendsList(currentPage,friendsPerPage );
}
public function loadImages(evt:GameEvent) {
try {
//bulkLoader = new BulkLoader("friends_list");
//bulkLoader.logLevel = BulkLoader.LOG_INFO;
trace("starting loadImages");
jsonObj= evt.gameData[0];
//get friend count param and remove it
trace("friend_count is "+ jsonObj['friend_count']);
if (jsonObj['friend_count'])
friendsCount=Number(jsonObj['friend_count']);
setPageCount();
//friendsCount=Number(jsonObj.pop() );
delete jsonObj['friend_count'];
var friendInfo:Object;
var friendObj:FriendScore;
var friendIndex:int=0;
//clear prevoius page pictures
for (var a:int=0;a<friendsPerPage;a++){
friendObj=this.getChildByName("friend"+a) as FriendScore;
friendObj.resetFriendField();
}
friendIndex=0;
for(var i in jsonObj){
//if (friendIndex>friendsPerPage) break;
friendInfo=jsonObj[i];
friendObj=this.getChildByName("friend"+friendIndex) as FriendScore;
friendObj.setupFriend(friendInfo['uid'],friendInfo['img'],friendInfo['level'],Number(friendInfo['experience']));
friendIndex=friendIndex+1;
}
/*
bulkLoader.addEventListener(BulkLoader.COMPLETE, onAllItemsLoaded);
bulkLoader.addEventListener(BulkLoader.PROGRESS, onAllItemsProgress);
bulkLoader.start();
*/
}
catch (e:Error) {
trace("loadImages error "+e.message);
}
}
private function loadProgress(event:ProgressEvent):void
{
var percentLoaded:Number = Math.round((event.bytesLoaded/event.bytesTotal) * 100);
trace("Loading: "+percentLoaded+"%");
}
public function onAllItemsLoaded(evt : Event) : void{
try {
trace("all items loaded" );
//TODO: labaaks veids kaa ielaadeet bildes
var picObj:Object;
var picBmp:Bitmap;
var tmpMov: FriendScore ;
for(var i in jsonObj){
picObj=jsonObj[i];
picBmp=bulkLoader.getBitmap(picObj['img']);
tmpMov= this.getChildByName("friend"+i) as FriendScore;
trace("player level is "+picObj['level']+" exp "+picObj['experience']);
}
}
catch (e:Error) {
trace("onAllItemsLoaded error "+e.message);
}
}
public function onAllItemsProgress(evt : BulkProgressEvent) : void{
//trace(evt.loadingStatus());
}
public function getImagesBulk(){
bulkLoader=BulkLoader.getLoader("friends_list");
if (!bulkLoader) {
loadFriendsImagesBulk();
return;
}
// if exists load from cache
else {
trace(bulkLoader.items.length);
}
}
public function loadFriendsImagesBulk(){
var db:DbAccess=new DbAccess();
jsonObj=db.getFriendsList(currentPage,friendsPerPage);
bulkLoader = new BulkLoader("friends_list");
//bulkLoader.logLevel = BulkLoader.LOG_INFO;
var a:Object;
for(var i in jsonObj){
//trace(jsonObj[i]);
a=jsonObj[i];
//trace(a['img']);
bulkLoader.add(a['img']);
}
bulkLoader.addEventListener(BulkLoader.COMPLETE, onAllItemsLoaded);
bulkLoader.addEventListener(BulkLoader.PROGRESS, onAllItemsProgress);
bulkLoader.start();
}
private function loadComplete(event:Event):void
{
try{
var bmp:Bitmap=Bitmap(myLoader.content);
addChild(bmp);
//bmp.y=this.numChildren*20;
//loader=null;
}
catch(err:Error){
trace("loadComplete error "+err.message );
}
}
public function getAndLoadImages(){
var db:DbAccess=new DbAccess();
jsonObj=db.testgetFriendsList("");
var a:Object;
for(var i in jsonObj){
//trace(jsonObj[i]);
//FIXME: for testing
//if (i==10226) return;
a=jsonObj[i];
trace(a['img']);
loadImage(a['img']);
}
}
public function loadImage(url:String){
trace("start loading image "+url );
var urlObj:URLRequest = new URLRequest(url);
myLoader= new Loader();
myLoader.load(urlObj);
trace("start loading pictures");
myLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, loadProgress);
myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete);
myLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, handleError);
}
private function setPageCount(){
try {
pageCount=Math.ceil(friendsCount/friendsPerPage);
trace("page count is-" + pageCount);
}
catch(err:Error){
trace("loadComplete error "+err.message );
}
}
private function getFriendCountFromJs(count){
trace("count is "+count);
friendsCount=Number(count);
if (!friendsCount || friendsCount<1 ) {
pageCount=1;
return;
}
pageCount=Math.ceil(friendsCount/friendsPerPage);
}
public function handleError(event:IOErrorEvent):void{
trace(event.target);
}
private function goBack(evt:MouseEvent){
if (currentPage==1) return;
trace("go back");
currentPage=currentPage-1;
loadData();
}
private function goForward(evt:MouseEvent){
if (pageCount==currentPage) return;
trace("go Forward");
currentPage=currentPage+1;
loadData();
}
}
}