Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wiki #43

Merged
merged 4 commits into from
Apr 18, 2023
Merged

Wiki #43

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,4 @@ __pycache__/
/configuration.ini
/pyinstaller.exe
/cache
/web wiki/static
213 changes: 213 additions & 0 deletions web wiki/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
import sys
import os
import pygame
from flask import Flask, render_template, redirect

# ---
# to get this to work on my computer I acutal has to have this (I tested using both this current directory and the actual "main" directory
# as working directory and it does not work. /coppermouse
# This appends main directory to "sys.path" which makes it possible to import gamescript.
file_path = os.path.dirname(os.path.realpath(os.path.join(os.getcwd(), os.path.expanduser(__file__))))
main_dir = os.path.normpath(os.path.join(file_path, '..'))
sys.path.append(main_dir)
# ---

from gamescript.common import utility # nopep8
from gamescript.common.game.create_troop_sprite_pool import create_troop_sprite_pool # nopep8
from gamescript.game import Game # nopep8
from gamescript.common.game.setup.make_faction_troop_leader_data import make_faction_troop_leader_data # nopep8
from gamescript import datasprite # nopep8

csv_read = utility.csv_read

# this wiki make use of the create_troop_sprite_pool method.
# that method is depended on the Game class but when making a Game instance it
# starts the game, we do not want that, that is why we make use of a custom minified Game class
# that works better for this script.


class MinifiedGame(Game):
"""
Like Game but with less functionality but enough to view sprites.
"""

def __init__(self, main_dir):
"""
Minified init-method. Prevents the game from starting an only setup a required state.
"""
self.main_dir = main_dir
self.screen_scale = (1, 1)
self.ruleset = 0
self.ruleset_list = csv_read(
self.main_dir, "ruleset_list.csv", ("data", "ruleset"))
self.ruleset_folder = str(self.ruleset_list[self.ruleset][0]).strip("/")
self.language = 'en'

def change_ruleset(self):
"""
Minified change ruleset-method. Removed things that otherwise wouldn't allow
the script to be able to run.
"""

self.troop_data, self.leader_data, self.faction_data = make_faction_troop_leader_data(self.main_dir, self.screen_scale, self.ruleset_folder, self.language)

self.troop_animation = datasprite.TroopAnimationData(
self.main_dir,
[str(self.troop_data.race_list[key]["Name"])
for key in self.troop_data.race_list],
self.team_colour
)

self.subunit_animation_data = self.troop_animation.subunit_animation_data
self.gen_body_sprite_pool = self.troop_animation.gen_body_sprite_pool
self.gen_armour_sprite_pool = self.troop_animation.gen_armour_sprite_pool
self.colour_list = self.troop_animation.colour_list
self.gen_weapon_sprite_pool = self.troop_animation.gen_weapon_sprite_pool
self.weapon_joint_list = self.troop_animation.weapon_joint_list


pygame.init()

screen = pygame.display.set_mode((1, 1), pygame.HIDDEN)

assert type(main_dir) == str
print("main dir: {0}".format(main_dir))

game = MinifiedGame(main_dir)
game.change_ruleset()

app = Flask(__name__)


def get_subunit_icon(subunit_id, scale):
"""get a icon for a specific subunit"""

subunits = (game.troop_data.troop_list | game.leader_data.leader_list)
who_todo = {key: value for key, value in subunits.items() if key == subunit_id}

# make idle animation, first frame, right side (change to l_side for left), non-specific so it can make for any troops
preview_sprite_pool, _ = create_troop_sprite_pool(game, who_todo, preview=True, specific_preview=("Idle_0", 0, "r_side", "non-specific"),
max_preview_size=scale)
sprite = preview_sprite_pool[subunit_id]["sprite"]
icon = pygame.Surface((36, 36), pygame.SRCALPHA)
icon.blit(sprite, (0, 0))
return icon


@app.route("/")
def index():
return redirect("/leaders")


@app.route("/regions")
def regions():

regions = set()
for faction in game.faction_data.faction_list.values():
regions.add(faction['Type'])

# There is a "All" Region/Type found on the faction "All Factions" in faction.csv.
# This is obviously not an actual faction nor region so I discard it from being visible.
regions.discard("All")

return render_template("regions.j2", regions=regions)


@app.route("/factions")
def factions():
return render_template("factions.j2")


@app.route("/troop-classes")
def troop_classes():
return render_template("troop-classes.j2")


@app.route("/leaders")
def sub_units():

skill_list = game.troop_data.skill_list
weapon_list = game.troop_data.weapon_list
armour_list = game.troop_data.armour_list

leaders = list()
for k, v in game.leader_data.leader_list.items():

leader = {
"name": v["Name"],
"strength": v.get("Strength", "-"),
"dexterity": v.get("Dexterity"),
"agility": v.get("Agility"),
"constitution": v.get("Constitution"),
"intelligence": v.get("Intelligence"),
"wisdom": v.get("Wisdom"),
"charisma": v.get("Charisma"),
"troop-class": v.get("Troop Class", "-"),
"race": v["Race"],
"melee-speciality": v.get('Melee Speciality'),
"range-speciality": v.get('Range Speciality'),
"cavalry-speciality": v.get('Cavalry Speciality'),
"social-class": v.get('Social Class'),

"primary-main-weapon": (
v.get('Primary Main Weapon'),
(weapon_list[v.get("Primary Main Weapon")[1]]["Name"]
if v.get('Primary Main Weapon') and type(v.get('Primary Main Weapon')[0]) == int
else "-")
),

"primary-sub-weapon": (
v.get('Primary Sub Weapon'),
(weapon_list[v.get("Primary Sub Weapon")[1]]["Name"]
if v.get('Primary Sub Weapon') and type(v.get('Primary Sub Weapon')[0]) == int
else "-")
),

"secondary-main-weapon": (
v.get('Secondary Main Weapon'),
(weapon_list[v.get("Secondary Main Weapon")[1]]["Name"]
if v.get('Secondary Main Weapon') and type(v.get('Secondary Main Weapon')[0]) == int
else "-")
),

"secondary-sub-weapon": (
v.get('Secondary Sub Weapon'),
(weapon_list[v.get("Secondary Sub Weapon")[1]]["Name"]
if v.get('Secondary Sub Weapon') and type(v.get('Secondary Sub Weapon')[0]) == int
else "-")
),

"armour": (
v.get('Armour'),
(armour_list[v.get("Armour")[1]]["Name"]
if v.get('Armour') and type(v.get('Armour')[0]) == int
else "-")
),

"mount": v.get('Mount'),
"charge-skill": v.get('Charge Skill'),
# "skill": [ skill_list.get(s,{"Name":"-"})["Name"] for s in v.get('Skill',[]) ] ,
"skill": v.get('Skill', []),
"trait": v.get('Trait'),
"formations": v.get('Formation'),
"type": v.get('Type'),
"size": v.get('Size'),
}

leaders.append(leader)

subunit_icon_server_path = os.path.join(main_dir, "web wiki", "static", "{0}.png".format(k))
subunit_icon_web_path = "static/{0}.png".format(k)
leader['icon'] = [subunit_icon_web_path, k]

if not os.path.isfile(subunit_icon_server_path):
try:
subunit_icon = get_subunit_icon(k, 38*leader['size'])
pygame.image.save(subunit_icon, subunit_icon_server_path)
except Exception as e:
pass
# raise e

return render_template("leaders.j2", leaders=leaders)

app.run(debug=True)
Empty file added web wiki/static/.gitignore
Empty file.
40 changes: 40 additions & 0 deletions web wiki/templates/base.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<!DOCTYPE html>
<html>
<head>
<title>Web wiki</title>
<style>
* { font-family: Helvetica; color: #111 }
body { background: #d7d0c4; padding: 0; margin: 0 }
th { background: #a3856c }
h1 { margin: 0; padding: 0; color: #222222; font-size: 42px }
h2 { margin: 20px 0 0 0; padding: 0; color: #71410a; font-size: 28px }
table { border-collapse: collapse; border-spacing: 0 }
hr { margin: 6px 0 6px 0; height: 1px; border: 0; border-bottom: 1px solid #ccc; display: block; padding: 0 }
th { text-align: left; color: #fefefe; font-size: 12px; font-weight: normal; padding: 6px; border-bottom: 1px solid #604030; border-top: 1px solid #604030; margin-bottom: 28px }
#content { background: #fefefe; padding:40px; border-left: 1px solid #ccc; border-right: 1px solid #ccc; width: 1300px; margin: 0 auto 0 auto }
a{ color: #a13c12 }
tr:nth-child(odd) td { background: #fef2e8 }
td{ padding: 6px; font-size: 12px; vertical-align:top }
.inner td{ padding: 2px; background: 0 !important; border: 0; color: #000; padding-right: 14px }
ul{ display: block; padding: 0; margin: 0 }
li{ display: block; padding: 0; margin: 0 }
.inner th{ padding:2px; background: 0; border: 0; font-weight:bold; padding-right:0; color: #000 }
</style>
</head>

<body>
<div id="content">
<h1>Masendor</h1>

<hr/>
<a href="/regions">Regions</a> |
<a href="/factions">Factions</a> |
<a href="/troop-classes">Troop Classes</a> |
<a href="/leaders">Leaders</a>
<hr/>

{% block content %}{% endblock %}

</div>
</body>
</html>
8 changes: 8 additions & 0 deletions web wiki/templates/factions.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{% extends "base.j2" %}


{% block content %}
<h2>Factions</h2>

{% endblock %}

84 changes: 84 additions & 0 deletions web wiki/templates/leaders.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
{% extends "base.j2" %}

{% block content %}
<h2>Leaders</h2>

<style>
.attributes th{ color: #777; font-size: 10px; }
</style>

<table>
<tr>
<th>Icon</th>
<th>Name/Type</th>
<th>Attributes</th>
<th>Speciality</th>
<th>Primary W.</th>
<th>Second. W.</th>
<th>Form.</th>
<th>Cavalry S.</th>
<th>Social C.</th>
<th>Armour</th>
<th>Mount</th>
<th>Cha. Sk.</th>
<th>Skill</th>
</tr>
{% for unit in leaders %}
<tr>
<td><img src="{{unit["icon"][0]}}" alt="{{unit["icon"][1]}}" title="{{unit["icon"][1]}}"></td>
<td><a href="">{{unit["name"]}}</a><br>{{unit["type"]}}</td>

<td>
<table class="inner attributes">
<tr>
<th>STR:</th><td>{{unit["strength"]}}</td>
<th>DEX:</th><td>{{unit["dexterity"]}}</td>
<th>AGL:</th><td>{{unit["agility"]}}</td>
<th>CON:</th><td>{{unit["constitution"]}}</td>
</tr>
<tr>
<th>INT:</th><td>{{unit["intelligence"]}}</td>
<th>WIS:</th><td>{{unit["wisdom"]}}</td>
<th>CHR:</th><td>{{unit["charisma"]}}</td>
</tr>
</table>
</td>

<td>
<table class="inner">
<tr>
<th>Melee: </th><td>{{unit["melee-speciality"]}}</td>
</tr><tr>
<th>Range: </th><td>{{unit["range-speciality"]}}</td>
</tr>
</table>
</td>

<td>
<b>m: </b>{{unit["primary-main-weapon"][1]}}<br>
<b>s: </b>{{unit["primary-sub-weapon"][1]}}
</td>

<td>
<b>m: </b>{{unit["secondary-main-weapon"][1]}}<br>
<b>s: </b>{{unit["secondary-sub-weapon"][1]}}
</td>

<td>
<ul>{% for formation in unit["formations"] %}<li>{{formation}}</li>{% endfor %}</ul>
</td>

<td>{{unit["cavalry-speciality"]}}</td>
<td>{{unit["social-class"]}}</td>
<td>{{unit["armour"][1]}}</td>
<td>{{unit["mount"]}}</td>

<td>{{unit["charge-skill"]}}</td>
<td>{{unit["skill"]}}</td>

</tr>
{% endfor %}
</table>

{% endblock %}

16 changes: 16 additions & 0 deletions web wiki/templates/regions.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{% extends "base.j2" %}


{% block content %}
<h2>Regions</h2>
<table>
<tr><th>Name</th><th>Number of factions</th><th>Number of troops</th></tr>
{% for region in regions %}
<tr>
<td>{{region}}</td>
<td>TODO</td>
<td>TODO</td>
</tr>
{% endfor %}
{% endblock %}

8 changes: 8 additions & 0 deletions web wiki/templates/troop-classes.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{% extends "base.j2" %}


{% block content %}
<h2>Troop Classes</h2>

{% endblock %}