MySQL conditions in game development

When a player transfers an item to their character, the item type is communicated to the server.

In scenarios where a player equips an armband with the type "bracelet," I want it to attempt placing the item ID in the leftbracer column of the game_moblist table (which contains players and adversaries in the game). If the left slot is already occupied by an item ID, then attempt to place it in the right slot. I signify an empty slot with a value of 0.

if (type=="bracelet"){
    to_sql="UPDATE game_moblist SET leftbracer"="+item_id+" WHERE id="+player_id
}

Previously, I used a select statement first, but now I prefer doing it all in one query. Thank you.

Answer №1

To determine the value, you can simply use an IF statement to check if leftbracer equals 0. If it does, set it to the new item ID; otherwise, keep the current value.

UPDATE game_moblist SET 
   rightbracer = IF(leftbracer = 0, rightbracer, :item_id),
   leftbracer = IF(leftbracer = 0, :item_id, leftbracer );

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

showing sections that collapse next to each other

I am currently designing a portfolio website using HTML, CSS, and vanilla JavaScript. I have implemented collapsing sections that expand when clicked on. However, the buttons for these sections are stacked vertically and I want to place them side by side. ...

Steps for correctly displaying a success message after clicking and copying to the clipboard:

In the process of developing a color palette, I am incorporating a clipboard icon enclosed in a Tooltip component alongside each color. The functionality involves copying the color's name to the user's clipboard upon clicking the icon. Subsequent ...

Can I safely execute JSON.parse() on the window's location hash?

Is it safe to store JavaScript variables in the URL's hash for web application state restoration through bookmarks? I've been considering using JSON serialization as a method. Storing variables like this: var params = { var1: window.val1, var2: ...

I continue to encounter a TypeError when using the mongoose-paginate function

Every time I try to run my code, I encounter the following error message: TypeError undefined paginate function Below is the snippet of code causing the issue: var mongoose = require('mongoose'); var mongoosastic = require('mongoosastic ...

comprehending the concept of express and mastering its usage

Can you confirm if my understanding is correct? 1) So, when I write this line of code... const express = require(“express”) I am assigning a "Class" to the variable express. 2) And then, when I call this function... express.jason() Am I correctly ...

What is the best method to generate a distinct identifier for individual input fields using either JavaScript or jQuery?

I have attempted to copy the table n number of times using a for loop. Unfortunately, the for loop seems to only work on the first iteration. I am aware that this is due to not having unique IDs assigned to each table. As a beginner, I am unsure how to cre ...

The window fails to load properly after building, but functions perfectly while in development server mode

My application is not displaying a window after it's built, but it works perfectly fine when I execute npm run serve Even though there is a process running in the task manager, the same issue persists if I try using the installer. I'm not receiv ...

Transitioning from using sendfile() to sendFile() function within the Express framework

Here is the code snippet I am using: router.get('/image',(req,res,next)=>{ const fileName = "path_to.jpg" res.sendfile(fileName,(err)=>{ if (err) { next(err); } else { console.log('Sent:', fileName); } ...

While iterating over each item in the List<string> retrieved from an AJAX GET request in JavaScript

Trying to iterate through a list of strings and display them on the page, but facing an error as described in the title... "Uncaught TypeError: response.forEach is not a function" I've looked into for loops in JavaScript, but they seem to work wit ...

Welcome to the launch of OpenWeatherMap!

I just signed up for an account on the OpenWeatherMap website. My goal is to retrieve the current weather information for a specific location using the City ID API Call: http://api.openweathermap.org/data/2.5/weather?id=2172797&appid=myAPIKey How ca ...

Guide on implementing a template within a form using Vue.js

I have set up a Vue instance for handling the form data var formInstance = new Vue({ el: '#amount_form', data: { logdate: '', amount:'', description:'' }, methods: { ...

Failing to retrieve data from Ajax response

When handling requests in a servlet, the following code snippet processes the request received: Gson gson = new Gson(); JsonObject myObj = new JsonObject(); LoginBean loginInfo = getInfo(userId,userPwd); JsonElement loginObj = gson.toJsonTree(loginInfo) ...

Display just the minutes using react-countdown-circle-timer in a React application

Looking to create a test page in React featuring a countdown timer set at 50 minutes, I integrated the react-countdown-circle-timer module. However, I encountered an issue where the minutes displayed do not change dynamically as they should. My goal is to ...

Injecting CSS styles into dynamically inserted DOM elements

Utilizing Javascript, I am injecting several DOM elements into the page. Currently, I can successfully inject a single DOM element and apply CSS styling to it: var $e = $('<div id="header"></div>'); $('body').append($e); $ ...

Do you think my approach is foolproof against XSS attacks?

My website has a chat feature and I am wondering if it is protected against XSS attacks. Here is how my method works: To display incoming messages from an AJAX request, I utilize the following jQuery code: $("#message").prepend(req.msg); Although I am a ...

Tips for modifying a request api through a select form in a REACT application

Apologies if this question seems a bit basic, but I need some help. I am working on creating a film catalog using The Movie Database API. I have successfully developed the home and search system, but now I am struggling to implement a way to filter the fi ...

Angular2 Dropdown not updating with values from API

Here is the structure of my project flow: import_product.html <div class="row custom_row"> <div class="col-md-2">Additional Duty: </div> <div class="col-md-2"> < ...

How to conditionally prevent event propagation to children in VueJs

This Vue component is called ColorButtonGroup, and it serves as a group of checkbox/toggle buttons. It has a maximum limit of 4 colors that can be selected. The component utilizes another component called ToggleButton, which is a simple toggle selection w ...

The React live search functionality is operational, however, it is not effectively canceling previous requests in the

Currently, I am in the process of following a helpful tutorial over at "alligator.io". You can check out the specific link here: https://alligator.io/react/live-search-with-axios/ The code snippet below belongs to App.js: import React, { Component } from ...

Send a single piece of data using AJAX in Flask

I have a very basic HTML form containing only one <input type='text'> field for entering an email address. I am trying to send this value back to a Python script using AJAX, but I am having trouble receiving it on the other end. Is there a ...