-
Notifications
You must be signed in to change notification settings - Fork 2
/
xp.inl
1346 lines (1137 loc) · 32.8 KB
/
xp.inl
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
// Use this to create the table in MySQL
CREATE TABLE IF NOT EXISTS `pm_save` (
`SAVE_KEY` varchar(34) binary NOT NULL default '',
`XPS` varchar(255) binary NOT NULL default '',
`LAST_PLAY_DATE` timestamp(14) NOT NULL,
PRIMARY KEY (`SAVE_KEY`)
) TYPE=MyISAM COMMENT='PokeMod Saving Table';
// now it should work fine
//if using MySQL and are updating from v1.2.0 Beta J2 or lower, run these queries
ALTER TABLE `pm_save` ADD `LAST_PLAY_DATE` timestamp(14) NOT NULL;
ALTER TABLE `pm_save` MODIFY `XPS` varchar(255) binary NOT NULL default '';
//if using MySQL and are updating from v1.2.0 Beta K2 or lower, run this query
UPDATE `pm_save` SET `LAST_PLAY_DATE`=SYSDATE()
*/
// This makes sure theres no gaps in PlayersPoke
// Example:
// From: 5 390 0 0 10 200 16 1000
// To: 5 390 10 200 16 1000 0 0
public RefreshPoke(id)
{
if(Safari() || !id || !Pokemod_Connected(id))
return
new PlaceHolder[3][MAX_POKES]
new i, p = 0;
for(i = 0; i<MAX_ACTIVE_POKES; i++){
if( ValidPoke( PlayersPoke[id][i] ) ){
PlaceHolder[0][p] = PlayersPoke[id][i];
PlaceHolder[1][p] = PlayersXP[id][i];
PlaceHolder[2][p] = PlayersLevels[id][i];
p++;
}
}
for(i = p; i<MAX_ACTIVE_POKES; i++){
PlayersPoke[id][i] = 0;
PlayersXP[id][i] = 0;
PlayersLevels[id][i] = 0;
}
p = MAX_ACTIVE_POKES;
for(i = MAX_ACTIVE_POKES; i<MAX_POKES; i++){
if( ValidPoke( PlayersPoke[id][i] ) ){
PlaceHolder[0][p] = PlayersPoke[id][i];
PlaceHolder[1][p] = PlayersXP[id][i];
PlaceHolder[2][p] = PlayersLevels[id][i];
p++;
}
}
for(i = p; i<MAX_POKES; i++){
PlayersPoke[id][i] = 0;
PlayersXP[id][i] = 0;
PlayersLevels[id][i] = 0;
}
for(i = 0; i<MAX_POKES; i++){
PlayersPoke[id][i] = PlaceHolder[0][i];
PlayersXP[id][i] = PlaceHolder[1][i];
PlayersLevels[id][i] = PlaceHolder[2][i];
}
if(!SwitchPoke(id, Pokemon[id])){
for(i=0; i<MAX_ACTIVE_POKES; i++){
if(SwitchPoke(id, PlayersPoke[id][i]))
break;
}
}
}
///////////////////////////////////////////
// For MYSQL, find the database to use //
///////////////////////////////////////////
#if SAVE==SAVE_MYSQL
public adminCopyVault(id)
{
if (!(get_user_flags(id)&ADMIN_RCON ) && id!=0) return PLUGIN_HANDLED
mySQLConnect()
if ( gMySQL <= SQL_FAILED ) return PLUGIN_HANDLED
//Turn Persistant on for this function
gPersistentTemp = true
// Open up the vault file - read line by line
if ( !file_exists(g_VaultFile) ) return PLUGIN_HANDLED
console_print(id,"Please wait while the XP data is copied")
// Read through the file looking for shinfo entries
new parm[2]
parm[0] = id
parm[1] = 0
adminCopyVHelper(parm)
//Rest of this is done in the helper function
return PLUGIN_HANDLED
}
public adminCopyVHelper(parm[])
{
//Get data from parm
new id = parm[0]
new nextLine = parm[1]
// Read through the file looking for shinfo entries
new lengthRead
new data[512], Right[401], Left[111]
while ((nextLine = read_file(g_VaultFile,nextLine,data,511,lengthRead)) != 0) {
if ( lengthRead > 0 && equali(data, "PokeMod", 7) ) {
strbreak(data, Left, 110, Right, 400)
replace(Left,110,"PokeMod-",EMPTY)
replace_all(Left,34,"`","\\`")
replace_all(Left,34,"'","\\'")
writeData(0, Left, Right)
if (nextLine % 200 == 0) {
parm[1] = nextLine
console_print(id,"Delaying a few seconds before copying the next set of entries")
set_task(3.0, "adminCopyVHelper",0,parm,2)
return
}
}
}
//If it makes it this far it must be done copying
// OK now load the XP for any players playing
load_all();
console_print(id,"Saved XP has been copied from the vault to the MySQL Database Successfully")
//Set Persistent back how it was
gPersistentTemp = false
//Closes the connection if set not to be persistent
close_mysql()
}
public saving_init()
{
register_pokeadmincmd("vault_to_sql","adminCopyVault", ADMIN_RCON,"- Copies vault XP to MySQL, should only be done once.")
// Verify the type of DBI module that is loaded is supported
dbi_type(SQLtype, 15);
if (equali(SQLtype, g_MySQL))
{
SQL_TYPE = SQL_MYSQL;
}
else if (equali(SQLtype, g_SQLite))
{
SQL_TYPE = SQL_SQLITE;
}
else
{
SQL_TYPE = SQL_NONE;
debugMessage(_,_,"XP", "Unsupported database type found (%s), the supported databases are %s or %s", SQLtype, g_MySQL, g_SQLite);
return;
}
debugMessage( 0,7,"XP", "MYSQL registering done" )
}
public mySQLConnect()
{
gPersistent = get_ppcvar_num(pm_mysql_persistent)
if ( gMySQL >= SQL_OK ) {
if (!gPersistent && !gPersistentTemp) close_mysql()
else return
}
new host[64],user[32],pass[32],db[32],error[128]
get_ppcvar_string(pm_mysql_host,host,63)
get_ppcvar_string(pm_mysql_user,user,31)
get_ppcvar_string(pm_mysql_pass,pass,31)
get_ppcvar_string(pm_mysql_db,db,31)
if ( !strlen(host) && !strlen(user) && !strlen(db) ) {
get_cvar_string("amx_mysql_host",host,63)
get_cvar_string("amx_mysql_user",user,31)
get_cvar_string("amx_mysql_pass",pass,31)
get_cvar_string("amx_mysql_db",db,31)
}
// Set a default DB if it's SQLite and the user didn't supply one
if ( SQL_TYPE == SQL_SQLITE && strlen(db) < 1 )
copy( db, 127, "addons/amxmodx/data/amxx.db" );
gMySQL = dbi_connect(host,user,pass,db,error,127)
if (gMySQL <= SQL_FAILED) {
debugMessage(0,0,"XP", "MySQL connect error: '%s' (%s,%s,%s)",error,host,user,db)
return
}
//Only try to create the tables once
else if (CreateTables) {
new sql[512]
new Result:retval
if ( SQL_TYPE == SQL_SQLITE ){
if ( !sqlite_table_exists( gMySQL, "pm_save" ) ){
copy(sql, 511, "CREATE TABLE `pm_save` ( `SAVE_KEY` varchar(34) binary NOT NULL default '', `XPS` varchar(255) binary NOT NULL default '', `LAST_PLAY_DATE` timestamp(14) NOT NULL, PRIMARY KEY (`SAVE_KEY`) ) TYPE=MyISAM COMMENT='PokeMod Saving Table'")
retval = dbi_query(gMySQL,sql)
if (retval <= RESULT_FAILED) {
dbi_error(gMySQL,error,127)
debugMessage(0,0,"XP", "Error Making Tables: '%s' - '%s'", error, sql)
return
}
if ( SQL_TYPE == SQL_SQLITE )
{
new IntegrityCheck[64];
copy(sql, 511, "PRAGMA integrity_check");
new Result:res = dbi_query(gMySQL, sql);
// Check for an error
if ( res < RESULT_NONE )
{
dbi_error(gMySQL,error,127)
debugMessage(0,0,"XP", "Error Making Tables: '%s' - '%s'", error, sql)
return;
}
// Get the integrity check value
while ( res && dbi_nextrow(res) > 0 )
{
dbi_result(res, "integrity_check", IntegrityCheck, 63);
}
// Free the result
dbi_free_result(res);
// Check to make sure the integrity check passed
if ( !equali(IntegrityCheck, "ok") )
{
// Should we disable saving here?
debugMessage(_,_,"XP", "SQL Lite integrity check failed");
return;
}
// Do some synchronous crap
format(sql, 511, "PRAGMA synchronous = %d", SQLITE_SYNC_OFF);
dbi_query(gMySQL, sql);
}
}
}
else {
copy(sql, 511, "CREATE TABLE IF NOT EXISTS `pm_save` ( `SAVE_KEY` varchar(34) binary NOT NULL default '', `XPS` varchar(255) binary NOT NULL default '', `LAST_PLAY_DATE` timestamp(14) NOT NULL, PRIMARY KEY (`SAVE_KEY`) ) TYPE=MyISAM COMMENT='PokeMod Saving Table'")
retval = dbi_query(gMySQL,sql)
if (retval <= RESULT_FAILED) {
dbi_error(gMySQL,error,127)
debugMessage(0,0,"XP", "Error Making Tables: '%s' - '%s'", error, sql)
return
}
}
CreateTables = false
}
}
writeData(x, key[], xps_to_save[])
{
mySQLConnect()
if ( gMySQL <= SQL_FAILED ) return
debugMessage( x, 8,"XP", "Trying to save XP data: '%s' - '%s'", key, xps_to_save)
new error[128],sql[512]
new Result:retval, Result:retvalins
//Thanks to HC for help with writing more efficient queries
//Check if this user has an entry already, if not make one
formatex(sql, 511, "SELECT * FROM `pm_save` WHERE `SAVE_KEY` = '%s'", key)
retval = dbi_query(gMySQL,sql)
if (retval <= RESULT_FAILED) {
dbi_error(gMySQL,error,127)
debugMessage(0,0,"XP", "Error Querying MySQL DB for %s: '%s' - '%s'", key, error, sql)
return
}
else if (!dbi_nextrow(retval)) {
formatex(sql, 511, "INSERT INTO `pm_save` (SAVE_KEY) VALUES ('%s')", key )
retvalins = dbi_query(gMySQL,sql)
if (retvalins <= RESULT_FAILED) {
dbi_error(gMySQL,error,127)
debugMessage(0,0,"XP", "Error Writing MySQL XP for %s: '%s' - '%s'", key, error, sql)
return
}
}
if (retval >= RESULT_OK) dbi_free_result(retval)
//Update users entry with current data
formatex(sql, 511, "UPDATE `pm_save` SET `XPS`='%s' WHERE (SAVE_KEY='%s')",xps_to_save,key)
retval = dbi_query(gMySQL,sql)
if (retval <= RESULT_FAILED) {
dbi_error(gMySQL,error,127)
debugMessage(0,0,"XP", "Error Writing MySQL XP for %s: '%s' - '%s'", key, error, sql)
return
}
//Closes the connection if set not to be persistent
close_mysql()
}
public close_mysql()
{
if (gMySQL <= SQL_FAILED || gPersistent || gPersistentTemp) return
dbi_close(gMySQL)
}
public saving_end()
{
if ( gMySQL <= SQL_FAILED ) return
dbi_close(gMySQL)
}
#endif
#if SAVE==SAVE_VAULT
writeData(x, key[], xps_to_save[])
{
debugMessage(x, 8,"XP", "Trying to save XP data: '%s' - '%s'", key, xps_to_save)
new vaultSaveKey[40]
//Make the key to save to
formatex(vaultSaveKey,39,"PokeMod-%s",key)
set_vaultdata(vaultSaveKey, xps_to_save)
}
#endif
#if SAVE==SAVE_NVAULT
public saving_init()
{
register_pokeadmincmd("vault_to_nvault","adminCopyVault", ADMIN_RCON,"- Copies vault XP to NVault, should only be done once.")
nvault_db = nvault_open("pm_save")
debugMessage( 0,7,"XP", "NVault registering done" )
}
writeData(x, key[], xps_to_save[])
{
debugMessage( x, 8,"XP", "Trying to save XP data: '%s' - '%s'", key, xps_to_save)
new vaultSaveKey[40]
//Make the key to save to
formatex(vaultSaveKey,39,"PokeMod-%s",key)
nvault_set(nvault_db, key, xps_to_save)
}
public adminCopyVault(id)
{
if (!(get_user_flags(id)&ADMIN_RCON ) && id!=0) return PLUGIN_HANDLED
// Open up the vault file - read line by line
if ( !file_exists(g_VaultFile) ) return PLUGIN_HANDLED
console_print(id,"Please wait while the XP data is copied")
// Read through the file looking for shinfo entries
new parm[2]
parm[0] = id
parm[1] = 0
adminCopyVHelper(parm)
//Rest of this is done in the helper function
return PLUGIN_HANDLED
}
public adminCopyVHelper(parm[])
{
//Get data from parm
new id = parm[0]
new nextLine = parm[1]
// Read through the file looking for shinfo entries
new lengthRead
new data[512], Right[401], Left[111]
while ((nextLine = read_file(g_VaultFile,nextLine,data,511,lengthRead)) != 0) {
if ( lengthRead > 0 && equali(data, "PokeMod", 7) ) {
strbreak(data, Left, 110, Right, 400)
replace(Left,110,"PokeMod-",EMPTY)
writeData(0, Left, Right)
if (nextLine % 200 == 0) {
parm[1] = nextLine
console_print(id,"Delaying a few seconds before copying the next set of entries")
set_task(3.0, "adminCopyVHelper",0,parm,2)
return
}
}
}
//If it makes it this far it must be done copying
// OK now load the XP for any players playing
load_all();
console_print(id,"Saved XP has been copied from the vault to the NVault Database Successfully")
}
#endif
public LoadXP(id)
{
if( !PM_Loaded ){
Pokemod_Connect(id,0);
return false;
}
if( g_isWild[id] > 0 )
return true
if( !SaveXPon() )
return true
new key[35]
if( getSaveKey(id, key, true) )
{
#if SAVE==SAVE_NVAULT
new Right[1501]
new vaultSaveKey[40], time
formatex(vaultSaveKey,39,"PokeMod-%s",key)
nvault_lookup(nvault_db, key, Right, 1500, time)
#endif
#if SAVE==SAVE_VAULT
new Right[1501]
new vaultSaveKey[40]
formatex(vaultSaveKey,39,"PokeMod-%s",key)
get_vaultdata(vaultSaveKey, Right, 1500)
#endif
#if SAVE==SAVE_MYSQL
new Right[1501]
new sql[512], error[128]
new Result:retvalxp
mySQLConnect()
if ( gMySQL <= SQL_FAILED ) return false
formatex(sql, 511, "SELECT `XPS` FROM `pm_save` WHERE `SAVE_KEY` = '%s'", key)
retvalxp = dbi_query(gMySQL,sql)
if(retvalxp <= RESULT_FAILED){
dbi_error(gMySQL,error,127)
debugMessage(id,0,"XP", "Error Querying MySQL DB for %s: '%s' - '%s'", key, error, sql)
//Closes the connection if set not to be persistent
close_mysql()
return false
}
else if (!dbi_nextrow(retvalxp)) {
dbi_error(gMySQL,error,127)
debugMessage(id,3,"XP", "No Saved XP Loaded for %s: '%s'", key, error)
if (retvalxp >= RESULT_OK) dbi_free_result(retvalxp)
//Closes the connection if set not to be persistent
close_mysql()
Pokemod_Connect(id,-1);
return true
}
dbi_field(retvalxp, 1, Right, 500)
#endif
return HandleLoad(id,Right)
}
Pokemod_Connect(id,0);
return false;
}
HandleLoad(id, Right[1501])
{
debugMessage(id,8,"XP", "loading %s",Right );
//we don't have any saved data
if(equali(Right,EMPTY))
return NewPlayerSettings(id);
new Left[51], i;
for( i = 0; i<MAX_POKES; i++)
{
strbreak(Right, Left, 50, Right, 1500)
if( equal(Left, COLON, 1) ) break //stop if this is the end of the data we want to look at
PlayersPoke[id][i] = str_to_num(Left)
strbreak(Right, Left, 50, Right, 1500)
if( equal(Left, COLON, 1) ) break //stop if this is the end of the data we want to look at
PlayersXP[id][i] = str_to_num(Left)
PlayersLevel(id, i, true);
}
while( !equal(Left, COLON, 1) && !equali(Right,EMPTY)){ //if we didn't stop because we found a :, lets get the next one
strbreak(Right, Left, 50, Right, 1500)
}
//we ran out of data
if(equali(Right,EMPTY))
return NewPlayerSettings(id, true);
strbreak(Right, Left, 50, Right, 1500)
if( !equal(Left, COLON, 1) ){ //we found the time, and not the next seperator
PlayersTime[id] = str_to_num(Left)
}
while( !equal(Left, COLON, 1) && !equali(Right,EMPTY)) //if we didn't stop because we found a :, lets get the next one
strbreak(Right, Left, 50, Right, 1500)
//strbreak(Right, Left, 50, Right, 1500)
if( equal(Left, COLON, 1) ){ //we found the seperator, now this will break it down in here
for( i = 0; i<MAX_SETS; i++)
{
strbreak(Right, Left, 50, Right, 1500)
if( equal(Left, COLON, 1) ) break //stop if this is the end of the data we want to look at
PlayersSet[id][i] = str_to_num(Left)
}
}
while( !equal(Left, COLON, 1) && !equali(Right,EMPTY)) //if we didn't stop because we found a :, lets get the next one
strbreak(Right, Left, 50, Right, 1500)
strbreak(Right, Left, 50, Right, 1500)
if( !equal(Left, COLON, 1) ){ //we found their catches
PlayersCatch[id] = str_to_num(Left)
}
if( is_user_bot(id) )
BotSelect(id);
return ConnectPlayer(id)
}
NewPlayerSettings(id, bool:had_pokemon=false)
{
PlayersSet[id][SET_HUD_Y] = -4;
if( !had_pokemon )
Pokemod_Connect(id,-1);
else
return ConnectPlayer(id);
return true;
}
ConnectPlayer(id)
{
Pokemod_Connect(id,1);
RefreshPoke(id)
return true
}
public load_all()
{
for( new x=0; x < MAX_PLAYERS; x++ )
LoadXP(x);
#if POKERANK_SYSTEM==1
LoadRanks()
#endif
}
public save_all()
{
if( !PM_Loaded )
return;
poke_print(0, print_center, "%s Saving All Data",PREFIX);
new players[MAX_PLAYERS], pnum;
poke_get_players(players, pnum);
for( new i=0; i<pnum; i++)
set_task( 1.5*(i+1), "delayed_save", players[i]);
#if POKERANK_SYSTEM==1
SaveRanks();
#endif
}
public delayed_save(id)
SaveXP(id, -1);
SaveXP(id, forced=1)
{
if(g_isWild[id]>0)
return false;
static key[35]
static Float:last_save[MAX_PLAYERS+1], Float:time_left, Float:current_time;
if( id ){
current_time = get_gametime();
time_left = current_time - last_save[id];
if( forced < 1 && time_left < SAVE_SPAM){
if( !forced )
poke_printlang(id,"POKEMOD_SAVESPAM", SAVE_SPAM-time_left);
return false;
}
last_save[id] = current_time;
}
if( Pokemod_Connected(id) && getSaveKey(id, key) && SaveXPon() )
{
RefreshPoke(id)
new Data[1501], iLen = 0, i
//save their pokemon and xp
for(i = 0; i<MAX_POKES; i++)
iLen += formatex(Data[iLen],(1501-iLen),"%d %d ", PlayersPoke[id][i], PlayersXP[id][i])
//save the time
iLen += copy(Data[iLen],(1501-iLen),": ")
iLen += formatex(Data[iLen],(1501-iLen),"%d ",get_systime())
iLen += copy(Data[iLen],(1501-iLen),": ")
//save their settings
for(i = 0; i<MAX_SETS; i++)
iLen += formatex(Data[iLen],(1501-iLen),"%d ", PlayersSet[id][i])
//save their pokerank crap
iLen += formatex(Data[iLen],(1501-iLen),": %d",PlayersCatch[id])
writeData(id, key, Data)
debugMessage(id,5,"XP", "saved data with save key %s", key)
return true;
}
return false;
}
#if SAVE_ID != 0
public client_infochanged(id)
{
new savekey[35], newname[NAME_LENGTH];
getSaveKey(id, savekey);
get_user_info(id, "name", newname, NAME_LENGTH-1);
if( !equal(savekey, newname) ){
SaveXP(id);
LoadXP(id);
}
}
#endif
//thanks to SHmod
getSaveKey(id, key[35]="", bool:update=false)
{
static player_key[MAX_PLAYERS+1][35];
if( update ) {
#if SAVE_ID == 0
if(is_user_bot(id)) {
new botname[32]
get_user_name(id,botname,31)
//Get Rid of BOT Tag
//PODBot
replace(botname,31,"[POD]",EMPTY)
replace(botname,31,"[P*D]",EMPTY)
replace(botname,31,"[P0D]",EMPTY)
//CZ Bots
replace(botname,31,"[BOT] ",EMPTY)
//Attempt to get rid of the skill tag so we save with bots true name
new lastchar = strlen(botname) - 1
if ( equal(botname[lastchar],")",1) ) {
new x
for ( x = lastchar - 1; x > 0; x--) {
if ( equal(botname[x],"(",1) ) {
botname[x - 1] = 0
break
}
if ( !isdigit(botname[x]) ) break
}
}
if (strlen(botname) > 0 ) {
#if SAVE==SAVE_MYSQL
replace_all(botname,31,"`","\\`")
replace_all(botname,31,"'","\\'")
#endif
replace_all(botname,31," ","_")
formatex( player_key[id], 34, "[BOT]%s", botname )
}
}
//Hack for STEAM's retardedness with listen servers
else if (!is_dedicated_server() && id == 1) {
copy( player_key[id], 34, "loopback" )
} else {
player_key[id]=g_szIdentifier[id]
}
#else
get_user_name( id, player_key[id], 34 )
#endif
debugMessage(id,5,"XP", "updated savekey");
}
//Check to make sure we got something useable
if( equali(player_key[id], "STEAM_ID_PENDING") || equali(player_key[id], EMPTY) ){
debugMessage(id,5,"XP", "does not have a valid savekey ('%s')", player_key[id]);
return false;
}
copy( key, 34, player_key[id] );
return true;
}
///////////////////////////
// Levels a pokemon up //
///////////////////////////
LevelUpPokemon(id, num, levels=1, take_item=MAX_ITEMS)
{
new pastlevel = PlayersLevel(id, num)
new pname[NAME_LENGTH]
PokeToName(PlayersPoke[id][num], pname, NAME_LENGTH)
if( take_item != MAX_ITEMS ){
if(pastlevel == TotalLevels){
poke_printlang(id,"POKEMOD_MAXLEVEL", pname)
return false
}
PlayersItem[id][take_item]--
}
new newlevel = pastlevel+levels
if(newlevel > TotalLevels)
newlevel = TotalLevels
PlayersXP[id][num] = PokeLevels[newlevel-1]
PlayersLevels[id][num] = newlevel;
poke_printlang(id,"POKEMOD_NEWLEVEL", pname,newlevel)
Check_Evolve(id, PlayersPoke[id][num], newlevel)
return true
}
///////////////////////////////////////
// Returns what level a players is //
///////////////////////////////////////
PlayersLevel(id, num=-1, bool:update=false )
{
if(0>id || id>MAX_PLAYERS)
return 0;
if(Safari())
return SafariLevel(Pokemon[id]);
if( num == -1 )
num = PlayersActive[id];
if( !ValidSlot(num) )
return 0;
static i;
if( update ){
i = 1;
if(g_isWild[id]<1){
while(PlayersXP[id][num]>=PokeLevels[i]){
i++;
if(i==TotalLevels)
break;
}
}
else{
while(PlayersXP[id][num]>=PokeLevels[i]/2){
i++;
if(i==TotalLevels)
break;
}
}
PlayersLevels[id][num] = i;
}
else i = PlayersLevels[id][num];
#if RARE_CANDY_MODE == 1 || RARE_CANDY_MODE == 3
static k;
for(k=0; k<ItemsCreated; k++)
if( ItemInfo[k] & II_LEVEL )
i += PlayersItem[id][k];
#endif
return i;
}
RefreshLevels(id)
{
for(new i=0; i<MAX_POKES; i++)
PlayersLevel(id, i, true);
}
/////////////////////////////////////////////
// Returns what players average level is //
/////////////////////////////////////////////
AverageLevel(id)
{
if(Safari())
return SafariLevel(Pokemon[id]);
if(g_isWild[id]>0)
return PlayersLevel(id, 0);
new pokes, total
for(new i = 0; i<MAX_ACTIVE_POKES; i++)
{
if( ValidPoke(PlayersPoke[id][i]) ){
pokes++;
total += PlayersLevel(id, i);
}
}
if(pokes){
new average = total/pokes;
return average;
}
return 0
}
/////////////////////////////////////
// Adds a pokemon to playerspoke //
/////////////////////////////////////
bool:AddPoke(id, pid, xp=1, giver=0, CHECK_POKES:checker=CHECK_ACTIVE)
{
RefreshPoke(id);
if(!ValidPoke(pid) || AvailableSlot(id, pid, checker)!=AS_OK || Safari())
return false;
new check_rcon = RCON_POKEADMINS;
if( check_rcon && giver && PokeInfo[pid] & PI_SPECIAL && !(get_user_flags(giver)&ADMIN_RCON) )
return false;
if(!xp)
xp = 1;
new i = Slot(id,_,checker);
PlayersPoke[id][i] = pid;
PlayersXP[id][i] = xp;
PlayersLevel(id, i, true);
if( i >= MAX_ACTIVE_POKES )
poke_printlang(id,"POKEMOD_PCADDSENT", PokeToName(pid) );
ShowPokeInfo(id, pid);
SaveXP(id);
return true;
}
////////////////
// Gives XP //
////////////////
GiveXP(id, poke, amount)
{
if(Safari())
return
if( !ValidSlot(poke) || !ValidPoke(PlayersPoke[id][poke]) )
return
new oldlevel, newlevel
oldlevel = PlayersLevel(id, poke)
PlayersXP[id][poke] += amount
if(PlayersXP[id][poke]<1)
PlayersXP[id][poke] = 1
newlevel = PlayersLevel(id, poke, true)
if(oldlevel<newlevel){
new oldmax = PlayerStat[id][STAT_MAXHP]
new name[32]
get_user_name(id, name, 31)
poke_printlang(LANG_PLAYER,"POKEMOD_LEVELUP", name,PokeToName(PlayersPoke[id][poke]),newlevel)
//ding for level up
poke_sound(id, CHAN_AUTO, SND_BELL)
if(Pokemod_Alive(id)){
get_user_origin(id, origin[id])
//sprites to show level up (only once)
message_begin(MSG_BROADCAST ,SVC_TEMPENTITY) //message begin
write_byte(TE_LARGEFUNNEL)
write_coord(origin[id][0]) // funnel position
write_coord(origin[id][1])
write_coord(origin[id][2]-32)
write_short(SPRITE_INDEX[SPR_MUZZLEFLASH1]) // sprite index
write_short(8) // flags
message_end()
}
Check_Evolve(id, PlayersPoke[id][poke], newlevel)
#if POKERANK_SYSTEM==1
PlayersCatch[id] += newlevel
CheckMaxHP(id)
if(oldmax!=PlayerStat[id][STAT_MAXHP]){
poke_printlang(id,"POKEMOD_NEWHPRANK", PlayerStat[id][STAT_MAXHP],PlayersCatch[id])
}
else
poke_printlang(id,"POKEMOD_NEWRANK", PlayersCatch[id])
#else
CheckMaxHP(id)
if(oldmax!=PlayerStat[id][STAT_MAXHP]){
poke_printlang(id,"POKEMOD_NEWHP", PlayerStat[id][STAT_MAXHP])
}
#endif
}
else if(oldlevel>newlevel){
new oldmax = PlayerStat[id][STAT_MAXHP]
new name[32]
get_user_name(id, name, 31)
poke_printlang(id,"POKEMOD_LEVELDOWN", name,PokeToName(PlayersPoke[id][poke]),newlevel)
#if POKERANK_SYSTEM==1
PlayersCatch[id] -= oldlevel
CheckMaxHP(id)
if(oldmax!=PlayerStat[id][STAT_MAXHP]){
poke_printlang(id,"POKEMOD_NEWHPRANKL", PlayerStat[id][STAT_MAXHP],PlayersCatch[id])
}
else
poke_printlang(id,"POKEMOD_NEWRANKL", PlayersCatch[id])
#else
CheckMaxHP(id)
if(oldmax!=PlayerStat[id][STAT_MAXHP]){
poke_printlang(id,"POKEMOD_NEWHPL", PlayerStat[id][STAT_MAXHP])
}
#endif
#if POKEMON_UNEVOLVE==1
Check_UnEvolve(id, PlayersPoke[id][poke])
#endif
}
#if POKERANK_SYSTEM==1
if(oldlevel != newlevel){
if(SpecialRank(id)!=-1) //update their thing, and check if they went higher
UpdateSpecialRank(id)
else
CheckUpdateRank(id) //only check if they went higher than the last person in Elite Four
}
#endif
}
/////////////////////
// Gives Kill XP //
/////////////////////
KillXP(attacker, victim)
{
if(attacker==victim)
return
new xp_to_give = PlayersLevel(victim)
if( Pokemod_FF(victim, attacker) ){
GiveXP(attacker, PlayersActive[attacker], -1*xp_to_give)
poke_printlang(attacker,"POKEMOD_KILLMATE", xp_to_give)
}
else
GiveXP(attacker, PlayersActive[attacker], xp_to_give)
}
/////////////////////////
// Adds level to num //
/////////////////////////
AddLvl(id, num)
{
num += PlayersLevel(id);
return true;
}
///////////////////////////
// Adds level to float //
///////////////////////////
f_AddLvl(id, Float:num)
{
num += PlayersLevel(id);
return true;
}
////////////////////////////////
// Switches a users pokemon //
////////////////////////////////
SwitchPoke(id, pid, bool:force=true)
{
if( !ValidPoke(pid) )
return false;
if( pid == Pokemon[id] )
return true;
#if ADMIN_SPECIAL == 1
if( PokeInfo[pid] & PI_SPECIAL && !poke_access(id) ){
poke_printlang(id,"POKEMOD_RESTRICTUSE", PokeToName(pid));
return false;
}
#endif
if(Safari()){
if(SafariLevel(pid)){
ResetSkills(id, false)
Pokemon[id] = pid
SetSpeed(id)
return true
}
return false
}
#if CHANGE_POKES > 0
if( !force && ChangedPoke[id] < 1 && Pokemod_Alive(id) ){