<?php

include("db.php");

$MAX_X = 32;
$MAX_Y = 24;

$session = (int) $_GET['sid'];
$player = (int) $_GET['pid'];
$new_x = (int) $_GET['x'];
$new_y = (int) $_GET['y'];

$now = time();

if ($new_x < 1 || $new_x > $MAX_X) return;
if ($new_y < 1 || $new_y > $MAX_Y) return;

if ($player == 1 || $player == 2)
{
    // Session Activity anpassen
    $db->sql_query("UPDATE pacmp_sessions SET activity = $now WHERE session_id = $session");

    // Eigene Position in der Datenbank updaten
    $db->sql_query("UPDATE pacmp_table SET x=$new_x , y=$new_y WHERE session_id = $session AND player_id = $player");

    // Position des anderen Spielers aus der Datenbank holen
    $otherplayer = ($player == 1 ? 2 : 1);
    $result = $db->sql_query("SELECT x, y FROM pacmp_table WHERE session_id = $session AND player_id = $otherplayer");
    if ($row = $db->sql_fetchrow($result))
    {
        // Position zurückgeben
        header("Content-type: text/plain");
        echo $row['x'] . " " . $row['y'];
    }
}

?>