Skip to content

Application overview

Wiktor Mazur edited this page Mar 8, 2020 · 1 revision

<Game Message>

Defined messages

Already defined game messages are placed here

How to add new message

Game messages are created based on Events and operations. Adding entry to messages.json file is going to generate photon messages during compilation.

<Game Event>

Already defined game events are placed here. Translation between <Game Message> and <Game Event> is performed in world and convert modules.

Such translation is necessary because of inconsistency between player ids placed in <Game Message>. E.g player id is different in each zone. Such solution yields much easier use in Application development.

Example translation

To visualize process we're going to show how UpdateFame <Game Message> is converted and then published as UpdateFame <Game Event>

  1. Find event / operation which might contain fame information. Based on Events and operations UpdateFame is probably a jackpot.

  2. Adding entry to messages.json to generate UpdateFame message

    {
        "name": "UpdateFame",
        "code": 72,
        "params": []
    }

    From decompiled .dll we know that UpdateFame has code 72 (it's 72th event on the list)

  3. In the backend.log file we can see following entry

    17:41:57 [ INFO] UpdateFame parameters: {7: Short(371), 2: Integer(100000000), 6: Float(1.0), 252: Short(72), 1: Long(781457927639), 0: Short(327)}
    

    As I've used a book which gives 10k fame it looks like parameter 2 contains information about fame.

    Parameter 7 is probably responsible for identifying source of this message (in this case our character)

    Let's update JSON entry

    {
        "name": "UpdateFame",
        "code": 72,
        "params": [
            {
                "name": "source",
                "id": 0,
                "param_type": "Number"
            },
            {
                "name": "fame",
                "id": 2,
                "param_type": "Number"
            }
        ]
    },
  4. Translating UpdateFame message into UpdateFame event

    Since we knew that book gave us 10k fame but value visible in message was 10k bigger we need to adjust that.

    // convert.rs
    
    impl From<EventIntermediate<messages::UpdateFame>> for events::Event {
        fn from(intermediate: EventIntermediate<messages::UpdateFame>) -> Self {
            Self::UpdateFame(events::Fame {
                source: intermediate.id,
                value: intermediate.message.fame as f32 / 10000.0,
            })
        }
    }

    We want to have static id's accessible from GUI, therefore final event is constructed in the following way:

    // world.rs
    
    photon_messages::Message::UpdateFame(msg) => {
        let static_id = self.get_static_id(msg.source)?;
        Some(vec![self.get_intermediate(static_id, msg)?.into()])
    }
  5. Publishing UpdateFame game event to the application

    Since our target application is created in python we need to translate GameEvent into python dictionary. Such translation is done in crosslang/python/convert.rs module

    // convert.rs
    
    impl ToPyObject for Event {
        type ObjectType = PyObject;
        fn to_py_object(&self, py: Python) -> Self::ObjectType {
            let event = PyDict::new(py);
    
            match self {
                // other events omitted
                Event::UpdateFame(e) => {
                    event.set_item(py, "name", "UpdateFame".into_py_object(py)).unwrap_or(());
                    event.set_item(py, "value", e.into_py_object(py)).unwrap_or(());
                }
            }
    
            event.into_object()
        }
    }
    
    impl ToPyObject for events::Fame {
        type ObjectType = PyObject;
        fn to_py_object(&self, py: Python) -> Self::ObjectType {
            let event = PyDict::new(py);
    
            event.set_item(py, "source", self.source.into_py_object(py)).unwrap_or(());
            event.set_item(py, "value", self.value.into_py_object(py).into_object()).unwrap_or(());
            event.into_object()
        }
    }
  6. Accessing FameUpdate event in python application

    class EventReceiver(abc.ABC):
        def receive(self, event):
            # other events omitted
            if event_name == consts.EvNameUpdateFame:
                self.on_fame_update(value[consts.EvKeyValue])