File: cmdeco.c

package info (click to toggle)
xshipwars 1.32-5
  • links: PTS
  • area: main
  • in suites: potato
  • size: 17,176 kB
  • ctags: 6,357
  • sloc: ansic: 157,152; makefile: 226; sh: 75
file content (671 lines) | stat: -rw-r--r-- 16,605 bytes parent folder | download
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
#include "swserv.h"


int CMDECO_PERMISSION_CHECK(int condescriptor, int permission);


/*
 *	Checks if connection matches or passes the given permission
 *	level.
 */
int CMDECO_PERMISSION_CHECK(int condescriptor, int permission)
{
	long object_num;
	xsw_object_struct *obj_ptr;


	/* Connection must be logged in. */
	if(!ConIsLoggedIn(condescriptor))
	    return(0);

	/* Get connection's object. */
	object_num = connection[condescriptor]->object_num;
	if(DBIsObjectGarbage(object_num))
	    return(0);
	else
	    obj_ptr = xsw_object[object_num];

	/* Check object's permission. */
	if(obj_ptr->permission.uid <= permission)
	    return(1);
	else
	    return(0);
}



/*
 *	Creates an eco product on an object.
 *
 *	Syntax: "<object>=<product_name>"
 */
int CmdEcoProductCreate(int condescriptor, char *arg)
{
	int i, status;
	char *strptr;

        char name[XSW_OBJ_NAME_MAX];
	char product_name[ECO_PRODUCT_NAME_MAX];
        char sndbuf[CS_DATA_MAX_LEN];
	char stringa[1024];
        char tmp_name1[XSW_OBJ_NAME_MAX];
        char tmp_name2[XSW_OBJ_NAME_MAX];

        long object_num;
	xsw_object_struct *obj_ptr;
	xsw_ecodata_struct *eco_ptr;


        /* Get object name. */
        strncpy(name, arg, XSW_OBJ_NAME_MAX);
        name[XSW_OBJ_NAME_MAX - 1] = '\0';   
	strptr = strchr(name, '=');
	if(strptr == NULL)
	{
            sprintf(sndbuf,
 "ecoprodcreate: Usage: `ecoprodcreate <object>=<product_name>'"
            );
            NetSendLiveMessage(condescriptor, sndbuf);
            return(-1);
	}
	else
	{
	    *strptr = '\0';
	}

	/* Get product name. */
	strptr = strchr(arg, '=');
	if(strptr == NULL)
	{
	    return(-1);
	}
	else
	{
	    strncpy(
		product_name,
		strptr + 1,
		ECO_PRODUCT_NAME_MAX
	    );
	    product_name[ECO_PRODUCT_NAME_MAX - 1] = '\0';
	}


        StringStripSpaces(name);
	StringStripSpaces(product_name);


	/* Product name must be valid. */
	if(product_name[0] == '\0')
	    return(-1);


        /* Match object. */
	if(!strcmp(name, "me"))
	    object_num = connection[condescriptor]->object_num;
        else
            object_num = MatchObjectByName(name, -1);


        /* Make sure object_num is valid. */
        if(DBIsObjectGarbage(object_num))   
        {
            sprintf(sndbuf, "%s: No such object.",
                name
            );
            NetSendLiveMessage(condescriptor, sndbuf);
            return(-1);
        }
	else
	{
	    obj_ptr = xsw_object[object_num];
	}

	/* Permission check. */
	if(obj_ptr->owner != connection[condescriptor]->object_num)
	{
	    if(!CMDECO_PERMISSION_CHECK(condescriptor, ACCESS_UID_SETO))
	    {
                sprintf(sndbuf,
   "ecoprodcreate: You do not own that object."
                );
                NetSendLiveMessage(condescriptor, sndbuf);
                return(-1);
	    }
	}
	else
	{
            if(!CMDECO_PERMISSION_CHECK(condescriptor, ACCESS_UID_SET))
            {
                sprintf(sndbuf,
   "ecoprodcreate: Requires access level %i: Permission denied.",
                    ACCESS_UID_SET
                );
                NetSendLiveMessage(condescriptor, sndbuf);
		return(-1);
            }
	}


        /* Allocate main economy structure on object as needed. */
        if(DBCreateObjectEconomy(object_num))
        {
            sprintf(sndbuf, "%s: Memory allocation error.",
                name
            );
            NetSendLiveMessage(condescriptor, sndbuf);

            return(-1);
        }

	eco_ptr = obj_ptr->eco;
	if(eco_ptr == NULL)
	    return(-1);


        /* ******************************************************** */
	/* Check if product already exists. */
	for(i = 0; i < eco_ptr->total_products; i++)
	{
	    if(eco_ptr->product[i] == NULL)
		continue;

	    if(!strcasecmp(eco_ptr->product[i]->name, product_name))
	    {
                sprintf(sndbuf,
                    "Product `%s' already exists on %s.",
                    product_name, name
                );
                NetSendLiveMessage(condescriptor, sndbuf);

                return(-2);
	    }
	}


	/* ******************************************************** */
	/* Create new product. */
	status = DBCreateObjectEconomyProduct(
            object_num,
            product_name,
            1.0,		/* Sell price. */
            1.0,                /* Buy price. */
            1.0,                /* Amount. */
            1.0			/* Amount max. */
	);
	if(status)
	{
            sprintf(sndbuf, "%s: Cannot create product.",
		product_name
            );
            NetSendLiveMessage(condescriptor, sndbuf);
            
            return(-1);
        }
	else
	{
	    /* Print response and log. */

	    strncpy(tmp_name1, DBGetFormalNameStr(object_num), XSW_OBJ_NAME_MAX);
	    tmp_name1[XSW_OBJ_NAME_MAX - 1] = '\0';
            strncpy(
		tmp_name2,
		DBGetFormalNameStr(connection[condescriptor]->object_num),
		XSW_OBJ_NAME_MAX
	    );
            tmp_name2[XSW_OBJ_NAME_MAX - 1] = '\0';

            sprintf(sndbuf,
"Created product %s on %s.",
                product_name, tmp_name1
            );
            NetSendLiveMessage(condescriptor, sndbuf);

            sprintf(stringa,
 "%s: Created economy product %s on %s.",
		tmp_name2,
		product_name,
		tmp_name1
            );
            if(sysparm.log_general == 1)
                LogAppendLineFormatted(fname.primary_log, stringa);
	}


        /* Sort products. */
        DBSortEconomyProducts(object_num);


        return(0);
}


/*
 *	Sets parameter of product on object.
 *
 *	Syntax: "<object>=<product_name>=<parm>:<value>"
 */
int CmdEcoProductSet(int condescriptor, char *arg)
{
        int i;
        char *strptr;

        char name[XSW_OBJ_NAME_MAX];
        char product_name[ECO_PRODUCT_NAME_MAX];

	char parm[CS_DATA_MAX_LEN];
	char val[CS_DATA_MAX_LEN];

        char sndbuf[CS_DATA_MAX_LEN];
        char stringa[1024];
        char tmp_name1[XSW_OBJ_NAME_MAX];
        char tmp_name2[XSW_OBJ_NAME_MAX];

        long object_num;
        xsw_object_struct *obj_ptr;
        xsw_ecodata_struct *eco_ptr;
	xsw_ecoproduct_struct *product_ptr;


        /* Get object name. */
        strncpy(name, arg, XSW_OBJ_NAME_MAX);
        name[XSW_OBJ_NAME_MAX - 1] = '\0';
        strptr = strchr(name, '=');
        if(strptr == NULL)
        {   
            sprintf(sndbuf,
 "ecoprodset: Usage: `ecoprodset <object>=<product_name>=<parm>:<value>'"
            );
            NetSendLiveMessage(condescriptor, sndbuf);
            return(-1);
        }
        else
        {
            *strptr = '\0';
        }

        /* Get product name. */
        strptr = strchr(arg, '=');
        if(strptr == NULL)
        {
            return(-1);
        }
        else
        {
            strncpy(
                product_name,
                strptr + 1,
                ECO_PRODUCT_NAME_MAX
            );
            product_name[ECO_PRODUCT_NAME_MAX - 1] = '\0';
        }
	strptr = strchr(product_name, '=');
	if(strptr != NULL)
	    *strptr = '\0';

	/* Get parameter. */
	strptr = strchr(arg, '=');
	if(strptr == NULL)
	    return(-1);
	strptr = strchr(strptr + 1, '=');	/* Second '='. */
        if(strptr == NULL)
        {
            sprintf(sndbuf,
 "ecoprodset: Usage: `ecoprodset <object>=<product_name>=<parm>:<value>'"
            );
            NetSendLiveMessage(condescriptor, sndbuf);
            return(-1);
        }   

	strncpy(parm, strptr + 1, CS_DATA_MAX_LEN);
	strptr = strchr(parm, ':');
	if(strptr == NULL)
        {
            sprintf(sndbuf,
 "ecoprodset: Usage: `ecoprodset <object>=<product_name>=<parm>:<value>'"
            );
            NetSendLiveMessage(condescriptor, sndbuf);
            return(-1);
        }

	*strptr = '\0';
	strncpy(val, strptr + 1, CS_DATA_MAX_LEN);


        StringStripSpaces(name);
        StringStripSpaces(product_name);  
        StringStripSpaces(parm);
        StringStripSpaces(val);


        /* Product name must be valid. */
        if(product_name[0] == '\0')
            return(-1);


        /* Match object. */
        if(!strcmp(name, "me"))
            object_num = connection[condescriptor]->object_num;
        else
            object_num = MatchObjectByName(name, -1);

        /* Make sure object_num is valid. */
        if(DBIsObjectGarbage(object_num))
        {
            sprintf(sndbuf, "%s: No such object.",
                name   
            );
            NetSendLiveMessage(condescriptor, sndbuf);
            return(-1);
        }
        else
        {
            obj_ptr = xsw_object[object_num];
        }

        /* Permission check. */
        if(obj_ptr->owner != connection[condescriptor]->object_num)
        {
            if(!CMDECO_PERMISSION_CHECK(condescriptor, ACCESS_UID_SETO))
            {
                sprintf(sndbuf,
   "ecoprodset: You do not own that object."
                );
                NetSendLiveMessage(condescriptor, sndbuf);
                return(-1);
            }
        }
        else  
        {
            if(!CMDECO_PERMISSION_CHECK(condescriptor, ACCESS_UID_SET))
            {
                sprintf(sndbuf,
   "ecoprodset: Requires access level %i: Permission denied.",
                    ACCESS_UID_SET
                );
                NetSendLiveMessage(condescriptor, sndbuf);
                return(-1);
            }
        }


	/* Get pointer to economy structure on object. */
        eco_ptr = obj_ptr->eco;
        if(eco_ptr == NULL)
            return(-1);

        /* Get pointer to product. */
	product_ptr = NULL;
        for(i = 0; i < eco_ptr->total_products; i++)
        {
            if(eco_ptr->product[i] == NULL)
                continue;

            if(!strcasecmp(eco_ptr->product[i]->name, product_name))
            {
		product_ptr = eco_ptr->product[i];
		break;
            } 
        }
	if(product_ptr == NULL)
	{
            sprintf(sndbuf,
 "ecoprodset: %s: No such product on %s.",
		product_name,
		name
            );
            NetSendLiveMessage(condescriptor, sndbuf);
            return(-1);
	}


	/* ****************************************************** */

	/* Name. */
	if(!strcmp(parm, "name"))
	{
	    strncpy(
		product_ptr->name,
		val,
		ECO_PRODUCT_NAME_MAX
	    );
	    product_ptr->name[ECO_PRODUCT_NAME_MAX - 1] = '\0';
	}
	/* Buy price. */
        else if(!strcmp(parm, "buy_price"))
        {
	    product_ptr->buy_price = atof(val);
        }
        /* Sell price. */
        else if(!strcmp(parm, "sell_price"))
        {
            product_ptr->sell_price = atof(val);
        }
        /* Amount. */
        else if(!strcmp(parm, "amount"))
        {
            product_ptr->amount = atof(val);
        }
        /* Amount max. */
        else if(!strcmp(parm, "amount_max"))
        {
            product_ptr->amount_max = atof(val);
        }
 

        /* ****************************************************** */
	/* Print response and log. */

        strncpy(tmp_name1, DBGetFormalNameStr(object_num), XSW_OBJ_NAME_MAX);
        tmp_name1[XSW_OBJ_NAME_MAX - 1] = '\0';
        strncpy(
            tmp_name2,
            DBGetFormalNameStr(connection[condescriptor]->object_num),
            XSW_OBJ_NAME_MAX
        );
        tmp_name2[XSW_OBJ_NAME_MAX - 1] = '\0';

        sprintf(sndbuf,
"ecoprodset: %s: Set product `%s' %s to value `%s'.",
            tmp_name1, product_name, parm, val
        );
        NetSendLiveMessage(condescriptor, sndbuf);

        sprintf(stringa,
 "%s: Set economy product `%s' %s to `%s' on %s.",
            tmp_name2,
            product_name,
            parm,
            val,
            tmp_name1
        );
        if(sysparm.log_general == 1)
            LogAppendLineFormatted(fname.primary_log, stringa);


        /* Sort products. */
        DBSortEconomyProducts(object_num);


        return(0);
}


/*
 *	Deletes a product entry on an object.
 *
 *	Syntax: "<object>=<product_name>"
 */
int CmdEcoProductDelete(int condescriptor, char *arg)
{
        int i;
        char *strptr;

        char name[XSW_OBJ_NAME_MAX];
        char product_name[ECO_PRODUCT_NAME_MAX];
        char sndbuf[CS_DATA_MAX_LEN];

        char stringa[1024];
        char tmp_name1[XSW_OBJ_NAME_MAX];
        char tmp_name2[XSW_OBJ_NAME_MAX];
                
        long object_num;
        xsw_object_struct *obj_ptr;
        xsw_ecodata_struct *eco_ptr;
         
 
        /* Get object name. */
        strncpy(name, arg, XSW_OBJ_NAME_MAX);
        name[XSW_OBJ_NAME_MAX - 1] = '\0';
        strptr = strchr(name, '=');
        if(strptr == NULL)
        {
            sprintf(sndbuf,
 "ecoprodcreate: Usage: `ecoproddelete <object>=<product_name>'"
            );
            NetSendLiveMessage(condescriptor, sndbuf);
            return(-1);
        }
        else
        {   
            *strptr = '\0';
        }
        
        /* Get product name. */
        strptr = strchr(arg, '=');
        if(strptr == NULL)
        {
            return(-1);
        }
        else
        {
            strncpy(
                product_name,
                strptr + 1,
                ECO_PRODUCT_NAME_MAX
            );
            product_name[ECO_PRODUCT_NAME_MAX - 1] = '\0';
        }
 
   
        StringStripSpaces(name);
        StringStripSpaces(product_name);  
 
        
        /* Product name must be valid. */
        if(product_name[0] == '\0')
            return(-1);
        
        /* Match object. */
        if(!strcmp(name, "me"))
            object_num = connection[condescriptor]->object_num;
        else
            object_num = MatchObjectByName(name, -1);
        
         
        /* Make sure object_num is valid. */
        if(DBIsObjectGarbage(object_num))
        {
            sprintf(sndbuf, "%s: No such object.",
                name
            );
            NetSendLiveMessage(condescriptor, sndbuf);
            return(-1);
        }
        else
        {
            obj_ptr = xsw_object[object_num];
        }   


        /* Permission check. */
        if(obj_ptr->owner != connection[condescriptor]->object_num)
        {
            if(!CMDECO_PERMISSION_CHECK(condescriptor, ACCESS_UID_SETO))
            {
                sprintf(sndbuf,
   "ecoproddelete: You do not own that object."
                );
                NetSendLiveMessage(condescriptor, sndbuf);
                return(-1);
            }
        }   
        else
        {
            if(!CMDECO_PERMISSION_CHECK(condescriptor, ACCESS_UID_SET))
            {
                sprintf(sndbuf,
  "ecoproddelete: Requires access level %i: Permission denied.",
                    ACCESS_UID_SET
                );
                NetSendLiveMessage(condescriptor, sndbuf);
                return(-1);
            }
        }

	/* Get pointer to eco structure. */
        eco_ptr = obj_ptr->eco;
        if(eco_ptr == NULL)
            return(-1);


	/* ******************************************************* */
	/* Search for product. */
        for(i = 0; i < eco_ptr->total_products; i++)
        {
            if(eco_ptr->product[i] == NULL)   
                continue;
        
            if(!strcasecmp(eco_ptr->product[i]->name, product_name))
            {
		/* Delete product. */
		free(eco_ptr->product[i]);
		eco_ptr->product[i] = NULL;

		/* Print response and log. */
                strncpy(tmp_name1, DBGetFormalNameStr(object_num), XSW_OBJ_NAME_MAX);
                tmp_name1[XSW_OBJ_NAME_MAX - 1] = '\0';
                strncpy(
                    tmp_name2,
                    DBGetFormalNameStr(connection[condescriptor]->object_num),
                    XSW_OBJ_NAME_MAX
                );
                tmp_name2[XSW_OBJ_NAME_MAX - 1] = '\0';

                sprintf(sndbuf,
"Deleted product %s on %s.",
                    product_name, tmp_name1
                );
                NetSendLiveMessage(condescriptor, sndbuf);

                sprintf(stringa,
 "%s: Deleted economy product %s on %s.",
                    tmp_name2,
                    product_name,
                    tmp_name1
                );
                if(sysparm.log_general == 1)
                    LogAppendLineFormatted(fname.primary_log, stringa);
            } 
        }

	/* If all product pointers are NULL, delete products. */
        for(i = 0; i < eco_ptr->total_products; i++)
        {
            if(eco_ptr->product[i] != NULL)
                break;
	}
	if(i >= eco_ptr->total_products)
	{
	    free(eco_ptr->product);
	    eco_ptr->product = NULL;

	    eco_ptr->total_products = 0;
	}


	/* Sort products. */
	DBSortEconomyProducts(object_num);

         
        return(0);
}