mourne_rcpp_fw/Mourne-CI/application/models/assignment_model.php

392 lines
11 KiB
PHP
Raw Normal View History

2021-10-30 19:02:07 +02:00
<?php
class Assignment_model extends MO_Model
{
public function __construct()
{
parent::__construct();
}
2021-10-30 19:02:07 +02:00
public function assign_unit($assid, $num, $slotid, $res, $villageid, $userid)
{
$this->set_resources($res);
2021-10-30 19:02:07 +02:00
//check if there is building in that slot
$sql = "SELECT buildings.* FROM village_buildings
2021-10-30 19:02:07 +02:00
INNER JOIN buildings ON village_buildings.buildingid=buildings.id
WHERE slotid='$slotid' AND villageid='$villageid'";
$q = $this->db->query($sql);
2021-10-30 19:02:07 +02:00
if (!$q->num_rows()) {
return 1;
}
2021-10-30 19:02:07 +02:00
$building = $q->row_array();
2021-10-30 19:02:07 +02:00
//check if there is already an assignment in which case we just deassign
//in this case num will be 0 unless they play with the form,
//but we shouldn't care about that since the user can write 0 into the form
$sql = "SELECT * FROM building_assignments
2021-10-30 19:02:07 +02:00
WHERE assignmentid='$assid'
AND slotid='$slotid'
AND villageid='$villageid'";
$q = $this->db->query($sql);
2021-10-30 19:02:07 +02:00
if ($q->num_rows()) {
//the user wants to just deassign
$ass = $q->row_array();
2021-10-30 19:02:07 +02:00
//delete assignment
$sql = "DELETE FROM building_assignments WHERE id='" . $ass['id'] . "'";
$this->db->query($sql);
2021-10-30 19:02:07 +02:00
//giving back units
//getting units
$sql = "SELECT * FROM village_units
2021-10-30 19:02:07 +02:00
WHERE villageid='$villageid'
AND userid='$userid'";
$q = $this->db->query($sql);
if ($q->num_rows()) {
$units = $q->result_array();
} else {
$units = false;
}
$found = false;
if ($units) {
foreach ($units as $row) {
if ($row['unitid'] == $ass['unitid']) {
$found = true;
$fdata = $row['id'];
}
}
}
if ($found) {
$sql = "UPDATE village_units
2021-10-30 19:02:07 +02:00
SET unitcount = unitcount + '". $ass['num_unit'] . "'
WHERE id='$fdata'";
} else {
$sql = "INSERT INTO village_units
2021-10-30 19:02:07 +02:00
VALUES(default, '$userid', '$villageid',
'" . $ass['unitid'] . "', '" . $ass['num_unit'] . "')";
}
2021-10-30 19:02:07 +02:00
$this->db->query($sql);
2021-10-30 19:02:07 +02:00
//substract the bonuses
$this->substract_modifiers(
$assid,
$villageid,
'assignment',
$ass['num_bonus']
);
2021-10-30 19:02:07 +02:00
$this->write_resources();
2021-10-30 19:02:07 +02:00
//taking away spells
$sql = "DELETE FROM building_spells
2021-10-30 19:02:07 +02:00
WHERE assignmentid='$assid'
AND slotid='$slotid'
AND villageid='$villageid'";
$this->db->query($sql);
return 0;
}
//check if assignment exists
$sql = "SELECT * FROM assignments WHERE id='$assid'";
$q = $this->db->query($sql);
if (!$q->num_rows()) {
return 2;
}
$ass = $q->row_array();
//checking technology
if ($ass['req_tech']) {
if (!$this->has_req_tech($ass['req_tech'], $villageid, $slotid)) {
return 3;
}
}
//check if building has that assignment
if (!($building['assignment1'] == $ass['id'] ||
$building['assignment2'] == $ass['id'] ||
$building['assignment3'] == $ass['id'] ||
$building['assignment4'] == $ass['id'] ||
$building['assignment5'] == $ass['id'])) {
return 3;
}
//getting unit
$sql = "SELECT * FROM village_units
2021-10-30 19:02:07 +02:00
WHERE unitid='" . $ass['unitid'] . "'
AND userid='$userid'";
$q = $this->db->query($sql);
2021-10-30 19:02:07 +02:00
if (!$q->num_rows()) {
return 4;
}
2021-10-30 19:02:07 +02:00
$unit = $q->row_array();
2021-10-30 19:02:07 +02:00
//it probably shouldn't happen but who knows
if (!$unit['unitcount']) {
return 5;
}
2021-10-30 19:02:07 +02:00
//if the user just clicks on the assign we should assign max
if (!$num) {
$num = $ass['max'];
}
2021-10-30 19:02:07 +02:00
//we shouldn't assign more than allowed
if ($ass['max'] < $num) {
$num = $ass['max'];
}
2021-10-30 19:02:07 +02:00
//determining how much can be assigned
if ($unit['unitcount'] < $num) {
$num = $unit['unitcount'];
}
2021-10-30 19:02:07 +02:00
//determining number of bonuses granted
$num_bonus = floor(($num / $ass['bonus_per_assigned']));
2021-10-30 19:02:07 +02:00
$count = ($unit['unitcount'] - $num);
2021-10-30 19:02:07 +02:00
//updating unit field in db
if ($count <= 0) {
$sql = "DELETE FROM village_units WHERE id='" . $unit['id'] . "'";
} else {
$sql = "UPDATE village_units
2021-10-30 19:02:07 +02:00
SET unitcount=unitcount - '$num'
WHERE id='" . $unit['id'] . "'";
}
2021-10-30 19:02:07 +02:00
$this->db->query($sql);
2021-10-30 19:02:07 +02:00
//adding assignment
$sql = "INSERT INTO building_assignments
2021-10-30 19:02:07 +02:00
VALUES(default, '$villageid', '$slotid', '" . $unit['unitid'] . "',
'$num', '" . $assid . "', '$num_bonus')";
$this->db->query($sql);
2021-10-30 19:02:07 +02:00
//grant resource bonus
$this->add_modifiers($assid, $villageid, 'assignment', $num_bonus);
$this->write_resources();
2021-10-30 19:02:07 +02:00
//assignment has spell
if (!$ass['spellid']) {
return 0;
}
2021-10-30 19:02:07 +02:00
/*
//only give spells if bonus is granted
if (!$num_bonus)
return 0;
*/
2021-10-30 19:02:07 +02:00
//only give spell if max unit assigned
if (!($ass['max'] == $num)) {
return 0;
}
2021-10-30 19:02:07 +02:00
//granting spell
$sql = "INSERT INTO building_spells
2021-10-30 19:02:07 +02:00
VALUES(default, '$villageid', '$slotid', '$assid',
'" . $ass['spellid'] . "')";
$this->db->query($sql);
2021-10-30 19:02:07 +02:00
return 0;
}
2021-10-30 19:02:07 +02:00
public function get_assignments($slotid, $villageid, $userid)
{
if ($slotid > parent::TOTAL_BUILDINGS) {
return 1;
} //that shouldn't happen
2021-10-30 19:02:07 +02:00
$sql = "SELECT buildings.* FROM village_buildings
2021-10-30 19:02:07 +02:00
INNER JOIN buildings ON village_buildings.buildingid = buildings.id
WHERE village_buildings.slotid='$slotid'
AND village_buildings.villageid='$villageid'";
$q = $this->db->query($sql);
2021-10-30 19:02:07 +02:00
if (!$q->num_rows()) {
return 2;
} //nothing in that slot
2021-10-30 19:02:07 +02:00
$res = $q->row_array();
2021-10-30 19:02:07 +02:00
//getting assignment data
$sql = "SELECT assignments.*,units.name
2021-10-30 19:02:07 +02:00
FROM assignments
INNER JOIN units ON assignments.unitid=units.id
WHERE assignments.id='" . $res['assignment1'] . "'
OR assignments.id='" . $res['assignment2'] . "'
OR assignments.id='" . $res['assignment3'] . "'
OR assignments.id='" . $res['assignment4'] . "'
OR assignments.id='" . $res['assignment5'] . "'";
$q = $this->db->query($sql);
2021-10-30 19:02:07 +02:00
if (!$q->num_rows()) {
$data['assigndata'] = false; //building doesn't have assignments
return $data;
}
2021-10-30 19:02:07 +02:00
$adata = $q->result_array();
2021-10-30 19:02:07 +02:00
$tech = $this->get_village_technologies($villageid, $slotid);
2021-10-30 19:02:07 +02:00
foreach ($adata as $row) {
if ($this->have_technology($tech, $row['req_tech'])) {
$data['assigndata'][] = $row;
}
}
2021-10-30 19:02:07 +02:00
if (!isset($data['assigndata'])) {
$data['assigndata'] = false;
return $data;
}
2021-10-30 19:02:07 +02:00
//getting assigned data
$sql = "SELECT * FROM building_assignments
2021-10-30 19:02:07 +02:00
WHERE slotid='$slotid'
AND villageid='$villageid'";
$q = $this->db->query($sql);
2021-10-30 19:02:07 +02:00
if (!$q->num_rows()) {
$data['assigned'] = false;
} else {
$data['assigned'] = $q->result_array();
}
2021-10-30 19:02:07 +02:00
$data['building'] = $res;
2021-10-30 19:02:07 +02:00
//getting units
$sql = "SELECT * FROM village_units
2021-10-30 19:02:07 +02:00
WHERE villageid='$villageid'
AND userid='$userid'";
$q = $this->db->query($sql);
2021-10-30 19:02:07 +02:00
if ($q->num_rows) {
$data['units'] = $q->result_array();
} else {
$data['units'] = false;
}
2021-10-30 19:02:07 +02:00
return $data;
}
2021-10-30 19:02:07 +02:00
public function get_assignment_list_drop_admin()
{
$sql = "SELECT * FROM assignments";
$q = $this->db->query($sql);
2021-10-30 19:02:07 +02:00
$res = $q->result_array();
2021-10-30 19:02:07 +02:00
$data[0] = 'Nothing';
2021-10-30 19:02:07 +02:00
foreach ($res as $row) {
$data[$row['id']] = $row['description'];
}
2021-10-30 19:02:07 +02:00
return $data;
2021-10-30 19:02:07 +02:00
}
public function get_assignments_admin()
{
$sql = "SELECT * FROM assignments";
2021-10-30 19:02:07 +02:00
$q = $this->db->query($sql);
2021-10-30 19:02:07 +02:00
return $q->result_array();
}
2021-10-30 19:02:07 +02:00
public function add_assignment_admin($data)
{
$sql = "INSERT INTO assignments
2021-10-30 19:02:07 +02:00
VALUES (default,
'" . $data['unitid'] . "',
'" . $data['max'] . "',
'" . $data['bonus_per_assigned'] . "',
'" . $data['spellid'] . "',
'" . $data['req_tech'] . "',
'" . $data['mod_max_food'] . "',
'" . $data['mod_max_wood'] . "',
'" . $data['mod_max_stone'] . "',
'" . $data['mod_max_iron'] . "',
'" . $data['mod_max_mana'] . "',
'" . $data['mod_rate_food'] . "',
'" . $data['mod_rate_wood'] . "',
'" . $data['mod_rate_stone'] . "',
'" . $data['mod_rate_iron'] . "',
'" . $data['mod_rate_mana'] . "',
'" . $data['mod_percent_food'] . "',
'" . $data['mod_percent_wood'] . "',
'" . $data['mod_percent_stone'] . "',
'" . $data['mod_percent_iron'] . "',
'" . $data['mod_percent_mana'] . "',
'" . $data['description'] . "')";
$this->db->query($sql);
2021-10-30 19:02:07 +02:00
$this->_create_sql($sql);
}
2021-10-30 19:02:07 +02:00
public function edit_assignment_admin($data)
{
$sql = "UPDATE assignments
2021-10-30 19:02:07 +02:00
SET unitid='" . $data['unitid'] . "',
max='" . $data['max'] . "',
bonus_per_assigned='" . $data['bonus_per_assigned'] . "',
spellid='" . $data['spellid'] . "',
req_tech='" . $data['req_tech'] . "',
mod_max_food='" . $data['mod_max_food'] . "',
mod_max_wood='" . $data['mod_max_wood'] . "',
mod_max_stone='" . $data['mod_max_stone'] . "',
mod_max_iron='" . $data['mod_max_iron'] . "',
mod_max_mana='" . $data['mod_max_mana'] . "',
mod_rate_food='" . $data['mod_rate_food'] . "',
mod_rate_wood='" . $data['mod_rate_wood'] . "',
mod_rate_stone='" . $data['mod_rate_stone'] . "',
mod_rate_iron='" . $data['mod_rate_iron'] . "',
mod_rate_mana='" . $data['mod_rate_mana'] . "',
mod_percent_food='" . $data['mod_percent_food'] . "',
mod_percent_wood='" . $data['mod_percent_wood'] . "',
mod_percent_stone='" . $data['mod_percent_stone'] . "',
mod_percent_iron='" . $data['mod_percent_iron'] . "',
mod_percent_mana='" . $data['mod_percent_mana'] . "',
description='" . $data['description'] . "'
WHERE id='" . $data['id'] . "'";
$this->db->query($sql);
2021-10-30 19:02:07 +02:00
$this->_create_sql($sql);
}
2021-10-30 19:02:07 +02:00
public function get_assignment_admin($id)
{
$sql = "SELECT * FROM assignments WHERE id='$id'";
2021-10-30 19:02:07 +02:00
$q = $this->db->query($sql);
2021-10-30 19:02:07 +02:00
return $q->row_array();
}
2021-10-30 19:02:07 +02:00
}
//nowhitesp