Mercurial > hg > batmud > maputils
comparison www/common.inc.php @ 2763:78ad0e51b7b5
Improve wizards.txt parser, add functionality for specifying alternative /
additional names as some wizards have used more than one.
Also other improvements in wizard data handling.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Tue, 12 Mar 2024 15:47:58 +0200 |
parents | 06ce4399639c |
children |
comparison
equal
deleted
inserted
replaced
2762:4caa26c12052 | 2763:78ad0e51b7b5 |
---|---|
458 | 458 |
459 return $locations; | 459 return $locations; |
460 } | 460 } |
461 | 461 |
462 | 462 |
463 function mpStoreWizInfoField(&$table, $field, $data) | 463 function mpStoreWizInfoField(&$wizInfo, $field, $data) |
464 { | 464 { |
465 $str = trim($data); | 465 $str = trim($data); |
466 if (strlen($str) > 0) | 466 if (strlen($str) > 0) |
467 $table[$field] = $str; | 467 $wizInfo[$field] = $str; |
468 } | 468 } |
469 | 469 |
470 | 470 |
471 function mpStoreWizInfoRecordInfo(&$table, $record) | 471 function mpStoreWizInfoRecordInfo($nline, &$wizInfo, $record) |
472 { | 472 { |
473 $name = trim($record[1]); | 473 // Parse and check names |
474 if (isset($table[$name])) | 474 $allnames = preg_split("/\s*\|\s*/", trim($record[1])); |
475 { | 475 foreach ($allnames as $name) |
476 mpError("Item ".$name." is already set!"); | 476 { |
477 if ($name == "" || | |
478 $name != strtoupper(substr($name, 0, 1)).strtolower(substr($name, 1))) | |
479 { | |
480 mpError("Invalid name in wizard record (line #".$nline."): '".$name."'."); | |
481 } | |
482 } | |
483 | |
484 // Use first name as primary "key" | |
485 $keyname = $allnames[0]; | |
486 if (isset($wizInfo[$keyname])) | |
487 { | |
488 mpError("Wizard record '".$keyname."' is already set on line #". | |
489 $wizInfo[$keyname]["nline"].", vs line #".$nline); | |
477 return FALSE; | 490 return FALSE; |
478 } | 491 } |
479 else | 492 else |
480 { | 493 { |
481 $tmp = ["name" => $name, "areas" => 0]; | 494 $data = [ |
495 "name" => $keyname, | |
496 "names" => array_slice($allnames, 1), | |
497 "areas" => 0, | |
498 "nline" => $nline, | |
499 ]; | |
482 | 500 |
483 $countries = array_map( | 501 $countries = array_map( |
484 function ($item) | 502 function ($item) |
485 { | 503 { |
486 return strtolower($item); | 504 return strtolower($item); |
490 { | 508 { |
491 return $item != ""; | 509 return $item != ""; |
492 })); | 510 })); |
493 | 511 |
494 if (count($countries) > 0) | 512 if (count($countries) > 0) |
495 $tmp["countries"] = $countries; | 513 $data["countries"] = $countries; |
496 | 514 |
497 mpStoreWizInfoField($tmp, "homeURL", $record[3]); | 515 mpStoreWizInfoField($data, "homeURL", $record[3]); |
498 mpStoreWizInfoField($tmp, "imageURL", $record[4]); | 516 mpStoreWizInfoField($data, "imageURL", $record[4]); |
499 mpStoreWizInfoField($tmp, "desc", $record[5]); | 517 mpStoreWizInfoField($data, "desc", $record[5]); |
500 | 518 |
501 $table[$name] = $tmp; | 519 $wizInfo[$keyname] = $data; |
502 | 520 |
503 return TRUE; | 521 return TRUE; |
504 } | 522 } |
505 } | 523 } |
506 | 524 |
507 | 525 |
508 function mpParseWizInfoFile($filename, &$table) | 526 function mpParseWizInfoFile($filename, &$wizInfo) |
509 { | 527 { |
528 $startLine = $nline = 0; | |
510 if (($file = @fopen($filename, "r")) === false) | 529 if (($file = @fopen($filename, "r")) === false) |
511 return FALSE; | 530 return FALSE; |
512 | 531 |
513 $contMode = FALSE; | 532 $contMode = FALSE; |
514 while (!feof($file)) | 533 while (!feof($file)) |
515 { | 534 { |
516 $line = mpChConv(trim(fgets($file))); | 535 $line = mpChConv(trim(fgets($file))); |
536 $nline++; | |
517 | 537 |
518 if (strlen($line) == 0 || $line[0] == "#") | 538 if (strlen($line) == 0 || $line[0] == "#") |
519 continue; | 539 continue; |
520 | 540 |
521 if ($contMode) | 541 if ($contMode) |
522 { | 542 { |
523 if (substr($line, -1, 1) == '$') | 543 if (substr($line, -1, 1) == '$') |
524 { | 544 { |
525 $record[5] .= " ".trim(substr($line, 0, -1)); | 545 $record[5] .= " ".trim(substr($line, 0, -1)); |
526 $contMode = FALSE; | 546 $contMode = FALSE; |
527 mpStoreWizInfoRecordInfo($table, $record); | 547 mpStoreWizInfoRecordInfo($startLine, $wizInfo, $record); |
528 } | 548 } |
529 else | 549 else |
530 { | 550 { |
531 $record[5] .= " ".trim($line); | 551 $record[5] .= " ".trim($line); |
532 } | 552 } |
533 } | 553 } |
534 else | 554 else |
535 if (preg_match("/^([A-Z][a-z]+);([a-z\|]+)?;(https?:\/\/[^;]+|bat)?;([^;]+)?;([^\$]*)\\\$$/", $line, $record)) | 555 if (preg_match("/^([A-Za-z\|]+);([a-z\|]+)?;(https?:\/\/[^;]+|bat)?;([^;]+)?;([^\$]*)\\\$$/", $line, $record)) |
536 mpStoreWizInfoRecordInfo($table, $record); | 556 mpStoreWizInfoRecordInfo($nline, $wizInfo, $record); |
537 else | 557 else |
538 if (preg_match("/^([A-Z][a-z]+);([a-z\|]+)?;(https?:\/\/[^;]+|bat)?;([^;]+)?;(.*)$/", $line, $record)) | 558 if (preg_match("/^([A-Za-z\|]+);([a-z\|]+)?;(https?:\/\/[^;]+|bat)?;([^;]+)?;(.*)$/", $line, $record)) |
559 { | |
560 $startLine = $nline; | |
539 $contMode = TRUE; | 561 $contMode = TRUE; |
562 } | |
540 else | 563 else |
541 mpError($line); | 564 mpError("Wizard record line #".$nline.": ".$line); |
542 } | 565 } |
543 | 566 |
544 fclose($file); | 567 fclose($file); |
545 return TRUE; | 568 return TRUE; |
569 } | |
570 | |
571 | |
572 function mpMakeWizInfoAliases($wizInfo) | |
573 { | |
574 $aliases = []; | |
575 foreach ($wizInfo as $name => $data) | |
576 { | |
577 foreach ($data["names"] as $nname) | |
578 $aliases[$nname] = $name; | |
579 } | |
580 return $aliases; | |
546 } | 581 } |
547 | 582 |
548 | 583 |
549 function mpReadWizInfoFiles() | 584 function mpReadWizInfoFiles() |
550 { | 585 { |